mirror of https://github.com/webpack/webpack.git
added named chunks
This commit is contained in:
parent
f2d412cac0
commit
e48e2a2c12
|
@ -33,7 +33,7 @@ module.exports = function buildDeps(context, mainModule, options, callback) {
|
|||
modulesById: {},
|
||||
chunks: {},
|
||||
nextModuleId: 0,
|
||||
nextChunkId: 0,
|
||||
nextChunkId: 1,
|
||||
chunkModules: {} // used by checkObsolete
|
||||
}
|
||||
|
||||
|
@ -58,6 +58,7 @@ module.exports = function buildDeps(context, mainModule, options, callback) {
|
|||
options.events.emit("task-end", "build modules");
|
||||
|
||||
// split the modules into chunks
|
||||
depTree.modulesById[mainModuleId].name = "main";
|
||||
addChunk(depTree, depTree.modulesById[mainModuleId], options);
|
||||
|
||||
// rename the module ids after a defined sheme
|
||||
|
@ -424,13 +425,23 @@ function createRealIds(depTree, options) {
|
|||
|
||||
// add a chunk
|
||||
function addChunk(depTree, chunkStartpoint, options) {
|
||||
var chunk = {
|
||||
id: depTree.nextChunkId++,
|
||||
modules: {},
|
||||
context: chunkStartpoint,
|
||||
usages: 1
|
||||
};
|
||||
depTree.chunks[chunk.id] = chunk;
|
||||
var chunk;
|
||||
if(chunkStartpoint && chunkStartpoint.name) {
|
||||
chunk = depTree.chunks[chunkStartpoint.name];
|
||||
if(chunk) {
|
||||
chunk.usages++;
|
||||
chunk.contexts.push(chunkStartpoint);
|
||||
}
|
||||
}
|
||||
if(!chunk) {
|
||||
chunk = {
|
||||
id: (chunkStartpoint && chunkStartpoint.name) || depTree.nextChunkId++,
|
||||
modules: {},
|
||||
contexts: chunkStartpoint ? [chunkStartpoint] : [],
|
||||
usages: 1
|
||||
};
|
||||
depTree.chunks[chunk.id] = chunk;
|
||||
}
|
||||
if(chunkStartpoint) {
|
||||
chunkStartpoint.chunkId = chunk.id;
|
||||
addModuleToChunk(depTree, chunkStartpoint, chunk.id, options);
|
||||
|
@ -498,7 +509,7 @@ function removeChunkIfEmpty(depTree, chunk) {
|
|||
}
|
||||
}
|
||||
if(!hasModules) {
|
||||
chunk.context.chunkId = null;
|
||||
chunk.contexts.forEach(function(c) { c.chunkId = null; });
|
||||
chunk.empty = true;
|
||||
}
|
||||
}
|
||||
|
@ -515,8 +526,9 @@ function checkObsolete(depTree, chunk) {
|
|||
var moduleString = modules.join(" ");
|
||||
if(depTree.chunkModules[moduleString]) {
|
||||
chunk.equals = depTree.chunkModules[moduleString];
|
||||
if(chunk.context)
|
||||
chunk.context.chunkId = chunk.equals;
|
||||
chunk.contexts.forEach(function(c) {
|
||||
c.chunkId = chunk.equals;
|
||||
});
|
||||
} else
|
||||
depTree.chunkModules[moduleString] = chunk.id;
|
||||
}
|
||||
|
@ -525,13 +537,13 @@ function checkObsolete(depTree, chunk) {
|
|||
function createRealChunkIds(depTree, options) {
|
||||
var sortedChunks = [];
|
||||
for(var id in depTree.chunks) {
|
||||
if(""+id === "0") continue;
|
||||
if(id === "main") continue;
|
||||
var chunk = depTree.chunks[id];
|
||||
if(chunk.empty) continue;
|
||||
if(chunk.equals !== undefined) continue;
|
||||
sortedChunks.push(chunk);
|
||||
}
|
||||
depTree.chunks["0"].realId = 0;
|
||||
depTree.chunks["main"].realId = 0;
|
||||
sortedChunks.sort(function(a, b) {
|
||||
if(a.usages < b.usages)
|
||||
return -1;
|
||||
|
|
|
@ -274,6 +274,10 @@ function walkExpression(context, expression) {
|
|||
expression.arguments[1].body.range[0]+1,
|
||||
expression.arguments[1].body.range[1]-1
|
||||
];
|
||||
if(expression.arguments[2]) {
|
||||
newContext.name = parseString(expression.arguments[2]);
|
||||
newContext.nameRange = expression.arguments[2].range;
|
||||
}
|
||||
context.asyncs = context.asyncs || [];
|
||||
context.asyncs.push(newContext);
|
||||
context = newContext;
|
||||
|
|
|
@ -214,7 +214,7 @@ function webpack(context, moduleName, options, callback) {
|
|||
// all ids of the chunks, in desc order
|
||||
var chunkIds = Object.keys(depTree.chunks);
|
||||
chunkIds.sort(function(a,b) {
|
||||
return parseInt(depTree.chunks[b].realId, 10) - parseInt(depTree.chunks[a].realId, 10);
|
||||
return depTree.chunks[b].realId - depTree.chunks[a].realId;
|
||||
});
|
||||
|
||||
// the template used
|
||||
|
@ -398,8 +398,8 @@ function webpack(context, moduleName, options, callback) {
|
|||
buffer.modulesIncludingDuplicates = sum;
|
||||
buffer.modulesPerChunk = Math.round(sum / chunksCount*10)/10; // DEPRECATED: useless info
|
||||
sum = 0;
|
||||
for(var moduleId in depTree.chunks[0].modules) {
|
||||
if(depTree.chunks[0].modules[moduleId] === "include")
|
||||
for(var moduleId in depTree.chunks.main.modules) {
|
||||
if(depTree.chunks.main.modules[moduleId] === "include")
|
||||
sum++;
|
||||
}
|
||||
buffer.modulesFirstChunk = sum;
|
||||
|
|
|
@ -108,6 +108,13 @@ module.exports = function(module, options, toRealId, toRealChuckId) {
|
|||
value: ((asyncItem.chunkId && toRealChuckId(asyncItem.chunkId) || "0") + "")
|
||||
});
|
||||
}
|
||||
if(asyncItem.nameRange) {
|
||||
replaces.push({
|
||||
from: asyncItem.nameRange[0],
|
||||
to: asyncItem.nameRange[1],
|
||||
value: "/* "+ asyncItem.name.replace(/\/\*/, "* nice try /") + " */0"
|
||||
});
|
||||
}
|
||||
if(asyncItem.blockRange) {
|
||||
genReplacesFreeVars(asyncItem.blockRange, freeVars);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "webpack",
|
||||
"version": "0.4.1",
|
||||
"version": "0.4.2",
|
||||
"author": "Tobias Koppers @sokra",
|
||||
"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.",
|
||||
"dependencies": {
|
||||
|
|
|
@ -21,9 +21,9 @@ vows.describe("buildDeps").addBatch({
|
|||
},
|
||||
|
||||
"one chunk": function(depTree) {
|
||||
assert.deepEqual(Object.keys(depTree.chunks), ["0"]);
|
||||
assert.deepEqual(Object.keys(depTree.chunks), ["main"]);
|
||||
for(var i in depTree.modulesById) {
|
||||
assert.deepEqual(depTree.modulesById[i].chunks, [0]);
|
||||
assert.deepEqual(depTree.modulesById[i].chunks, ["main"]);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
@ -42,12 +42,12 @@ vows.describe("buildDeps").addBatch({
|
|||
},
|
||||
|
||||
"two chunks": function(depTree) {
|
||||
assert.deepEqual(Object.keys(depTree.chunks), ["0", "1"]);
|
||||
assert.deepEqual(depTree.modulesByFile[path.join(__dirname, "fixtures", "main2.js")].chunks, [0]);
|
||||
assert.deepEqual(depTree.modulesByFile[path.join(__dirname, "fixtures", "a.js")].chunks, [0]);
|
||||
assert.deepEqual(depTree.modulesByFile[path.join(__dirname, "fixtures", "b.js")].chunks, [0]);
|
||||
assert.deepEqual(depTree.modulesByFile[path.join(__dirname, "fixtures", "node_modules", "m1", "a.js")].chunks, [1]);
|
||||
assert.deepEqual(depTree.modulesByFile[path.join(__dirname, "fixtures", "node_modules", "m1", "b.js")].chunks, [1]);
|
||||
assert.deepEqual(Object.keys(depTree.chunks), ["1", "main"]);
|
||||
assert.deepEqual(depTree.modulesByFile[path.join(__dirname, "fixtures", "main2.js")].chunks, ["main"]);
|
||||
assert.deepEqual(depTree.modulesByFile[path.join(__dirname, "fixtures", "a.js")].chunks, ["main"]);
|
||||
assert.deepEqual(depTree.modulesByFile[path.join(__dirname, "fixtures", "b.js")].chunks, ["main"]);
|
||||
assert.deepEqual(depTree.modulesByFile[path.join(__dirname, "fixtures", "node_modules", "m1", "a.js")].chunks, ["1"]);
|
||||
assert.deepEqual(depTree.modulesByFile[path.join(__dirname, "fixtures", "node_modules", "m1", "b.js")].chunks, ["1"]);
|
||||
}
|
||||
},
|
||||
|
||||
|
@ -63,19 +63,19 @@ vows.describe("buildDeps").addBatch({
|
|||
},
|
||||
|
||||
"two chunks": function(depTree) {
|
||||
assert.deepEqual(Object.keys(depTree.chunks), ["0", "1"]);
|
||||
assert.deepEqual(depTree.modulesByFile[path.join(__dirname, "fixtures", "main3.js")].chunks, [0]);
|
||||
assert.deepEqual(depTree.modulesByFile[path.join(__dirname, "fixtures", "a.js")].chunks, [0, 1]);
|
||||
assert.deepEqual(depTree.modulesByFile[path.join(__dirname, "fixtures", "c.js")].chunks, [1]);
|
||||
assert.deepEqual(Object.keys(depTree.chunks), ["1", "main"]);
|
||||
assert.deepEqual(depTree.modulesByFile[path.join(__dirname, "fixtures", "main3.js")].chunks, ["main"]);
|
||||
assert.deepEqual(depTree.modulesByFile[path.join(__dirname, "fixtures", "a.js")].chunks, ["main", "1"]);
|
||||
assert.deepEqual(depTree.modulesByFile[path.join(__dirname, "fixtures", "c.js")].chunks, ["1"]);
|
||||
var main3id = ""+depTree.modulesByFile[path.join(__dirname, "fixtures", "main3.js")].id;
|
||||
var aid = ""+depTree.modulesByFile[path.join(__dirname, "fixtures", "a.js")].id;
|
||||
var cid = ""+depTree.modulesByFile[path.join(__dirname, "fixtures", "c.js")].id;
|
||||
assert.deepEqual(Object.keys(depTree.chunks[0].modules), [main3id, aid]);
|
||||
assert.deepEqual(Object.keys(depTree.chunks[1].modules), [cid, aid]);
|
||||
assert.deepEqual(depTree.chunks[0].modules[main3id], "include");
|
||||
assert.deepEqual(depTree.chunks[0].modules[aid], "include");
|
||||
assert.deepEqual(depTree.chunks[1].modules[aid], "in-parent");
|
||||
assert.deepEqual(depTree.chunks[1].modules[cid], "include");
|
||||
assert.deepEqual(Object.keys(depTree.chunks.main.modules), [main3id, aid]);
|
||||
assert.deepEqual(Object.keys(depTree.chunks["1"].modules), [cid, aid]);
|
||||
assert.deepEqual(depTree.chunks.main.modules[main3id], "include");
|
||||
assert.deepEqual(depTree.chunks.main.modules[aid], "include");
|
||||
assert.deepEqual(depTree.chunks["1"].modules[aid], "in-parent");
|
||||
assert.deepEqual(depTree.chunks["1"].modules[cid], "include");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue