document and test chunk merging

This commit is contained in:
Tobias Koppers 2012-10-26 00:47:51 +02:00
parent 0460211742
commit 323e386f25
5 changed files with 20 additions and 4 deletions

View File

@ -522,6 +522,16 @@ You can also save this options object in a JSON file and use it with the shell c
// normal loaders. This cannot be overridden in the require call.
// You must pass a string instead of a RegExp if you use workers
maxChunks: 5, // (experimental)
// default: undefined
// limit the maximum number of chunks.
// chunks are merged until the required number is reached
mergeSizeRatio: 2, // (experimental)
// default: 0.2
// when choosing the merged chunks the maximum of this formular is searched:
// sizeSaveByChunkMerging - mergedChunkSize * mergeSizeRatio
workers: true,
// default: false
// options: true, false, number > 0, object of type webpack/lib/Workers

View File

@ -78,7 +78,7 @@ module.exports = function buildDeps(context, mainModule, options, callback) {
if(options.maxChunks) {
while(depTree.chunkCount > options.maxChunks) {
if(!removeOneChunk(depTree, true))
if(!removeOneChunk(depTree, options, true))
break;
}
}
@ -747,7 +747,7 @@ function moduleSize(depTree, moduleId) {
return depTree.modulesById[moduleId].source && depTree.modulesById[moduleId].source.length || 0;
}
function removeOneChunk(depTree, force) {
function removeOneChunk(depTree, options, force) {
var chunks = [];
for(var chunkId in depTree.chunks) {
var chunk = depTree.chunks[chunkId];
@ -775,7 +775,7 @@ function removeOneChunk(depTree, force) {
sizeMerged += size + 10;
}
}
var value = sizeSum * 2 - sizeMerged;
var value = sizeSum - sizeMerged * (options.mergeSizeRatio === undefined ? 1.2 : options.mergeSizeRatio + 1);
if(best == null || best[0] < value)
best = [value, chunkA.id, chunkB.id];
});

View File

@ -10,5 +10,6 @@ module.exports = {
}
]
}
}
},
maxChunks: 2
}

View File

@ -9,6 +9,10 @@ require.ensure("./extra", function(require) {
asnycOk2 = true;
window.test(require("./extra") === "Lib2 extra2 with post loader", "Lib2 extra loaded");
window.test(sameTick, "Lib2 Should be in the same tick, as it is a empty chunk");
require.ensure(["./test.js"], function(require) {
window.test(require("./test.js") === "test module", "Lib2 recursive require.ensure");
window.test(sameTick, "Lib2 Should be in the same tick, as it should be merged into one chunk");
});
});
sameTick = false;
});

1
test/browsertest/node_modules/libary2/lib/test.js generated vendored Normal file
View File

@ -0,0 +1 @@
module.exports = "test module";