Skip to content

Commit

Permalink
Merge pull request #7201 from webpack/bugfix/content-hash
Browse files Browse the repository at this point in the history
avoid injection jsonpScriptSrc function when not needed
  • Loading branch information
sokra committed May 4, 2018
2 parents 88bf798 + 0a6ba95 commit ac7c28c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 12 deletions.
19 changes: 12 additions & 7 deletions lib/web/JsonpMainTemplatePlugin.js
Expand Up @@ -108,9 +108,9 @@ class JsonpMainTemplatePlugin {
mainTemplate.hooks.localVars.tap(
"JsonpMainTemplatePlugin",
(source, chunk, hash) => {
const extraCode = [];
if (needChunkLoadingCode(chunk)) {
return Template.asString([
source,
extraCode.push(
"",
"// object to store loaded and loading chunks",
"// undefined = chunk not loaded, null = chunk preloaded/prefetched",
Expand All @@ -120,6 +120,12 @@ class JsonpMainTemplatePlugin {
chunk.ids.map(id => `${JSON.stringify(id)}: 0`).join(",\n")
),
"};",
"",
needEntryDeferringCode(chunk) ? "var deferredModules = [];" : ""
);
}
if (needChunkOnDemandLoadingCode(chunk)) {
extraCode.push(
"",
"// script path function",
"function jsonpScriptSrc(chunkId) {",
Expand All @@ -130,12 +136,11 @@ class JsonpMainTemplatePlugin {
"chunkId"
)}`
]),
"}",
"",
needEntryDeferringCode(chunk) ? "var deferredModules = [];" : ""
]);
"}"
);
}
return source;
if (extraCode.length === 0) return source;
return Template.asString([source, ...extraCode]);
}
);

Expand Down
10 changes: 5 additions & 5 deletions test/__snapshots__/StatsTestCases.test.js.snap
Expand Up @@ -615,7 +615,7 @@ exports[`StatsTestCases should print correct stats for commons-chunk-min-size-0
Time: Xms
Built at: Thu Jan 01 1970 00:00:00 GMT
Asset Size Chunks Chunk Names
entry-1.js 5.79 KiB 0 [emitted] entry-1
entry-1.js 5.62 KiB 0 [emitted] entry-1
vendor-1~entry-1.js 314 bytes 1 [emitted] vendor-1~entry-1
Entrypoint entry-1 = vendor-1~entry-1.js entry-1.js
[0] ./entry-1.js 145 bytes {0} [built]
Expand All @@ -632,7 +632,7 @@ exports[`StatsTestCases should print correct stats for commons-chunk-min-size-In
Time: Xms
Built at: Thu Jan 01 1970 00:00:00 GMT
Asset Size Chunks Chunk Names
entry-1.js 5.79 KiB 0 [emitted] entry-1
entry-1.js 5.62 KiB 0 [emitted] entry-1
vendor-1.js 314 bytes 1 [emitted] vendor-1
Entrypoint entry-1 = vendor-1.js entry-1.js
[0] ./entry-1.js 145 bytes {0} [built]
Expand All @@ -651,7 +651,7 @@ Child
Time: Xms
Built at: Thu Jan 01 1970 00:00:00 GMT
Asset Size Chunks Chunk Names
app.js 5.9 KiB 0 [emitted] app
app.js 5.71 KiB 0 [emitted] app
vendor.6a3bdffda9f0de672978.js 619 bytes 1 [emitted] vendor
Entrypoint app = vendor.6a3bdffda9f0de672978.js app.js
[./constants.js] 87 bytes {1} [built]
Expand All @@ -664,7 +664,7 @@ Child
Time: Xms
Built at: Thu Jan 01 1970 00:00:00 GMT
Asset Size Chunks Chunk Names
app.js 5.92 KiB 0 [emitted] app
app.js 5.72 KiB 0 [emitted] app
vendor.6a3bdffda9f0de672978.js 619 bytes 1 [emitted] vendor
Entrypoint app = vendor.6a3bdffda9f0de672978.js app.js
[./constants.js] 87 bytes {1} [built]
Expand Down Expand Up @@ -1321,7 +1321,7 @@ exports[`StatsTestCases should print correct stats for named-chunks-plugin 1`] =
Time: Xms
Built at: Thu Jan 01 1970 00:00:00 GMT
Asset Size Chunks Chunk Names
entry.js 5.64 KiB entry [emitted] entry
entry.js 5.47 KiB entry [emitted] entry
vendor.js 269 bytes vendor [emitted] vendor
Entrypoint entry = vendor.js entry.js
[./entry.js] 72 bytes {entry} [built]
Expand Down
@@ -0,0 +1 @@
it("should compile and evaluate fine", () => {});
@@ -0,0 +1,7 @@
const fs = require("fs");
module.exports = {
findBundle: function(i, options) {
var files = fs.readdirSync(options.output.path);
return ["runtime.js", files.filter(f => /^main/.test(f))[0]];
}
};
@@ -0,0 +1,13 @@
module.exports = {
entry: {
main: "./index"
},
target: "web",
output: {
filename: "[name].js",
chunkFilename: "main.[contenthash:8].js"
},
optimization: {
runtimeChunk: "single"
}
};

0 comments on commit ac7c28c

Please sign in to comment.