Skip to content

Commit

Permalink
fix handling of onChunksLoaded with prefetching
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Jul 16, 2021
1 parent 08112f1 commit 8ce0f24
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 210 deletions.
14 changes: 6 additions & 8 deletions lib/javascript/JavascriptModulesPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,9 @@ class JavascriptModulesPlugin {
}
}
if (runtimeRequirements.has(RuntimeGlobals.onChunksLoaded)) {
startupSource.add(`${RuntimeGlobals.onChunksLoaded}();\n`);
startupSource.add(
`__webpack_exports__ = ${RuntimeGlobals.onChunksLoaded}(__webpack_exports__);\n`
);
}
source.add(
hooks.renderStartup.call(startupSource, lastInlinedModule, {
Expand Down Expand Up @@ -1152,13 +1154,9 @@ class JavascriptModulesPlugin {
}
}
if (runtimeRequirements.has(RuntimeGlobals.onChunksLoaded)) {
if (runtimeRequirements.has(RuntimeGlobals.prefetchChunk)) {
buf2.push(`${RuntimeGlobals.onChunksLoaded}();`);
} else {
buf2.push(
`__webpack_exports__ = ${RuntimeGlobals.onChunksLoaded}(__webpack_exports__);`
);
}
buf2.push(
`__webpack_exports__ = ${RuntimeGlobals.onChunksLoaded}(__webpack_exports__);`
);
}
if (
runtimeRequirements.has(RuntimeGlobals.startup) ||
Expand Down
9 changes: 5 additions & 4 deletions lib/prefetch/ChunkPrefetchStartupRuntimeModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,17 @@ class ChunkPrefetchStartupRuntimeModule extends RuntimeModule {
`${RuntimeGlobals.onChunksLoaded}(0, ${JSON.stringify(
// This need to include itself to delay execution after this chunk has been fully loaded
onChunks.filter(c => c === chunk).map(c => c.id)
)}, ${runtimeTemplate.expressionFunction(
)}, ${runtimeTemplate.basicFunction(
"",
chunks.size < 3
? Array.from(
chunks,
c =>
`${RuntimeGlobals.prefetchChunk}(${JSON.stringify(c.id)})`
).join(", ")
`${RuntimeGlobals.prefetchChunk}(${JSON.stringify(c.id)});`
)
: `${JSON.stringify(Array.from(chunks, c => c.id))}.map(${
RuntimeGlobals.prefetchChunk
})`
});`
)}, 5);`
)
);
Expand Down
6 changes: 5 additions & 1 deletion lib/runtime/OnChunksLoadedRuntimeModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ class OnChunksLoadedRuntimeModule extends RuntimeModule {
]),
"}",
"if(fulfilled) {",
Template.indent(["deferred.splice(i--, 1)", "result = fn();"]),
Template.indent([
"deferred.splice(i--, 1)",
"var r = fn();",
"if (r !== undefined) result = r;"
]),
"}"
]),
"}",
Expand Down
384 changes: 192 additions & 192 deletions test/__snapshots__/StatsTestCases.basictest.js.snap

Large diffs are not rendered by default.

8 changes: 3 additions & 5 deletions test/configCases/issues/issue-12993/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import(/* webpackPrefetch: true */ "./dynamic.js");

export const main = "main";

it("library output should be accurate value", done => {
expect(global.lib.main).toBe("main");
done();
it("library output should be accurate value", async () => {
expect(global.lib).toEqual(nsObj({ main: "main" }));
await import(/* webpackPrefetch: true */ "./dynamic.js");
});
5 changes: 5 additions & 0 deletions test/configCases/issues/issue-12993/test.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
afterExecute() {
delete global.lib;
}
};
8 changes: 8 additions & 0 deletions test/configCases/issues/issue-12993/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,13 @@ module.exports = [
library: "lib",
libraryTarget: "global"
}
},
{
mode: "development",
devtool: false,
output: {
library: "lib",
libraryTarget: "global"
}
}
];

0 comments on commit 8ce0f24

Please sign in to comment.