1 | ;
|
2 | /**
|
3 | * @license
|
4 | * Copyright (c) 2018 The Polymer Project Authors. All rights reserved.
|
5 | * This code may only be used under the BSD style license found at
|
6 | * http://polymer.github.io/LICENSE.txt
|
7 | * The complete set of authors may be found at
|
8 | * http://polymer.github.io/AUTHORS.txt
|
9 | * The complete set of contributors may be found at
|
10 | * http://polymer.github.io/CONTRIBUTORS.txt
|
11 | * Code distributed by Google as part of the polymer project is also
|
12 | * subject to an additional IP rights grant found at
|
13 | * http://polymer.github.io/PATENTS.txt
|
14 | */
|
15 | Object.defineProperty(exports, "__esModule", { value: true });
|
16 | const plugin_syntax_dynamic_import_1 = require("@babel/plugin-syntax-dynamic-import");
|
17 | const template_1 = require("@babel/template");
|
18 | const ast = template_1.default.ast;
|
19 | /**
|
20 | * Rewrites dynamic import() calls to AMD async require() calls.
|
21 | */
|
22 | exports.dynamicImportAmd = {
|
23 | inherits: plugin_syntax_dynamic_import_1.default,
|
24 | visitor: {
|
25 | Program(path) {
|
26 | // We transform dynamic import() into a Promise whose initializer calls
|
27 | // require(). We must use the "local" require - the one provided by the
|
28 | // AMD loaded when a module explicitly depends on the "require" module ID.
|
29 | // To get the emitted define() call to depend on "require", we inject an
|
30 | // import of a module called "require".
|
31 | // Collect all the NodePaths to dynamic import() expressions
|
32 | // and all the identifiers in scope for them.
|
33 | const identifiers = new Set();
|
34 | const dynamicImports = [];
|
35 | path.traverse({
|
36 | CallExpression(path) {
|
37 | if (path.node.callee.type === 'Import') {
|
38 | dynamicImports.push(path);
|
39 | const bindings = path.scope.getAllBindings();
|
40 | for (const name of Object.keys(bindings)) {
|
41 | identifiers.add(name);
|
42 | }
|
43 | }
|
44 | }
|
45 | });
|
46 | if (dynamicImports.length === 0) {
|
47 | return;
|
48 | }
|
49 | // Choose a unique name to import "require" as.
|
50 | let requireId = undefined;
|
51 | do {
|
52 | requireId = path.scope.generateUidIdentifier('require');
|
53 | } while (identifiers.has(requireId.name));
|
54 | // Inject the import of "require"
|
55 | const statements = path.node.body;
|
56 | statements.unshift(ast `import * as ${requireId} from 'require';`);
|
57 | // Transform the dynamic import callsites
|
58 | for (const importPath of dynamicImports) {
|
59 | const specifier = importPath.node.arguments[0];
|
60 | // Call as `require.default` because the AMD transformer that we assume
|
61 | // is running next will rewrite `require` from a function to a module
|
62 | // object with the function at `default`.
|
63 | importPath.replaceWith(ast `(
|
64 | new Promise((res, rej) => ${requireId}.default([${specifier}], res, rej))
|
65 | )`);
|
66 | }
|
67 | },
|
68 | },
|
69 | };
|
70 | //# sourceMappingURL=babel-plugin-dynamic-import-amd.js.map |
\ | No newline at end of file |