Skip to content

Commit de1ea9d

Browse files
committed
allow multiple chunks for a dependency block webpack#640
1 parent 775a908 commit de1ea9d

4 files changed

Lines changed: 50 additions & 17 deletions

File tree

lib/AsyncDependenciesBlock.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,19 @@ var DependenciesBlock = require("./DependenciesBlock");
77
function AsyncDependenciesBlock(name, module, loc) {
88
DependenciesBlock.call(this);
99
this.chunkName = name;
10-
this.chunk = null;
10+
this.chunks = null;
1111
this.module = module;
1212
this.loc = loc;
13+
14+
Object.defineProperty(this, "chunk", {
15+
get: function() {
16+
throw new Error("`chunk` was been renamed to `chunks` and is now an array");
17+
},
18+
set: function() {
19+
throw new Error("`chunk` was been renamed to `chunks` and is now an array");
20+
}
21+
});
22+
1323
}
1424
module.exports = AsyncDependenciesBlock;
1525

lib/Chunk.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ Chunk.prototype.remove = function(reason) {
9898
});
9999
}, this);
100100
this.blocks.forEach(function(b) {
101-
b.chunk = null;
101+
b.chunks = null;
102102
b.chunkReason = reason;
103103
}, this);
104104
};
@@ -138,7 +138,9 @@ Chunk.prototype.integrate = function(other, reason) {
138138
}.bind(this));
139139
other.chunks.length = 0;
140140
other.blocks.forEach(function(b) {
141-
b.chunk = this;
141+
b.chunks = (b.chunks || [this]).map(function(c) {
142+
return c === other ? this : c;
143+
}, this);
142144
b.chunkReason = reason;
143145
this.addBlock(b);
144146
}, this);

lib/Compilation.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -561,12 +561,12 @@ Compilation.prototype.processDependenciesBlockForChunk = function processDepende
561561
if(block.blocks) {
562562
block.blocks.forEach(function(b) {
563563
var c;
564-
if(!b.chunk) {
564+
if(!b.chunks) {
565565
c = this.addChunk(b.chunkName, b.module, b.loc);
566-
b.chunk = c;
566+
b.chunks = [c];
567567
c.addBlock(b);
568568
} else {
569-
c = b.chunk;
569+
c = b.chunks[0];
570570
}
571571
chunk.addChunk(c);
572572
c.addParent(chunk);
@@ -598,10 +598,11 @@ Compilation.prototype.processDependenciesBlockForChunk = function processDepende
598598

599599
Compilation.prototype.removeChunkFromDependencies = function removeChunkFromDependencies(block, chunk) {
600600
block.blocks.forEach(function(b) {
601-
var c = b.chunk;
602-
chunk.removeChunk(c);
603-
c.removeParent(chunk);
604-
this.removeChunkFromDependencies(b, c);
601+
b.chunks.forEach(function(c) {
602+
chunk.removeChunk(c);
603+
c.removeParent(chunk);
604+
this.removeChunkFromDependencies(b, c);
605+
}, this);
605606
}, this);
606607
function iteratorDependency(d) {
607608
if(!d.module) {

lib/dependencies/DepBlockHelpers.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,33 @@
55
var DepBlockHelpers = exports;
66

77
DepBlockHelpers.getLoadDepBlockWrapper = function(depBlock, outputOptions, requestShortener, name) {
8-
if(depBlock.chunk && !depBlock.chunk.entry && typeof depBlock.chunk.id === "number") {
9-
return [
10-
"__webpack_require__.e" + asComment(name) + "(" + depBlock.chunk.id + "" +
11-
(outputOptions.pathinfo && depBlock.chunkName ? "/*! " + requestShortener.shorten(depBlock.chunkName) + " */" : "") +
12-
asComment(depBlock.chunkReason) + ", ",
13-
")"
14-
];
8+
if(depBlock.chunks) {
9+
var chunks = depBlock.chunks.filter(function(chunk) {
10+
return !chunk.entry && typeof chunk.id === "number";
11+
});
12+
if(chunks.length === 1) {
13+
var chunk = chunks[0];
14+
return [
15+
"__webpack_require__.e" + asComment(name) + "(" + chunk.id + "" +
16+
(outputOptions.pathinfo && depBlock.chunkName ? "/*! " + requestShortener.shorten(depBlock.chunkName) + " */" : "") +
17+
asComment(depBlock.chunkReason) + ", ",
18+
")"
19+
];
20+
} else if(chunks.length > 0) {
21+
return [
22+
"(function(" + asComment(name) + ") {"+
23+
"var __WEBPACK_REMAINING_CHUNKS__ = " + chunks.length + ";" +
24+
"var __WEBPACK_CALLBACK__ = function() {" +
25+
"if(--__WEBPACK_REMAINING_CHUNKS__ < 1) (",
26+
27+
"(__webpack_require__));"+
28+
"};"+
29+
chunks.map(function(chunk) {
30+
return "__webpack_require__.e(" + chunk.id + ", __WEBPACK_CALLBACK__);";
31+
}).join("")+
32+
"}())"
33+
];
34+
}
1535
}
1636
};
1737

0 commit comments

Comments
 (0)