Skip to content

Commit 32d784d

Browse files
author
Vlad Zhukov
committed
Visit Import nodes instead of CallExpressions
1 parent a8b373f commit 32d784d

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

src/index.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import template from 'babel-template';
22
import syntax from 'babel-plugin-syntax-dynamic-import';
33
import * as t from 'babel-types';
44

5-
const TYPE_IMPORT = 'Import';
6-
75
const buildImport = template(`
86
Promise.resolve().then(() => require(SOURCE))
97
`);
@@ -12,16 +10,14 @@ export default () => ({
1210
inherits: syntax,
1311

1412
visitor: {
15-
CallExpression(path) {
16-
if (path.node.callee.type === TYPE_IMPORT) {
17-
const importArgument = path.node.arguments[0];
18-
const newImport = buildImport({
19-
SOURCE: (t.isStringLiteral(importArgument) || t.isTemplateLiteral(importArgument))
20-
? path.node.arguments
21-
: t.templateLiteral([t.templateElement({ raw: '', cooked: '' }), t.templateElement({ raw: '', cooked: '' }, true)], path.node.arguments),
22-
});
23-
path.replaceWith(newImport);
24-
}
13+
Import(path) {
14+
const importArguments = path.parentPath.node.arguments;
15+
const newImport = buildImport({
16+
SOURCE: (t.isStringLiteral(importArguments[0]) || t.isTemplateLiteral(importArguments[0]))
17+
? importArguments
18+
: t.templateLiteral([t.templateElement({ raw: '', cooked: '' }), t.templateElement({ raw: '', cooked: '' }, true)], importArguments),
19+
});
20+
path.parentPath.replaceWith(newImport);
2521
},
2622
},
2723
});

0 commit comments

Comments
 (0)