201 lines
7.1 KiB
TypeScript
201 lines
7.1 KiB
TypeScript
|
// buildDynamicImport
|
||
|
function buildDynamicImport(node, deferToThen, wrapWithPromise, builder) {
|
||
|
const specifier = _core.types.isCallExpression(node) ? node.arguments[0] : node.source;
|
||
|
if (_core.types.isStringLiteral(specifier) || _core.types.isTemplateLiteral(specifier) && specifier.quasis.length === 0) {
|
||
|
if (deferToThen) {
|
||
|
return _core.template.expression.ast`
|
||
|
Promise.resolve().then(() => ${builder(specifier)})
|
||
|
`;
|
||
|
} else return builder(specifier);
|
||
|
}
|
||
|
const specifierToString = _core.types.isTemplateLiteral(specifier) ? _core.types.identifier("specifier") : _core.types.templateLiteral([_core.types.templateElement({
|
||
|
raw: ""
|
||
|
}), _core.types.templateElement({
|
||
|
raw: ""
|
||
|
})], [_core.types.identifier("specifier")]);
|
||
|
if (deferToThen) {
|
||
|
return _core.template.expression.ast`
|
||
|
(specifier =>
|
||
|
new Promise(r => r(${specifierToString}))
|
||
|
.then(s => ${builder(_core.types.identifier("s"))})
|
||
|
)(${specifier})
|
||
|
`;
|
||
|
} else if (wrapWithPromise) {
|
||
|
return _core.template.expression.ast`
|
||
|
(specifier =>
|
||
|
new Promise(r => r(${builder(specifierToString)}))
|
||
|
)(${specifier})
|
||
|
`;
|
||
|
} else {
|
||
|
return _core.template.expression.ast`
|
||
|
(specifier => ${builder(specifierToString)})(${specifier})
|
||
|
`;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// buildNamespaceInitStatements
|
||
|
function buildNamespaceInitStatements(metadata, sourceMetadata, constantReexports = false, wrapReference = Lazy.wrapReference) {
|
||
|
var _wrapReference;
|
||
|
const statements = [];
|
||
|
const srcNamespaceId = _core.types.identifier(sourceMetadata.name);
|
||
|
for (const localName of sourceMetadata.importsNamespace) {
|
||
|
if (localName === sourceMetadata.name) continue;
|
||
|
statements.push(_core.template.statement`var NAME = SOURCE;`({
|
||
|
NAME: localName,
|
||
|
SOURCE: _core.types.cloneNode(srcNamespaceId)
|
||
|
}));
|
||
|
}
|
||
|
const srcNamespace = (_wrapReference = wrapReference(srcNamespaceId, sourceMetadata.wrap)) != null ? _wrapReference : srcNamespaceId;
|
||
|
if (constantReexports) {
|
||
|
statements.push(...buildReexportsFromMeta(metadata, sourceMetadata, true, wrapReference));
|
||
|
}
|
||
|
for (const exportName of sourceMetadata.reexportNamespace) {
|
||
|
statements.push((!_core.types.isIdentifier(srcNamespace) ? _core.template.statement`
|
||
|
Object.defineProperty(EXPORTS, "NAME", {
|
||
|
enumerable: true,
|
||
|
get: function() {
|
||
|
return NAMESPACE;
|
||
|
}
|
||
|
});
|
||
|
` : _core.template.statement`EXPORTS.NAME = NAMESPACE;`)({
|
||
|
EXPORTS: metadata.exportName,
|
||
|
NAME: exportName,
|
||
|
NAMESPACE: _core.types.cloneNode(srcNamespace)
|
||
|
}));
|
||
|
}
|
||
|
if (sourceMetadata.reexportAll) {
|
||
|
const statement = buildNamespaceReexport(metadata, _core.types.cloneNode(srcNamespace), constantReexports);
|
||
|
statement.loc = sourceMetadata.reexportAll.loc;
|
||
|
statements.push(statement);
|
||
|
}
|
||
|
return statements;
|
||
|
}
|
||
|
|
||
|
// ensureStatementsHoisted
|
||
|
function ensureStatementsHoisted(statements) {
|
||
|
statements.forEach(header => {
|
||
|
header._blockHoist = 3;
|
||
|
});
|
||
|
}
|
||
|
|
||
|
// getModuleName
|
||
|
function getModuleName(rootOpts, pluginOpts) {
|
||
|
var _pluginOpts$moduleId, _pluginOpts$moduleIds, _pluginOpts$getModule, _pluginOpts$moduleRoo;
|
||
|
return originalGetModuleName(rootOpts, {
|
||
|
moduleId: (_pluginOpts$moduleId = pluginOpts.moduleId) != null ? _pluginOpts$moduleId : rootOpts.moduleId,
|
||
|
moduleIds: (_pluginOpts$moduleIds = pluginOpts.moduleIds) != null ? _pluginOpts$moduleIds : rootOpts.moduleIds,
|
||
|
getModuleId: (_pluginOpts$getModule = pluginOpts.getModuleId) != null ? _pluginOpts$getModule : rootOpts.getModuleId,
|
||
|
moduleRoot: (_pluginOpts$moduleRoo = pluginOpts.moduleRoot) != null ? _pluginOpts$moduleRoo : rootOpts.moduleRoot
|
||
|
});
|
||
|
}
|
||
|
|
||
|
// hasExports
|
||
|
function hasExports(metadata) {
|
||
|
return metadata.hasExports;
|
||
|
}
|
||
|
|
||
|
// isModule
|
||
|
function isModule(path) {
|
||
|
return path.node.sourceType === "module";
|
||
|
}
|
||
|
|
||
|
// isSideEffectImport
|
||
|
function isSideEffectImport(source) {
|
||
|
return source.imports.size === 0 && source.importsNamespace.size === 0 && source.reexports.size === 0 && source.reexportNamespace.size === 0 && !source.reexportAll;
|
||
|
}
|
||
|
|
||
|
// rewriteModuleStatementsAndPrepareHeader
|
||
|
function rewriteModuleStatementsAndPrepareHeader(path, {
|
||
|
exportName,
|
||
|
strict,
|
||
|
allowTopLevelThis,
|
||
|
strictMode,
|
||
|
noInterop,
|
||
|
importInterop = noInterop ? "none" : "babel",
|
||
|
lazy,
|
||
|
getWrapperPayload = Lazy.toGetWrapperPayload(lazy != null ? lazy : false),
|
||
|
wrapReference = Lazy.wrapReference,
|
||
|
esNamespaceOnly,
|
||
|
filename,
|
||
|
constantReexports = arguments[1].loose,
|
||
|
enumerableModuleMeta = arguments[1].loose,
|
||
|
noIncompleteNsImportDetection
|
||
|
}) {
|
||
|
(0, _normalizeAndLoadMetadata.validateImportInteropOption)(importInterop);
|
||
|
_assert((0, _helperModuleImports.isModule)(path), "Cannot process module statements in a script");
|
||
|
path.node.sourceType = "script";
|
||
|
const meta = (0, _normalizeAndLoadMetadata.default)(path, exportName, {
|
||
|
importInterop,
|
||
|
initializeReexports: constantReexports,
|
||
|
getWrapperPayload,
|
||
|
esNamespaceOnly,
|
||
|
filename
|
||
|
});
|
||
|
if (!allowTopLevelThis) {
|
||
|
(0, _rewriteThis.default)(path);
|
||
|
}
|
||
|
(0, _rewriteLiveReferences.default)(path, meta, wrapReference);
|
||
|
if (strictMode !== false) {
|
||
|
const hasStrict = path.node.directives.some(directive => {
|
||
|
return directive.value.value === "use strict";
|
||
|
});
|
||
|
if (!hasStrict) {
|
||
|
path.unshiftContainer("directives", _core.types.directive(_core.types.directiveLiteral("use strict")));
|
||
|
}
|
||
|
}
|
||
|
const headers = [];
|
||
|
if ((0, _normalizeAndLoadMetadata.hasExports)(meta) && !strict) {
|
||
|
headers.push(buildESModuleHeader(meta, enumerableModuleMeta));
|
||
|
}
|
||
|
const nameList = buildExportNameListDeclaration(path, meta);
|
||
|
if (nameList) {
|
||
|
meta.exportNameListName = nameList.name;
|
||
|
headers.push(nameList.statement);
|
||
|
}
|
||
|
headers.push(...buildExportInitializationStatements(path, meta, wrapReference, constantReexports, noIncompleteNsImportDetection));
|
||
|
return {
|
||
|
meta,
|
||
|
headers
|
||
|
};
|
||
|
}
|
||
|
|
||
|
// rewriteThis
|
||
|
function rewriteThis(programPath) {
|
||
|
if (!rewriteThisVisitor) {
|
||
|
rewriteThisVisitor = _traverse.visitors.environmentVisitor({
|
||
|
ThisExpression(path) {
|
||
|
path.replaceWith(_core.types.unaryExpression("void", _core.types.numericLiteral(0), true));
|
||
|
}
|
||
|
});
|
||
|
rewriteThisVisitor.noScope = true;
|
||
|
}
|
||
|
(0, _traverse.default)(programPath.node, rewriteThisVisitor);
|
||
|
}
|
||
|
|
||
|
// wrapInterop
|
||
|
function wrapInterop(programPath, expr, type) {
|
||
|
if (type === "none") {
|
||
|
return null;
|
||
|
}
|
||
|
if (type === "node-namespace") {
|
||
|
return _core.types.callExpression(programPath.hub.addHelper("interopRequireWildcard"), [expr, _core.types.booleanLiteral(true)]);
|
||
|
} else if (type === "node-default") {
|
||
|
return null;
|
||
|
}
|
||
|
let helper;
|
||
|
if (type === "default") {
|
||
|
helper = "interopRequireDefault";
|
||
|
} else if (type === "namespace") {
|
||
|
helper = "interopRequireWildcard";
|
||
|
} else {
|
||
|
throw new Error(`Unknown interop: ${type}`);
|
||
|
}
|
||
|
return _core.types.callExpression(programPath.hub.addHelper(helper), [expr]);
|
||
|
}
|
||
|
|
||
|
// getDynamicImportSource
|
||
|
function getDynamicImportSource(node) {
|
||
|
const [source] = node.arguments;
|
||
|
return _core.types.isStringLiteral(source) || _core.types.isTemplateLiteral(source) ? source : _core.template.expression.ast`\`\${${source}}\``;
|
||
|
}
|