From 1dfde116520f2dfdb355fc60c65e04539a2a8c65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=AF=E7=84=B6?= Date: Mon, 12 Apr 2021 18:11:45 +0800 Subject: [PATCH] fix: dynamic import & add tests --- utils/moduleVisitor.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/utils/moduleVisitor.js b/utils/moduleVisitor.js index 8466fa65e7..69269985bd 100644 --- a/utils/moduleVisitor.js +++ b/utils/moduleVisitor.js @@ -36,7 +36,17 @@ exports.default = function visitModules(visitor, options) { // for esmodule dynamic `import()` calls function checkImportCall(node) { - const modulePath = node.source; + let modulePath; + // refs https://github.com/estree/estree/blob/master/es2020.md#importexpression + if (node.type === 'ImportExpression') { + modulePath = node.source; + } else if (node.type === 'CallExpression') { + if (node.callee.type !== 'Import') return; + if (node.arguments.length !== 1) return; + + modulePath = node.arguments[0]; + } + if (modulePath.type !== 'Literal') return; if (typeof modulePath.value !== 'string') return; @@ -83,6 +93,7 @@ exports.default = function visitModules(visitor, options) { 'ImportDeclaration': checkSource, 'ExportNamedDeclaration': checkSource, 'ExportAllDeclaration': checkSource, + 'CallExpression': checkImportCall, 'ImportExpression': checkImportCall, }); }