Repository navigation
feat(bundler-plugins): Enable hidden source maps and remove them after upload in the Vite plugin #25646
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
timfish
wants to merge
1
commit into
feat/bundler-plugins-delete-sourcemaps-rollup
from
feat/bundler-plugins-delete-sourcemaps-vite
+434
−34
Open
feat(bundler-plugins): Enable hidden source maps and remove them after upload in the Vite plugin #25646
Changes from all commits
Commits
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
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
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,128 @@ | ||
| import * as path from 'node:path'; | ||
| import type { SentryBuildPluginManager, SourceMapDecision } from '../core'; | ||
| import type { RollupSourceMapSetting, SourceMapHandling } from '../rollup/source-map-handling'; | ||
| import { sourceMapKindFromRollupSetting } from '../rollup/source-map-handling'; | ||
|
|
||
| type BundlerOutput = { dir?: string; sourcemap?: RollupSourceMapSetting }; | ||
| type BundlerOutputOptions = BundlerOutput | BundlerOutput[] | undefined; | ||
|
|
||
| type ViteBuildOptions = { | ||
| sourcemap?: RollupSourceMapSetting; | ||
| outDir?: string; | ||
| rollupOptions?: { output?: BundlerOutputOptions }; | ||
| rolldownOptions?: { output?: BundlerOutputOptions }; | ||
| }; | ||
|
|
||
| export type ViteUserConfig = { | ||
| build?: ViteBuildOptions; | ||
| environments?: Record<string, { build?: ViteBuildOptions } | undefined>; | ||
| }; | ||
|
|
||
| export type ViteResolvedConfig = { | ||
| root: string; | ||
| build: ViteBuildOptions; | ||
| environments?: Record<string, { build: ViteBuildOptions }>; | ||
| }; | ||
|
|
||
| export type ViteSourceMapHooks = { | ||
| config: (userConfig: ViteUserConfig, env: { command: string }) => ViteUserConfig | null; | ||
| configResolved: (config: ViteResolvedConfig) => void; | ||
| }; | ||
|
|
||
| const NO_CHANGE: SourceMapDecision = { enableHidden: false, deleteAfterUpload: false }; | ||
|
|
||
| type EnvironmentOutput = { destination: string; sourcemap: RollupSourceMapSetting }; | ||
|
|
||
| /** | ||
| * The outputs an environment writes: `build.outDir` (relative to the root) unless an output in | ||
| * `rollupOptions` or `rolldownOptions` sets its own `dir`, which the bundler resolves from the | ||
| * working directory. An output's own `sourcemap` overrides `build.sourcemap`. | ||
| */ | ||
| function getOutputs(root: string, build: ViteBuildOptions): EnvironmentOutput[] { | ||
| const configured = build.rolldownOptions?.output ?? build.rollupOptions?.output; | ||
| const outputs = (Array.isArray(configured) ? configured : [configured]).filter( | ||
| (output): output is BundlerOutput => output !== undefined, | ||
| ); | ||
| const outputsWithDir = outputs.filter( | ||
| (output): output is BundlerOutput & { dir: string } => typeof output.dir === 'string', | ||
| ); | ||
|
|
||
| if (outputsWithDir.length > 0) { | ||
| return outputsWithDir.map(output => ({ | ||
| destination: path.resolve(output.dir), | ||
| sourcemap: output.sourcemap ?? build.sourcemap, | ||
| })); | ||
| } | ||
|
|
||
| return [ | ||
| { | ||
| destination: path.resolve(root, build.outDir ?? 'dist'), | ||
| sourcemap: outputs.find(output => output.sourcemap !== undefined)?.sourcemap ?? build.sourcemap, | ||
| }, | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * Vite warns about `rollupOptions.output.sourcemap`, so the Vite plugin decides on `build.sourcemap` | ||
| * in its `config` hook. The output directories are only known in `configResolved`, which then | ||
| * records the decision for each of them. | ||
| */ | ||
| export function createViteSourceMapHooks( | ||
| sentryBuildPluginManager: SentryBuildPluginManager, | ||
| sourceMapHandling: SourceMapHandling, | ||
| ): ViteSourceMapHooks { | ||
| // `'*'` for the top-level setting, otherwise the name of an environment with its own. | ||
| const decisions = new Map<string, SourceMapDecision>(); | ||
|
|
||
| return { | ||
| config: (userConfig, env) => { | ||
| if (env.command !== 'build') { | ||
| return null; | ||
| } | ||
|
|
||
| const decision = sentryBuildPluginManager.sourceMaps.decide( | ||
| sourceMapKindFromRollupSetting(userConfig.build?.sourcemap), | ||
| ); | ||
| decisions.set('*', decision); | ||
|
|
||
| const result: ViteUserConfig = decision.enableHidden ? { build: { sourcemap: 'hidden' } } : {}; | ||
|
|
||
| for (const [name, environment] of Object.entries(userConfig.environments ?? {})) { | ||
| const setting = environment?.build?.sourcemap; | ||
| if (setting === undefined) { | ||
| continue; | ||
| } | ||
|
|
||
| const environmentDecision = sentryBuildPluginManager.sourceMaps.decide(sourceMapKindFromRollupSetting(setting)); | ||
| decisions.set(name, environmentDecision); | ||
|
|
||
| if (environmentDecision.enableHidden) { | ||
| result.environments = { ...result.environments, [name]: { build: { sourcemap: 'hidden' } } }; | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| }, | ||
|
|
||
| configResolved: config => { | ||
| const environments = config.environments ?? { client: { build: config.build } }; | ||
|
|
||
| for (const [name, environment] of Object.entries(environments)) { | ||
| const decision = decisions.get(name) ?? decisions.get('*') ?? NO_CHANGE; | ||
|
|
||
| for (const output of getOutputs(config.root, environment.build)) { | ||
| // A plugin that ran after ours may have changed the setting again, | ||
| // or the output sets its own. | ||
| if (decision.deleteAfterUpload && output.sourcemap !== 'hidden') { | ||
| sentryBuildPluginManager.logger.debug( | ||
| `Source maps of the "${name}" environment are set to ${JSON.stringify(output.sourcemap)}. Not removing them after upload.`, | ||
| ); | ||
| sourceMapHandling.setDecision(output.destination, NO_CHANGE); | ||
| } else { | ||
| sourceMapHandling.setDecision(output.destination, decision); | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| }; | ||
| } | ||
Oops, something went wrong.
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.
Silent sourcemap default change undocumented
Low Severity
Direct
sentryVitePlugin()users who never setbuild.sourcemapnow get hidden source maps enabled and removed after upload. That is a silent default change for existing apps, and this commit has no changelog or migration note. Flagged because the review rules require migration notes for silent default changes that affect existing apps.Triggered by project rule: PR Review Guidelines for Cursor Bot
Reviewed by Cursor Bugbot for commit f2ee9aa. Configure here.