From 450fee48820c525a4cdf10eed5d84a426f75cd64 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Fri, 25 Nov 2022 21:45:08 +0100 Subject: [PATCH 1/3] Wait for this.load before closing bundle --- src/ModuleLoader.ts | 12 ++++++---- test/function/samples/preload-wait/_config.js | 24 +++++++++++++++++++ test/function/samples/preload-wait/main.js | 1 + test/function/samples/preload-wait/other.js | 1 + 4 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 test/function/samples/preload-wait/_config.js create mode 100644 test/function/samples/preload-wait/main.js create mode 100644 test/function/samples/preload-wait/other.js diff --git a/src/ModuleLoader.ts b/src/ModuleLoader.ts index 1c6f36db70d..a1411b949e7 100644 --- a/src/ModuleLoader.ts +++ b/src/ModuleLoader.ts @@ -183,11 +183,13 @@ export class ModuleLoader { public async preloadModule( resolvedId: { id: string; resolveDependencies?: boolean } & Partial> ): Promise { - const module = await this.fetchModule( - this.getResolvedIdWithDefaults(resolvedId, EMPTY_OBJECT)!, - undefined, - false, - resolvedId.resolveDependencies ? RESOLVE_DEPENDENCIES : true + const module = await this.extendLoadModulesPromise( + this.fetchModule( + this.getResolvedIdWithDefaults(resolvedId, EMPTY_OBJECT)!, + undefined, + false, + resolvedId.resolveDependencies ? RESOLVE_DEPENDENCIES : true + ) ); return module.info; } diff --git a/test/function/samples/preload-wait/_config.js b/test/function/samples/preload-wait/_config.js new file mode 100644 index 00000000000..816ebb3963a --- /dev/null +++ b/test/function/samples/preload-wait/_config.js @@ -0,0 +1,24 @@ +const assert = require('node:assert'); +const path = require('node:path'); +let otherLoaded = false; + +module.exports = { + description: 'waits for pre-loaded modules before ending build phase', + options: { + plugins: [ + { + name: 'test', + buildEnd() { + assert.ok(otherLoaded); + }, + moduleParsed({ id }) { + if (id.endsWith('main.js')) { + this.load({ id: path.join(__dirname, 'other.js') }); + } else { + otherLoaded = true; + } + } + } + ] + } +}; diff --git a/test/function/samples/preload-wait/main.js b/test/function/samples/preload-wait/main.js new file mode 100644 index 00000000000..cc1d88a24fa --- /dev/null +++ b/test/function/samples/preload-wait/main.js @@ -0,0 +1 @@ +assert.ok(true); diff --git a/test/function/samples/preload-wait/other.js b/test/function/samples/preload-wait/other.js new file mode 100644 index 00000000000..cc1d88a24fa --- /dev/null +++ b/test/function/samples/preload-wait/other.js @@ -0,0 +1 @@ +assert.ok(true); From 09f834f47d687e7047361e77da0619173799341e Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Fri, 25 Nov 2022 21:53:56 +0100 Subject: [PATCH 2/3] Support this.load after build without awaiting it --- src/Module.ts | 6 ++++-- .../samples/preload-after-build/_config.js | 18 ++++++++++++++++++ .../samples/preload-after-build/main.js | 1 + .../samples/preload-after-build/other1.js | 1 + .../samples/preload-after-build/other2.js | 1 + 5 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 test/function/samples/preload-after-build/_config.js create mode 100644 test/function/samples/preload-after-build/main.js create mode 100644 test/function/samples/preload-after-build/other1.js create mode 100644 test/function/samples/preload-after-build/other2.js diff --git a/src/Module.ts b/src/Module.ts index c7057240875..264f10cc6be 100644 --- a/src/Module.ts +++ b/src/Module.ts @@ -685,8 +685,10 @@ export default class Module { this.includeAllExports(false); } - isIncluded(): boolean { - return this.ast!.included || this.namespace.included || this.importedFromNotTreeshaken; + isIncluded(): boolean | null { + return ( + this.ast && (this.ast.included || this.namespace.included || this.importedFromNotTreeshaken) + ); } linkImports(): void { diff --git a/test/function/samples/preload-after-build/_config.js b/test/function/samples/preload-after-build/_config.js new file mode 100644 index 00000000000..0d3d27f63c5 --- /dev/null +++ b/test/function/samples/preload-after-build/_config.js @@ -0,0 +1,18 @@ +const path = require('node:path'); + +module.exports = { + description: 'supports this.load() in buildEnd and renderStart', + options: { + plugins: [ + { + name: 'test', + buildEnd() { + this.load({ id: path.join(__dirname, 'other1.js') }); + }, + renderStart() { + this.load({ id: path.join(__dirname, 'other2.js') }); + } + } + ] + } +}; diff --git a/test/function/samples/preload-after-build/main.js b/test/function/samples/preload-after-build/main.js new file mode 100644 index 00000000000..cc1d88a24fa --- /dev/null +++ b/test/function/samples/preload-after-build/main.js @@ -0,0 +1 @@ +assert.ok(true); diff --git a/test/function/samples/preload-after-build/other1.js b/test/function/samples/preload-after-build/other1.js new file mode 100644 index 00000000000..cc1d88a24fa --- /dev/null +++ b/test/function/samples/preload-after-build/other1.js @@ -0,0 +1 @@ +assert.ok(true); diff --git a/test/function/samples/preload-after-build/other2.js b/test/function/samples/preload-after-build/other2.js new file mode 100644 index 00000000000..cc1d88a24fa --- /dev/null +++ b/test/function/samples/preload-after-build/other2.js @@ -0,0 +1 @@ +assert.ok(true); From 823887edf1556ddb0ce7c9585978743a19f6a2f7 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Sat, 26 Nov 2022 06:44:14 +0100 Subject: [PATCH 3/3] Do not await this.load in build as that would be the job of the plugin --- src/Module.ts | 2 ++ src/ModuleLoader.ts | 12 ++++------ .../samples/preload-after-build/_config.js | 9 +++++-- .../main.js => preload-after-build/other3.js} | 0 test/function/samples/preload-wait/_config.js | 24 ------------------- test/function/samples/preload-wait/other.js | 1 - 6 files changed, 14 insertions(+), 34 deletions(-) rename test/function/samples/{preload-wait/main.js => preload-after-build/other3.js} (100%) delete mode 100644 test/function/samples/preload-wait/_config.js delete mode 100644 test/function/samples/preload-wait/other.js diff --git a/src/Module.ts b/src/Module.ts index 264f10cc6be..e2f23dd83b4 100644 --- a/src/Module.ts +++ b/src/Module.ts @@ -686,6 +686,8 @@ export default class Module { } isIncluded(): boolean | null { + // Modules where this.ast is missing have been loaded via this.load and are + // not yet fully processed, hence they cannot be included. return ( this.ast && (this.ast.included || this.namespace.included || this.importedFromNotTreeshaken) ); diff --git a/src/ModuleLoader.ts b/src/ModuleLoader.ts index a1411b949e7..1c6f36db70d 100644 --- a/src/ModuleLoader.ts +++ b/src/ModuleLoader.ts @@ -183,13 +183,11 @@ export class ModuleLoader { public async preloadModule( resolvedId: { id: string; resolveDependencies?: boolean } & Partial> ): Promise { - const module = await this.extendLoadModulesPromise( - this.fetchModule( - this.getResolvedIdWithDefaults(resolvedId, EMPTY_OBJECT)!, - undefined, - false, - resolvedId.resolveDependencies ? RESOLVE_DEPENDENCIES : true - ) + const module = await this.fetchModule( + this.getResolvedIdWithDefaults(resolvedId, EMPTY_OBJECT)!, + undefined, + false, + resolvedId.resolveDependencies ? RESOLVE_DEPENDENCIES : true ); return module.info; } diff --git a/test/function/samples/preload-after-build/_config.js b/test/function/samples/preload-after-build/_config.js index 0d3d27f63c5..11faa824341 100644 --- a/test/function/samples/preload-after-build/_config.js +++ b/test/function/samples/preload-after-build/_config.js @@ -7,10 +7,15 @@ module.exports = { { name: 'test', buildEnd() { - this.load({ id: path.join(__dirname, 'other1.js') }); + this.load({ id: path.join(__dirname, 'other2.js') }); + }, + moduleParsed({ id }) { + if (id.endsWith('main.js')) { + this.load({ id: path.join(__dirname, 'other1.js') }); + } }, renderStart() { - this.load({ id: path.join(__dirname, 'other2.js') }); + this.load({ id: path.join(__dirname, 'other3.js') }); } } ] diff --git a/test/function/samples/preload-wait/main.js b/test/function/samples/preload-after-build/other3.js similarity index 100% rename from test/function/samples/preload-wait/main.js rename to test/function/samples/preload-after-build/other3.js diff --git a/test/function/samples/preload-wait/_config.js b/test/function/samples/preload-wait/_config.js deleted file mode 100644 index 816ebb3963a..00000000000 --- a/test/function/samples/preload-wait/_config.js +++ /dev/null @@ -1,24 +0,0 @@ -const assert = require('node:assert'); -const path = require('node:path'); -let otherLoaded = false; - -module.exports = { - description: 'waits for pre-loaded modules before ending build phase', - options: { - plugins: [ - { - name: 'test', - buildEnd() { - assert.ok(otherLoaded); - }, - moduleParsed({ id }) { - if (id.endsWith('main.js')) { - this.load({ id: path.join(__dirname, 'other.js') }); - } else { - otherLoaded = true; - } - } - } - ] - } -}; diff --git a/test/function/samples/preload-wait/other.js b/test/function/samples/preload-wait/other.js deleted file mode 100644 index cc1d88a24fa..00000000000 --- a/test/function/samples/preload-wait/other.js +++ /dev/null @@ -1 +0,0 @@ -assert.ok(true);