Skip to content

Commit

Permalink
Inline dynamic import argument extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed May 9, 2020
1 parent 4744586 commit bbe994c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 31 deletions.
31 changes: 11 additions & 20 deletions src/Module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Literal from './ast/nodes/Literal';
import MetaProperty from './ast/nodes/MetaProperty';
import * as NodeType from './ast/nodes/NodeType';
import Program from './ast/nodes/Program';
import { Node, NodeBase } from './ast/nodes/shared/Node';
import { ExpressionNode, NodeBase } from './ast/nodes/shared/Node';
import TemplateLiteral from './ast/nodes/TemplateLiteral';
import VariableDeclaration from './ast/nodes/VariableDeclaration';
import ModuleScope from './ast/scopes/ModuleScope';
Expand Down Expand Up @@ -200,6 +200,7 @@ export default class Module {
dynamicDependencies = new Set<Module | ExternalModule>();
dynamicImporters: string[] = [];
dynamicImports: {
argument: string | ExpressionNode;
node: ImportExpression;
resolution: Module | ExternalModule | string | null;
}[] = [];
Expand Down Expand Up @@ -392,24 +393,6 @@ export default class Module {
return (this.relevantDependencies = relevantDependencies);
}

// TODO Lukas could we do these transformations when pushing and then simplify ModuleLoaderL282?
getDynamicImportExpressions(): (string | Node)[] {
return this.dynamicImports.map(({ node }) => {
const importArgument = node.source;
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 importArgument;
});
}

getExportNamesByVariable(): Map<Variable, string[]> {
if (this.exportNamesByVariable) {
return this.exportNamesByVariable;
Expand Down Expand Up @@ -809,7 +792,15 @@ export default class Module {
}

private addDynamicImport(node: ImportExpression) {
this.dynamicImports.push({ node, resolution: null });
let argument: ExpressionNode | string = node.source;
if (argument instanceof TemplateLiteral) {
if (argument.quasis.length === 1 && argument.quasis[0].value.cooked) {
argument = argument.quasis[0].value.cooked;
}
} else if (argument instanceof Literal && typeof argument.value === 'string') {
argument = argument.value;
}
this.dynamicImports.push({ node, resolution: null, argument });
}

private addExport(
Expand Down
25 changes: 14 additions & 11 deletions src/ModuleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,21 +321,24 @@ export class ModuleLoader {
resolution.importers.push(module.id);
resolution.importers.sort();
}),
...module.getDynamicImportExpressions().map(async (specifier, index) => {
const resolvedId = await this.resolveDynamicImport(module, specifier, module.id);
...module.dynamicImports.map(async dynamicImport => {
const resolvedId = await this.resolveDynamicImport(
module,
dynamicImport.argument,
module.id
);
if (resolvedId === null) return;
const dynamicImport = module.dynamicImports[index];
if (typeof resolvedId === 'string') {
dynamicImport.resolution = resolvedId;
return;
} else {
const resolution = (dynamicImport.resolution = await this.fetchResolvedDependency(
relativeId(resolvedId.id),
module.id,
resolvedId
));
resolution.dynamicImporters.push(module.id);
resolution.dynamicImporters.sort();
}
const resolution = (dynamicImport.resolution = await this.fetchResolvedDependency(
relativeId(resolvedId.id),
module.id,
resolvedId
));
resolution.dynamicImporters.push(module.id);
resolution.dynamicImporters.sort();
})
]);
}
Expand Down

0 comments on commit bbe994c

Please sign in to comment.