-
Notifications
You must be signed in to change notification settings - Fork 66
/
utils.js
33 lines (28 loc) · 1.02 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
export function getImportSource(t, callNode) {
const importArguments = callNode.arguments;
const [importPath] = importArguments;
const isString = t.isStringLiteral(importPath) || t.isTemplateLiteral(importPath);
if (isString) {
t.removeComments(importPath);
return importPath;
}
return t.templateLiteral([
t.templateElement({ raw: '', cooked: '' }),
t.templateElement({ raw: '', cooked: '' }, true),
], importArguments);
}
export function createDynamicImportTransform({ template, types: t }) {
const buildImport = template('Promise.resolve().then(() => MODULE)');
return (context, path) => {
const requireCall = t.callExpression(
t.identifier('require'),
[getImportSource(t, path.parent)],
);
const { noInterop = false } = context.opts;
const MODULE = noInterop === true ? requireCall : t.callExpression(context.addHelper('interopRequireWildcard'), [requireCall]);
const newImport = buildImport({
MODULE,
});
path.parentPath.replaceWith(newImport);
};
}