Skip to content

Commit 2f618e7

Browse files
committed
refactoring and aggressive-splitting plugin
1 parent 3a6b649 commit 2f618e7

76 files changed

Lines changed: 2152 additions & 218 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

bin/webpack.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ yargs.options({
6868
group: DISPLAY_GROUP,
6969
describe: "Display chunks in the output"
7070
},
71+
"display-entrypoints": {
72+
type: "boolean",
73+
group: DISPLAY_GROUP,
74+
describe: "Display entry points in the output"
75+
},
7176
"display-origins": {
7277
type: "boolean",
7378
group: DISPLAY_GROUP,
@@ -110,6 +115,7 @@ var argv = yargs.argv;
110115

111116
if(argv.verbose) {
112117
argv["display-reasons"] = true;
118+
argv["display-entrypoints"] = true;
113119
argv["display-used-exports"] = true;
114120
argv["display-error-details"] = true;
115121
argv["display-modules"] = true;
@@ -185,6 +191,10 @@ function processOptions(options) {
185191
outputOptions.chunks = bool;
186192
});
187193

194+
ifArg("display-entrypoints", function(bool) {
195+
outputOptions.entrypoints = bool;
196+
});
197+
188198
ifArg("display-reasons", function(bool) {
189199
outputOptions.reasons = bool;
190200
});
@@ -216,6 +226,8 @@ function processOptions(options) {
216226
} else {
217227
if(typeof outputOptions.chunks === "undefined")
218228
outputOptions.chunks = true;
229+
if(typeof outputOptions.entrypoints === "undefined")
230+
outputOptions.entrypoints = true;
219231
if(typeof outputOptions.modules === "undefined")
220232
outputOptions.modules = true;
221233
if(typeof outputOptions.chunkModules === "undefined")

examples/build-common.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var extraArgs = "";
1111
var targetArgs = global.NO_TARGET_ARGS ? "" : " ./example.js js/output.js";
1212
var displayReasons = global.NO_REASONS ? "" : " --display-reasons --display-used-exports";
1313
(function doIt(remainingTimes) {
14-
cp.exec("node ../../bin/webpack.js" + displayReasons + " --display-chunks --display-modules --display-origins --output-public-path \"js/\" -p " + extraArgs + targetArgs, function (error, stdout, stderr) {
14+
cp.exec("node ../../bin/webpack.js" + displayReasons + " --display-chunks --display-modules --display-origins --display-entrypoints --output-public-path \"js/\" -p " + extraArgs + targetArgs, function (error, stdout, stderr) {
1515
if(stderr && remainingTimes === 1)
1616
console.log(stderr);
1717
if (error !== null && remainingTimes === 1)
@@ -22,7 +22,7 @@ var displayReasons = global.NO_REASONS ? "" : " --display-reasons --display-used
2222
console.log(stderr);
2323
throw e;
2424
}
25-
cp.exec("node ../../bin/webpack.js" + displayReasons + " --display-chunks --display-modules --display-origins --output-public-path \"js/\" --output-pathinfo " + extraArgs + targetArgs, function (error, stdout, stderr) {
25+
cp.exec("node ../../bin/webpack.js" + displayReasons + " --display-chunks --display-modules --display-origins --display-entrypoints --output-public-path \"js/\" --output-pathinfo " + extraArgs + targetArgs, function (error, stdout, stderr) {
2626
if(remainingTimes === 1)
2727
console.log(stdout);
2828
if(stderr && remainingTimes === 1)

examples/http2-aggressive-splitting/README.md

Lines changed: 843 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
global.NO_TARGET_ARGS = true;
2+
global.NO_REASONS = true;
3+
require("../build-common");
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require("react");
2+
require(["react-dom"]);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Info
2+
3+
## Uncompressed
4+
5+
```
6+
{{stdout}}
7+
```
8+
9+
## Minimized (uglify-js, no zip)
10+
11+
```
12+
{{min:stdout}}
13+
```
14+
15+
## Records
16+
17+
```
18+
{{js/records.json}}
19+
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var path = require("path");
2+
var webpack = require("../../");
3+
module.exports = {
4+
entry: "./example",
5+
output: {
6+
path: path.join(__dirname, "js"),
7+
filename: "[chunkhash].js",
8+
chunkFilename: "[chunkhash].js"
9+
},
10+
plugins: [
11+
new webpack.optimize.AggressiveSplittingPlugin({
12+
minSize: 30000,
13+
maxSize: 50000
14+
}),
15+
new webpack.DefinePlugin({
16+
"process.env.NODE_ENV": JSON.stringify("production")
17+
})
18+
],
19+
recordsOutputPath: path.join(__dirname, "js", "records.json")
20+
};

lib/AsyncDependenciesBlock.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,25 @@ AsyncDependenciesBlock.prototype.disconnect = function() {
3838
this.chunks = null;
3939
DependenciesBlock.prototype.disconnect.call(this);
4040
};
41+
42+
AsyncDependenciesBlock.prototype.unseal = function() {
43+
this.chunks = null;
44+
DependenciesBlock.prototype.unseal.call(this);
45+
};
46+
47+
AsyncDependenciesBlock.prototype.sortItems = function() {
48+
DependenciesBlock.prototype.sortItems.call(this);
49+
if(this.chunks) {
50+
this.chunks.sort(function(a, b) {
51+
var i = 0;
52+
while(true) {
53+
if(!a.modules[i] && !b.modules[i]) return 0;
54+
if(!a.modules[i]) return -1;
55+
if(!b.modules[i]) return 1;
56+
if(a.modules[i].id > b.modules[i].id) return 1;
57+
if(a.modules[i].id < b.modules[i].id) return -1;
58+
i++;
59+
}
60+
});
61+
}
62+
};

lib/Chunk.js

Lines changed: 91 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5+
var compareLocations = require("./compareLocations");
56
var debugId = 1000;
67

78
function Chunk(name, module, loc) {
@@ -10,14 +11,13 @@ function Chunk(name, module, loc) {
1011
this.debugId = debugId++;
1112
this.name = name;
1213
this.modules = [];
14+
this.entrypoints = [];
1315
this.chunks = [];
1416
this.parents = [];
1517
this.blocks = [];
1618
this.origins = [];
1719
this.files = [];
1820
this.rendered = false;
19-
this.entry = false;
20-
this.initial = false;
2121
if(module) {
2222
this.origins.push({
2323
module: module,
@@ -28,6 +28,39 @@ function Chunk(name, module, loc) {
2828
}
2929
module.exports = Chunk;
3030

31+
Object.defineProperty(Chunk.prototype, "entry", {
32+
configurable: false,
33+
get: function() {
34+
throw new Error("Chunk.entry was removed. Use hasRuntime()");
35+
},
36+
set: function() {
37+
throw new Error("Chunk.entry was removed. Use hasRuntime()");
38+
}
39+
});
40+
41+
Object.defineProperty(Chunk.prototype, "initial", {
42+
configurable: false,
43+
get: function() {
44+
throw new Error("Chunk.initial was removed. Use isInitial()");
45+
},
46+
set: function() {
47+
throw new Error("Chunk.initial was removed. Use isInitial()");
48+
}
49+
});
50+
51+
Chunk.prototype.hasRuntime = function() {
52+
if(this.entrypoints.length === 0) return false;
53+
return this.entrypoints[0].chunks[0] === this;
54+
};
55+
56+
Chunk.prototype.isInitial = function() {
57+
return this.entrypoints.length > 0;
58+
};
59+
60+
Chunk.prototype.hasEntryModule = function() {
61+
return !!this.entryModule;
62+
};
63+
3164
Chunk.prototype.addModule = function(module) {
3265
if(this.modules.indexOf(module) >= 0) {
3366
return false;
@@ -117,6 +150,13 @@ Chunk.prototype.remove = function(reason) {
117150
}, this);
118151
};
119152

153+
Chunk.prototype.moveModule = function(module, other) {
154+
module.removeChunk(this);
155+
module.addChunk(other);
156+
other.addModule(module);
157+
module.rewriteChunkInReasons(this, [other]);
158+
};
159+
120160
Chunk.prototype.integrate = function(other, reason) {
121161
if(!this.canBeIntegrated(other)) {
122162
return false;
@@ -161,44 +201,65 @@ Chunk.prototype.integrate = function(other, reason) {
161201
}, this);
162202
other.blocks.length = 0;
163203
other.origins.forEach(function(origin) {
204+
this.origins.push(origin);
205+
}, this);
206+
this.origins.forEach(function(origin) {
164207
if(!origin.reasons) {
165208
origin.reasons = [reason];
166209
} else if(origin.reasons[0] !== reason) {
167210
origin.reasons.unshift(reason);
168211
}
169-
this.origins.push(origin);
170-
}, this);
212+
})
171213
return true;
172214
};
173215

216+
Chunk.prototype.split = function(newChunk) {
217+
var _this = this;
218+
this.blocks.forEach(function(b) {
219+
newChunk.blocks.push(b);
220+
b.chunks.push(newChunk);
221+
});
222+
this.chunks.forEach(function(c) {
223+
newChunk.chunks.push(c);
224+
c.parents.push(newChunk);
225+
});
226+
this.parents.forEach(function(p) {
227+
p.chunks.push(newChunk);
228+
newChunk.parents.push(p);
229+
});
230+
this.entrypoints.forEach(function(e) {
231+
e.insertChunk(newChunk, _this);
232+
});
233+
};
234+
174235
Chunk.prototype.isEmpty = function() {
175236
return this.modules.length === 0;
176237
};
177238

178239
Chunk.prototype.updateHash = function(hash) {
179240
hash.update(this.id + " ");
180241
hash.update(this.ids ? this.ids.join(",") : "");
181-
hash.update(this.name + "");
242+
hash.update((this.name || "") + " ");
182243
this.modules.forEach(function(m) {
183244
m.updateHash(hash);
184245
});
185246
};
186247

187248
Chunk.prototype.size = function(options) {
188-
var CHUNK_OVERHEAD = options.chunkOverhead || 10000;
249+
var CHUNK_OVERHEAD = typeof options.chunkOverhead === "number" ? options.chunkOverhead : 10000;
189250
var ENTRY_CHUNK_MULTIPLICATOR = options.entryChunkMultiplicator || 10;
190251

191252
var modulesSize = this.modules.reduce(function(a, b) {
192253
return a + b.size();
193254
}, 0);
194-
return modulesSize * (this.initial ? ENTRY_CHUNK_MULTIPLICATOR : 1) + CHUNK_OVERHEAD;
255+
return modulesSize * (this.isInitial() ? ENTRY_CHUNK_MULTIPLICATOR : 1) + CHUNK_OVERHEAD;
195256
};
196257

197258
Chunk.prototype.canBeIntegrated = function(other) {
198-
if(other.initial) {
259+
if(other.isInitial()) {
199260
return false;
200261
}
201-
if(this.initial) {
262+
if(this.isInitial()) {
202263
if(other.parents.length !== 1 || other.parents[0] !== this) {
203264
return false;
204265
}
@@ -225,23 +286,17 @@ Chunk.prototype.integratedSize = function(other, options) {
225286
var modulesSize = mergedModules.reduce(function(a, m) {
226287
return a + m.size();
227288
}, 0);
228-
return modulesSize * (this.initial || other.initial ? ENTRY_CHUNK_MULTIPLICATOR : 1) + CHUNK_OVERHEAD;
289+
return modulesSize * (this.isInitial() || other.isInitial() ? ENTRY_CHUNK_MULTIPLICATOR : 1) + CHUNK_OVERHEAD;
229290
};
230291

231-
Chunk.prototype.hasEntryModule = function() {
232-
return this.modules.some(function(module) {
233-
return module.entry;
234-
});
235-
}
236-
237292
Chunk.prototype.getChunkMaps = function(includeEntries, realHash) {
238293
var chunksProcessed = [];
239294
var chunkHashMap = {};
240295
var chunkNameMap = {};
241296
(function addChunk(c) {
242297
if(chunksProcessed.indexOf(c) >= 0) return;
243298
chunksProcessed.push(c);
244-
if(!c.entry || includeEntries) {
299+
if(!c.hasRuntime() || includeEntries) {
245300
chunkHashMap[c.id] = realHash ? c.hash : c.renderedHash;
246301
if(c.name)
247302
chunkNameMap[c.id] = c.name;
@@ -254,6 +309,25 @@ Chunk.prototype.getChunkMaps = function(includeEntries, realHash) {
254309
};
255310
};
256311

312+
function byId(a, b) {
313+
return a.id - b.id;
314+
}
315+
316+
Chunk.prototype.sortItems = function() {
317+
this.modules.sort(byId);
318+
this.origins.sort(function(a, b) {
319+
var aIdent = a.module.identifier();
320+
var bIdent = b.module.identifier();
321+
if(aIdent < bIdent) return -1;
322+
if(aIdent > bIdent) return 1;
323+
return compareLocations(a.loc, b.loc);
324+
});
325+
this.origins.forEach(function(origin) {
326+
if(origin.reasons)
327+
origin.reasons.sort();
328+
});
329+
};
330+
257331
Chunk.prototype.toString = function() {
258332
return "Chunk[" + this.modules.join() + "]";
259333
};

0 commit comments

Comments
 (0)