Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always forward AST nodes for unresolvable dynamic imports #2984

Merged
merged 1 commit into from Jul 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);