diff --git a/src/Module.ts b/src/Module.ts index c7057240875..e2f23dd83b4 100644 --- a/src/Module.ts +++ b/src/Module.ts @@ -685,8 +685,12 @@ export default class Module { this.includeAllExports(false); } - isIncluded(): boolean { - return this.ast!.included || this.namespace.included || this.importedFromNotTreeshaken; + 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) + ); } 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..11faa824341 --- /dev/null +++ b/test/function/samples/preload-after-build/_config.js @@ -0,0 +1,23 @@ +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, 'other2.js') }); + }, + moduleParsed({ id }) { + if (id.endsWith('main.js')) { + this.load({ id: path.join(__dirname, 'other1.js') }); + } + }, + renderStart() { + this.load({ id: path.join(__dirname, 'other3.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); diff --git a/test/function/samples/preload-after-build/other3.js b/test/function/samples/preload-after-build/other3.js new file mode 100644 index 00000000000..cc1d88a24fa --- /dev/null +++ b/test/function/samples/preload-after-build/other3.js @@ -0,0 +1 @@ +assert.ok(true);