Skip to content

Commit e48e2a2

Browse files
committed
added named chunks
1 parent f2d412c commit e48e2a2

6 files changed

Lines changed: 58 additions & 35 deletions

File tree

lib/buildDeps.js

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ module.exports = function buildDeps(context, mainModule, options, callback) {
3333
modulesById: {},
3434
chunks: {},
3535
nextModuleId: 0,
36-
nextChunkId: 0,
36+
nextChunkId: 1,
3737
chunkModules: {} // used by checkObsolete
3838
}
3939

@@ -58,6 +58,7 @@ module.exports = function buildDeps(context, mainModule, options, callback) {
5858
options.events.emit("task-end", "build modules");
5959

6060
// split the modules into chunks
61+
depTree.modulesById[mainModuleId].name = "main";
6162
addChunk(depTree, depTree.modulesById[mainModuleId], options);
6263

6364
// rename the module ids after a defined sheme
@@ -424,13 +425,23 @@ function createRealIds(depTree, options) {
424425

425426
// add a chunk
426427
function addChunk(depTree, chunkStartpoint, options) {
427-
var chunk = {
428-
id: depTree.nextChunkId++,
429-
modules: {},
430-
context: chunkStartpoint,
431-
usages: 1
432-
};
433-
depTree.chunks[chunk.id] = chunk;
428+
var chunk;
429+
if(chunkStartpoint && chunkStartpoint.name) {
430+
chunk = depTree.chunks[chunkStartpoint.name];
431+
if(chunk) {
432+
chunk.usages++;
433+
chunk.contexts.push(chunkStartpoint);
434+
}
435+
}
436+
if(!chunk) {
437+
chunk = {
438+
id: (chunkStartpoint && chunkStartpoint.name) || depTree.nextChunkId++,
439+
modules: {},
440+
contexts: chunkStartpoint ? [chunkStartpoint] : [],
441+
usages: 1
442+
};
443+
depTree.chunks[chunk.id] = chunk;
444+
}
434445
if(chunkStartpoint) {
435446
chunkStartpoint.chunkId = chunk.id;
436447
addModuleToChunk(depTree, chunkStartpoint, chunk.id, options);
@@ -498,7 +509,7 @@ function removeChunkIfEmpty(depTree, chunk) {
498509
}
499510
}
500511
if(!hasModules) {
501-
chunk.context.chunkId = null;
512+
chunk.contexts.forEach(function(c) { c.chunkId = null; });
502513
chunk.empty = true;
503514
}
504515
}
@@ -515,8 +526,9 @@ function checkObsolete(depTree, chunk) {
515526
var moduleString = modules.join(" ");
516527
if(depTree.chunkModules[moduleString]) {
517528
chunk.equals = depTree.chunkModules[moduleString];
518-
if(chunk.context)
519-
chunk.context.chunkId = chunk.equals;
529+
chunk.contexts.forEach(function(c) {
530+
c.chunkId = chunk.equals;
531+
});
520532
} else
521533
depTree.chunkModules[moduleString] = chunk.id;
522534
}
@@ -525,13 +537,13 @@ function checkObsolete(depTree, chunk) {
525537
function createRealChunkIds(depTree, options) {
526538
var sortedChunks = [];
527539
for(var id in depTree.chunks) {
528-
if(""+id === "0") continue;
540+
if(id === "main") continue;
529541
var chunk = depTree.chunks[id];
530542
if(chunk.empty) continue;
531543
if(chunk.equals !== undefined) continue;
532544
sortedChunks.push(chunk);
533545
}
534-
depTree.chunks["0"].realId = 0;
546+
depTree.chunks["main"].realId = 0;
535547
sortedChunks.sort(function(a, b) {
536548
if(a.usages < b.usages)
537549
return -1;

lib/parse.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,10 @@ function walkExpression(context, expression) {
274274
expression.arguments[1].body.range[0]+1,
275275
expression.arguments[1].body.range[1]-1
276276
];
277+
if(expression.arguments[2]) {
278+
newContext.name = parseString(expression.arguments[2]);
279+
newContext.nameRange = expression.arguments[2].range;
280+
}
277281
context.asyncs = context.asyncs || [];
278282
context.asyncs.push(newContext);
279283
context = newContext;

lib/webpack.js

Lines changed: 3 additions & 3 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(depTree.chunks[b].realId, 10) - parseInt(depTree.chunks[a].realId, 10);
217+
return depTree.chunks[b].realId - depTree.chunks[a].realId;
218218
});
219219

220220
// the template used
@@ -398,8 +398,8 @@ function webpack(context, moduleName, options, callback) {
398398
buffer.modulesIncludingDuplicates = sum;
399399
buffer.modulesPerChunk = Math.round(sum / chunksCount*10)/10; // DEPRECATED: useless info
400400
sum = 0;
401-
for(var moduleId in depTree.chunks[0].modules) {
402-
if(depTree.chunks[0].modules[moduleId] === "include")
401+
for(var moduleId in depTree.chunks.main.modules) {
402+
if(depTree.chunks.main.modules[moduleId] === "include")
403403
sum++;
404404
}
405405
buffer.modulesFirstChunk = sum;

lib/writeSource.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ module.exports = function(module, options, toRealId, toRealChuckId) {
108108
value: ((asyncItem.chunkId && toRealChuckId(asyncItem.chunkId) || "0") + "")
109109
});
110110
}
111+
if(asyncItem.nameRange) {
112+
replaces.push({
113+
from: asyncItem.nameRange[0],
114+
to: asyncItem.nameRange[1],
115+
value: "/* "+ asyncItem.name.replace(/\/\*/, "* nice try /") + " */0"
116+
});
117+
}
111118
if(asyncItem.blockRange) {
112119
genReplacesFreeVars(asyncItem.blockRange, freeVars);
113120
}

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.4.1",
3+
"version": "0.4.2",
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": {

test/buildDeps_test.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ vows.describe("buildDeps").addBatch({
2121
},
2222

2323
"one chunk": function(depTree) {
24-
assert.deepEqual(Object.keys(depTree.chunks), ["0"]);
24+
assert.deepEqual(Object.keys(depTree.chunks), ["main"]);
2525
for(var i in depTree.modulesById) {
26-
assert.deepEqual(depTree.modulesById[i].chunks, [0]);
26+
assert.deepEqual(depTree.modulesById[i].chunks, ["main"]);
2727
}
2828
}
2929
},
@@ -42,12 +42,12 @@ vows.describe("buildDeps").addBatch({
4242
},
4343

4444
"two chunks": function(depTree) {
45-
assert.deepEqual(Object.keys(depTree.chunks), ["0", "1"]);
46-
assert.deepEqual(depTree.modulesByFile[path.join(__dirname, "fixtures", "main2.js")].chunks, [0]);
47-
assert.deepEqual(depTree.modulesByFile[path.join(__dirname, "fixtures", "a.js")].chunks, [0]);
48-
assert.deepEqual(depTree.modulesByFile[path.join(__dirname, "fixtures", "b.js")].chunks, [0]);
49-
assert.deepEqual(depTree.modulesByFile[path.join(__dirname, "fixtures", "node_modules", "m1", "a.js")].chunks, [1]);
50-
assert.deepEqual(depTree.modulesByFile[path.join(__dirname, "fixtures", "node_modules", "m1", "b.js")].chunks, [1]);
45+
assert.deepEqual(Object.keys(depTree.chunks), ["1", "main"]);
46+
assert.deepEqual(depTree.modulesByFile[path.join(__dirname, "fixtures", "main2.js")].chunks, ["main"]);
47+
assert.deepEqual(depTree.modulesByFile[path.join(__dirname, "fixtures", "a.js")].chunks, ["main"]);
48+
assert.deepEqual(depTree.modulesByFile[path.join(__dirname, "fixtures", "b.js")].chunks, ["main"]);
49+
assert.deepEqual(depTree.modulesByFile[path.join(__dirname, "fixtures", "node_modules", "m1", "a.js")].chunks, ["1"]);
50+
assert.deepEqual(depTree.modulesByFile[path.join(__dirname, "fixtures", "node_modules", "m1", "b.js")].chunks, ["1"]);
5151
}
5252
},
5353

@@ -63,19 +63,19 @@ vows.describe("buildDeps").addBatch({
6363
},
6464

6565
"two chunks": function(depTree) {
66-
assert.deepEqual(Object.keys(depTree.chunks), ["0", "1"]);
67-
assert.deepEqual(depTree.modulesByFile[path.join(__dirname, "fixtures", "main3.js")].chunks, [0]);
68-
assert.deepEqual(depTree.modulesByFile[path.join(__dirname, "fixtures", "a.js")].chunks, [0, 1]);
69-
assert.deepEqual(depTree.modulesByFile[path.join(__dirname, "fixtures", "c.js")].chunks, [1]);
66+
assert.deepEqual(Object.keys(depTree.chunks), ["1", "main"]);
67+
assert.deepEqual(depTree.modulesByFile[path.join(__dirname, "fixtures", "main3.js")].chunks, ["main"]);
68+
assert.deepEqual(depTree.modulesByFile[path.join(__dirname, "fixtures", "a.js")].chunks, ["main", "1"]);
69+
assert.deepEqual(depTree.modulesByFile[path.join(__dirname, "fixtures", "c.js")].chunks, ["1"]);
7070
var main3id = ""+depTree.modulesByFile[path.join(__dirname, "fixtures", "main3.js")].id;
7171
var aid = ""+depTree.modulesByFile[path.join(__dirname, "fixtures", "a.js")].id;
7272
var cid = ""+depTree.modulesByFile[path.join(__dirname, "fixtures", "c.js")].id;
73-
assert.deepEqual(Object.keys(depTree.chunks[0].modules), [main3id, aid]);
74-
assert.deepEqual(Object.keys(depTree.chunks[1].modules), [cid, aid]);
75-
assert.deepEqual(depTree.chunks[0].modules[main3id], "include");
76-
assert.deepEqual(depTree.chunks[0].modules[aid], "include");
77-
assert.deepEqual(depTree.chunks[1].modules[aid], "in-parent");
78-
assert.deepEqual(depTree.chunks[1].modules[cid], "include");
73+
assert.deepEqual(Object.keys(depTree.chunks.main.modules), [main3id, aid]);
74+
assert.deepEqual(Object.keys(depTree.chunks["1"].modules), [cid, aid]);
75+
assert.deepEqual(depTree.chunks.main.modules[main3id], "include");
76+
assert.deepEqual(depTree.chunks.main.modules[aid], "include");
77+
assert.deepEqual(depTree.chunks["1"].modules[aid], "in-parent");
78+
assert.deepEqual(depTree.chunks["1"].modules[cid], "include");
7979
}
8080

8181
}

0 commit comments

Comments
 (0)