1 | 'use strict';
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 |
|
---|
7 | var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
|
---|
8 |
|
---|
9 | exports.getImportSource = getImportSource;
|
---|
10 | exports.createDynamicImportTransform = createDynamicImportTransform;
|
---|
11 | function getImportSource(t, callNode) {
|
---|
12 | var importArguments = callNode.arguments;
|
---|
13 |
|
---|
14 | var _importArguments = _slicedToArray(importArguments, 1),
|
---|
15 | importPath = _importArguments[0];
|
---|
16 |
|
---|
17 | var isString = t.isStringLiteral(importPath) || t.isTemplateLiteral(importPath);
|
---|
18 | if (isString) {
|
---|
19 | t.removeComments(importPath);
|
---|
20 | return importPath;
|
---|
21 | }
|
---|
22 |
|
---|
23 | return t.templateLiteral([t.templateElement({ raw: '', cooked: '' }), t.templateElement({ raw: '', cooked: '' }, true)], importArguments);
|
---|
24 | }
|
---|
25 |
|
---|
26 | function createDynamicImportTransform(_ref) {
|
---|
27 | var template = _ref.template,
|
---|
28 | t = _ref.types;
|
---|
29 |
|
---|
30 | var builders = {
|
---|
31 | 'static': {
|
---|
32 | interop: template('Promise.resolve().then(() => INTEROP(require(SOURCE)))'),
|
---|
33 | noInterop: template('Promise.resolve().then(() => require(SOURCE))')
|
---|
34 | },
|
---|
35 | dynamic: {
|
---|
36 | interop: template('Promise.resolve(SOURCE).then(s => INTEROP(require(s)))'),
|
---|
37 | noInterop: template('Promise.resolve(SOURCE).then(s => require(s))')
|
---|
38 | }
|
---|
39 | };
|
---|
40 |
|
---|
41 | var visited = typeof WeakSet === 'function' && new WeakSet();
|
---|
42 |
|
---|
43 | var isString = function isString(node) {
|
---|
44 | return t.isStringLiteral(node) || t.isTemplateLiteral(node) && node.expressions.length === 0;
|
---|
45 | };
|
---|
46 |
|
---|
47 | return function (context, path) {
|
---|
48 | if (visited) {
|
---|
49 | if (visited.has(path)) {
|
---|
50 | return;
|
---|
51 | }
|
---|
52 | visited.add(path);
|
---|
53 | }
|
---|
54 |
|
---|
55 | var SOURCE = getImportSource(t, path.parent);
|
---|
56 |
|
---|
57 | var builder = isString(SOURCE) ? builders['static'] : builders.dynamic;
|
---|
58 |
|
---|
59 | var newImport = context.opts.noInterop ? builder.noInterop({ SOURCE: SOURCE }) : builder.interop({ SOURCE: SOURCE, INTEROP: context.addHelper('interopRequireWildcard') });
|
---|
60 |
|
---|
61 | path.parentPath.replaceWith(newImport);
|
---|
62 | };
|
---|
63 | } |
---|