Skip to content

Commit

Permalink
Merge pull request #4923 from webpack/bugfix/promise-later
Browse files Browse the repository at this point in the history
Use Promise only when chunk load is triggered
  • Loading branch information
sokra committed May 24, 2017
2 parents ae389b0 + 09d9533 commit 0dd0830
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 31 deletions.
17 changes: 8 additions & 9 deletions lib/JsonpMainTemplatePlugin.js
Expand Up @@ -19,9 +19,7 @@ class JsonpMainTemplatePlugin {
this.indent(
chunk.ids.map(id => `${JSON.stringify(id)}: 0`).join(",\n")
),
"};",
"",
"var resolvedPromise = new Promise(function(resolve) { resolve(); });"
"};"
]);
}
return source;
Expand Down Expand Up @@ -81,26 +79,27 @@ class JsonpMainTemplatePlugin {
});
mainTemplate.plugin("require-ensure", function(_, chunk, hash) {
return this.asString([
"if(installedChunks[chunkId] === 0) {",
"var installedChunkData = installedChunks[chunkId];",
"if(installedChunkData === 0) {",
this.indent([
"return resolvedPromise;"
"return new Promise(function(resolve) { resolve(); });"
]),
"}",
"",
"// a Promise means \"currently loading\".",
"if(installedChunks[chunkId]) {",
"if(installedChunkData) {",
this.indent([
"return installedChunks[chunkId][2];"
"return installedChunkData[2];"
]),
"}",
"",
"// setup Promise in chunk cache",
"var promise = new Promise(function(resolve, reject) {",
this.indent([
"installedChunks[chunkId] = [resolve, reject];"
"installedChunkData = installedChunks[chunkId] = [resolve, reject];"
]),
"});",
"installedChunks[chunkId][2] = promise;",
"installedChunkData[2] = promise;",
"",
"// start chunk loading",
"var head = document.getElementsByTagName('head')[0];",
Expand Down
36 changes: 19 additions & 17 deletions lib/webworker/WebWorkerMainTemplatePlugin.js
Expand Up @@ -21,32 +21,34 @@ class WebWorkerMainTemplatePlugin {
return id + ": 1";
}).join(",\n")
),
"};",
"",
"var resolvedPromise = new Promise(function(resolve) { resolve(); });"
"};"
]);
}
return source;
});
mainTemplate.plugin("require-ensure", function(_, chunk, hash) {
const chunkFilename = this.outputOptions.chunkFilename;
return this.asString([
"// \"1\" is the signal for \"already loaded\"",
"if(!installedChunks[chunkId]) {",
"return new Promise(function(resolve) {",
this.indent([
"importScripts(" +
this.applyPluginsWaterfall("asset-path", JSON.stringify(chunkFilename), {
hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"",
hashWithLength: function(length) {
return "\" + " + this.renderCurrentHashCode(hash, length) + " + \"";
}.bind(this),
chunk: {
id: "\" + chunkId + \""
}
}) + ");"
"// \"1\" is the signal for \"already loaded\"",
"if(!installedChunks[chunkId]) {",
this.indent([
"importScripts(" +
this.applyPluginsWaterfall("asset-path", JSON.stringify(chunkFilename), {
hash: "\" + " + this.renderCurrentHashCode(hash) + " + \"",
hashWithLength: function(length) {
return "\" + " + this.renderCurrentHashCode(hash, length) + " + \"";
}.bind(this),
chunk: {
id: "\" + chunkId + \""
}
}) + ");"
]),
"}",
"resolve();"
]),
"}",
"return resolvedPromise;"
"});"
]);
});
mainTemplate.plugin("bootstrap", function(source, chunk, hash) {
Expand Down
6 changes: 3 additions & 3 deletions test/WebWorkerMainTemplatePlugin.test.js
Expand Up @@ -85,8 +85,6 @@ var installedChunks = {
2: 1,
3: 1
};
var resolvedPromise = new Promise(function(resolve) { resolve(); });
`.trim())
});
});
Expand All @@ -109,11 +107,13 @@ var resolvedPromise = new Promise(function(resolve) { resolve(); });

it("creates import scripts call and promise resolve", () => {
env.source.should.be.exactly(`
return new Promise(function(resolve) {
// "1" is the signal for "already loaded"
if(!installedChunks[chunkId]) {
importScripts("asset-path" + abc123 + "" + abc123 + "" + chunkId + "");
}
return resolvedPromise;
resolve();
});
`.trim())
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/statsCases/commons-chunk-min-size-0/expected.txt
Expand Up @@ -2,7 +2,7 @@ Hash: dc6038bec87a57d1a45e
Time: Xms
Asset Size Chunks Chunk Names
entry-1.js 25 bytes 0 [emitted] entry-1
vendor-1.js 6.92 kB 1 [emitted] vendor-1
vendor-1.js 6.93 kB 1 [emitted] vendor-1
chunk {0} entry-1.js (entry-1) 0 bytes {1} [initial] [rendered]
chunk {1} vendor-1.js (vendor-1) 329 bytes [entry] [rendered]
[0] (webpack)/test/statsCases/commons-chunk-min-size-0/modules/a.js 22 bytes {1} [built]
Expand Down
2 changes: 1 addition & 1 deletion test/statsCases/limit-chunk-count-plugin/expected.txt
Expand Up @@ -31,7 +31,7 @@ Child
Asset Size Chunks Chunk Names
0.bundle.js 445 bytes 0 [emitted]
1.bundle.js 204 bytes 1 [emitted]
bundle.js 6.27 kB 2 [emitted] main
bundle.js 6.28 kB 2 [emitted] main
chunk {0} 0.bundle.js 74 bytes {2} [rendered]
[0] (webpack)/test/statsCases/limit-chunk-count-plugin/a.js 22 bytes {0} [built]
[2] (webpack)/test/statsCases/limit-chunk-count-plugin/c.js 30 bytes {0} [built]
Expand Down

0 comments on commit 0dd0830

Please sign in to comment.