Skip to content

Commit 61f021e

Browse files
authored
Always forward AST nodes for unresolvable dynamic imports (#2984)
1 parent c4dbb51 commit 61f021e

File tree

4 files changed

+36
-11
lines changed

4 files changed

+36
-11
lines changed

src/Module.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -302,18 +302,17 @@ export default class Module {
302302
getDynamicImportExpressions(): (string | Node)[] {
303303
return this.dynamicImports.map(({ node }) => {
304304
const importArgument = node.parent.arguments[0];
305-
if (importArgument instanceof TemplateLiteral) {
306-
if (importArgument.expressions.length === 0 && importArgument.quasis.length === 1) {
307-
return importArgument.quasis[0].value.cooked;
308-
}
309-
} else if (importArgument instanceof Literal) {
310-
if (typeof importArgument.value === 'string') {
311-
return importArgument.value;
312-
}
313-
} else {
314-
return importArgument;
305+
if (
306+
importArgument instanceof TemplateLiteral &&
307+
importArgument.quasis.length === 1 &&
308+
importArgument.quasis[0].value.cooked
309+
) {
310+
return importArgument.quasis[0].value.cooked;
311+
}
312+
if (importArgument instanceof Literal && typeof importArgument.value === 'string') {
313+
return importArgument.value;
315314
}
316-
return undefined as any;
315+
return importArgument;
317316
});
318317
}
319318

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const assert = require('assert');
2+
3+
module.exports = {
4+
solo: true,
5+
description: 'Returns the raw AST nodes for unresolvable dynamic imports',
6+
options: {
7+
plugins: [
8+
{
9+
resolveDynamicImport(specifier) {
10+
assert.ok(specifier);
11+
assert.strictEqual(typeof specifier, 'object');
12+
if (specifier.type !== 'TemplateLiteral' && specifier.type !== 'Literal') {
13+
throw new Error(`Unexpected specifier type ${specifier.type}.`);
14+
}
15+
return false;
16+
}
17+
}
18+
]
19+
}
20+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import(`${globalVar}`);
2+
import(`My ${globalVar}`);
3+
import(7);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import(`${globalVar}`);
2+
import(`My ${globalVar}`);
3+
import(7);

0 commit comments

Comments
 (0)