Permalink
Browse files

change jsonp function to jsonp array push

  • Loading branch information...
sokra committed Apr 22, 2017
1 parent 2b1046e commit 7387c50c3a50d95756e6f92c36d2b0368e7adb56
Showing with 335 additions and 271 deletions.
  1. +1 −1 lib/Chunk.js
  2. +28 −4 lib/Entrypoint.js
  3. +6 −4 lib/JsonpChunkTemplatePlugin.js
  4. +47 −11 lib/JsonpMainTemplatePlugin.js
  5. +12 −4 lib/optimize/AggressiveSplittingPlugin.js
  6. +2 −2 lib/optimize/CommonsChunkPlugin.js
  7. +1 −1 lib/optimize/MergeDuplicateChunksPlugin.js
  8. +1 −1 test/Compiler.test.js
  9. +1 −1 test/Stats.test.js
  10. +19 −19 test/statsCases/aggressive-splitting-entry/expected.txt
  11. +8 −6 test/statsCases/aggressive-splitting-entry/input-records.json
  12. +51 −48 test/statsCases/aggressive-splitting-on-demand/expected.txt
  13. +35 −46 test/statsCases/aggressive-splitting-on-demand/input-records.json
  14. +5 −5 test/statsCases/chunks/expected.txt
  15. +1 −1 test/statsCases/color-disabled/expected.txt
  16. +1 −1 test/statsCases/color-enabled-custom/expected.txt
  17. +1 −1 test/statsCases/color-enabled/expected.txt
  18. +3 −3 test/statsCases/commons-chunk-min-size-0/expected.txt
  19. +1 −1 test/statsCases/commons-chunk-min-size-Infinity/expected.txt
  20. +9 −9 test/statsCases/commons-plugin-issue-4980/expected.txt
  21. +3 −3 test/statsCases/define-plugin/expected.txt
  22. +1 −1 test/statsCases/exclude-with-loader/expected.txt
  23. +1 −1 test/statsCases/external/expected.txt
  24. +14 −14 test/statsCases/filter-warnings/expected.txt
  25. +4 −4 test/statsCases/import-weak/expected.txt
  26. +14 −14 test/statsCases/limit-chunk-count-plugin/expected.txt
  27. +1 −1 test/statsCases/max-modules-default/expected.txt
  28. +1 −1 test/statsCases/max-modules/expected.txt
  29. +4 −4 test/statsCases/named-chunks-plugin-async/expected.txt
  30. +6 −6 test/statsCases/named-chunks-plugin/expected.txt
  31. +9 −9 test/statsCases/optimize-chunks/expected.txt
  32. +3 −3 test/statsCases/performance-disabled/expected.txt
  33. +3 −3 test/statsCases/performance-error/expected.txt
  34. +3 −3 test/statsCases/performance-no-hints/expected.txt
  35. +5 −5 test/statsCases/preset-detailed/expected.txt
  36. +3 −3 test/statsCases/preset-normal-performance-ensure-filter-sourcemaps/expected.txt
  37. +3 −3 test/statsCases/preset-normal-performance/expected.txt
  38. +5 −5 test/statsCases/preset-normal/expected.txt
  39. +5 −5 test/statsCases/preset-verbose/expected.txt
  40. +1 −1 test/statsCases/resolve-plugin-context/expected.txt
  41. +1 −1 test/statsCases/reverse-sort-modules/expected.txt
  42. +3 −3 test/statsCases/scope-hoisting-multi/expected.txt
  43. +5 −5 test/statsCases/separate-css-bundle/expected.txt
  44. +1 −1 test/statsCases/simple-more-info/expected.txt
  45. +1 −1 test/statsCases/simple/expected.txt
  46. +1 −1 test/statsCases/tree-shaking/expected.txt
  47. +1 −1 test/statsCases/warnings-uglifyjs/expected.txt
