-
-
Notifications
You must be signed in to change notification settings - Fork 843
feat(javascript): add JavascriptModulesPlugin.getChunkFilenameTemplate #15280
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
Open
kakiuwang-ui
wants to merge
8
commits into
web-infra-dev:main
Choose a base branch
from
kakiuwang-ui:feat/js-modules-get-chunk-filename-template
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+311
−10
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5afd818
feat(javascript): add JavascriptModulesPlugin.getChunkFilenameTemplate
kakiuwang-ui bab1ca5
Merge branch 'main' into feat/js-modules-get-chunk-filename-template
kakiuwang-ui 47756a3
Merge branch 'main' into feat/js-modules-get-chunk-filename-template
kakiuwang-ui d055e35
docs(javascript): simplify the hot update chunk note
kakiuwang-ui 02331fa
fix(core): evaluate Filename callbacks in Compilation path helpers
kakiuwang-ui 4af5599
Merge remote-tracking branch 'origin/main' into feat/js-modules-get-c…
kakiuwang-ui 75e2b5c
docs(javascript): drop the stale addedVersion on getChunkFilenameTemp…
kakiuwang-ui b3654b1
fix: align filename callback handling with webpack
SyMind 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
2 changes: 2 additions & 0 deletions
2
tests/rspack-test/configCases/hooks/get-chunk-filename-template/async.js
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,2 @@ | ||
| require('./shared'); | ||
| module.exports = 'async'; |
7 changes: 7 additions & 0 deletions
7
tests/rspack-test/configCases/hooks/get-chunk-filename-template/index.js
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,7 @@ | ||
| require("./shared"); | ||
|
|
||
| it("should render every chunk with the template returned by getChunkFilenameTemplate", () => { | ||
| return import("./async").then(() => { | ||
| expect(true).toBe(true); | ||
| }); | ||
| }); |
72 changes: 72 additions & 0 deletions
72
tests/rspack-test/configCases/hooks/get-chunk-filename-template/rspack.config.js
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,72 @@ | ||
| const { javascript } = require('@rspack/core'); | ||
|
|
||
| const pluginName = 'plugin'; | ||
|
|
||
| class Plugin { | ||
| apply(compiler) { | ||
| let called = false; | ||
| compiler.hooks.compilation.tap(pluginName, (compilation) => { | ||
| compilation.hooks.afterSeal.tap(pluginName, () => { | ||
| called = true; | ||
|
|
||
| const templates = {}; | ||
| for (const chunk of compilation.chunks) { | ||
| const template = | ||
| javascript.JavascriptModulesPlugin.getChunkFilenameTemplate( | ||
| chunk, | ||
| compilation.outputOptions, | ||
| ); | ||
| templates[chunk.name || chunk.id] = template; | ||
|
|
||
| // The returned template has to be the one rspack actually rendered | ||
| // the chunk with. | ||
| expect([...chunk.files]).toContain( | ||
| compilation.getPath(template, { | ||
| chunk, | ||
| contentHashType: 'javascript', | ||
| }), | ||
| ); | ||
| } | ||
|
|
||
| expect(templates).toEqual({ | ||
| // initial chunk without its own template -> output.filename | ||
| main: '[name].js', | ||
| // async chunk without its own template -> output.chunkFilename | ||
| async_js: 'async-[name].js', | ||
| // chunk carrying its own template -> that template wins | ||
| 'shared-shared_js': 'shared-[name].js', | ||
| }); | ||
| }); | ||
| }); | ||
| compiler.hooks.done.tap(pluginName, (stats) => { | ||
| expect(stats.toJson().errors.length).toBe(0); | ||
| expect(called).toBe(true); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| /**@type {import("@rspack/core").Configuration}*/ | ||
| module.exports = { | ||
| context: __dirname, | ||
| mode: 'development', | ||
| entry: './index.js', | ||
| target: 'node', | ||
| output: { | ||
| filename: '[name].js', | ||
| chunkFilename: 'async-[name].js', | ||
| }, | ||
| optimization: { | ||
| chunkIds: 'named', | ||
| splitChunks: { | ||
| cacheGroups: { | ||
| shared: { | ||
| chunks: 'all', | ||
| test: /shared/, | ||
| filename: 'shared-[name].js', | ||
| enforce: true, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| plugins: [new Plugin()], | ||
| }; |
1 change: 1 addition & 0 deletions
1
tests/rspack-test/configCases/hooks/get-chunk-filename-template/shared.js
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 @@ | ||
| module.exports = 'shared'; |
3 changes: 3 additions & 0 deletions
3
tests/rspack-test/configCases/hooks/get-path-filename-callback/index.js
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,3 @@ | ||
| it("should resolve filename callbacks passed to the path helpers", () => { | ||
| expect(1).toBe(1); | ||
| }); |
87 changes: 87 additions & 0 deletions
87
tests/rspack-test/configCases/hooks/get-path-filename-callback/rspack.config.js
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,87 @@ | ||
| const pluginName = 'plugin'; | ||
|
|
||
| class Plugin { | ||
| apply(compiler) { | ||
| let called = false; | ||
| compiler.hooks.compilation.tap(pluginName, (compilation) => { | ||
| compilation.hooks.processAssets.tap(pluginName, () => { | ||
| called = true; | ||
| const chunk = Array.from(compilation.chunks).find( | ||
| (c) => c.name === 'main', | ||
| ); | ||
| expect(chunk).toBeDefined(); | ||
|
|
||
| // a string template keeps working | ||
| expect(compilation.getPath('[name].js', { chunk })).toBe('main.js'); | ||
|
|
||
| // a callback is evaluated, and placeholders it returns are still rendered | ||
| expect( | ||
| compilation.getPath(() => 'from-callback-[name].js', { chunk }), | ||
| ).toBe('from-callback-main.js'); | ||
|
|
||
| // the callback receives the path data it was called with | ||
| let seen; | ||
| compilation.getPath( | ||
| (pathData) => { | ||
| seen = pathData; | ||
| return '[name].js'; | ||
| }, | ||
| { chunk, contentHashType: 'javascript' }, | ||
| ); | ||
| expect(seen.chunk).toBe(chunk); | ||
| expect(seen.contentHashType).toBe('javascript'); | ||
|
|
||
| // getPath helpers provide the compilation hash to callbacks by default | ||
| const pathWithDefaultHash = compilation.getPath('[fullhash].js'); | ||
| expect(compilation.getPath(({ hash }) => `${hash}.js`)).toBe( | ||
| pathWithDefaultHash, | ||
| ); | ||
| expect( | ||
| compilation.getPath((pathData) => { | ||
| pathData.hash = 'callback-hash'; | ||
| return '[fullhash].js'; | ||
| }), | ||
| ).toBe('callback-hash.js'); | ||
|
|
||
| // with-info helpers pass one mutable info object through the callback | ||
| // and placeholder rendering | ||
| const defaultHash = pathWithDefaultHash.slice(0, -'.js'.length); | ||
| let callbackInfo; | ||
| const pathWithInfo = compilation.getPathWithInfo(({ hash }, info) => { | ||
| expect(hash).toBe(defaultHash); | ||
| callbackInfo = info; | ||
| info.custom = 'from-callback'; | ||
| info.fullhash = 'from-callback'; | ||
| return '[fullhash].js'; | ||
| }); | ||
| expect(pathWithInfo.path).toBe(pathWithDefaultHash); | ||
| expect(pathWithInfo.info).toBe(callbackInfo); | ||
| expect(pathWithInfo.info.custom).toBe('from-callback'); | ||
| expect(new Set(pathWithInfo.info.fullhash)).toEqual( | ||
| new Set(['from-callback', defaultHash]), | ||
| ); | ||
|
|
||
| // the other three helpers accept callbacks too | ||
| expect(compilation.getAssetPath(() => '[name].js', { chunk })).toBe( | ||
| 'main.js', | ||
| ); | ||
| expect( | ||
| compilation.getPathWithInfo(() => '[name].js', { chunk }).path, | ||
| ).toBe('main.js'); | ||
| expect( | ||
| compilation.getAssetPathWithInfo(() => '[name].js', { chunk }).path, | ||
| ).toBe('main.js'); | ||
| }); | ||
| }); | ||
| compiler.hooks.done.tap(pluginName, (stats) => { | ||
| expect(stats.toJson().errors.length).toBe(0); | ||
| expect(called).toBe(true); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| /**@type {import("@rspack/core").Configuration}*/ | ||
| module.exports = { | ||
| context: __dirname, | ||
| plugins: [new Plugin()], | ||
| }; |
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
Oops, something went wrong.
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.