Skip to content

Commit 58ba91d

Browse files
committed
fix bug which prevented some chunks to show up in Chunk.getAllAsyncChunks
fixes webpack#6931
1 parent babc8a4 commit 58ba91d

File tree

8 files changed

+106
-7
lines changed

8 files changed

+106
-7
lines changed

lib/Chunk.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
const util = require("util");
88
const SortableSet = require("./util/SortableSet");
9+
const intersect = require("./util/SetHelpers").intersect;
910
const GraphHelpers = require("./GraphHelpers");
1011
let debugId = 1000;
1112
const ERR_CHUNK_ENTRY = "Chunk.entry was removed. Use hasRuntime()";
@@ -321,12 +322,15 @@ class Chunk {
321322
}
322323

323324
getAllAsyncChunks() {
324-
const initialChunks = new Set();
325-
const queue = new Set(this.groupsIterable);
325+
const queue = new Set();
326326
const chunks = new Set();
327327

328-
for (const chunkGroup of queue) {
329-
for (const chunk of chunkGroup.chunks) initialChunks.add(chunk);
328+
const initialChunks = intersect(
329+
Array.from(this.groupsIterable, g => new Set(g.chunks))
330+
);
331+
332+
for (const chunkGroup of this.groupsIterable) {
333+
for (const child of chunkGroup.childrenIterable) queue.add(child);
330334
}
331335

332336
for (const chunkGroup of queue) {

test/ConfigTestCases.test.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,18 @@ describe("ConfigTestCases", () => {
149149
return test;
150150
}
151151

152+
function _beforeEach(title, fn) {
153+
return suite.beforeEach(title, fn);
154+
}
155+
156+
function _afterEach(title, fn) {
157+
return suite.afterEach(title, fn);
158+
}
159+
152160
const globalContext = {
153-
console: console
161+
console: console,
162+
setTimeout: setTimeout,
163+
clearTimeout: clearTimeout
154164
};
155165

156166
function _require(currentDirectory, module) {
@@ -175,15 +185,15 @@ describe("ConfigTestCases", () => {
175185
options.target === "webworker"
176186
) {
177187
fn = vm.runInNewContext(
178-
"(function(require, module, exports, __dirname, __filename, it, window) {" +
188+
"(function(require, module, exports, __dirname, __filename, it, beforeEach, afterEach, window) {" +
179189
content +
180190
"\n})",
181191
globalContext,
182192
p
183193
);
184194
} else {
185195
fn = vm.runInThisContext(
186-
"(function(require, module, exports, __dirname, __filename, it) {" +
196+
"(function(require, module, exports, __dirname, __filename, it, beforeEach, afterEach) {" +
187197
content +
188198
"\n})",
189199
p
@@ -200,6 +210,8 @@ describe("ConfigTestCases", () => {
200210
path.dirname(p),
201211
p,
202212
_it,
213+
_beforeEach,
214+
_afterEach,
203215
globalContext
204216
);
205217
return m.exports;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const should = require("should");
2+
const FakeDocument = require("../../../helpers/FakeDocument");
3+
4+
beforeEach(() => {
5+
global.document = new FakeDocument();
6+
});
7+
8+
afterEach(() => {
9+
delete global.document;
10+
})
11+
12+
it("should be able to load the split chunk on demand", () => {
13+
const promise = import(/* webpackChunkName: "shared" */ "./shared");
14+
15+
const script = document.head._children[0];
16+
should(script.src).be.eql("dep~b~shared.js");
17+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import "./shared";

test/configCases/split-chunks/runtime-chunk/shared.js

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
findBundle: function(i, options) {
3+
return ["runtime.js", "a.js"];
4+
}
5+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const path = require("path");
2+
3+
module.exports = {
4+
entry: {
5+
a: "./a",
6+
b: "./b"
7+
},
8+
target: "web",
9+
output: {
10+
filename: "[name].js"
11+
},
12+
optimization: {
13+
runtimeChunk: "single",
14+
splitChunks: {
15+
cacheGroups: {
16+
dep: {
17+
chunks: "all",
18+
test: path.resolve(__dirname, "shared.js"),
19+
enforce: true
20+
}
21+
}
22+
}
23+
}
24+
};

test/helpers/FakeDocument.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module.exports = class FakeDocument {
2+
constructor() {
3+
this.head = this.createElement("head");
4+
}
5+
6+
createElement(type) {
7+
return new FakeElement(type);
8+
}
9+
10+
getElementsByTagName(name) {
11+
if (name === "head") return [this.head];
12+
throw new Error(
13+
`FakeDocument.getElementsByTagName(${name}): not implemented`
14+
);
15+
}
16+
};
17+
18+
class FakeElement {
19+
constructor(type) {
20+
this._type = type;
21+
this._children = [];
22+
this._attributes = Object.create(null);
23+
}
24+
25+
appendChild(node) {
26+
this._children.push(node);
27+
}
28+
29+
setAttribute(name, value) {
30+
this._attributes[name] = value;
31+
}
32+
33+
getAttribute(name) {
34+
return this._attributes[name];
35+
}
36+
}

0 commit comments

Comments
 (0)