Skip to content

Commit ac7c28c

Browse files
authored
Merge pull request #7201 from webpack/bugfix/content-hash
avoid injection jsonpScriptSrc function when not needed
2 parents 88bf798 + 0a6ba95 commit ac7c28c

File tree

5 files changed

+38
-12
lines changed

5 files changed

+38
-12
lines changed

lib/web/JsonpMainTemplatePlugin.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ class JsonpMainTemplatePlugin {
108108
mainTemplate.hooks.localVars.tap(
109109
"JsonpMainTemplatePlugin",
110110
(source, chunk, hash) => {
111+
const extraCode = [];
111112
if (needChunkLoadingCode(chunk)) {
112-
return Template.asString([
113-
source,
113+
extraCode.push(
114114
"",
115115
"// object to store loaded and loading chunks",
116116
"// undefined = chunk not loaded, null = chunk preloaded/prefetched",
@@ -120,6 +120,12 @@ class JsonpMainTemplatePlugin {
120120
chunk.ids.map(id => `${JSON.stringify(id)}: 0`).join(",\n")
121121
),
122122
"};",
123+
"",
124+
needEntryDeferringCode(chunk) ? "var deferredModules = [];" : ""
125+
);
126+
}
127+
if (needChunkOnDemandLoadingCode(chunk)) {
128+
extraCode.push(
123129
"",
124130
"// script path function",
125131
"function jsonpScriptSrc(chunkId) {",
@@ -130,12 +136,11 @@ class JsonpMainTemplatePlugin {
130136
"chunkId"
131137
)}`
132138
]),
133-
"}",
134-
"",
135-
needEntryDeferringCode(chunk) ? "var deferredModules = [];" : ""
136-
]);
139+
"}"
140+
);
137141
}
138-
return source;
142+
if (extraCode.length === 0) return source;
143+
return Template.asString([source, ...extraCode]);
139144
}
140145
);
141146

test/__snapshots__/StatsTestCases.test.js.snap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ exports[`StatsTestCases should print correct stats for commons-chunk-min-size-0
615615
Time: Xms
616616
Built at: Thu Jan 01 1970 00:00:00 GMT
617617
Asset Size Chunks Chunk Names
618-
entry-1.js 5.79 KiB 0 [emitted] entry-1
618+
entry-1.js 5.62 KiB 0 [emitted] entry-1
619619
vendor-1~entry-1.js 314 bytes 1 [emitted] vendor-1~entry-1
620620
Entrypoint entry-1 = vendor-1~entry-1.js entry-1.js
621621
[0] ./entry-1.js 145 bytes {0} [built]
@@ -632,7 +632,7 @@ exports[`StatsTestCases should print correct stats for commons-chunk-min-size-In
632632
Time: Xms
633633
Built at: Thu Jan 01 1970 00:00:00 GMT
634634
Asset Size Chunks Chunk Names
635-
entry-1.js 5.79 KiB 0 [emitted] entry-1
635+
entry-1.js 5.62 KiB 0 [emitted] entry-1
636636
vendor-1.js 314 bytes 1 [emitted] vendor-1
637637
Entrypoint entry-1 = vendor-1.js entry-1.js
638638
[0] ./entry-1.js 145 bytes {0} [built]
@@ -651,7 +651,7 @@ Child
651651
Time: Xms
652652
Built at: Thu Jan 01 1970 00:00:00 GMT
653653
Asset Size Chunks Chunk Names
654-
app.js 5.9 KiB 0 [emitted] app
654+
app.js 5.71 KiB 0 [emitted] app
655655
vendor.6a3bdffda9f0de672978.js 619 bytes 1 [emitted] vendor
656656
Entrypoint app = vendor.6a3bdffda9f0de672978.js app.js
657657
[./constants.js] 87 bytes {1} [built]
@@ -664,7 +664,7 @@ Child
664664
Time: Xms
665665
Built at: Thu Jan 01 1970 00:00:00 GMT
666666
Asset Size Chunks Chunk Names
667-
app.js 5.92 KiB 0 [emitted] app
667+
app.js 5.72 KiB 0 [emitted] app
668668
vendor.6a3bdffda9f0de672978.js 619 bytes 1 [emitted] vendor
669669
Entrypoint app = vendor.6a3bdffda9f0de672978.js app.js
670670
[./constants.js] 87 bytes {1} [built]
@@ -1321,7 +1321,7 @@ exports[`StatsTestCases should print correct stats for named-chunks-plugin 1`] =
13211321
Time: Xms
13221322
Built at: Thu Jan 01 1970 00:00:00 GMT
13231323
Asset Size Chunks Chunk Names
1324-
entry.js 5.64 KiB entry [emitted] entry
1324+
entry.js 5.47 KiB entry [emitted] entry
13251325
vendor.js 269 bytes vendor [emitted] vendor
13261326
Entrypoint entry = vendor.js entry.js
13271327
[./entry.js] 72 bytes {entry} [built]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
it("should compile and evaluate fine", () => {});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const fs = require("fs");
2+
module.exports = {
3+
findBundle: function(i, options) {
4+
var files = fs.readdirSync(options.output.path);
5+
return ["runtime.js", files.filter(f => /^main/.test(f))[0]];
6+
}
7+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = {
2+
entry: {
3+
main: "./index"
4+
},
5+
target: "web",
6+
output: {
7+
filename: "[name].js",
8+
chunkFilename: "main.[contenthash:8].js"
9+
},
10+
optimization: {
11+
runtimeChunk: "single"
12+
}
13+
};

0 commit comments

Comments
 (0)