Skip to content

Commit

Permalink
Cache dynamic ids if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Jun 9, 2019
1 parent 2b8d0f4 commit ecce2ec
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 47 deletions.
101 changes: 55 additions & 46 deletions src/ModuleLoader.ts
Expand Up @@ -262,21 +262,23 @@ export class ModuleLoader {
private fetchAllDependencies(module: Module) {
const fetchDynamicImportsPromise = Promise.all(
module.getDynamicImportExpressions().map((specifier, index) =>
this.resolveDynamicImport(specifier as string | ESTree.Node, module.id).then(resolvedId => {
if (resolvedId === null) return;
const dynamicImport = module.dynamicImports[index];
if (typeof resolvedId === 'string') {
dynamicImport.resolution = resolvedId;
return;
this.resolveDynamicImport(module, specifier as string | ESTree.Node, module.id).then(
resolvedId => {
if (resolvedId === null) return;
const dynamicImport = module.dynamicImports[index];
if (typeof resolvedId === 'string') {
dynamicImport.resolution = resolvedId;
return;
}
return this.fetchResolvedDependency(
relativeId(resolvedId.id),
module.id,
resolvedId
).then(module => {
dynamicImport.resolution = module;
});
}
return this.fetchResolvedDependency(
relativeId(resolvedId.id),
module.id,
resolvedId
).then(module => {
dynamicImport.resolution = module;
});
})
)
)
);
fetchDynamicImportsPromise.catch(() => {});
Expand Down Expand Up @@ -487,44 +489,51 @@ export class ModuleLoader {
module: Module,
source: string
): Promise<Module | ExternalModule> {
const resolvedId =
module.resolvedIds[source] ||
this.handleMissingImports(await this.resolveId(source, module.id), source, module.id);
module.resolvedIds[source] = resolvedId;
return this.fetchResolvedDependency(source, module.id, resolvedId);
return this.fetchResolvedDependency(
source,
module.id,
(module.resolvedIds[source] =
module.resolvedIds[source] ||
this.handleMissingImports(await this.resolveId(source, module.id), source, module.id))
);
}

private resolveDynamicImport(
private async resolveDynamicImport(
module: Module,
specifier: string | ESTree.Node,
importer: string
): Promise<ResolvedId | string | null> {
// TODO we only should expose the acorn AST here
return this.pluginDriver
.hookFirst('resolveDynamicImport', [specifier, importer])
.then(resolution => {
if (typeof specifier !== 'string') {
if (typeof resolution === 'string') {
return resolution;
}
if (!resolution) {
return null as any;
}
return {
external: false,
moduleSideEffects: true,
...resolution
};
}
if (resolution == null) {
return this.resolveId(specifier, importer).then(resolvedId =>
this.handleMissingImports(resolvedId, specifier, importer)
);
}
return this.handleMissingImports(
this.normalizeResolveIdResult(resolution, importer, specifier),
const resolution = await this.pluginDriver.hookFirst('resolveDynamicImport', [
specifier,
importer
]);
if (typeof specifier !== 'string') {
if (typeof resolution === 'string') {
return resolution;
}
if (!resolution) {
return null;
}
return {
external: false,
moduleSideEffects: true,
...resolution
} as ResolvedId;
}
if (resolution == null) {
return (module.resolvedIds[specifier] =
module.resolvedIds[specifier] ||
this.handleMissingImports(
await this.resolveId(specifier, module.id),
specifier,
importer
);
});
module.id
));
}
return this.handleMissingImports(
this.normalizeResolveIdResult(resolution, importer, specifier),
specifier,
importer
);
}
}
27 changes: 26 additions & 1 deletion test/incremental/index.js
Expand Up @@ -35,7 +35,7 @@ describe('incremental', () => {
};
});

it('does not resolves id and transforms in the second time', () => {
it('does not resolve ids and transforms in the second time', () => {
return rollup
.rollup({
input: 'entry',
Expand All @@ -61,6 +61,31 @@ describe('incremental', () => {
});
});

it('does not resolve dynamic ids and transforms in the second time', () => {
modules = {
entry: `export default import('foo');`,
foo: `export default 42`
};
return rollup
.rollup({
input: 'entry',
plugins: [plugin]
})
.then(bundle => {
assert.equal(resolveIdCalls, 2);
assert.equal(transformCalls, 2);
return rollup.rollup({
input: 'entry',
plugins: [plugin],
cache: bundle
});
})
.then(bundle => {
assert.equal(resolveIdCalls, 3); // +1 for entry point which is resolved every time
assert.equal(transformCalls, 2);
});
});

it('transforms modified sources', () => {
let cache;

Expand Down

0 comments on commit ecce2ec

Please sign in to comment.