Skip to content

Commit 7387c50

Browse files
committed
change jsonp function to jsonp array push
1 parent 2b1046e commit 7387c50

47 files changed

Lines changed: 335 additions & 271 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: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,39 @@ class Entrypoint {
1010
this.chunks = [];
1111
}
1212

13+
removeChunk(chunk) {
14+
var idx = this.chunks.indexOf(chunk);
15+
if(idx >= 0) this.chunks.splice(idx, 1);
16+
idx = chunk.entrypoints.indexOf(this);
17+
if(idx >= 0) chunk.entrypoints.splice(idx, 1);
18+
}
19+
1320
unshiftChunk(chunk) {
1421
this.chunks.unshift(chunk);
1522
chunk.entrypoints.push(this);
1623
}
1724

1825
insertChunk(chunk, before) {
26+
const oldIdx = this.chunks.indexOf(chunk);
1927
const idx = this.chunks.indexOf(before);
20-
if(idx >= 0) {
21-
this.chunks.splice(idx, 0, chunk);
22-
} else {
28+
if(idx < 0) {
2329
throw new Error("before chunk not found");
2430
}
25-
chunk.entrypoints.push(this);
31+
if(oldIdx >= 0 && oldIdx > idx) {
32+
this.chunks.splice(oldIdx, 1);
33+
this.chunks.splice(idx, 0, chunk);
34+
} else if(oldIdx < 0) {
35+
this.chunks.splice(idx, 0, chunk);
36+
chunk.entrypoints.push(this);
37+
}
38+
}
39+
40+
appendChunk(chunk) {
41+
const idx = this.chunks.indexOf(chunk);
42+
if(idx < 0) {
43+
this.chunks.push(chunk);
44+
chunk.entrypoints.push(this);
45+
}
2646
}
2747

2848
getFiles() {
@@ -38,6 +58,10 @@ class Entrypoint {
3858

3959
return files;
4060
}
61+
62+
getRuntimeChunk() {
63+
return this.chunks[0];
64+
}
4165
}
4266

4367
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 => [chunk.entrypoints[0].chunks.map(c => c.id), m.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: 47 additions & 11 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;
@@ -120,15 +129,14 @@ class JsonpMainTemplatePlugin {
120129
]);
121130
});
122131
mainTemplate.plugin("bootstrap", function(source, chunk, hash) {
123-
if(chunk.chunks.length > 0) {
124-
var jsonpFunction = this.outputOptions.jsonpFunction;
132+
if(needChunkLoadingCode(chunk)) {
125133
return this.asString([
126134
source,
127135
"",
128136
"// install a JSONP callback for chunk loading",
129-
`var parentJsonpFunction = window[${JSON.stringify(jsonpFunction)}];`,
130-
`window[${JSON.stringify(jsonpFunction)}] = function webpackJsonpCallback(chunkIds, moreModules, executeModules) {`,
137+
"function webpackJsonpCallback(data) {",
131138
this.indent([
139+
"var chunkIds = data[0], moreModules = data[1], executeModules = data[2];",
132140
"// add \"moreModules\" to the modules object,",
133141
"// then flag all \"chunkIds\" as loaded and fire callback",
134142
"var moduleId, chunkId, i = 0, resolves = [], result;",
@@ -148,15 +156,28 @@ class JsonpMainTemplatePlugin {
148156
"}"
149157
]),
150158
"}",
151-
"if(parentJsonpFunction) parentJsonpFunction(chunkIds, moreModules, executeModules);",
159+
"if(parentJsonpFunction) parentJsonpFunction(data);",
152160
"while(resolves.length) {",
153161
this.indent("resolves.shift()();"),
154162
"}",
155163
this.entryPointInChildren(chunk) ? [
156-
"if(executeModules) {",
164+
"scheduledModules.push.apply(scheduledModules, executeModules || []);",
165+
"",
166+
"for(i = 0; i < scheduledModules.length; i++) {",
157167
this.indent([
158-
"for(i=0; i < executeModules.length; i++) {",
159-
this.indent(`result = ${this.requireFn}(${this.requireFn}.s = executeModules[i]);`),
168+
"var scheduledModule = scheduledModules[i];",
169+
"var fullfilled = true;",
170+
"for(var j = 0; j < scheduledModule[0].length; j++) {",
171+
this.indent([
172+
"var depId = scheduledModule[0][j];",
173+
"if(installedChunks[depId] !== 0) fullfilled = false;"
174+
]),
175+
"}",
176+
"if(fullfilled) {",
177+
this.indent([
178+
"scheduledModules.splice(i--, 1);",
179+
"result = " + this.requireFn + "(" + this.requireFn + ".s = scheduledModule[1]);",
180+
]),
160181
"}"
161182
]),
162183
"}",
@@ -168,6 +189,21 @@ class JsonpMainTemplatePlugin {
168189
}
169190
return source;
170191
});
192+
mainTemplate.plugin("startup", function(source, chunk, hash) {
193+
if(needChunkLoadingCode(chunk)) {
194+
var jsonpFunction = this.outputOptions.jsonpFunction;
195+
return this.asString([
196+
`var jsonpArray = window[${JSON.stringify(jsonpFunction)}] = window[${JSON.stringify(jsonpFunction)}] || [];`,
197+
"var parentJsonpFunction = jsonpArray.push.bind(jsonpArray);",
198+
"jsonpArray.push = webpackJsonpCallback;",
199+
"jsonpArray = jsonpArray.slice();",
200+
"for(var i = 0; i < jsonpArray.length; i++) webpackJsonpCallback(jsonpArray[i]);",
201+
"",
202+
source
203+
]);
204+
}
205+
return source;
206+
});
171207
mainTemplate.plugin("hot-bootstrap", function(source, chunk, hash) {
172208
const hotUpdateChunkFilename = this.outputOptions.hotUpdateChunkFilename;
173209
const hotUpdateMainFilename = this.outputOptions.hotUpdateMainFilename;
@@ -198,7 +234,7 @@ this[${JSON.stringify(hotUpdateFunction)}] = ${runtimeSource}`;
198234
});
199235
mainTemplate.plugin("hash", function(hash) {
200236
hash.update("jsonp");
201-
hash.update("4");
237+
hash.update("5");
202238
hash.update(`${this.outputOptions.filename}`);
203239
hash.update(`${this.outputOptions.chunkFilename}`);
204240
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,4 @@ describe("Stats", () => {
180180
});
181181
});
182182
});
183-
});
183+
}, 10000);
Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
Hash: c4756fe25e35ccb187f7
1+
Hash: c55fcf171c46cbd1e5a1
22
Time: Xms
3-
Asset Size Chunks Chunk Names
4-
48c8b1dae03a37363ec8.js 4.2 kB 1 [emitted]
5-
002fc3bb6fc14459f8e8.js 2.23 kB 2 [emitted]
6-
9356e9a0fb00a97b2e73.js 1.94 kB 3 [emitted]
7-
88d78642a86768757078.js 979 bytes 4 [emitted]
8-
Entrypoint main = 48c8b1dae03a37363ec8.js 9356e9a0fb00a97b2e73.js 88d78642a86768757078.js 002fc3bb6fc14459f8e8.js
9-
chunk {1} 48c8b1dae03a37363ec8.js 1.8 kB [entry] [rendered]
3+
Asset Size Chunks Chunk Names
4+
9748a8a04a5102209105.js 2.29 kB 1 [emitted]
5+
79e8b67b4f31cdb0299f.js 1.99 kB 2 [emitted]
6+
628e75ea29b4a779b369.js 1.03 kB 3 [emitted]
7+
dd43a4ed55c20668ec9b.js 5.72 kB 444 [emitted]
8+
Entrypoint main = dd43a4ed55c20668ec9b.js 79e8b67b4f31cdb0299f.js 628e75ea29b4a779b369.js 9748a8a04a5102209105.js
9+
chunk {1} 9748a8a04a5102209105.js 1.91 kB [initial] [rendered]
1010
> aggressive-splitted main [4] (webpack)/test/statsCases/aggressive-splitting-entry/index.js
11-
[0] (webpack)/test/statsCases/aggressive-splitting-entry/b.js 899 bytes {1} [built]
12-
[1] (webpack)/test/statsCases/aggressive-splitting-entry/c.js 899 bytes {1} [built]
13-
chunk {2} 002fc3bb6fc14459f8e8.js 1.91 kB [initial] [rendered]
11+
[4] (webpack)/test/statsCases/aggressive-splitting-entry/index.js 112 bytes {1} [built]
12+
[6] (webpack)/test/statsCases/aggressive-splitting-entry/f.js 899 bytes {1} [built]
13+
[7] (webpack)/test/statsCases/aggressive-splitting-entry/g.js 899 bytes {1} [built]
14+
chunk {2} 79e8b67b4f31cdb0299f.js 1.8 kB [initial] [rendered] [recorded]
1415
> aggressive-splitted main [4] (webpack)/test/statsCases/aggressive-splitting-entry/index.js
15-
[4] (webpack)/test/statsCases/aggressive-splitting-entry/index.js 112 bytes {2} [built]
16-
[6] (webpack)/test/statsCases/aggressive-splitting-entry/f.js 899 bytes {2} [built]
17-
[7] (webpack)/test/statsCases/aggressive-splitting-entry/g.js 899 bytes {2} [built]
18-
chunk {3} 9356e9a0fb00a97b2e73.js 1.8 kB [initial] [rendered] [recorded]
16+
[2] (webpack)/test/statsCases/aggressive-splitting-entry/d.js 899 bytes {2} [built]
17+
[5] (webpack)/test/statsCases/aggressive-splitting-entry/a.js 899 bytes {2} [built]
18+
chunk {3} 628e75ea29b4a779b369.js 899 bytes [initial] [rendered]
1919
> aggressive-splitted main [4] (webpack)/test/statsCases/aggressive-splitting-entry/index.js
20-
[2] (webpack)/test/statsCases/aggressive-splitting-entry/d.js 899 bytes {3} [built]
21-
[5] (webpack)/test/statsCases/aggressive-splitting-entry/a.js 899 bytes {3} [built]
22-
chunk {4} 88d78642a86768757078.js 899 bytes [initial] [rendered]
20+
[3] (webpack)/test/statsCases/aggressive-splitting-entry/e.js 899 bytes {3} [built]
21+
chunk {444} dd43a4ed55c20668ec9b.js 1.8 kB [entry] [rendered]
2322
> aggressive-splitted main [4] (webpack)/test/statsCases/aggressive-splitting-entry/index.js
24-
[3] (webpack)/test/statsCases/aggressive-splitting-entry/e.js 899 bytes {4} [built]
23+
[0] (webpack)/test/statsCases/aggressive-splitting-entry/b.js 899 bytes {444} [built]
24+
[1] (webpack)/test/statsCases/aggressive-splitting-entry/c.js 899 bytes {444} [built]

0 commit comments

Comments
 (0)