-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
refactor(core): rewrite Webpack ChunkAssetPlugin with RuntimeModule #10485
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f6427b9
Refactor ChunkAssetPlugin
slorber 80af817
refactor: apply lint autofix
slorber 3367b2f
minimize diff/history, restore former plugin name
slorber 2df3b3f
rename ChunkAssetRuntimeModule
slorber a266f8c
more refactor
slorber 2927f98
Merge branch 'main' into slorber/webpack-refactor-gca-plugin
slorber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 0 additions & 55 deletions
55
packages/docusaurus/src/webpack/plugins/ChunkAssetPlugin.ts
This file was deleted.
Oops, something went wrong.
76 changes: 76 additions & 0 deletions
76
packages/docusaurus/src/webpack/plugins/GetChunkAssetRuntimePlugin.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /** | ||
| * Copyright (c) Facebook, Inc. and its affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| import webpack, {type Compiler} from 'webpack'; | ||
|
|
||
| const PluginName = 'Docusaurus-ChunkAssetPlugin'; | ||
|
|
||
| // This generates a __webpack_require__.gca fn to the Webpack runtime chunk | ||
| // It is called in docusaurus.ts for chunk prefetching | ||
| function generateGetChunkAssetRuntimeCode(chunk: webpack.Chunk): string { | ||
| const chunkIdToName = chunk.getChunkMaps(false).name; | ||
| const chunkNameToId = Object.fromEntries( | ||
| Object.entries(chunkIdToName).map(([chunkId, chunkName]) => [ | ||
| chunkName, | ||
| chunkId, | ||
| ]), | ||
| ); | ||
|
|
||
| // Custom Docusaurus runtime function to convert from chunkId to chunk url | ||
| const DocusaurusGetChunkAsset = '__webpack_require__.gca'; | ||
|
|
||
| const { | ||
| // publicPath = __webpack_require__.p | ||
| // Example: "/" or "/baseUrl/" | ||
| // https://github.com/webpack/webpack/blob/v5.94.0/lib/runtime/PublicPathRuntimeModule.js | ||
| publicPath, | ||
|
|
||
| // getChunkScriptFilename = __webpack_require__.u | ||
| // Example: getChunkScriptFilename("814f3328") = "814f3328.03fcc178.js" | ||
| // https://github.com/webpack/webpack/blob/v5.94.0/lib/runtime/GetChunkFilenameRuntimeModule.js | ||
| getChunkScriptFilename, | ||
| } = webpack.RuntimeGlobals; | ||
|
|
||
| const code = `// Docusaurus function to get chunk asset | ||
| ${DocusaurusGetChunkAsset} = function(chunkId) { chunkId = ${JSON.stringify( | ||
| chunkNameToId, | ||
| )}[chunkId]||chunkId; return ${publicPath} + ${getChunkScriptFilename}(chunkId); };`; | ||
|
|
||
| return webpack.Template.asString(code); | ||
| } | ||
|
|
||
| /* | ||
| Note: we previously used "mainTemplate.hooks.requireExtensions.tap" | ||
| But it will be removed in Webpack 6 and is not supported by Rspack | ||
| So instead we use equivalent code inspired by: | ||
| - https://github.com/webpack/webpack/blob/v5.94.0/lib/RuntimePlugin.js#L462 | ||
| - https://github.com/webpack/webpack/blob/v5.94.0/lib/runtime/CompatRuntimeModule.js | ||
| */ | ||
| export default class GetChunkAssetRuntimePlugin { | ||
| apply(compiler: Compiler): void { | ||
| compiler.hooks.thisCompilation.tap(PluginName, (compilation) => { | ||
| compilation.hooks.additionalTreeRuntimeRequirements.tap( | ||
| PluginName, | ||
| (chunk) => { | ||
| compilation.addRuntimeModule(chunk, new ChunkAssetRuntimeModule()); | ||
| }, | ||
| ); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| // Inspired by https://github.com/webpack/webpack/blob/v5.94.0/lib/runtime/CompatRuntimeModule.js | ||
| class ChunkAssetRuntimeModule extends webpack.RuntimeModule { | ||
| constructor() { | ||
| super('compat', webpack.RuntimeModule.STAGE_ATTACH); | ||
| this.fullHash = true; | ||
| } | ||
|
|
||
| override generate() { | ||
| return generateGetChunkAssetRuntimeCode(this.chunk!); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.