Skip to content

Commit 140af04

Browse files
committed
WIP6
1 parent 1fae5ec commit 140af04

File tree

12 files changed

+78
-41
lines changed

12 files changed

+78
-41
lines changed

lib/Chunk.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,12 @@ class Chunk {
123123
return this._modules;
124124
}
125125

126+
// TODO remove and replace calls with for of loop
126127
forEachModule(fn) {
127128
this._modules.forEach(fn);
128129
}
129130

131+
// TODO remove and replace calls with Array.from
130132
mapModules(fn) {
131133
return Array.from(this._modules, fn);
132134
}
@@ -138,6 +140,13 @@ class Chunk {
138140
return true;
139141
}
140142

143+
removeGroup(chunkGroup) {
144+
if(!this._groups.has(chunkGroup))
145+
return false;
146+
this._groups.delete(chunkGroup);
147+
return true;
148+
}
149+
141150
isInGroup(chunkGroup) {
142151
return this._groups.has(chunkGroup);
143152
}

lib/ChunkGroup.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ class ChunkGroup {
156156
this._parents.add(p);
157157
}
158158

159+
// TODO remove and replace calls with Array.from
159160
mapParents(fn) {
160161
return Array.from(this._parents, fn);
161162
}
@@ -193,6 +194,7 @@ class ChunkGroup {
193194
this._blocks.add(p);
194195
}
195196

197+
// TODO remove and replace calls with Array.from
196198
mapBlocks(fn) {
197199
return Array.from(this._blocks, fn);
198200
}
@@ -235,11 +237,6 @@ class ChunkGroup {
235237
}
236238

237239
remove(reason) {
238-
// remove chunks
239-
for(const chunk of this.chunks) {
240-
chunk.remove(reason);
241-
}
242-
243240
// cleanup parents
244241
for(const parentChunkGroup of this._parents) {
245242
// remove this chunk from its parents
@@ -273,6 +270,11 @@ class ChunkGroup {
273270
for(const block of this._blocks) {
274271
block.chunkGroup = null;
275272
}
273+
274+
// remove chunks
275+
for(const chunk of this.chunks) {
276+
chunk.removeGroup(this);
277+
}
276278
}
277279

278280
sortItems() {

lib/Compilation.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,7 @@ class Compilation extends Tapable {
12641264
this.chunks.splice(idx, 1);
12651265
chunk.remove("unconnected");
12661266
}
1267+
chunkGroup.remove("unconnected");
12671268
}
12681269
}
12691270
}

lib/GraphHelpers.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ exports.connectChunkAndModule = (chunk, module) => {
3333
}
3434
};
3535

36+
exports.disconnectChunkAndModule = (chunk, module) => {
37+
assert(chunk, Chunk, "chunk");
38+
assert(module, Module, "module");
39+
chunk.removeModule(module);
40+
module.removeChunk(chunk);
41+
};
42+
3643
exports.connectDependenciesBlockAndChunkGroup = (depBlock, chunkGroup) => {
3744
assert(depBlock, DependenciesBlock, "depBlock");
3845
assert(chunkGroup, ChunkGroup, "chunkGroup");

lib/optimize/AutomaticCommonsChunksPlugin.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@ module.exports = class AutomaticCommonsChunksPlugin {
3232
name: undefined, // function(module, chunks) => string | undefined
3333
enforce: undefined // function(module, module) => true | false
3434
}, options);
35+
this.alreadyOptimized = new WeakSet();
3536
}
3637

