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