Skip to content

Commit

Permalink
fix bug which prevented some chunks to show up in Chunk.getAllAsyncCh…
Browse files Browse the repository at this point in the history
…unks

fixes #6931
  • Loading branch information
sokra committed Apr 17, 2018
1 parent babc8a4 commit 58ba91d
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 7 deletions.
12 changes: 8 additions & 4 deletions lib/Chunk.js
Expand Up @@ -6,6 +6,7 @@

const util = require("util");
const SortableSet = require("./util/SortableSet");
const intersect = require("./util/SetHelpers").intersect;
const GraphHelpers = require("./GraphHelpers");
let debugId = 1000;
const ERR_CHUNK_ENTRY = "Chunk.entry was removed. Use hasRuntime()";
Expand Down Expand Up @@ -321,12 +322,15 @@ class Chunk {
}

getAllAsyncChunks() {
const initialChunks = new Set();
const queue = new Set(this.groupsIterable);
const queue = new Set();
const chunks = new Set();

for (const chunkGroup of queue) {
for (const chunk of chunkGroup.chunks) initialChunks.add(chunk);
const initialChunks = intersect(
Array.from(this.groupsIterable, g => new Set(g.chunks))
);

for (const chunkGroup of this.groupsIterable) {
for (const child of chunkGroup.childrenIterable) queue.add(child);
}

for (const chunkGroup of queue) {
Expand Down
18 changes: 15 additions & 3 deletions test/ConfigTestCases.test.js
Expand Up @@ -149,8 +149,18 @@ describe("ConfigTestCases", () => {
return test;
}

function _beforeEach(title, fn) {
return suite.beforeEach(title, fn);
}

function _afterEach(title, fn) {
return suite.afterEach(title, fn);
}

const globalContext = {
console: console
console: console,
setTimeout: setTimeout,
clearTimeout: clearTimeout
};

function _require(currentDirectory, module) {
Expand All @@ -175,15 +185,15 @@ describe("ConfigTestCases", () => {
options.target === "webworker"
) {
fn = vm.runInNewContext(
"(function(require, module, exports, __dirname, __filename, it, window) {" +
"(function(require, module, exports, __dirname, __filename, it, beforeEach, afterEach, window) {" +
content +
"\n})",
globalContext,
p
);
} else {
fn = vm.runInThisContext(
"(function(require, module, exports, __dirname, __filename, it) {" +
"(function(require, module, exports, __dirname, __filename, it, beforeEach, afterEach) {" +
content +
"\n})",
p
Expand All @@ -200,6 +210,8 @@ describe("ConfigTestCases", () => {
path.dirname(p),
p,
_it,
_beforeEach,
_afterEach,
globalContext
);
return m.exports;
Expand Down
17 changes: 17 additions & 0 deletions test/configCases/split-chunks/runtime-chunk/a.js
@@ -0,0 +1,17 @@
const should = require("should");
const FakeDocument = require("../../../helpers/FakeDocument");

beforeEach(() => {
global.document = new FakeDocument();
});

afterEach(() => {
delete global.document;
})

it("should be able to load the split chunk on demand", () => {
const promise = import(/* webpackChunkName: "shared" */ "./shared");

const script = document.head._children[0];
should(script.src).be.eql("dep~b~shared.js");
});
1 change: 1 addition & 0 deletions test/configCases/split-chunks/runtime-chunk/b.js
@@ -0,0 +1 @@
import "./shared";
Empty file.
5 changes: 5 additions & 0 deletions test/configCases/split-chunks/runtime-chunk/test.config.js
@@ -0,0 +1,5 @@
module.exports = {
findBundle: function(i, options) {
return ["runtime.js", "a.js"];
}
};
24 changes: 24 additions & 0 deletions test/configCases/split-chunks/runtime-chunk/webpack.config.js
@@ -0,0 +1,24 @@
const path = require("path");

module.exports = {
entry: {
a: "./a",
b: "./b"
},
target: "web",
output: {
filename: "[name].js"
},
optimization: {
runtimeChunk: "single",
splitChunks: {
cacheGroups: {
dep: {
chunks: "all",
test: path.resolve(__dirname, "shared.js"),
enforce: true
}
}
}
}
};
36 changes: 36 additions & 0 deletions test/helpers/FakeDocument.js
@@ -0,0 +1,36 @@
module.exports = class FakeDocument {
constructor() {
this.head = this.createElement("head");
}

createElement(type) {
return new FakeElement(type);
}

getElementsByTagName(name) {
if (name === "head") return [this.head];
throw new Error(
`FakeDocument.getElementsByTagName(${name}): not implemented`
);
}
};

class FakeElement {
constructor(type) {
this._type = type;
this._children = [];
this._attributes = Object.create(null);
}

appendChild(node) {
this._children.push(node);
}

setAttribute(name, value) {
this._attributes[name] = value;
}

getAttribute(name) {
return this._attributes[name];
}
}

0 comments on commit 58ba91d

Please sign in to comment.