3738
apply(compiler) {
3839
compiler.hooks.compilation.tap("AutomaticCommonsChunksPlugin", compilation => {
3940
compilation.hooks.optimizeChunksAdvanced.tap("AutomaticCommonsChunksPlugin", chunks => {
41+
if(this.alreadyOptimized.has(compilation)) return;
42+
this.alreadyOptimized.add(compilation);
4043
// Give each selected chunk an index (to create strings from chunks)
4144
const indexMap = new Map();
4245
let index = 1;
@@ -144,6 +147,7 @@ module.exports = class AutomaticCommonsChunksPlugin {
144147
for(const pair of item.chunks) {
145148
if(pair[1] === item.modules.size) {
146149
const chunk = pair[0];
150+
if(chunk.hasEntryModule()) continue;
147151
if(!newChunk || !newChunk.name)
148152
newChunk = chunk;
149153
else if(chunk.name && chunk.name.length < newChunk.name.length)

lib/optimize/EnsureChunkConditionsPlugin.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,53 @@
44
*/
55
"use strict";
66

7+
const GraphHelpers = require("../GraphHelpers");
8+
79
class EnsureChunkConditionsPlugin {
810

911
apply(compiler) {
1012
compiler.hooks.compilation.tap("EnsureChunkConditionsPlugin", (compilation) => {
1113
const triesMap = new Map();
1214
const handler = (chunks) => {
1315
let changed = false;
16+
for(const module of compilation.modules) {
17+
if(!module.chunkCondition) continue;
18+
const sourceChunks = new Set();
19+
const chunkGroups = new Set();
20+
for(const chunk of module.chunksIterable) {
21+
if(!module.chunkCondition(chunk)) {
22+
sourceChunks.add(chunk);
23+
for(const group of chunk.groupsIterable) {
24+
chunkGroups.add(group);
25+
}
26+
}
27+
}
28+
if(sourceChunks.size === 0) continue;
29+
const targetChunks = new Set();
30+
chunkGroupLoop: for(const chunkGroup of chunkGroups) {
31+
// Can module be placed in a chunk of this group?
32+
for(const chunk of chunkGroup.chunks) {
33+
if(module.chunkCondition(chunk)) {
34+
targetChunks.add(chunk);
35+
continue chunkGroupLoop;
36+
}
37+
}
38+
// We reached the entrypoint: fail
39+
if(chunkGroup.isInitial()) {
40+
throw new Error("Cannot fullfill chunk condition of " + module.identifier());
41+
}
42+
// Try placing in all parents
43+
for(const group of chunkGroup.parentsIterable) {
44+
chunkGroups.add(group);
45+
}
46+
}
47+
for(const sourceChunk of sourceChunks) {
48+
GraphHelpers.disconnectChunkAndModule(sourceChunk, module);
49+
}
50+
for(const targetChunk of targetChunks) {
51+
GraphHelpers.connectChunkAndModule(targetChunk, module);
52+
}
53+
}
1454
chunks.forEach((chunk) => {
1555
for(const module of chunk.modulesIterable) {
1656
if(!module.chunkCondition) continue;

lib/web/JsonpMainTemplatePlugin.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ class JsonpMainTemplatePlugin {
171171
"var chunkIds = data[0], moreModules = data[1], executeModules = data[2];",
172172
"// add \"moreModules\" to the modules object,",
173173
"// then flag all \"chunkIds\" as loaded and fire callback",
174-
"var moduleId, chunkId, i = 0, resolves = [], result;",
174+
"var moduleId, chunkId, i = 0, resolves = [];",
175175
"for(;i < chunkIds.length; i++) {",
176176
Template.indent([
177177
"chunkId = chunkIds[i];",
@@ -192,19 +192,20 @@ class JsonpMainTemplatePlugin {
192192
"while(resolves.length) {",
193193
Template.indent("resolves.shift()();"),
194194
"}",
195-
needEntryDeferringCode(chunk) ? [
195+
needEntryDeferringCode(chunk) ? Template.asString([
196196
"",
197197
"// add entry modules from loaded chunk to deferred list",
198198
"deferredModules.push.apply(deferredModules, executeModules || []);",
199199
"",
200200
"// run deferred modules when all chunks ready",
201201
"return checkDeferredModules();"
202-
] : ""
202+
]) : ""
203203
]),
204204
"};",
205-
needEntryDeferringCode(chunk) ? [
205+
needEntryDeferringCode(chunk) ? Template.asString([
206206
"function checkDeferredModules() {",
207207
Template.indent([
208+
"var result;",
208209
"for(i = 0; i < deferredModules.length; i++) {",
209210
Template.indent([
210211
"var deferredModule = deferredModules[i];",
@@ -226,7 +227,7 @@ class JsonpMainTemplatePlugin {
226227
"return result;",
227228
]),
228229
"}"
229-
] : ""
230+
]) : ""
230231
]);
231232
}
232233
return source;

test/configCases/commons-chunk-plugin/library/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ require.ensure([], function() {
44
require.include("external2");
55
})
66

7-
it("should have externals in vendor file", function() {
7+
it("should have externals in main file", function() {
88
var a = require("./a");
9-
a.vendor.should.containEql("require(\"external0\")");
9+
a.main.should.containEql("require(\"external0\")");
1010
a.main.should.containEql("require(\"external1\")");
1111
a.main.should.containEql("require(\"external2\")");
12-
});
12+
});

test/configCases/commons-chunk-plugin/move-to-parent/a.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/configCases/commons-chunk-plugin/move-to-parent/index.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

test/configCases/commons-chunk-plugin/move-to-parent/webpack.config.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

test/configCases/externals/externals-in-commons-chunk/webpack.config.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
var webpack = require("../../../../");
21
module.exports = {
32
entry: {
43
main: "./index",
@@ -20,7 +19,7 @@ module.exports = {
2019
},
2120
optimization: {
2221
minimize: false,
23-
initialCommonsChunk: {
22+
initialCommonsChunks: {
2423
minSize: 1,
2524
name: "common"
2625
}

0 commit comments

Comments
 (0)