[79a0317] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.default = void 0;
|
---|
| 7 | var _helperPluginUtils = require("@babel/helper-plugin-utils");
|
---|
| 8 | var _helperModuleTransforms = require("@babel/helper-module-transforms");
|
---|
| 9 | var _core = require("@babel/core");
|
---|
| 10 | const buildWrapper = _core.template.statement(`
|
---|
| 11 | define(MODULE_NAME, AMD_ARGUMENTS, function(IMPORT_NAMES) {
|
---|
| 12 | })
|
---|
| 13 | `);
|
---|
| 14 | const buildAnonymousWrapper = _core.template.statement(`
|
---|
| 15 | define(["require"], function(REQUIRE) {
|
---|
| 16 | })
|
---|
| 17 | `);
|
---|
| 18 | function injectWrapper(path, wrapper) {
|
---|
| 19 | const {
|
---|
| 20 | body,
|
---|
| 21 | directives
|
---|
| 22 | } = path.node;
|
---|
| 23 | path.node.directives = [];
|
---|
| 24 | path.node.body = [];
|
---|
| 25 | const amdFactoryCall = path.pushContainer("body", wrapper)[0].get("expression");
|
---|
| 26 | const amdFactoryCallArgs = amdFactoryCall.get("arguments");
|
---|
| 27 | const amdFactory = amdFactoryCallArgs[amdFactoryCallArgs.length - 1].get("body");
|
---|
| 28 | amdFactory.pushContainer("directives", directives);
|
---|
| 29 | amdFactory.pushContainer("body", body);
|
---|
| 30 | }
|
---|
| 31 | var _default = exports.default = (0, _helperPluginUtils.declare)((api, options) => {
|
---|
| 32 | var _api$assumption, _api$assumption2;
|
---|
| 33 | api.assertVersion(7);
|
---|
| 34 | const {
|
---|
| 35 | allowTopLevelThis,
|
---|
| 36 | strict,
|
---|
| 37 | strictMode,
|
---|
| 38 | importInterop,
|
---|
| 39 | noInterop
|
---|
| 40 | } = options;
|
---|
| 41 | const constantReexports = (_api$assumption = api.assumption("constantReexports")) != null ? _api$assumption : options.loose;
|
---|
| 42 | const enumerableModuleMeta = (_api$assumption2 = api.assumption("enumerableModuleMeta")) != null ? _api$assumption2 : options.loose;
|
---|
| 43 | return {
|
---|
| 44 | name: "transform-modules-amd",
|
---|
| 45 | pre() {
|
---|
| 46 | this.file.set("@babel/plugin-transform-modules-*", "amd");
|
---|
| 47 | },
|
---|
| 48 | visitor: {
|
---|
| 49 | ["CallExpression" + (api.types.importExpression ? "|ImportExpression" : "")](path, state) {
|
---|
| 50 | if (!this.file.has("@babel/plugin-proposal-dynamic-import")) return;
|
---|
| 51 | if (path.isCallExpression() && !path.get("callee").isImport()) return;
|
---|
| 52 | let {
|
---|
| 53 | requireId,
|
---|
| 54 | resolveId,
|
---|
| 55 | rejectId
|
---|
| 56 | } = state;
|
---|
| 57 | if (!requireId) {
|
---|
| 58 | requireId = path.scope.generateUidIdentifier("require");
|
---|
| 59 | state.requireId = requireId;
|
---|
| 60 | }
|
---|
| 61 | if (!resolveId || !rejectId) {
|
---|
| 62 | resolveId = path.scope.generateUidIdentifier("resolve");
|
---|
| 63 | rejectId = path.scope.generateUidIdentifier("reject");
|
---|
| 64 | state.resolveId = resolveId;
|
---|
| 65 | state.rejectId = rejectId;
|
---|
| 66 | }
|
---|
| 67 | let result = _core.types.identifier("imported");
|
---|
| 68 | if (!noInterop) {
|
---|
| 69 | result = (0, _helperModuleTransforms.wrapInterop)(this.file.path, result, "namespace");
|
---|
| 70 | }
|
---|
| 71 | path.replaceWith((0, _helperModuleTransforms.buildDynamicImport)(path.node, false, false, specifier => _core.template.expression.ast`
|
---|
| 72 | new Promise((${resolveId}, ${rejectId}) =>
|
---|
| 73 | ${requireId}(
|
---|
| 74 | [${specifier}],
|
---|
| 75 | imported => ${_core.types.cloneNode(resolveId)}(${result}),
|
---|
| 76 | ${_core.types.cloneNode(rejectId)}
|
---|
| 77 | )
|
---|
| 78 | )
|
---|
| 79 | `));
|
---|
| 80 | },
|
---|
| 81 | Program: {
|
---|
| 82 | exit(path, {
|
---|
| 83 | requireId
|
---|
| 84 | }) {
|
---|
| 85 | if (!(0, _helperModuleTransforms.isModule)(path)) {
|
---|
| 86 | if (requireId) {
|
---|
| 87 | injectWrapper(path, buildAnonymousWrapper({
|
---|
| 88 | REQUIRE: _core.types.cloneNode(requireId)
|
---|
| 89 | }));
|
---|
| 90 | }
|
---|
| 91 | return;
|
---|
| 92 | }
|
---|
| 93 | const amdArgs = [];
|
---|
| 94 | const importNames = [];
|
---|
| 95 | if (requireId) {
|
---|
| 96 | amdArgs.push(_core.types.stringLiteral("require"));
|
---|
| 97 | importNames.push(_core.types.cloneNode(requireId));
|
---|
| 98 | }
|
---|
| 99 | let moduleName = (0, _helperModuleTransforms.getModuleName)(this.file.opts, options);
|
---|
| 100 | if (moduleName) moduleName = _core.types.stringLiteral(moduleName);
|
---|
| 101 | const {
|
---|
| 102 | meta,
|
---|
| 103 | headers
|
---|
| 104 | } = (0, _helperModuleTransforms.rewriteModuleStatementsAndPrepareHeader)(path, {
|
---|
| 105 | enumerableModuleMeta,
|
---|
| 106 | constantReexports,
|
---|
| 107 | strict,
|
---|
| 108 | strictMode,
|
---|
| 109 | allowTopLevelThis,
|
---|
| 110 | importInterop,
|
---|
| 111 | noInterop,
|
---|
| 112 | filename: this.file.opts.filename
|
---|
| 113 | });
|
---|
| 114 | if ((0, _helperModuleTransforms.hasExports)(meta)) {
|
---|
| 115 | amdArgs.push(_core.types.stringLiteral("exports"));
|
---|
| 116 | importNames.push(_core.types.identifier(meta.exportName));
|
---|
| 117 | }
|
---|
| 118 | for (const [source, metadata] of meta.source) {
|
---|
| 119 | amdArgs.push(_core.types.stringLiteral(source));
|
---|
| 120 | importNames.push(_core.types.identifier(metadata.name));
|
---|
| 121 | if (!(0, _helperModuleTransforms.isSideEffectImport)(metadata)) {
|
---|
| 122 | const interop = (0, _helperModuleTransforms.wrapInterop)(path, _core.types.identifier(metadata.name), metadata.interop);
|
---|
| 123 | if (interop) {
|
---|
| 124 | const header = _core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.identifier(metadata.name), interop));
|
---|
| 125 | header.loc = metadata.loc;
|
---|
| 126 | headers.push(header);
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 | headers.push(...(0, _helperModuleTransforms.buildNamespaceInitStatements)(meta, metadata, constantReexports));
|
---|
| 130 | }
|
---|
| 131 | (0, _helperModuleTransforms.ensureStatementsHoisted)(headers);
|
---|
| 132 | path.unshiftContainer("body", headers);
|
---|
| 133 | injectWrapper(path, buildWrapper({
|
---|
| 134 | MODULE_NAME: moduleName,
|
---|
| 135 | AMD_ARGUMENTS: _core.types.arrayExpression(amdArgs),
|
---|
| 136 | IMPORT_NAMES: importNames
|
---|
| 137 | }));
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 | };
|
---|
| 142 | });
|
---|
| 143 |
|
---|
| 144 | //# sourceMappingURL=index.js.map
|
---|