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

Cache dynamic ids #2908

Merged
merged 1 commit into from Jun 9, 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
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