diff --git a/lib/buildChunkGraph.js b/lib/buildChunkGraph.js index 1fa149438e5..f84fa1fc590 100644 --- a/lib/buildChunkGraph.js +++ b/lib/buildChunkGraph.js @@ -50,6 +50,7 @@ const { getEntryRuntime, mergeRuntime } = require("./util/runtime"); * @property {Set} availableChildren set of chunk groups which depend on the this chunk group as availableSource * @property {number} preOrderIndex next pre order index * @property {number} postOrderIndex next post order index + * @property {boolean} asyncChunkLoading create async chunks */ /** @@ -304,7 +305,11 @@ const visitModules = ( availableSources: undefined, availableChildren: undefined, preOrderIndex: 0, - postOrderIndex: 0 + postOrderIndex: 0, + asyncChunkLoading: + chunkGroup.options.chunkLoading === false + ? false + : compilation.outputOptions.chunkLoading !== false }; chunkGroup.index = nextChunkGroupIndex++; if (chunkGroup.getNumberOfParents() > 0) { @@ -418,7 +423,11 @@ const visitModules = ( availableSources: undefined, availableChildren: undefined, preOrderIndex: 0, - postOrderIndex: 0 + postOrderIndex: 0, + asyncChunkLoading: + entryOptions.chunkLoading !== undefined + ? entryOptions.chunkLoading !== false + : chunkGroupInfo.asyncChunkLoading }; chunkGroupInfoMap.set(entrypoint, cgi); @@ -442,8 +451,18 @@ const visitModules = ( chunkGroup: entrypoint, chunkGroupInfo: cgi }); + } else if (!chunkGroupInfo.asyncChunkLoading) { + // Just queue the block into the current chunk group + queue.push({ + action: PROCESS_BLOCK, + block: b, + module: module, + chunk, + chunkGroup, + chunkGroupInfo + }); } else { - cgi = namedChunkGroups.get(chunkName); + cgi = chunkName && namedChunkGroups.get(chunkName); if (!cgi) { c = compilation.addChunkInGroup( b.groupOptions || b.chunkName, @@ -464,7 +483,8 @@ const visitModules = ( availableSources: undefined, availableChildren: undefined, preOrderIndex: 0, - postOrderIndex: 0 + postOrderIndex: 0, + asyncChunkLoading: chunkGroupInfo.asyncChunkLoading }; allCreatedChunkGroups.add(c); chunkGroupInfoMap.set(c, cgi); @@ -518,7 +538,7 @@ const visitModules = ( chunkGroup: c, chunkGroupInfo: cgi }); - } else { + } else if (entrypoint !== undefined) { chunkGroupInfo.chunkGroup.addAsyncEntrypoint(entrypoint); } }; diff --git a/test/configCases/entry/no-chunking/a.js b/test/configCases/entry/no-chunking/a.js new file mode 100644 index 00000000000..b5c7af9a9a2 --- /dev/null +++ b/test/configCases/entry/no-chunking/a.js @@ -0,0 +1,12 @@ +import fs from "fs"; + +it("should load chunks on demand", async () => { + expect((await import("./async")).default).toEqual(42); + expect((await (await import("./async")).nested()).default).toEqual(43); + expect(fs.readFileSync(__filename, "utf-8")).not.toContain( + "This is the" + " async chunk" + ); + expect(fs.readFileSync(__filename, "utf-8")).not.toContain( + "This is the" + " nested async chunk" + ); +}); diff --git a/test/configCases/entry/no-chunking/async.js b/test/configCases/entry/no-chunking/async.js new file mode 100644 index 00000000000..5bb02b6aef8 --- /dev/null +++ b/test/configCases/entry/no-chunking/async.js @@ -0,0 +1,3 @@ +// This is the async chunk +export default 42; +export const nested = () => import("./nested"); diff --git a/test/configCases/entry/no-chunking/b.js b/test/configCases/entry/no-chunking/b.js new file mode 100644 index 00000000000..963cac2f617 --- /dev/null +++ b/test/configCases/entry/no-chunking/b.js @@ -0,0 +1,12 @@ +import fs from "fs"; + +it("should include all async imports in the main chunk", async () => { + expect((await import("./async")).default).toEqual(42); + expect((await (await import("./async")).nested()).default).toEqual(43); + expect(fs.readFileSync(__filename, "utf-8")).toContain( + "This is the async chunk" + ); + expect(fs.readFileSync(__filename, "utf-8")).toContain( + "This is the nested async chunk" + ); +}); diff --git a/test/configCases/entry/no-chunking/nested.js b/test/configCases/entry/no-chunking/nested.js new file mode 100644 index 00000000000..423e55b22e2 --- /dev/null +++ b/test/configCases/entry/no-chunking/nested.js @@ -0,0 +1,2 @@ +// This is the nested async chunk +export default 43; diff --git a/test/configCases/entry/no-chunking/test.config.js b/test/configCases/entry/no-chunking/test.config.js new file mode 100644 index 00000000000..668b0833d17 --- /dev/null +++ b/test/configCases/entry/no-chunking/test.config.js @@ -0,0 +1,5 @@ +module.exports = { + findBundle: function (i, options) { + return ["./a.js", "./b.js"]; + } +}; diff --git a/test/configCases/entry/no-chunking/webpack.config.js b/test/configCases/entry/no-chunking/webpack.config.js new file mode 100644 index 00000000000..6280043545e --- /dev/null +++ b/test/configCases/entry/no-chunking/webpack.config.js @@ -0,0 +1,13 @@ +/** @type {import("../../../../").Configuration} */ +module.exports = { + entry: { + a: "./a.js", + b: { + import: "./b.js", + chunkLoading: false + } + }, + output: { + filename: "[name].js" + } +};