Skip to content

Commit 1b459d9

Browse files
committed
move externals into chunk with entry
fixes webpack#2314 maybe fixes webpack#2066 fixes webpack#2375 fixes webpack#1673
1 parent 8c8544a commit 1b459d9

File tree

10 files changed

+73
-6
lines changed

10 files changed

+73
-6
lines changed

lib/Chunk.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,12 @@ Chunk.prototype.integratedSize = function(other, options) {
228228
return modulesSize * (this.initial || other.initial ? ENTRY_CHUNK_MULTIPLICATOR : 1) + CHUNK_OVERHEAD;
229229
};
230230

231+
Chunk.prototype.hasEntryModule = function() {
232+
return this.modules.some(function(module) {
233+
return module.entry;
234+
});
235+
}
236+
231237
Chunk.prototype.getChunkMaps = function(includeEntries, realHash) {
232238
var chunksProcessed = [];
233239
var chunkHashMap = {};

lib/ChunkTemplate.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ ChunkTemplate.prototype.render = function(chunk, moduleTemplate, dependencyTempl
1616
var modules = this.renderChunkModules(chunk, moduleTemplate, dependencyTemplates);
1717
var core = this.applyPluginsWaterfall("modules", modules, chunk, moduleTemplate, dependencyTemplates);
1818
var source = this.applyPluginsWaterfall("render", core, chunk, moduleTemplate, dependencyTemplates);
19-
if(chunk.modules.some(function(module) {
20-
return module.entry;
21-
})) {
19+
if(chunk.hasEntryModule()) {
2220
source = this.applyPluginsWaterfall("render-with-entry", source, chunk);
2321
}
2422
chunk.rendered = true;

lib/ExternalModule.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ var WebpackMissingModule = require("./dependencies/WebpackMissingModule");
99

1010
function ExternalModule(request, type) {
1111
Module.call(this);
12+
this.chunkCondition = function(chunk) {
13+
return chunk.hasEntryModule();
14+
};
1215
this.request = request;
1316
this.type = type;
1417
this.built = false;

lib/MainTemplate.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,7 @@ MainTemplate.prototype.render = function(hash, chunk, moduleTemplate, dependency
127127
buf.push("");
128128
buf.push(this.asString(this.applyPluginsWaterfall("startup", "", chunk, hash)));
129129
var source = this.applyPluginsWaterfall("render", new OriginalSource(this.prefix(buf, " \t") + "\n", "webpack/bootstrap " + hash), chunk, hash, moduleTemplate, dependencyTemplates);
130-
if(chunk.modules.some(function(module) {
131-
return module.entry;
132-
})) {
130+
if(chunk.hasEntryModule()) {
133131
source = this.applyPluginsWaterfall("render-with-entry", source, chunk, hash);
134132
}
135133
if(!source) throw new Error("Compiler error: MainTemplate plugin 'render' should return something");

lib/WebpackOptionsApply.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ var RequireContextPlugin = require("./dependencies/RequireContextPlugin");
3232
var RequireEnsurePlugin = require("./dependencies/RequireEnsurePlugin");
3333
var RequireIncludePlugin = require("./dependencies/RequireIncludePlugin");
3434

35+
var EnsureChunkConditionsPlugin = require("./optimize/EnsureChunkConditionsPlugin");
3536
var RemoveParentModulesPlugin = require("./optimize/RemoveParentModulesPlugin");
3637
var RemoveEmptyChunksPlugin = require("./optimize/RemoveEmptyChunksPlugin");
3738
var MergeDuplicateChunksPlugin = require("./optimize/MergeDuplicateChunksPlugin");
@@ -245,6 +246,7 @@ WebpackOptionsApply.prototype.process = function(options, compiler) {
245246
);
246247

247248
compiler.apply(
249+
new EnsureChunkConditionsPlugin(),
248250
new RemoveParentModulesPlugin(),
249251
new RemoveEmptyChunksPlugin(),
250252
new MergeDuplicateChunksPlugin(),
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
function EnsureChunkConditionsPlugin() {}
6+
module.exports = EnsureChunkConditionsPlugin;
7+
8+
EnsureChunkConditionsPlugin.prototype.apply = function(compiler) {
9+
compiler.plugin("compilation", function(compilation) {
10+
compilation.plugin(["optimize-chunks-basic", "optimize-extracted-chunks-basic"], function(chunks) {
11+
var changed = false;
12+
chunks.forEach(function(chunk) {
13+
chunk.modules.slice().forEach(function(module) {
14+
if(!module.chunkCondition) return;
15+
if(!module.chunkCondition(chunk)) {
16+
chunk.parents.forEach(function(parent) {
17+
parent.addModule(module);
18+
});
19+
module.rewriteChunkInReasons(chunk, chunk.parents);
20+
chunk.removeModule(module);
21+
changed = true;
22+
}
23+
});
24+
});
25+
if(changed) return true;
26+
});
27+
});
28+
};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
exports.a = require("external");
2+
exports.b = System.import("./chunk2");
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require("external2");
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
it("should move externals in chunks into entry chunk", function(done) {
2+
var fs = require("fs");
3+
var source = fs.readFileSync(__filename, "utf-8");
4+
source.should.containEql("1+" + (1+1));
5+
source.should.containEql("3+" + (2+2));
6+
source.should.containEql("5+" + (3+3));
7+
8+
System.import("./chunk").then(function(chunk) {
9+
chunk.a.should.be.eql(3);
10+
chunk.b.then(function(chunk2) {
11+
chunk2.should.be.eql(7);
12+
System.import("external3").then(function(ex) {
13+
ex.should.be.eql(11);
14+
done();
15+
});
16+
});
17+
});
18+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = {
2+
externals: {
3+
external: "1+2",
4+
external2: "3+4",
5+
external3: "5+6"
6+
},
7+
node: {
8+
__dirname: false,
9+
__filename: false
10+
}
11+
};

0 commit comments

Comments
 (0)