1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.getExportSpecifierName = getExportSpecifierName;
|
---|
7 | exports.default = void 0;
|
---|
8 |
|
---|
9 | var _helperPluginUtils = require("@babel/helper-plugin-utils");
|
---|
10 |
|
---|
11 | var _helperHoistVariables = require("@babel/helper-hoist-variables");
|
---|
12 |
|
---|
13 | var _core = require("@babel/core");
|
---|
14 |
|
---|
15 | var _utils = require("babel-plugin-dynamic-import-node/utils");
|
---|
16 |
|
---|
17 | var _helperModuleTransforms = require("@babel/helper-module-transforms");
|
---|
18 |
|
---|
19 | var _helperValidatorIdentifier = require("@babel/helper-validator-identifier");
|
---|
20 |
|
---|
21 | const buildTemplate = (0, _core.template)(`
|
---|
22 | SYSTEM_REGISTER(MODULE_NAME, SOURCES, function (EXPORT_IDENTIFIER, CONTEXT_IDENTIFIER) {
|
---|
23 | "use strict";
|
---|
24 | BEFORE_BODY;
|
---|
25 | return {
|
---|
26 | setters: SETTERS,
|
---|
27 | execute: EXECUTE,
|
---|
28 | };
|
---|
29 | });
|
---|
30 | `);
|
---|
31 | const buildExportAll = (0, _core.template)(`
|
---|
32 | for (var KEY in TARGET) {
|
---|
33 | if (KEY !== "default" && KEY !== "__esModule") EXPORT_OBJ[KEY] = TARGET[KEY];
|
---|
34 | }
|
---|
35 | `);
|
---|
36 | const MISSING_PLUGIN_WARNING = `\
|
---|
37 | WARNING: Dynamic import() transformation must be enabled using the
|
---|
38 | @babel/plugin-proposal-dynamic-import plugin. Babel 8 will
|
---|
39 | no longer transform import() without using that plugin.
|
---|
40 | `;
|
---|
41 | const MISSING_PLUGIN_ERROR = `\
|
---|
42 | ERROR: Dynamic import() transformation must be enabled using the
|
---|
43 | @babel/plugin-proposal-dynamic-import plugin. Babel 8
|
---|
44 | no longer transforms import() without using that plugin.
|
---|
45 | `;
|
---|
46 |
|
---|
47 | function getExportSpecifierName(node, stringSpecifiers) {
|
---|
48 | if (node.type === "Identifier") {
|
---|
49 | return node.name;
|
---|
50 | } else if (node.type === "StringLiteral") {
|
---|
51 | const stringValue = node.value;
|
---|
52 |
|
---|
53 | if (!(0, _helperValidatorIdentifier.isIdentifierName)(stringValue)) {
|
---|
54 | stringSpecifiers.add(stringValue);
|
---|
55 | }
|
---|
56 |
|
---|
57 | return stringValue;
|
---|
58 | } else {
|
---|
59 | throw new Error(`Expected export specifier to be either Identifier or StringLiteral, got ${node.type}`);
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | function constructExportCall(path, exportIdent, exportNames, exportValues, exportStarTarget, stringSpecifiers) {
|
---|
64 | const statements = [];
|
---|
65 |
|
---|
66 | if (!exportStarTarget) {
|
---|
67 | if (exportNames.length === 1) {
|
---|
68 | statements.push(_core.types.expressionStatement(_core.types.callExpression(exportIdent, [_core.types.stringLiteral(exportNames[0]), exportValues[0]])));
|
---|
69 | } else {
|
---|
70 | const objectProperties = [];
|
---|
71 |
|
---|
72 | for (let i = 0; i < exportNames.length; i++) {
|
---|
73 | const exportName = exportNames[i];
|
---|
74 | const exportValue = exportValues[i];
|
---|
75 | objectProperties.push(_core.types.objectProperty(stringSpecifiers.has(exportName) ? _core.types.stringLiteral(exportName) : _core.types.identifier(exportName), exportValue));
|
---|
76 | }
|
---|
77 |
|
---|
78 | statements.push(_core.types.expressionStatement(_core.types.callExpression(exportIdent, [_core.types.objectExpression(objectProperties)])));
|
---|
79 | }
|
---|
80 | } else {
|
---|
81 | const exportObj = path.scope.generateUid("exportObj");
|
---|
82 | statements.push(_core.types.variableDeclaration("var", [_core.types.variableDeclarator(_core.types.identifier(exportObj), _core.types.objectExpression([]))]));
|
---|
83 | statements.push(buildExportAll({
|
---|
84 | KEY: path.scope.generateUidIdentifier("key"),
|
---|
85 | EXPORT_OBJ: _core.types.identifier(exportObj),
|
---|
86 | TARGET: exportStarTarget
|
---|
87 | }));
|
---|
88 |
|
---|
89 | for (let i = 0; i < exportNames.length; i++) {
|
---|
90 | const exportName = exportNames[i];
|
---|
91 | const exportValue = exportValues[i];
|
---|
92 | statements.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.memberExpression(_core.types.identifier(exportObj), _core.types.identifier(exportName)), exportValue)));
|
---|
93 | }
|
---|
94 |
|
---|
95 | statements.push(_core.types.expressionStatement(_core.types.callExpression(exportIdent, [_core.types.identifier(exportObj)])));
|
---|
96 | }
|
---|
97 |
|
---|
98 | return statements;
|
---|
99 | }
|
---|
100 |
|
---|
101 | var _default = (0, _helperPluginUtils.declare)((api, options) => {
|
---|
102 | api.assertVersion(7);
|
---|
103 | const {
|
---|
104 | systemGlobal = "System",
|
---|
105 | allowTopLevelThis = false
|
---|
106 | } = options;
|
---|
107 | const IGNORE_REASSIGNMENT_SYMBOL = Symbol();
|
---|
108 | const reassignmentVisitor = {
|
---|
109 | "AssignmentExpression|UpdateExpression"(path) {
|
---|
110 | if (path.node[IGNORE_REASSIGNMENT_SYMBOL]) return;
|
---|
111 | path.node[IGNORE_REASSIGNMENT_SYMBOL] = true;
|
---|
112 | const arg = path.get(path.isAssignmentExpression() ? "left" : "argument");
|
---|
113 |
|
---|
114 | if (arg.isObjectPattern() || arg.isArrayPattern()) {
|
---|
115 | const exprs = [path.node];
|
---|
116 |
|
---|
117 | for (const name of Object.keys(arg.getBindingIdentifiers())) {
|
---|
118 | if (this.scope.getBinding(name) !== path.scope.getBinding(name)) {
|
---|
119 | return;
|
---|
120 | }
|
---|
121 |
|
---|
122 | const exportedNames = this.exports[name];
|
---|
123 | if (!exportedNames) return;
|
---|
124 |
|
---|
125 | for (const exportedName of exportedNames) {
|
---|
126 | exprs.push(this.buildCall(exportedName, _core.types.identifier(name)).expression);
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | path.replaceWith(_core.types.sequenceExpression(exprs));
|
---|
131 | return;
|
---|
132 | }
|
---|
133 |
|
---|
134 | if (!arg.isIdentifier()) return;
|
---|
135 | const name = arg.node.name;
|
---|
136 | if (this.scope.getBinding(name) !== path.scope.getBinding(name)) return;
|
---|
137 | const exportedNames = this.exports[name];
|
---|
138 | if (!exportedNames) return;
|
---|
139 | let node = path.node;
|
---|
140 | const isPostUpdateExpression = path.isUpdateExpression({
|
---|
141 | prefix: false
|
---|
142 | });
|
---|
143 |
|
---|
144 | if (isPostUpdateExpression) {
|
---|
145 | node = _core.types.binaryExpression(node.operator[0], _core.types.unaryExpression("+", _core.types.cloneNode(node.argument)), _core.types.numericLiteral(1));
|
---|
146 | }
|
---|
147 |
|
---|
148 | for (const exportedName of exportedNames) {
|
---|
149 | node = this.buildCall(exportedName, node).expression;
|
---|
150 | }
|
---|
151 |
|
---|
152 | if (isPostUpdateExpression) {
|
---|
153 | node = _core.types.sequenceExpression([node, path.node]);
|
---|
154 | }
|
---|
155 |
|
---|
156 | path.replaceWith(node);
|
---|
157 | }
|
---|
158 |
|
---|
159 | };
|
---|
160 | return {
|
---|
161 | name: "transform-modules-systemjs",
|
---|
162 |
|
---|
163 | pre() {
|
---|
164 | this.file.set("@babel/plugin-transform-modules-*", "systemjs");
|
---|
165 | },
|
---|
166 |
|
---|
167 | visitor: {
|
---|
168 | CallExpression(path, state) {
|
---|
169 | if (_core.types.isImport(path.node.callee)) {
|
---|
170 | if (!this.file.has("@babel/plugin-proposal-dynamic-import")) {
|
---|
171 | {
|
---|
172 | console.warn(MISSING_PLUGIN_WARNING);
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | path.replaceWith(_core.types.callExpression(_core.types.memberExpression(_core.types.identifier(state.contextIdent), _core.types.identifier("import")), [(0, _utils.getImportSource)(_core.types, path.node)]));
|
---|
177 | }
|
---|
178 | },
|
---|
179 |
|
---|
180 | MetaProperty(path, state) {
|
---|
181 | if (path.node.meta.name === "import" && path.node.property.name === "meta") {
|
---|
182 | path.replaceWith(_core.types.memberExpression(_core.types.identifier(state.contextIdent), _core.types.identifier("meta")));
|
---|
183 | }
|
---|
184 | },
|
---|
185 |
|
---|
186 | ReferencedIdentifier(path, state) {
|
---|
187 | if (path.node.name === "__moduleName" && !path.scope.hasBinding("__moduleName")) {
|
---|
188 | path.replaceWith(_core.types.memberExpression(_core.types.identifier(state.contextIdent), _core.types.identifier("id")));
|
---|
189 | }
|
---|
190 | },
|
---|
191 |
|
---|
192 | Program: {
|
---|
193 | enter(path, state) {
|
---|
194 | state.contextIdent = path.scope.generateUid("context");
|
---|
195 | state.stringSpecifiers = new Set();
|
---|
196 |
|
---|
197 | if (!allowTopLevelThis) {
|
---|
198 | (0, _helperModuleTransforms.rewriteThis)(path);
|
---|
199 | }
|
---|
200 | },
|
---|
201 |
|
---|
202 | exit(path, state) {
|
---|
203 | const scope = path.scope;
|
---|
204 | const exportIdent = scope.generateUid("export");
|
---|
205 | const {
|
---|
206 | contextIdent,
|
---|
207 | stringSpecifiers
|
---|
208 | } = state;
|
---|
209 | const exportMap = Object.create(null);
|
---|
210 | const modules = [];
|
---|
211 | const beforeBody = [];
|
---|
212 | const setters = [];
|
---|
213 | const sources = [];
|
---|
214 | const variableIds = [];
|
---|
215 | const removedPaths = [];
|
---|
216 |
|
---|
217 | function addExportName(key, val) {
|
---|
218 | exportMap[key] = exportMap[key] || [];
|
---|
219 | exportMap[key].push(val);
|
---|
220 | }
|
---|
221 |
|
---|
222 | function pushModule(source, key, specifiers) {
|
---|
223 | let module;
|
---|
224 | modules.forEach(function (m) {
|
---|
225 | if (m.key === source) {
|
---|
226 | module = m;
|
---|
227 | }
|
---|
228 | });
|
---|
229 |
|
---|
230 | if (!module) {
|
---|
231 | modules.push(module = {
|
---|
232 | key: source,
|
---|
233 | imports: [],
|
---|
234 | exports: []
|
---|
235 | });
|
---|
236 | }
|
---|
237 |
|
---|
238 | module[key] = module[key].concat(specifiers);
|
---|
239 | }
|
---|
240 |
|
---|
241 | function buildExportCall(name, val) {
|
---|
242 | return _core.types.expressionStatement(_core.types.callExpression(_core.types.identifier(exportIdent), [_core.types.stringLiteral(name), val]));
|
---|
243 | }
|
---|
244 |
|
---|
245 | const exportNames = [];
|
---|
246 | const exportValues = [];
|
---|
247 | const body = path.get("body");
|
---|
248 |
|
---|
249 | for (const path of body) {
|
---|
250 | if (path.isFunctionDeclaration()) {
|
---|
251 | beforeBody.push(path.node);
|
---|
252 | removedPaths.push(path);
|
---|
253 | } else if (path.isClassDeclaration()) {
|
---|
254 | variableIds.push(_core.types.cloneNode(path.node.id));
|
---|
255 | path.replaceWith(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(path.node.id), _core.types.toExpression(path.node))));
|
---|
256 | } else if (path.isImportDeclaration()) {
|
---|
257 | const source = path.node.source.value;
|
---|
258 | pushModule(source, "imports", path.node.specifiers);
|
---|
259 |
|
---|
260 | for (const name of Object.keys(path.getBindingIdentifiers())) {
|
---|
261 | scope.removeBinding(name);
|
---|
262 | variableIds.push(_core.types.identifier(name));
|
---|
263 | }
|
---|
264 |
|
---|
265 | path.remove();
|
---|
266 | } else if (path.isExportAllDeclaration()) {
|
---|
267 | pushModule(path.node.source.value, "exports", path.node);
|
---|
268 | path.remove();
|
---|
269 | } else if (path.isExportDefaultDeclaration()) {
|
---|
270 | const declar = path.get("declaration");
|
---|
271 | const id = declar.node.id;
|
---|
272 |
|
---|
273 | if (declar.isClassDeclaration()) {
|
---|
274 | if (id) {
|
---|
275 | exportNames.push("default");
|
---|
276 | exportValues.push(scope.buildUndefinedNode());
|
---|
277 | variableIds.push(_core.types.cloneNode(id));
|
---|
278 | addExportName(id.name, "default");
|
---|
279 | path.replaceWith(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(id), _core.types.toExpression(declar.node))));
|
---|
280 | } else {
|
---|
281 | exportNames.push("default");
|
---|
282 | exportValues.push(_core.types.toExpression(declar.node));
|
---|
283 | removedPaths.push(path);
|
---|
284 | }
|
---|
285 | } else if (declar.isFunctionDeclaration()) {
|
---|
286 | if (id) {
|
---|
287 | beforeBody.push(declar.node);
|
---|
288 | exportNames.push("default");
|
---|
289 | exportValues.push(_core.types.cloneNode(id));
|
---|
290 | addExportName(id.name, "default");
|
---|
291 | } else {
|
---|
292 | exportNames.push("default");
|
---|
293 | exportValues.push(_core.types.toExpression(declar.node));
|
---|
294 | }
|
---|
295 |
|
---|
296 | removedPaths.push(path);
|
---|
297 | } else {
|
---|
298 | path.replaceWith(buildExportCall("default", declar.node));
|
---|
299 | }
|
---|
300 | } else if (path.isExportNamedDeclaration()) {
|
---|
301 | const declar = path.get("declaration");
|
---|
302 |
|
---|
303 | if (declar.node) {
|
---|
304 | path.replaceWith(declar);
|
---|
305 |
|
---|
306 | if (path.isFunction()) {
|
---|
307 | const node = declar.node;
|
---|
308 | const name = node.id.name;
|
---|
309 | addExportName(name, name);
|
---|
310 | beforeBody.push(node);
|
---|
311 | exportNames.push(name);
|
---|
312 | exportValues.push(_core.types.cloneNode(node.id));
|
---|
313 | removedPaths.push(path);
|
---|
314 | } else if (path.isClass()) {
|
---|
315 | const name = declar.node.id.name;
|
---|
316 | exportNames.push(name);
|
---|
317 | exportValues.push(scope.buildUndefinedNode());
|
---|
318 | variableIds.push(_core.types.cloneNode(declar.node.id));
|
---|
319 | path.replaceWith(_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.cloneNode(declar.node.id), _core.types.toExpression(declar.node))));
|
---|
320 | addExportName(name, name);
|
---|
321 | } else {
|
---|
322 | for (const name of Object.keys(declar.getBindingIdentifiers())) {
|
---|
323 | addExportName(name, name);
|
---|
324 | }
|
---|
325 | }
|
---|
326 | } else {
|
---|
327 | const specifiers = path.node.specifiers;
|
---|
328 |
|
---|
329 | if (specifiers != null && specifiers.length) {
|
---|
330 | if (path.node.source) {
|
---|
331 | pushModule(path.node.source.value, "exports", specifiers);
|
---|
332 | path.remove();
|
---|
333 | } else {
|
---|
334 | const nodes = [];
|
---|
335 |
|
---|
336 | for (const specifier of specifiers) {
|
---|
337 | const {
|
---|
338 | local,
|
---|
339 | exported
|
---|
340 | } = specifier;
|
---|
341 | const binding = scope.getBinding(local.name);
|
---|
342 | const exportedName = getExportSpecifierName(exported, stringSpecifiers);
|
---|
343 |
|
---|
344 | if (binding && _core.types.isFunctionDeclaration(binding.path.node)) {
|
---|
345 | exportNames.push(exportedName);
|
---|
346 | exportValues.push(_core.types.cloneNode(local));
|
---|
347 | } else if (!binding) {
|
---|
348 | nodes.push(buildExportCall(exportedName, local));
|
---|
349 | }
|
---|
350 |
|
---|
351 | addExportName(local.name, exportedName);
|
---|
352 | }
|
---|
353 |
|
---|
354 | path.replaceWithMultiple(nodes);
|
---|
355 | }
|
---|
356 | } else {
|
---|
357 | path.remove();
|
---|
358 | }
|
---|
359 | }
|
---|
360 | }
|
---|
361 | }
|
---|
362 |
|
---|
363 | modules.forEach(function (specifiers) {
|
---|
364 | const setterBody = [];
|
---|
365 | const target = scope.generateUid(specifiers.key);
|
---|
366 |
|
---|
367 | for (let specifier of specifiers.imports) {
|
---|
368 | if (_core.types.isImportNamespaceSpecifier(specifier)) {
|
---|
369 | setterBody.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", specifier.local, _core.types.identifier(target))));
|
---|
370 | } else if (_core.types.isImportDefaultSpecifier(specifier)) {
|
---|
371 | specifier = _core.types.importSpecifier(specifier.local, _core.types.identifier("default"));
|
---|
372 | }
|
---|
373 |
|
---|
374 | if (_core.types.isImportSpecifier(specifier)) {
|
---|
375 | const {
|
---|
376 | imported
|
---|
377 | } = specifier;
|
---|
378 | setterBody.push(_core.types.expressionStatement(_core.types.assignmentExpression("=", specifier.local, _core.types.memberExpression(_core.types.identifier(target), specifier.imported, imported.type === "StringLiteral"))));
|
---|
379 | }
|
---|
380 | }
|
---|
381 |
|
---|
382 | if (specifiers.exports.length) {
|
---|
383 | const exportNames = [];
|
---|
384 | const exportValues = [];
|
---|
385 | let hasExportStar = false;
|
---|
386 |
|
---|
387 | for (const node of specifiers.exports) {
|
---|
388 | if (_core.types.isExportAllDeclaration(node)) {
|
---|
389 | hasExportStar = true;
|
---|
390 | } else if (_core.types.isExportSpecifier(node)) {
|
---|
391 | const exportedName = getExportSpecifierName(node.exported, stringSpecifiers);
|
---|
392 | exportNames.push(exportedName);
|
---|
393 | exportValues.push(_core.types.memberExpression(_core.types.identifier(target), node.local, _core.types.isStringLiteral(node.local)));
|
---|
394 | } else {}
|
---|
395 | }
|
---|
396 |
|
---|
397 | setterBody.push(...constructExportCall(path, _core.types.identifier(exportIdent), exportNames, exportValues, hasExportStar ? _core.types.identifier(target) : null, stringSpecifiers));
|
---|
398 | }
|
---|
399 |
|
---|
400 | sources.push(_core.types.stringLiteral(specifiers.key));
|
---|
401 | setters.push(_core.types.functionExpression(null, [_core.types.identifier(target)], _core.types.blockStatement(setterBody)));
|
---|
402 | });
|
---|
403 | let moduleName = (0, _helperModuleTransforms.getModuleName)(this.file.opts, options);
|
---|
404 | if (moduleName) moduleName = _core.types.stringLiteral(moduleName);
|
---|
405 | (0, _helperHoistVariables.default)(path, (id, name, hasInit) => {
|
---|
406 | variableIds.push(id);
|
---|
407 |
|
---|
408 | if (!hasInit && name in exportMap) {
|
---|
409 | for (const exported of exportMap[name]) {
|
---|
410 | exportNames.push(exported);
|
---|
411 | exportValues.push(scope.buildUndefinedNode());
|
---|
412 | }
|
---|
413 | }
|
---|
414 | }, null);
|
---|
415 |
|
---|
416 | if (variableIds.length) {
|
---|
417 | beforeBody.unshift(_core.types.variableDeclaration("var", variableIds.map(id => _core.types.variableDeclarator(id))));
|
---|
418 | }
|
---|
419 |
|
---|
420 | if (exportNames.length) {
|
---|
421 | beforeBody.push(...constructExportCall(path, _core.types.identifier(exportIdent), exportNames, exportValues, null, stringSpecifiers));
|
---|
422 | }
|
---|
423 |
|
---|
424 | path.traverse(reassignmentVisitor, {
|
---|
425 | exports: exportMap,
|
---|
426 | buildCall: buildExportCall,
|
---|
427 | scope
|
---|
428 | });
|
---|
429 |
|
---|
430 | for (const path of removedPaths) {
|
---|
431 | path.remove();
|
---|
432 | }
|
---|
433 |
|
---|
434 | let hasTLA = false;
|
---|
435 | path.traverse({
|
---|
436 | AwaitExpression(path) {
|
---|
437 | hasTLA = true;
|
---|
438 | path.stop();
|
---|
439 | },
|
---|
440 |
|
---|
441 | Function(path) {
|
---|
442 | path.skip();
|
---|
443 | },
|
---|
444 |
|
---|
445 | noScope: true
|
---|
446 | });
|
---|
447 | path.node.body = [buildTemplate({
|
---|
448 | SYSTEM_REGISTER: _core.types.memberExpression(_core.types.identifier(systemGlobal), _core.types.identifier("register")),
|
---|
449 | BEFORE_BODY: beforeBody,
|
---|
450 | MODULE_NAME: moduleName,
|
---|
451 | SETTERS: _core.types.arrayExpression(setters),
|
---|
452 | EXECUTE: _core.types.functionExpression(null, [], _core.types.blockStatement(path.node.body), false, hasTLA),
|
---|
453 | SOURCES: _core.types.arrayExpression(sources),
|
---|
454 | EXPORT_IDENTIFIER: _core.types.identifier(exportIdent),
|
---|
455 | CONTEXT_IDENTIFIER: _core.types.identifier(contextIdent)
|
---|
456 | })];
|
---|
457 | }
|
---|
458 |
|
---|
459 | }
|
---|
460 | }
|
---|
461 | };
|
---|
462 | });
|
---|
463 |
|
---|
464 | exports.default = _default; |
---|