Skip to content

Commit

Permalink
Merge pull request #13431 from Shinyaigeek/fix/overwritten_webpack__e…
Browse files Browse the repository at this point in the history
…xports_onChunksLoaded

Fix: broken library output by webpackPrefetch
  • Loading branch information
sokra committed Jul 16, 2021
2 parents e5570ab + 3967571 commit e15752c
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 198 deletions.
4 changes: 3 additions & 1 deletion 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
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.

1 change: 1 addition & 0 deletions test/configCases/issues/issue-12993/dynamic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default "dynamic";
6 changes: 6 additions & 0 deletions test/configCases/issues/issue-12993/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const main = "main";

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;
}
};
17 changes: 17 additions & 0 deletions test/configCases/issues/issue-12993/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = [
{
mode: "development",
output: {
library: "lib",
libraryTarget: "global"
}
},
{
mode: "development",
devtool: false,
output: {
library: "lib",
libraryTarget: "global"
}
}
];

0 comments on commit e15752c

Please sign in to comment.