Skip to content

Commit 0dd0830

Browse files
authored
Merge pull request webpack#4923 from webpack/bugfix/promise-later
Use Promise only when chunk load is triggered
2 parents ae389b0 + 09d9533 commit 0dd0830

File tree

5 files changed

+32
-31
lines changed

5 files changed

+32
-31
lines changed

lib/JsonpMainTemplatePlugin.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ class JsonpMainTemplatePlugin {
1919
this.indent(
2020
chunk.ids.map(id => `${JSON.stringify(id)}: 0`).join(",\n")
2121
),
22-
"};",
23-
"",
24-
"var resolvedPromise = new Promise(function(resolve) { resolve(); });"
22+
"};"
2523
]);
2624
}
2725
return source;
@@ -81,26 +79,27 @@ class JsonpMainTemplatePlugin {
8179
});
8280
mainTemplate.plugin("require-ensure", function(_, chunk, hash) {
8381
return this.asString([
84-
"if(installedChunks[chunkId] === 0) {",
82+
"var installedChunkData = installedChunks[chunkId];",
83+
"if(installedChunkData === 0) {",
8584
this.indent([
86-
"return resolvedPromise;"
85+
"return new Promise(function(resolve) { resolve(); });"
8786
]),
8887
"}",
8988
"",
9089
"// a Promise means \"currently loading\".",
91-
"if(installedChunks[chunkId]) {",
90+
"if(installedChunkData) {",
9291
this.indent([
93-
"return installedChunks[chunkId][2];"
92+
"return installedChunkData[2];"
9493
]),
9594
"}",
9695
"",
9796
"// setup Promise in chunk cache",
9897
"var promise = new Promise(function(resolve, reject) {",
9998
this.indent([
100-
"installedChunks[chunkId] = [resolve, reject];"
99+
"installedChunkData = installedChunks[chunkId] = [resolve, reject];"
101100
]),
102101
"});",
103-
"installedChunks[chunkId][2] = promise;",
102+
"installedChunkData[2] = promise;",
104103
"",
105104
"// start chunk loading",
106105
"var head = document.getElementsByTagName('head')[0];",

lib/webworker/WebWorkerMainTemplatePlugin.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,34 @@ class WebWorkerMainTemplatePlugin {
2121
return id + ": 1";
2222
}).join(",\n")
2323
),
24-
"};",
25-
"",
26-
"var resolvedPromise = new Promise(function(resolve) { resolve(); });"
24+
"};"
2725
]);
2826
}
2927
return source;
3028
});
3129
mainTemplate.plugin("require-ensure", function(_, chunk, hash) {
3230
const chunkFilename = this.outputOptions.chunkFilename;
3331
return this.asString([
34-
"// \"1\" is the signal for \"already loaded\"",
35-
"if(!installedChunks[chunkId]) {",
32+
"return new Promise(function(resolve) {",
3633
this.indent([
37-
"importScripts(" +
38-
this.applyPluginsWaterfall("asset-path", JSON.stringify(chunkFilename), {
39-
hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"",
40-
hashWithLength: function(length) {
41-
return "\" + " + this.renderCurrentHashCode(hash, length) + " + \"";
42-
}.bind(this),
43-
chunk: {
44-
id: "\" + chunkId + \""
45-
}
46-
}) + ");"
34+
"// \"1\" is the signal for \"already loaded\"",
35+
"if(!installedChunks[chunkId]) {",
36+
this.indent([
37+
"importScripts(" +
38+
this.applyPluginsWaterfall("asset-path", JSON.stringify(chunkFilename), {
39+
hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"",
40+
hashWithLength: function(length) {
41+
return "\" + " + this.renderCurrentHashCode(hash, length) + " + \"";
42+
}.bind(this),
43+
chunk: {
44+
id: "\" + chunkId + \""
45+
}
46+
}) + ");"
47+
]),
48+
"}",
49+
"resolve();"
4750
]),
48-
"}",
49-
"return resolvedPromise;"
51+
"});"
5052
]);
5153
});
5254
mainTemplate.plugin("bootstrap", function(source, chunk, hash) {

test/WebWorkerMainTemplatePlugin.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ var installedChunks = {
8585
2: 1,
8686
3: 1
8787
};
88-
89-
var resolvedPromise = new Promise(function(resolve) { resolve(); });
9088
`.trim())
9189
});
9290
});
@@ -109,11 +107,13 @@ var resolvedPromise = new Promise(function(resolve) { resolve(); });
109107

110108
it("creates import scripts call and promise resolve", () => {
111109
env.source.should.be.exactly(`
110+
return new Promise(function(resolve) {
112111
// "1" is the signal for "already loaded"
113112
if(!installedChunks[chunkId]) {
114113
importScripts("asset-path" + abc123 + "" + abc123 + "" + chunkId + "");
115114
}
116-
return resolvedPromise;
115+
resolve();
116+
});
117117
`.trim())
118118
});
119119
});

test/statsCases/commons-chunk-min-size-0/expected.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Hash: dc6038bec87a57d1a45e
22
Time: Xms
33
Asset Size Chunks Chunk Names
44
entry-1.js 25 bytes 0 [emitted] entry-1
5-
vendor-1.js 6.92 kB 1 [emitted] vendor-1
5+
vendor-1.js 6.93 kB 1 [emitted] vendor-1
66
chunk {0} entry-1.js (entry-1) 0 bytes {1} [initial] [rendered]
77
chunk {1} vendor-1.js (vendor-1) 329 bytes [entry] [rendered]
88
[0] (webpack)/test/statsCases/commons-chunk-min-size-0/modules/a.js 22 bytes {1} [built]

test/statsCases/limit-chunk-count-plugin/expected.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Child
3131
Asset Size Chunks Chunk Names
3232
0.bundle.js 445 bytes 0 [emitted]
3333
1.bundle.js 204 bytes 1 [emitted]
34-
bundle.js 6.27 kB 2 [emitted] main
34+
bundle.js 6.28 kB 2 [emitted] main
3535
chunk {0} 0.bundle.js 74 bytes {2} [rendered]
3636
[0] (webpack)/test/statsCases/limit-chunk-count-plugin/a.js 22 bytes {0} [built]
3737
[2] (webpack)/test/statsCases/limit-chunk-count-plugin/c.js 30 bytes {0} [built]

0 commit comments

Comments
 (0)