Skip to content

Commit

Permalink
Always forward AST nodes for unresolvable dynamic imports (#2984)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Jul 4, 2019
1 parent c4dbb51 commit 61f021e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/Module.ts
Expand Up @@ -302,18 +302,17 @@ export default class Module {
getDynamicImportExpressions(): (string | Node)[] {
return this.dynamicImports.map(({ node }) => {
const importArgument = node.parent.arguments[0];
if (importArgument instanceof TemplateLiteral) {
if (importArgument.expressions.length === 0 && importArgument.quasis.length === 1) {
return importArgument.quasis[0].value.cooked;
}
} else if (importArgument instanceof Literal) {
if (typeof importArgument.value === 'string') {
return importArgument.value;
}
} else {
return importArgument;
if (
importArgument instanceof TemplateLiteral &&
importArgument.quasis.length === 1 &&
importArgument.quasis[0].value.cooked
) {
return importArgument.quasis[0].value.cooked;
}
if (importArgument instanceof Literal && typeof importArgument.value === 'string') {
return importArgument.value;
}
return undefined as any;
return importArgument;
});
}

Expand Down
20 changes: 20 additions & 0 deletions test/form/samples/dynamic-import-unresolvable/_config.js
@@ -0,0 +1,20 @@
const assert = require('assert');

module.exports = {
solo: true,
description: 'Returns the raw AST nodes for unresolvable dynamic imports',
options: {
plugins: [
{
resolveDynamicImport(specifier) {
assert.ok(specifier);
assert.strictEqual(typeof specifier, 'object');
if (specifier.type !== 'TemplateLiteral' && specifier.type !== 'Literal') {
throw new Error(`Unexpected specifier type ${specifier.type}.`);
}
return false;
}
}
]
}
};
3 changes: 3 additions & 0 deletions test/form/samples/dynamic-import-unresolvable/_expected.js
@@ -0,0 +1,3 @@
import(`${globalVar}`);
import(`My ${globalVar}`);
import(7);
3 changes: 3 additions & 0 deletions test/form/samples/dynamic-import-unresolvable/main.js
@@ -0,0 +1,3 @@
import(`${globalVar}`);
import(`My ${globalVar}`);
import(7);

0 comments on commit 61f021e

Please sign in to comment.