Skip to content

Commit

Permalink
Make sure this.load waits for modules that are already loading (#4296)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Dec 11, 2021
1 parent e6d70d8 commit cb1419a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 19 deletions.
39 changes: 20 additions & 19 deletions src/ModuleLoader.ts
Expand Up @@ -56,7 +56,8 @@ type ResolveDynamicDependencyPromise = Promise<
type LoadModulePromise = Promise<
[
resolveStaticDependencies: ResolveStaticDependencyPromise[],
resolveDynamicDependencies: ResolveDynamicDependencyPromise[]
resolveDynamicDependencies: ResolveDynamicDependencyPromise[],
loadAndResolveDependencies: Promise<void>
]
>;

Expand All @@ -66,6 +67,7 @@ export class ModuleLoader {
private readonly indexedEntryModules: { index: number; module: Module }[] = [];
private latestLoadModulesPromise: Promise<unknown> = Promise.resolve();
private moduleLoadPromises = new Map<Module, LoadModulePromise>();
private modulesWithLoadedDependencies = new Set<Module>();
private nextEntryModuleIndex = 0;
private readQueue = new Queue();

Expand Down Expand Up @@ -353,7 +355,8 @@ export class ModuleLoader {
this.graph.watchFiles[id] = true;
const loadPromise: LoadModulePromise = this.addModuleSource(id, importer, module).then(() => [
this.getResolveStaticDependencyPromises(module),
this.getResolveDynamicImportPromises(module)
this.getResolveDynamicImportPromises(module),
loadAndResolveDependenciesPromise
]);
const loadAndResolveDependenciesPromise = loadPromise
.then(([resolveStaticDependencyPromises, resolveDynamicImportPromises]) =>
Expand All @@ -363,28 +366,31 @@ export class ModuleLoader {
loadAndResolveDependenciesPromise.catch(() => {
/* avoid unhandled promise rejections */
});

if (isPreload) {
this.moduleLoadPromises.set(module, loadPromise);
await loadPromise;
} else {
await this.fetchModuleDependencies(module, ...(await loadPromise));
// To handle errors when resolving dependencies or in moduleParsed
await loadAndResolveDependenciesPromise;
this.moduleLoadPromises.set(module, loadPromise);
const resolveDependencyPromises = await loadPromise;
if (!isPreload) {
await this.fetchModuleDependencies(module, ...resolveDependencyPromises);
}
return module;
}

private async fetchModuleDependencies(
module: Module,
resolveStaticDependencyPromises: ResolveStaticDependencyPromise[],
resolveDynamicDependencyPromises: ResolveDynamicDependencyPromise[]
resolveDynamicDependencyPromises: ResolveDynamicDependencyPromise[],
loadAndResolveDependenciesPromise: Promise<void>
) {
if (this.modulesWithLoadedDependencies.has(module)) {
return;
}
this.modulesWithLoadedDependencies.add(module);
await Promise.all([
this.fetchStaticDependencies(module, resolveStaticDependencyPromises),
this.fetchDynamicDependencies(module, resolveDynamicDependencyPromises)
]);
module.linkImports();
// To handle errors when resolving dependencies or in moduleParsed
await loadAndResolveDependenciesPromise;
}

private fetchResolvedDependency(
Expand Down Expand Up @@ -521,10 +527,9 @@ export class ModuleLoader {
}

private async handleExistingModule(module: Module, isEntry: boolean, isPreload: boolean) {
const loadPromise = this.moduleLoadPromises.get(module);
const loadPromise = this.moduleLoadPromises.get(module)!;
if (isPreload) {
await loadPromise;
return;
return loadPromise;
}
if (isEntry) {
module.info.isEntry = true;
Expand All @@ -534,11 +539,7 @@ export class ModuleLoader {
}
module.implicitlyLoadedAfter.clear();
}
if (loadPromise) {
this.moduleLoadPromises.delete(module);
await this.fetchModuleDependencies(module, ...(await loadPromise));
}
return;
return this.fetchModuleDependencies(module, ...(await loadPromise));
}

private handleResolveId(
Expand Down
20 changes: 20 additions & 0 deletions test/function/samples/preload-loading-module/_config.js
@@ -0,0 +1,20 @@
const assert = require('assert');

let preloadedCode;

module.exports = {
description: 'waits for pre-loaded modules that are currently loading',
options: {
plugins: [
{
name: 'test-plugin',
load(id) {
this.load({ id }).then(({ code }) => (preloadedCode = code));
},
buildEnd(err) {
assert.strictEqual(preloadedCode, 'assert.ok(true);\n');
}
}
]
}
};
1 change: 1 addition & 0 deletions test/function/samples/preload-loading-module/main.js
@@ -0,0 +1 @@
assert.ok(true);

0 comments on commit cb1419a

Please sign in to comment.