@@ -63,7 +63,7 @@ class Chunk {

hasRuntime() {
if(this.entrypoints.length === 0) return false;
return this.entrypoints[0].chunks[0] === this;
return this.entrypoints[0].getRuntimeChunk() === this;
}

isInitial() {
@@ -10,19 +10,39 @@ class Entrypoint {
this.chunks = [];
}

removeChunk(chunk) {
var idx = this.chunks.indexOf(chunk);
if(idx >= 0) this.chunks.splice(idx, 1);
idx = chunk.entrypoints.indexOf(this);
if(idx >= 0) chunk.entrypoints.splice(idx, 1);
}

unshiftChunk(chunk) {
this.chunks.unshift(chunk);
chunk.entrypoints.push(this);
}

insertChunk(chunk, before) {
const oldIdx = this.chunks.indexOf(chunk);
const idx = this.chunks.indexOf(before);
if(idx >= 0) {
this.chunks.splice(idx, 0, chunk);
} else {
if(idx < 0) {
throw new Error("before chunk not found");
}
chunk.entrypoints.push(this);
if(oldIdx >= 0 && oldIdx > idx) {
this.chunks.splice(oldIdx, 1);
this.chunks.splice(idx, 0, chunk);
} else if(oldIdx < 0) {
this.chunks.splice(idx, 0, chunk);
chunk.entrypoints.push(this);
}
}

appendChunk(chunk) {
const idx = this.chunks.indexOf(chunk);
if(idx < 0) {
this.chunks.push(chunk);
chunk.entrypoints.push(this);
}
}

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

return files;
}

getRuntimeChunk() {
return this.chunks[0];
}
}

module.exports = Entrypoint;
@@ -11,18 +11,20 @@ class JsonpChunkTemplatePlugin {
chunkTemplate.plugin("render", function(modules, chunk) {
const jsonpFunction = this.outputOptions.jsonpFunction;
const source = new ConcatSource();
source.add(`${jsonpFunction}(${JSON.stringify(chunk.ids)},`);
source.add(`(window[${JSON.stringify(jsonpFunction)}] = window[${JSON.stringify(jsonpFunction)}] || []).push([${JSON.stringify(chunk.ids)},`);
source.add(modules);
const entries = [chunk.entryModule].filter(Boolean).map(m => m.id);
const entries = [chunk.entryModule]
.filter(Boolean)
.map(m => [chunk.entrypoints[0].chunks.map(c => c.id), m.id]);
if(entries.length > 0) {
source.add(`,${JSON.stringify(entries)}`);
}
source.add(")");
source.add("])");
return source;
});
chunkTemplate.plugin("hash", function(hash) {
hash.update("JsonpChunkTemplatePlugin");
hash.update("3");
hash.update("4");
hash.update(`${this.outputOptions.jsonpFunction}`);
hash.update(`${this.outputOptions.library}`);
});
@@ -9,8 +9,15 @@ const Template = require("./Template");
class JsonpMainTemplatePlugin {

apply(mainTemplate) {
function needChunkLoadingCode(chunk) {
var otherChunksInEntry = chunk.entrypoints.some(function(entrypoint) {
return entrypoint.chunks.length > 1;
});
var onDemandChunks = chunk.chunks.length > 0;
return otherChunksInEntry || onDemandChunks;
}
mainTemplate.plugin("local-vars", function(source, chunk) {
if(chunk.chunks.length > 0) {
if(needChunkLoadingCode(chunk)) {
return this.asString([
source,
"",
@@ -19,7 +26,9 @@ class JsonpMainTemplatePlugin {
this.indent(
chunk.ids.map(id => `${JSON.stringify(id)}: 0`).join(",\n")
),
"};"
"};",
"",
"var scheduledModules = [];"
]);
}
return source;
@@ -120,15 +129,14 @@ class JsonpMainTemplatePlugin {
]);
});
mainTemplate.plugin("bootstrap", function(source, chunk, hash) {
if(chunk.chunks.length > 0) {
var jsonpFunction = this.outputOptions.jsonpFunction;
if(needChunkLoadingCode(chunk)) {
return this.asString([
source,
"",
"// install a JSONP callback for chunk loading",
`var parentJsonpFunction = window[${JSON.stringify(jsonpFunction)}];`,
`window[${JSON.stringify(jsonpFunction)}] = function webpackJsonpCallback(chunkIds, moreModules, executeModules) {`,
"function webpackJsonpCallback(data) {",
this.indent([
"var chunkIds = data[0], moreModules = data[1], executeModules = data[2];",
"// add \"moreModules\" to the modules object,",
"// then flag all \"chunkIds\" as loaded and fire callback",
"var moduleId, chunkId, i = 0, resolves = [], result;",
@@ -148,15 +156,28 @@ class JsonpMainTemplatePlugin {
"}"
]),
"}",
"if(parentJsonpFunction) parentJsonpFunction(chunkIds, moreModules, executeModules);",
"if(parentJsonpFunction) parentJsonpFunction(data);",
"while(resolves.length) {",
this.indent("resolves.shift()();"),
"}",
this.entryPointInChildren(chunk) ? [
"if(executeModules) {",
"scheduledModules.push.apply(scheduledModules, executeModules || []);",
"",
"for(i = 0; i < scheduledModules.length; i++) {",
this.indent([
"for(i=0; i < executeModules.length; i++) {",
this.indent(`result = ${this.requireFn}(${this.requireFn}.s = executeModules[i]);`),
"var scheduledModule = scheduledModules[i];",
"var fullfilled = true;",
"for(var j = 0; j < scheduledModule[0].length; j++) {",
this.indent([
"var depId = scheduledModule[0][j];",
"if(installedChunks[depId] !== 0) fullfilled = false;"
]),
"}",
"if(fullfilled) {",
this.indent([
"scheduledModules.splice(i--, 1);",
"result = " + this.requireFn + "(" + this.requireFn + ".s = scheduledModule[1]);",
]),
"}"
]),
"}",
@@ -168,6 +189,21 @@ class JsonpMainTemplatePlugin {
}
return source;
});
mainTemplate.plugin("startup", function(source, chunk, hash) {
if(needChunkLoadingCode(chunk)) {
var jsonpFunction = this.outputOptions.jsonpFunction;
return this.asString([
`var jsonpArray = window[${JSON.stringify(jsonpFunction)}] = window[${JSON.stringify(jsonpFunction)}] || [];`,
"var parentJsonpFunction = jsonpArray.push.bind(jsonpArray);",
"jsonpArray.push = webpackJsonpCallback;",
"jsonpArray = jsonpArray.slice();",
"for(var i = 0; i < jsonpArray.length; i++) webpackJsonpCallback(jsonpArray[i]);",
"",
source
]);
}
return source;
});
mainTemplate.plugin("hot-bootstrap", function(source, chunk, hash) {
const hotUpdateChunkFilename = this.outputOptions.hotUpdateChunkFilename;
const hotUpdateMainFilename = this.outputOptions.hotUpdateMainFilename;
@@ -198,7 +234,7 @@ this[${JSON.stringify(hotUpdateFunction)}] = ${runtimeSource}`;
});
mainTemplate.plugin("hash", function(hash) {
hash.update("jsonp");
hash.update("4");
hash.update("5");
hash.update(`${this.outputOptions.filename}`);
hash.update(`${this.outputOptions.chunkFilename}`);
hash.update(`${this.outputOptions.jsonpFunction}`);
@@ -153,15 +153,18 @@ class AggressiveSplittingPlugin {
// 3. save to made splittings to records
const minSize = this.options.minSize;
if(!records.aggressiveSplits) records.aggressiveSplits = [];
const newSplits = [];
let splittingInvalid = false;
compilation.chunks.forEach((chunk) => {
if(chunk.hasEntryModule()) return;
const size = chunk.size(this.options);
const incorrectSize = size < minSize;
const modules = chunk.mapModules(m => identifierUtils.makePathsRelative(compiler.context, m.identifier(), compilation.cache));
if(typeof chunk._fromAggressiveSplittingIndex === "undefined") {
if(incorrectSize) return;
// this is a new chunk splitting, we record it so we reuse it next time
chunk.recorded = true;
records.aggressiveSplits.push({
newSplits.push({
modules: modules,
hash: chunk.hash,
id: chunk.id
@@ -172,15 +175,20 @@ class AggressiveSplittingPlugin {
if(chunk._fromAggressiveSplitting) {
chunk._aggressiveSplittingInvalid = true;
splitData.invalid = true;
splittingInvalid = true;
} else {
splitData.hash = chunk.hash;
}
}
}
});
records.aggressiveSplits = records.aggressiveSplits.filter((splitData) => {
return !splitData.invalid;
});
if(splittingInvalid) {
records.aggressiveSplits = records.aggressiveSplits.filter((splitData) => {
return !splitData.invalid;
});
} else {
records.aggressiveSplits = records.aggressiveSplits.concat(newSplits);
}
});
compilation.plugin("need-additional-seal", (callback) => {
const invalid = compilation.chunks.some((chunk) => {
@@ -262,7 +262,7 @@ Take a look at the "name"/"names" or async/children option.`);
return allChunks.filter((chunk) => {
const found = targetChunks.indexOf(chunk);
if(found >= currentIndex) return false;
return chunk.hasRuntime();
return chunk.isInitial();
});
}

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

for(const entrypoint of chunk.entrypoints) {
for(const entrypoint of chunk.entrypoints.slice()) {
entrypoint.insertChunk(commonChunk, chunk);
}
}
@@ -11,7 +11,7 @@ class MergeDuplicateChunksPlugin {
compilation.plugin("optimize-chunks-basic", (chunks) => {
const map = Object.create(null);
chunks.slice().forEach((chunk) => {
if(chunk.hasRuntime() || chunk.hasEntryModule()) return;
if(chunk.hasEntryModule()) return;
const ident = chunk.getModulesIdent();
const otherChunk = map[ident];
if(otherChunk) {
@@ -161,7 +161,7 @@ describe("Compiler", () => {
bundle.should.not.containEql("fixtures");
chunk.should.not.containEql("fixtures");
bundle.should.containEql("webpackJsonp");
chunk.should.containEql("webpackJsonp(");
chunk.should.containEql("window[\"webpackJsonp\"] || []).push");
done();
});
});
@@ -180,4 +180,4 @@ describe("Stats", () => {
});
});
});
});
}, 10000);
@@ -1,24 +1,24 @@
Hash: c4756fe25e35ccb187f7
Hash: c55fcf171c46cbd1e5a1
Time: Xms
Asset Size Chunks Chunk Names
48c8b1dae03a37363ec8.js 4.2 kB 1 [emitted]
002fc3bb6fc14459f8e8.js 2.23 kB 2 [emitted]
9356e9a0fb00a97b2e73.js 1.94 kB 3 [emitted]
88d78642a86768757078.js 979 bytes 4 [emitted]
Entrypoint main = 48c8b1dae03a37363ec8.js 9356e9a0fb00a97b2e73.js 88d78642a86768757078.js 002fc3bb6fc14459f8e8.js
chunk {1} 48c8b1dae03a37363ec8.js 1.8 kB [entry] [rendered]
Asset Size Chunks Chunk Names
9748a8a04a5102209105.js 2.29 kB 1 [emitted]
79e8b67b4f31cdb0299f.js 1.99 kB 2 [emitted]
628e75ea29b4a779b369.js 1.03 kB 3 [emitted]
dd43a4ed55c20668ec9b.js 5.72 kB 444 [emitted]
Entrypoint main = dd43a4ed55c20668ec9b.js 79e8b67b4f31cdb0299f.js 628e75ea29b4a779b369.js 9748a8a04a5102209105.js
chunk {1} 9748a8a04a5102209105.js 1.91 kB [initial] [rendered]
> aggressive-splitted main [4] (webpack)/test/statsCases/aggressive-splitting-entry/index.js
[0] (webpack)/test/statsCases/aggressive-splitting-entry/b.js 899 bytes {1} [built]
[1] (webpack)/test/statsCases/aggressive-splitting-entry/c.js 899 bytes {1} [built]
chunk {2} 002fc3bb6fc14459f8e8.js 1.91 kB [initial] [rendered]
[4] (webpack)/test/statsCases/aggressive-splitting-entry/index.js 112 bytes {1} [built]
[6] (webpack)/test/statsCases/aggressive-splitting-entry/f.js 899 bytes {1} [built]
[7] (webpack)/test/statsCases/aggressive-splitting-entry/g.js 899 bytes {1} [built]
chunk {2} 79e8b67b4f31cdb0299f.js 1.8 kB [initial] [rendered] [recorded]
> aggressive-splitted main [4] (webpack)/test/statsCases/aggressive-splitting-entry/index.js
[4] (webpack)/test/statsCases/aggressive-splitting-entry/index.js 112 bytes {2} [built]
[6] (webpack)/test/statsCases/aggressive-splitting-entry/f.js 899 bytes {2} [built]
[7] (webpack)/test/statsCases/aggressive-splitting-entry/g.js 899 bytes {2} [built]
chunk {3} 9356e9a0fb00a97b2e73.js 1.8 kB [initial] [rendered] [recorded]
[2] (webpack)/test/statsCases/aggressive-splitting-entry/d.js 899 bytes {2} [built]
[5] (webpack)/test/statsCases/aggressive-splitting-entry/a.js 899 bytes {2} [built]
chunk {3} 628e75ea29b4a779b369.js 899 bytes [initial] [rendered]
> aggressive-splitted main [4] (webpack)/test/statsCases/aggressive-splitting-entry/index.js
[2] (webpack)/test/statsCases/aggressive-splitting-entry/d.js 899 bytes {3} [built]
[5] (webpack)/test/statsCases/aggressive-splitting-entry/a.js 899 bytes {3} [built]
chunk {4} 88d78642a86768757078.js 899 bytes [initial] [rendered]
[3] (webpack)/test/statsCases/aggressive-splitting-entry/e.js 899 bytes {3} [built]
chunk {444} dd43a4ed55c20668ec9b.js 1.8 kB [entry] [rendered]
> aggressive-splitted main [4] (webpack)/test/statsCases/aggressive-splitting-entry/index.js
[3] (webpack)/test/statsCases/aggressive-splitting-entry/e.js 899 bytes {4} [built]
[0] (webpack)/test/statsCases/aggressive-splitting-entry/b.js 899 bytes {444} [built]
[1] (webpack)/test/statsCases/aggressive-splitting-entry/c.js 899 bytes {444} [built]
@@ -5,22 +5,24 @@
"c.js": 1,
"d.js": 2,
"e.js": 3,
"index.js": 4
"index.js": 4,
"a.js": 5
},
"usedIds": {
"0": 0,
"1": 1,
"2": 2,
"3": 3,
"4": 4
"4": 4,
"5": 5
}
},
"chunks": {
"byName": {},
"byBlocks": {},
"usedIds": {
"0": 0,
"1": 1
"444": 444
}
},
"aggressiveSplits": [
@@ -29,8 +31,8 @@
"b.js",
"c.js"
],
"hash": "48c8b1dae03a37363ec82be4f7b781bc",
"id": 1
"hash": "dd43a4ed55c20668ec9be140e41d560b",
"id": 444
}
]
}
}
Oops, something went wrong.

0 comments on commit 7387c50

Please sign in to comment.