From f98c65a2d2956e4ba2d2293fb35bb99c3cebb6f0 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Mon, 10 May 2021 15:49:08 +0200 Subject: [PATCH] fix too large runtime chunks when using dependOn --- lib/Chunk.js | 10 ++++- .../runtime/entries-in-runtime/async.js | 1 + .../runtime/entries-in-runtime/index.js | 11 ++++++ .../runtime/entries-in-runtime/main.js | 0 .../runtime/entries-in-runtime/other-entry.js | 2 + .../runtime/entries-in-runtime/split.js | 1 + .../runtime/entries-in-runtime/test.config.js | 5 +++ .../entries-in-runtime/webpack.config.js | 39 +++++++++++++++++++ 8 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 test/configCases/runtime/entries-in-runtime/async.js create mode 100644 test/configCases/runtime/entries-in-runtime/index.js create mode 100644 test/configCases/runtime/entries-in-runtime/main.js create mode 100644 test/configCases/runtime/entries-in-runtime/other-entry.js create mode 100644 test/configCases/runtime/entries-in-runtime/split.js create mode 100644 test/configCases/runtime/entries-in-runtime/test.config.js create mode 100644 test/configCases/runtime/entries-in-runtime/webpack.config.js diff --git a/lib/Chunk.js b/lib/Chunk.js index ae8ac32a37f..6706bb346a8 100644 --- a/lib/Chunk.js +++ b/lib/Chunk.js @@ -568,9 +568,15 @@ class Chunk { Array.from(this.groupsIterable, g => new Set(g.chunks)) ); - for (const chunkGroup of this.groupsIterable) { + const initialQueue = new Set(this.groupsIterable); + + for (const chunkGroup of initialQueue) { for (const child of chunkGroup.childrenIterable) { - queue.add(child); + if (child instanceof Entrypoint) { + initialQueue.add(child); + } else { + queue.add(child); + } } } diff --git a/test/configCases/runtime/entries-in-runtime/async.js b/test/configCases/runtime/entries-in-runtime/async.js new file mode 100644 index 00000000000..7461f8f346d --- /dev/null +++ b/test/configCases/runtime/entries-in-runtime/async.js @@ -0,0 +1 @@ +console.log("split"); diff --git a/test/configCases/runtime/entries-in-runtime/index.js b/test/configCases/runtime/entries-in-runtime/index.js new file mode 100644 index 00000000000..d595b906601 --- /dev/null +++ b/test/configCases/runtime/entries-in-runtime/index.js @@ -0,0 +1,11 @@ +import path from "path"; +import fs from "fs"; + +it("should not have references to chunks of unrelated entrypoints in runtime", () => { + const content = fs.readFileSync( + path.resolve(__dirname, "runtime.js"), + "utf-8" + ); + expect(content).not.toContain("other-entry"); + expect(content).not.toContain("split"); +}); diff --git a/test/configCases/runtime/entries-in-runtime/main.js b/test/configCases/runtime/entries-in-runtime/main.js new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/configCases/runtime/entries-in-runtime/other-entry.js b/test/configCases/runtime/entries-in-runtime/other-entry.js new file mode 100644 index 00000000000..0a829ac01ff --- /dev/null +++ b/test/configCases/runtime/entries-in-runtime/other-entry.js @@ -0,0 +1,2 @@ +import "./split"; +import("./async"); diff --git a/test/configCases/runtime/entries-in-runtime/split.js b/test/configCases/runtime/entries-in-runtime/split.js new file mode 100644 index 00000000000..7461f8f346d --- /dev/null +++ b/test/configCases/runtime/entries-in-runtime/split.js @@ -0,0 +1 @@ +console.log("split"); diff --git a/test/configCases/runtime/entries-in-runtime/test.config.js b/test/configCases/runtime/entries-in-runtime/test.config.js new file mode 100644 index 00000000000..759a6f59cfc --- /dev/null +++ b/test/configCases/runtime/entries-in-runtime/test.config.js @@ -0,0 +1,5 @@ +module.exports = { + findBundle: function () { + return ["./runtime.js", "./main.js", "./first-entry.js"]; + } +}; diff --git a/test/configCases/runtime/entries-in-runtime/webpack.config.js b/test/configCases/runtime/entries-in-runtime/webpack.config.js new file mode 100644 index 00000000000..a45319b6213 --- /dev/null +++ b/test/configCases/runtime/entries-in-runtime/webpack.config.js @@ -0,0 +1,39 @@ +/** @type {import("../../../../").Configuration} */ +module.exports = { + mode: "production", + entry: { + main: "./main", + "first-entry": { + dependOn: "main", + import: "./index" + }, + "other-entry": { + dependOn: "main", + import: "./other-entry" + } + }, + target: "web", + node: { + __dirname: false, + __filename: false + }, + externalsPresets: { + node: true + }, + output: { + filename: "[name].js" + }, + optimization: { + runtimeChunk: "single", + splitChunks: { + cacheGroups: { + split: { + chunks: "all", + name: "split", + test: /split\.js$/, + enforce: true + } + } + } + } +};