-
-
Notifications
You must be signed in to change notification settings - Fork 252
fix: use relative paths in source maps #6450
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 all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import path from 'node:path'; | ||
| import { toPosixPath } from '../helpers/path'; | ||
| import type { | ||
| NormalizedEnvironmentConfig, | ||
| RsbuildPlugin, | ||
| Rspack, | ||
| } from '../types'; | ||
|
|
||
| const getDevtool = (config: NormalizedEnvironmentConfig): Rspack.DevTool => { | ||
| const { sourceMap } = config.output; | ||
| const isProd = config.mode === 'production'; | ||
|
|
||
| if (sourceMap === false) { | ||
| return false; | ||
| } | ||
| if (sourceMap === true) { | ||
| return isProd ? 'source-map' : 'cheap-module-source-map'; | ||
| } | ||
| if (sourceMap.js === undefined) { | ||
| return isProd ? false : 'cheap-module-source-map'; | ||
| } | ||
| return sourceMap.js; | ||
| }; | ||
|
|
||
| export const pluginSourceMap = (): RsbuildPlugin => ({ | ||
| name: 'rsbuild:source-map', | ||
|
|
||
| setup(api) { | ||
| const sourceMapFilenameTemplate = ({ | ||
| absoluteResourcePath, | ||
| }: { | ||
| absoluteResourcePath: string; | ||
| }) => absoluteResourcePath; | ||
|
|
||
| api.modifyBundlerChain((chain, { bundler, environment }) => { | ||
| const { config } = environment; | ||
| const devtool = getDevtool(config); | ||
|
|
||
| chain | ||
| .devtool(devtool) | ||
| .output.devtoolModuleFilenameTemplate(sourceMapFilenameTemplate); | ||
|
|
||
| // When JS source map is disabled, but CSS source map is enabled, | ||
| // add `SourceMapDevToolPlugin` to let Rspack generate CSS source map. | ||
| const { sourceMap } = config.output; | ||
| if (!devtool && typeof sourceMap === 'object' && sourceMap.css) { | ||
| chain.plugin('source-map-css').use(bundler.SourceMapDevToolPlugin, [ | ||
| { | ||
| test: /\.css$/, | ||
| filename: '[file].map[query]', | ||
| }, | ||
| ]); | ||
| } | ||
| }); | ||
|
|
||
| // Use project-relative POSIX paths in source maps: | ||
| // - Prevents leaking absolute system paths | ||
| // - Keeps maps portable across environments | ||
| // - Matches source map spec and debugger expectations | ||
| api.processAssets( | ||
| // Source maps has been extracted to separate files on this stage | ||
| { stage: 'optimize-transfer' }, | ||
| ({ assets, compilation, sources }) => { | ||
| // If devtoolModuleFilenameTemplate is not the default template, | ||
| // which means users want to customize it, skip the default processing. | ||
| if ( | ||
| compilation.outputOptions.devtoolModuleFilenameTemplate !== | ||
| sourceMapFilenameTemplate | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| const { distPath } = api.context; | ||
|
|
||
| for (const [filename, asset] of Object.entries(assets)) { | ||
| if (!filename.endsWith('.map')) { | ||
| continue; | ||
| } | ||
|
|
||
| const rawSource = asset.source(); | ||
| let map: Rspack.RawSourceMap; | ||
| try { | ||
| map = JSON.parse( | ||
| Buffer.isBuffer(rawSource) ? rawSource.toString() : rawSource, | ||
| ); | ||
| } catch { | ||
| continue; | ||
| } | ||
|
|
||
| if (!Array.isArray(map.sources)) { | ||
| continue; | ||
| } | ||
|
|
||
| const mapDir = path.dirname(path.join(distPath, filename)); | ||
|
|
||
| let isSourcesUpdated = false; | ||
|
|
||
| map.sources = map.sources.map((source) => { | ||
| if (path.isAbsolute(source)) { | ||
| isSourcesUpdated = true; | ||
| return toPosixPath(path.relative(mapDir, source)); | ||
| } | ||
| return source; | ||
| }); | ||
|
|
||
| if (!isSourcesUpdated) { | ||
| continue; | ||
| } | ||
|
|
||
| compilation.updateAsset( | ||
| filename, | ||
| new sources.RawSource(JSON.stringify(map)), | ||
| ); | ||
| } | ||
| }, | ||
| ); | ||
| }, | ||
| }); | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new source-map plugin derives
mapDirfromapi.context.distPath, which is the common parent directory for all environments. When building multiple environments whoseoutput.distPathvalues are nested (e.g.dist/webanddist/server), asset filenames are relative to each environment’s own output path, so combining them with the common root drops the environment subfolder. The subsequentpath.relative(mapDir, source)therefore emits paths like../../../src/foo.tsthat resolve to<dist>/src/foo.tsinstead of the real project source, breaking source map resolution for any environment that does not emit directly into the shared root. Use the environment-specific path (e.g.compilation.outputOptions.pathor theenvironment.distPathpassed to the hook) when computingmapDirto keep the relative paths correct.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will verify this issue and create another PR to fix it.