Skip to content

Commit 54ed0bf

Browse files
committed
deterministic chuck numbering
1 parent efe1e2f commit 54ed0bf

5 files changed

Lines changed: 47 additions & 17 deletions

File tree

‎lib/buildDeps.js‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ module.exports = function buildDeps(context, mainModule, options, callback) {
7474
// remove duplicate chunks
7575
checkObsolete(depTree, depTree.chunks[chunkId]);
7676
}
77+
78+
createRealChunkIds(depTree, options);
7779
options.events.emit("task-end", "optimize");
7880

7981
// cleanup temporary stuff
@@ -290,7 +292,7 @@ function addContextModule(depTree, context, contextModuleName, options, reason,
290292
options.events.emit("context", contextModule, dirname);
291293
var prependLoaders = loaders.length === 0 ? "" : loaders.join("!") + "!";
292294
var extensions = (options.resolve && options.resolve.extensions) || [".web.js", ".js"];
293-
295+
294296
// iterate all files in directory
295297
function doDir(dirname, moduleName, done) {
296298
fs.readdir(dirname, function(err, list) {
@@ -352,7 +354,7 @@ function addContextModule(depTree, context, contextModuleName, options, reason,
352354
endOne();
353355
} else {
354356
contextModule.requires.push({id: moduleId});
355-
357+
356358
// store the module id in a require map
357359
// when generating the source it is included
358360
contextModule.requireMap[moduleName + "/" + file] = moduleId;
@@ -425,7 +427,8 @@ function addChunk(depTree, chunkStartpoint, options) {
425427
var chunk = {
426428
id: depTree.nextChunkId++,
427429
modules: {},
428-
context: chunkStartpoint
430+
context: chunkStartpoint,
431+
usages: 1
429432
};
430433
depTree.chunks[chunk.id] = chunk;
431434
if(chunkStartpoint) {
@@ -452,6 +455,7 @@ function addModuleToChunk(depTree, context, chunkId, options) {
452455
var subChunk
453456
if(context.chunkId) {
454457
subChunk = depTree.chunks[context.chunkId];
458+
subChunk.usages++;
455459
} else {
456460
subChunk = addChunk(depTree, context, options);
457461
}
@@ -515,4 +519,29 @@ function checkObsolete(depTree, chunk) {
515519
chunk.context.chunkId = chunk.equals;
516520
} else
517521
depTree.chunkModules[moduleString] = chunk.id;
522+
}
523+
524+
// rename the chunk ids after a defined sheme
525+
function createRealChunkIds(depTree, options) {
526+
var sortedChunks = [];
527+
for(var id in depTree.chunks) {
528+
if(""+id === "0") continue;
529+
var chunk = depTree.chunks[id];
530+
if(chunk.empty) continue;
531+
if(chunk.equals !== undefined) continue;
532+
sortedChunks.push(chunk);
533+
}
534+
depTree.chunks["0"].realId = 0;
535+
sortedChunks.sort(function(a, b) {
536+
if(a.usages < b.usages)
537+
return -1;
538+
if(a.usages > b.usages)
539+
return 1;
540+
var aCount = Object.keys(a.modules).length;
541+
var bCount = Object.keys(b.modules).length;
542+
return aCount - bCount;
543+
});
544+
sortedChunks.forEach(function(chunk, idx) {
545+
chunk.realId = idx + 1;
546+
});
518547
}

‎lib/webpack.js‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ function webpack(context, moduleName, options, callback) {
214214
// all ids of the chunks, in desc order
215215
var chunkIds = Object.keys(depTree.chunks);
216216
chunkIds.sort(function(a,b) {
217-
return parseInt(b, 10) - parseInt(a, 10);
217+
return parseInt(depTree.chunks[b].realId, 10) - parseInt(depTree.chunks[a].realId, 10);
218218
});
219219

220220
// the template used
@@ -247,14 +247,14 @@ function webpack(context, moduleName, options, callback) {
247247
chunksCount++;
248248

249249
// build filename
250-
var filename = path.join(options.outputDirectory,
251-
chunk.id === 0 ? options.output : chunk.id + options.outputPostfix);
250+
var filename = chunk.filename = path.join(options.outputDirectory,
251+
chunk.realId === 0 ? options.output : chunk.realId + options.outputPostfix);
252252

253253
// get content of chunk
254254
var content = writeChunk(depTree, chunk, options);
255255
if(hash) hash.update(content);
256256
buffer = [];
257-
if(chunk.id === 0) { // initial chunk
257+
if(chunk.realId === 0) { // initial chunk
258258
if(hash)
259259
hash = hash.digest("hex");
260260
else
@@ -287,7 +287,7 @@ function webpack(context, moduleName, options, callback) {
287287
buffer.push("/******/");
288288
buffer.push(options.outputJsonpFunction);
289289
buffer.push("(");
290-
buffer.push(chunk.id);
290+
buffer.push(chunk.realId);
291291
buffer.push(", {\n");
292292
}
293293
// write content of chunk
@@ -374,8 +374,9 @@ function webpack(context, moduleName, options, callback) {
374374
buffer.chunkCount = chunksCount;
375375
buffer.modulesCount = Object.keys(depTree.modules).length;
376376
var sum = 0;
377-
for(var chunkId in depTree.chunks) {
377+
chunkIds.reverse().forEach(function(chunkId) {
378378
var chunk = depTree.chunks[chunkId]
379+
if(!chunk.filename) return;
379380
var modulesArray = [];
380381
for(var moduleId in chunk.modules) {
381382
if(chunk.modules[moduleId] === "include") {
@@ -392,10 +393,8 @@ function webpack(context, moduleName, options, callback) {
392393
modulesArray.sort(function(a, b) {
393394
return a.id - b.id;
394395
});
395-
var filename = path.join(options.outputDirectory,
396-
chunk.id === 0 ? options.output : chunk.id + options.outputPostfix);
397-
fileModulesMap[path.basename(filename)] = modulesArray;
398-
}
396+
fileModulesMap[path.basename(chunk.filename)] = modulesArray;
397+
});
399398
buffer.modulesIncludingDuplicates = sum;
400399
buffer.modulesPerChunk = Math.round(sum / chunksCount*10)/10; // DEPRECATED: useless info
401400
sum = 0;

‎lib/writeChunk.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ module.exports = function(depTree, chunk, options) {
3636
buffer.push(module.filename);
3737
buffer.push(" ***/\n\n");
3838
}
39-
buffer.push(writeSource(module, options, function(id) { return depTree.modules[id].realId }));
39+
buffer.push(writeSource(module, options,
40+
function(id) { return depTree.modules[id].realId },
41+
function(id) { return depTree.chunks[id].realId }));
4042
buffer.push("\n\n/******/},\n/******/\n");
4143
});
4244
return buffer.join("");

‎lib/writeSource.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5-
module.exports = function(module, options, toRealId) {
5+
module.exports = function(module, options, toRealId, toRealChuckId) {
66
var result;
77
if(typeof module.source !== "string") {
88
// if no source is avalible, try to generate it
@@ -105,7 +105,7 @@ module.exports = function(module, options, toRealId) {
105105
replaces.push({
106106
from: asyncItem.namesRange[0],
107107
to: asyncItem.namesRange[1],
108-
value: ((asyncItem.chunkId || "0") + "")
108+
value: ((asyncItem.chunkId && toRealChuckId(asyncItem.chunkId) || "0") + "")
109109
});
110110
}
111111
if(asyncItem.blockRange) {

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "webpack",
3-
"version": "0.3.17",
3+
"version": "0.3.18",
44
"author": "Tobias Koppers @sokra",
55
"description": "Packs CommonJs Modules for the browser. Allows to split your codebase into multiple bundles, which can be loaded on demand. Support loading of js, json, jade, coffee, css, ... out of the box and more with custom loaders.",
66
"dependencies": {

0 commit comments

Comments
 (0)