2023-12-18 13:12:25 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
|
|
value: true
|
|
|
|
});
|
|
|
|
exports.default = void 0;
|
2024-01-16 21:26:16 +08:00
|
|
|
var _helperPluginUtils = require("@babel/helper-plugin-utils");
|
|
|
|
var _helperModuleTransforms = require("@babel/helper-module-transforms");
|
|
|
|
var _core = require("@babel/core");
|
|
|
|
const buildWrapper = _core.template.statement(`
|
2023-12-18 13:12:25 +08:00
|
|
|
define(MODULE_NAME, AMD_ARGUMENTS, function(IMPORT_NAMES) {
|
|
|
|
})
|
|
|
|
`);
|
2024-01-16 21:26:16 +08:00
|
|
|
const buildAnonymousWrapper = _core.template.statement(`
|
2023-12-18 13:12:25 +08:00
|
|
|
define(["require"], function(REQUIRE) {
|
|
|
|
})
|
|
|
|
`);
|
|
|
|
function injectWrapper(path, wrapper) {
|
|
|
|
const {
|
|
|
|
body,
|
|
|
|
directives
|
|
|
|
} = path.node;
|
|
|
|
path.node.directives = [];
|
|
|
|
path.node.body = [];
|
2024-01-16 21:26:16 +08:00
|
|
|
const amdFactoryCall = path.pushContainer("body", wrapper)[0].get("expression");
|
|
|
|
const amdFactoryCallArgs = amdFactoryCall.get("arguments");
|
|
|
|
const amdFactory = amdFactoryCallArgs[amdFactoryCallArgs.length - 1].get("body");
|
2023-12-18 13:12:25 +08:00
|
|
|
amdFactory.pushContainer("directives", directives);
|
|
|
|
amdFactory.pushContainer("body", body);
|
|
|
|
}
|
2024-01-16 21:26:16 +08:00
|
|
|
var _default = exports.default = (0, _helperPluginUtils.declare)((api, options) => {
|
|
|
|
var _api$assumption, _api$assumption2;
|
2023-12-18 13:12:25 +08:00
|
|
|
api.assertVersion(7);
|
|
|
|
const {
|
|
|
|
allowTopLevelThis,
|
|
|
|
strict,
|
|
|
|
strictMode,
|
2024-01-16 21:26:16 +08:00
|
|
|
importInterop,
|
2023-12-18 13:12:25 +08:00
|
|
|
noInterop
|
|
|
|
} = options;
|
2024-01-16 21:26:16 +08:00
|
|
|
const constantReexports = (_api$assumption = api.assumption("constantReexports")) != null ? _api$assumption : options.loose;
|
|
|
|
const enumerableModuleMeta = (_api$assumption2 = api.assumption("enumerableModuleMeta")) != null ? _api$assumption2 : options.loose;
|
2023-12-18 13:12:25 +08:00
|
|
|
return {
|
|
|
|
name: "transform-modules-amd",
|
|
|
|
pre() {
|
|
|
|
this.file.set("@babel/plugin-transform-modules-*", "amd");
|
|
|
|
},
|
|
|
|
visitor: {
|
2024-01-16 21:26:16 +08:00
|
|
|
["CallExpression" + (api.types.importExpression ? "|ImportExpression" : "")](path, state) {
|
2023-12-18 13:12:25 +08:00
|
|
|
if (!this.file.has("@babel/plugin-proposal-dynamic-import")) return;
|
2024-01-16 21:26:16 +08:00
|
|
|
if (path.isCallExpression() && !path.get("callee").isImport()) return;
|
2023-12-18 13:12:25 +08:00
|
|
|
let {
|
|
|
|
requireId,
|
|
|
|
resolveId,
|
|
|
|
rejectId
|
|
|
|
} = state;
|
|
|
|
if (!requireId) {
|
|
|
|
requireId = path.scope.generateUidIdentifier("require");
|
|
|
|
state.requireId = requireId;
|
|
|
|
}
|
|
|
|
if (!resolveId || !rejectId) {
|
|
|
|
resolveId = path.scope.generateUidIdentifier("resolve");
|
|
|
|
rejectId = path.scope.generateUidIdentifier("reject");
|
|
|
|
state.resolveId = resolveId;
|
|
|
|
state.rejectId = rejectId;
|
|
|
|
}
|
2024-01-16 21:26:16 +08:00
|
|
|
let result = _core.types.identifier("imported");
|
|
|
|
if (!noInterop) {
|
|
|
|
result = (0, _helperModuleTransforms.wrapInterop)(this.file.path, result, "namespace");
|
|
|
|
}
|
|
|
|
path.replaceWith((0, _helperModuleTransforms.buildDynamicImport)(path.node, false, false, specifier => _core.template.expression.ast`
|
|
|
|
new Promise((${resolveId}, ${rejectId}) =>
|
|
|
|
${requireId}(
|
|
|
|
[${specifier}],
|
|
|
|
imported => ${_core.types.cloneNode(resolveId)}(${result}),
|
|
|
|
${_core.types.cloneNode(rejectId)}
|
|
|
|
)
|
2023-12-18 13:12:25 +08:00
|
|
|
)
|
2024-01-16 21:26:16 +08:00
|
|
|
`));
|
2023-12-18 13:12:25 +08:00
|
|
|
},
|
|
|
|
Program: {
|
|
|
|
exit(path, {
|
|
|
|
requireId
|
|
|
|
}) {
|
2024-01-16 21:26:16 +08:00
|
|
|
if (!(0, _helperModuleTransforms.isModule)(path)) {
|
2023-12-18 13:12:25 +08:00
|
|
|
if (requireId) {
|
|
|
|
injectWrapper(path, buildAnonymousWrapper({
|
2024-01-16 21:26:16 +08:00
|
|
|
REQUIRE: _core.types.cloneNode(requireId)
|
2023-12-18 13:12:25 +08:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const amdArgs = [];
|
|
|
|
const importNames = [];
|
|
|
|
if (requireId) {
|
2024-01-16 21:26:16 +08:00
|
|
|
amdArgs.push(_core.types.stringLiteral("require"));
|
|
|
|
importNames.push(_core.types.cloneNode(requireId));
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
2024-01-16 21:26:16 +08:00
|
|
|
let moduleName = (0, _helperModuleTransforms.getModuleName)(this.file.opts, options);
|
|
|
|
if (moduleName) moduleName = _core.types.stringLiteral(moduleName);
|
2023-12-18 13:12:25 +08:00
|
|
|
const {
|
|
|
|
meta,
|
|
|
|
headers
|
2024-01-16 21:26:16 +08:00
|
|
|
} = (0, _helperModuleTransforms.rewriteModuleStatementsAndPrepareHeader)(path, {
|
|
|
|
enumerableModuleMeta,
|
|
|
|
constantReexports,
|
2023-12-18 13:12:25 +08:00
|
|
|
strict,
|
|
|
|
strictMode,
|
|
|
|
allowTopLevelThis,
|
2024-01-16 21:26:16 +08:00
|
|
|
importInterop,
|
|
|
|
noInterop,
|
|
|
|
filename: this.file.opts.filename
|
2023-12-18 13:12:25 +08:00
|
|
|
});
|
2024-01-16 21:26:16 +08:00
|
|
|
if ((0, _helperModuleTransforms.hasExports)(meta)) {
|
|
|
|
amdArgs.push(_core.types.stringLiteral("exports"));
|
|
|
|
importNames.push(_core.types.identifier(meta.exportName));
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
|
|
|
for (const [source, metadata] of meta.source) {
|
2024-01-16 21:26:16 +08:00
|
|
|
amdArgs.push(_core.types.stringLiteral(source));
|
|
|
|
importNames.push(_core.types.identifier(metadata.name));
|
|
|
|
if (!(0, _helperModuleTransforms.isSideEffectImport)(metadata)) {
|
|
|
|
const interop = (0, _helperModuleTransforms.wrapInterop)(path, _core.types.identifier(metadata.name), metadata.interop);
|
2023-12-18 13:12:25 +08:00
|
|
|
if (interop) {
|
2024-01-16 21:26:16 +08:00
|
|
|
const header = _core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.identifier(metadata.name), interop));
|
2023-12-18 13:12:25 +08:00
|
|
|
header.loc = metadata.loc;
|
|
|
|
headers.push(header);
|
|
|
|
}
|
|
|
|
}
|
2024-01-16 21:26:16 +08:00
|
|
|
headers.push(...(0, _helperModuleTransforms.buildNamespaceInitStatements)(meta, metadata, constantReexports));
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
2024-01-16 21:26:16 +08:00
|
|
|
(0, _helperModuleTransforms.ensureStatementsHoisted)(headers);
|
2023-12-18 13:12:25 +08:00
|
|
|
path.unshiftContainer("body", headers);
|
|
|
|
injectWrapper(path, buildWrapper({
|
|
|
|
MODULE_NAME: moduleName,
|
2024-01-16 21:26:16 +08:00
|
|
|
AMD_ARGUMENTS: _core.types.arrayExpression(amdArgs),
|
2023-12-18 13:12:25 +08:00
|
|
|
IMPORT_NAMES: importNames
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
//# sourceMappingURL=index.js.map
|