Skip to content

Commit 8f58eee

Browse files
committed
Merge branch 'next' into feature/pure-module
# Conflicts: # test/statsCases/commons-plugin-issue-4980/expected.txt # test/statsCases/scope-hoisting-multi/expected.txt # test/statsCases/tree-shaking/expected.txt
2 parents c383d5e + 9e5765f commit 8f58eee

57 files changed

Lines changed: 497 additions & 295 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎lib/Chunk.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class Chunk {
6363

6464
hasRuntime() {
6565
if(this.entrypoints.length === 0) return false;
66-
return this.entrypoints[0].chunks[0] === this;
66+
return this.entrypoints[0].getRuntimeChunk() === this;
6767
}
6868

6969
isInitial() {

‎lib/Entrypoint.js‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@ class Entrypoint {
1616
}
1717

1818
insertChunk(chunk, before) {
19+
const oldIdx = this.chunks.indexOf(chunk);
1920
const idx = this.chunks.indexOf(before);
20-
if(idx >= 0) {
21-
this.chunks.splice(idx, 0, chunk);
22-
} else {
21+
if(idx < 0) {
2322
throw new Error("before chunk not found");
2423
}
25-
chunk.entrypoints.push(this);
24+
if(oldIdx >= 0 && oldIdx > idx) {
25+
this.chunks.splice(oldIdx, 1);
26+
this.chunks.splice(idx, 0, chunk);
27+
} else if(oldIdx < 0) {
28+
this.chunks.splice(idx, 0, chunk);
29+
chunk.entrypoints.push(this);
30+
}
2631
}
2732

2833
getFiles() {
@@ -38,6 +43,10 @@ class Entrypoint {
3843

3944
return files;
4045
}
46+
47+
getRuntimeChunk() {
48+
return this.chunks[0];
49+
}
4150
}
4251

4352
module.exports = Entrypoint;

‎lib/JsonpChunkTemplatePlugin.js‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,20 @@ class JsonpChunkTemplatePlugin {
1111
chunkTemplate.plugin("render", function(modules, chunk) {
1212
const jsonpFunction = this.outputOptions.jsonpFunction;
1313
const source = new ConcatSource();
14-
source.add(`${jsonpFunction}(${JSON.stringify(chunk.ids)},`);
14+
source.add(`(window[${JSON.stringify(jsonpFunction)}] = window[${JSON.stringify(jsonpFunction)}] || []).push([${JSON.stringify(chunk.ids)},`);
1515
source.add(modules);
16-
const entries = [chunk.entryModule].filter(Boolean).map(m => m.id);
16+
const entries = [chunk.entryModule]
17+
.filter(Boolean)
18+
.map(m => [m.id].concat(chunk.entrypoints[0].chunks.map(c => c.id)));
1719
if(entries.length > 0) {
1820
source.add(`,${JSON.stringify(entries)}`);
1921
}
20-
source.add(")");
22+
source.add("])");
2123
return source;
2224
});
2325
chunkTemplate.plugin("hash", function(hash) {
2426
hash.update("JsonpChunkTemplatePlugin");
25-
hash.update("3");
27+
hash.update("4");
2628
hash.update(`${this.outputOptions.jsonpFunction}`);
2729
hash.update(`${this.outputOptions.library}`);
2830
});

‎lib/JsonpMainTemplatePlugin.js‎

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,15 @@ const Template = require("./Template");
99
class JsonpMainTemplatePlugin {
1010

1111
apply(mainTemplate) {
12+
function needChunkLoadingCode(chunk) {
13+
var otherChunksInEntry = chunk.entrypoints.some(function(entrypoint) {
14+
return entrypoint.chunks.length > 1;
15+
});
16+
var onDemandChunks = chunk.chunks.length > 0;
17+
return otherChunksInEntry || onDemandChunks;
18+
}
1219
mainTemplate.plugin("local-vars", function(source, chunk) {
13-
if(chunk.chunks.length > 0) {
20+
if(needChunkLoadingCode(chunk)) {
1421
return this.asString([
1522
source,
1623
"",
@@ -19,7 +26,9 @@ class JsonpMainTemplatePlugin {
1926
this.indent(
2027
chunk.ids.map(id => `${JSON.stringify(id)}: 0`).join(",\n")
2128
),
22-
"};"
29+
"};",
30+
"",
31+
"var scheduledModules = [];"
2332
]);
2433
}
2534
return source;
@@ -57,9 +66,13 @@ class JsonpMainTemplatePlugin {
5766
this.indent(`script.setAttribute("nonce", ${this.requireFn}.nc);`),
5867
"}",
5968
`script.src = ${this.requireFn}.p + ${scriptSrcPath};`,
60-
`var timeout = setTimeout(onScriptComplete, ${chunkLoadTimeout});`,
69+
"var timeout = setTimeout(function(){",
70+
this.indent([
71+
"onScriptComplete({ type: 'timeout', target: script });",
72+
]),
73+
`}, ${chunkLoadTimeout});`,
6174
"script.onerror = script.onload = onScriptComplete;",
62-
"function onScriptComplete() {",
75+
"function onScriptComplete(event) {",
6376
this.indent([
6477
"// avoid mem leaks in IE.",
6578
"script.onerror = script.onload = null;",
@@ -68,7 +81,14 @@ class JsonpMainTemplatePlugin {
6881
"if(chunk !== 0) {",
6982
this.indent([
7083
"if(chunk) {",
71-
this.indent("chunk[1](new Error('Loading chunk ' + chunkId + ' failed.'));"),
84+
this.indent([
85+
"var errorType = event && (event.type === 'load' ? 'missing' : event.type);",
86+
"var realSrc = event && event.target && event.target.src;",
87+
"var error = new Error('Loading chunk ' + chunkId + ' failed.\\n(' + errorType + ': ' + realSrc + ')');",
88+
"error.type = errorType;",
89+
"error.request = realSrc;",
90+
"chunk[1](error);"
91+
]),
7292
"}",
7393
"installedChunks[chunkId] = undefined;"
7494
]),
@@ -120,15 +140,14 @@ class JsonpMainTemplatePlugin {
120140
]);
121141
});
122142
mainTemplate.plugin("bootstrap", function(source, chunk, hash) {
123-
if(chunk.chunks.length > 0) {
124-
var jsonpFunction = this.outputOptions.jsonpFunction;
143+
if(needChunkLoadingCode(chunk)) {
125144
return this.asString([
126145
source,
127146
"",
128147
"// install a JSONP callback for chunk loading",
129-
`var parentJsonpFunction = window[${JSON.stringify(jsonpFunction)}];`,
130-
`window[${JSON.stringify(jsonpFunction)}] = function webpackJsonpCallback(chunkIds, moreModules, executeModules) {`,
148+
"function webpackJsonpCallback(data) {",
131149
this.indent([
150+
"var chunkIds = data[0], moreModules = data[1], executeModules = data[2];",
132151
"// add \"moreModules\" to the modules object,",
133152
"// then flag all \"chunkIds\" as loaded and fire callback",
134153
"var moduleId, chunkId, i = 0, resolves = [], result;",
@@ -148,15 +167,28 @@ class JsonpMainTemplatePlugin {
148167
"}"
149168
]),
150169
"}",
151-
"if(parentJsonpFunction) parentJsonpFunction(chunkIds, moreModules, executeModules);",
170+
"if(parentJsonpFunction) parentJsonpFunction(data);",
152171
"while(resolves.length) {",
153172
this.indent("resolves.shift()();"),
154173
"}",
155174
this.entryPointInChildren(chunk) ? [
156-
"if(executeModules) {",
175+
"scheduledModules.push.apply(scheduledModules, executeModules || []);",
176+
"",
177+
"for(i = 0; i < scheduledModules.length; i++) {",
157178
this.indent([
158-
"for(i=0; i < executeModules.length; i++) {",
159-
this.indent(`result = ${this.requireFn}(${this.requireFn}.s = executeModules[i]);`),
179+
"var scheduledModule = scheduledModules[i];",
180+
"var fullfilled = true;",
181+
"for(var j = 1; j < scheduledModule.length; j++) {",
182+
this.indent([
183+
"var depId = scheduledModule[j];",
184+
"if(installedChunks[depId] !== 0) fullfilled = false;"
185+
]),
186+
"}",
187+
"if(fullfilled) {",
188+
this.indent([
189+
"scheduledModules.splice(i--, 1);",
190+
"result = " + this.requireFn + "(" + this.requireFn + ".s = scheduledModule[0]);",
191+
]),
160192
"}"
161193
]),
162194
"}",
@@ -168,6 +200,21 @@ class JsonpMainTemplatePlugin {
168200
}
169201
return source;
170202
});
203+
mainTemplate.plugin("startup", function(source, chunk, hash) {
204+
if(needChunkLoadingCode(chunk)) {
205+
var jsonpFunction = this.outputOptions.jsonpFunction;
206+
return this.asString([
207+
`var jsonpArray = window[${JSON.stringify(jsonpFunction)}] = window[${JSON.stringify(jsonpFunction)}] || [];`,
208+
"var parentJsonpFunction = jsonpArray.push.bind(jsonpArray);",
209+
"jsonpArray.push = webpackJsonpCallback;",
210+
"jsonpArray = jsonpArray.slice();",
211+
"for(var i = 0; i < jsonpArray.length; i++) webpackJsonpCallback(jsonpArray[i]);",
212+
"",
213+
source
214+
]);
215+
}
216+
return source;
217+
});
171218
mainTemplate.plugin("hot-bootstrap", function(source, chunk, hash) {
172219
const hotUpdateChunkFilename = this.outputOptions.hotUpdateChunkFilename;
173220
const hotUpdateMainFilename = this.outputOptions.hotUpdateMainFilename;
@@ -198,7 +245,7 @@ this[${JSON.stringify(hotUpdateFunction)}] = ${runtimeSource}`;
198245
});
199246
mainTemplate.plugin("hash", function(hash) {
200247
hash.update("jsonp");
201-
hash.update("4");
248+
hash.update("5");
202249
hash.update(`${this.outputOptions.filename}`);
203250
hash.update(`${this.outputOptions.chunkFilename}`);
204251
hash.update(`${this.outputOptions.jsonpFunction}`);

‎lib/optimize/AggressiveSplittingPlugin.js‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,18 @@ class AggressiveSplittingPlugin {
153153
// 3. save to made splittings to records
154154
const minSize = this.options.minSize;
155155
if(!records.aggressiveSplits) records.aggressiveSplits = [];
156+
const newSplits = [];
157+
let splittingInvalid = false;
156158
compilation.chunks.forEach((chunk) => {
157159
if(chunk.hasEntryModule()) return;
158160
const size = chunk.size(this.options);
159161
const incorrectSize = size < minSize;
160162
const modules = chunk.mapModules(m => identifierUtils.makePathsRelative(compiler.context, m.identifier(), compilation.cache));
161163
if(typeof chunk._fromAggressiveSplittingIndex === "undefined") {
162164
if(incorrectSize) return;
165+
// this is a new chunk splitting, we record it so we reuse it next time
163166
chunk.recorded = true;
164-
records.aggressiveSplits.push({
167+
newSplits.push({
165168
modules: modules,
166169
hash: chunk.hash,
167170
id: chunk.id
@@ -172,15 +175,20 @@ class AggressiveSplittingPlugin {
172175
if(chunk._fromAggressiveSplitting) {
173176
chunk._aggressiveSplittingInvalid = true;
174177
splitData.invalid = true;
178+
splittingInvalid = true;
175179
} else {
176180
splitData.hash = chunk.hash;
177181
}
178182
}
179183
}
180184
});
181-
records.aggressiveSplits = records.aggressiveSplits.filter((splitData) => {
182-
return !splitData.invalid;
183-
});
185+
if(splittingInvalid) {
186+
records.aggressiveSplits = records.aggressiveSplits.filter((splitData) => {
187+
return !splitData.invalid;
188+
});
189+
} else {
190+
records.aggressiveSplits = records.aggressiveSplits.concat(newSplits);
191+
}
184192
});
185193
compilation.plugin("need-additional-seal", (callback) => {
186194
const invalid = compilation.chunks.some((chunk) => {

‎lib/optimize/CommonsChunkPlugin.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ Take a look at the "name"/"names" or async/children option.`);
262262
return allChunks.filter((chunk) => {
263263
const found = targetChunks.indexOf(chunk);
264264
if(found >= currentIndex) return false;
265-
return chunk.hasRuntime();
265+
return chunk.isInitial();
266266
});
267267
}
268268

@@ -349,7 +349,7 @@ Take a look at the "name"/"names" or async/children option.`);
349349
// add chunk to commonChunk
350350
commonChunk.addChunk(chunk);
351351

352-
for(const entrypoint of chunk.entrypoints) {
352+
for(const entrypoint of chunk.entrypoints.slice()) {
353353
entrypoint.insertChunk(commonChunk, chunk);
354354
}
355355
}

‎lib/optimize/MergeDuplicateChunksPlugin.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class MergeDuplicateChunksPlugin {
1111
compilation.plugin("optimize-chunks-basic", (chunks) => {
1212
const map = Object.create(null);
1313
chunks.slice().forEach((chunk) => {
14-
if(chunk.hasRuntime() || chunk.hasEntryModule()) return;
14+
if(chunk.hasEntryModule()) return;
1515
const ident = chunk.getModulesIdent();
1616
const otherChunk = map[ident];
1717
if(otherChunk) {

‎test/Compiler.test.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ describe("Compiler", () => {
161161
bundle.should.not.containEql("fixtures");
162162
chunk.should.not.containEql("fixtures");
163163
bundle.should.containEql("webpackJsonp");
164-
chunk.should.containEql("webpackJsonp(");
164+
chunk.should.containEql("window[\"webpackJsonp\"] || []).push");
165165
done();
166166
});
167167
});

‎test/Stats.test.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ describe("Stats", () => {
9898
actual.should.be.eql(expected);
9999
done();
100100
});
101-
});
101+
}, 10000);
102102
});
103103
describe("Error Handling", () => {
104104
describe("does have", () => {
@@ -180,4 +180,4 @@ describe("Stats", () => {
180180
});
181181
});
182182
});
183-
});
183+
}, 10000);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "a";

0 commit comments

Comments
 (0)