-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(tanstackstart-react): Add Vite Config Wrapper for Source Map Uploads #18712
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
nicohrubec
wants to merge
8
commits into
develop
Choose a base branch
from
nh/tss-vite-config-wrapper
base: develop
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.
+625
−20
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
704a781
Add placeholder vite config wrapper
nicohrubec 6cc5784
align usage with solidstart
nicohrubec b4a0d83
Add placeholder to add plugins
nicohrubec b6ddbff
Add sentry vite plugin and enable source maps plugins automatically
nicohrubec 8c6f0fa
?
nicohrubec 207a96a
add unit tests
nicohrubec 53e9099
add vite wrapper to e2e tests
nicohrubec b9c51e9
simplify unit tests
nicohrubec 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| export {}; | ||
| export * from './types'; | ||
| export { wrapConfigWithSentry } from './wrapConfigWithSentry'; |
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,111 @@ | ||
| type BundleSizeOptimizationOptions = { | ||
| /** | ||
| * If set to `true`, the plugin will attempt to tree-shake (remove) any debugging code within the Sentry SDK. | ||
| * Note that the success of this depends on tree shaking being enabled in your build tooling. | ||
| * | ||
| * Setting this option to `true` will disable features like the SDK's `debug` option. | ||
| */ | ||
| excludeDebugStatements?: boolean; | ||
|
|
||
| /** | ||
| * If set to true, the plugin will try to tree-shake tracing statements out. | ||
| * Note that the success of this depends on tree shaking generally being enabled in your build. | ||
| * Attention: DO NOT enable this when you're using any performance monitoring-related SDK features (e.g. Sentry.startSpan()). | ||
| */ | ||
| excludeTracing?: boolean; | ||
|
|
||
| /** | ||
| * If set to `true`, the plugin will attempt to tree-shake (remove) code related to the Sentry SDK's Session Replay Shadow DOM recording functionality. | ||
| * Note that the success of this depends on tree shaking being enabled in your build tooling. | ||
| * | ||
| * This option is safe to be used when you do not want to capture any Shadow DOM activity via Sentry Session Replay. | ||
| */ | ||
| excludeReplayShadowDom?: boolean; | ||
|
|
||
| /** | ||
| * If set to `true`, the plugin will attempt to tree-shake (remove) code related to the Sentry SDK's Session Replay `iframe` recording functionality. | ||
| * Note that the success of this depends on tree shaking being enabled in your build tooling. | ||
| * | ||
| * You can safely do this when you do not want to capture any `iframe` activity via Sentry Session Replay. | ||
| */ | ||
| excludeReplayIframe?: boolean; | ||
|
|
||
| /** | ||
| * If set to `true`, the plugin will attempt to tree-shake (remove) code related to the Sentry SDK's Session Replay's Compression Web Worker. | ||
| * Note that the success of this depends on tree shaking being enabled in your build tooling. | ||
| * | ||
| * **Notice:** You should only do use this option if you manually host a compression worker and configure it in your Sentry Session Replay integration config via the `workerUrl` option. | ||
| */ | ||
| excludeReplayWorker?: boolean; | ||
| }; | ||
|
|
||
| type SourceMapsOptions = { | ||
| /** | ||
| * If this flag is `true`, and an auth token is detected, the Sentry SDK will | ||
| * automatically generate and upload source maps to Sentry during a production build. | ||
| * | ||
| * @default true | ||
| */ | ||
| enabled?: boolean; | ||
|
|
||
| /** | ||
| * If this flag is `true`, the Sentry plugin will collect some telemetry data and send it to Sentry. | ||
| * It will not collect any sensitive or user-specific data. | ||
| * | ||
| * @default true | ||
| */ | ||
| telemetry?: boolean; | ||
|
|
||
| /** | ||
| * A glob or an array of globs that specifies the build artifacts that should be deleted after the artifact | ||
| * upload to Sentry has been completed. | ||
| * | ||
| * @default [] - By default no files are deleted. | ||
| * | ||
| * The globbing patterns follow the implementation of the glob package. (https://www.npmjs.com/package/glob) | ||
| */ | ||
| filesToDeleteAfterUpload?: string | Array<string>; | ||
| }; | ||
|
|
||
| /** | ||
| * Build options for the Sentry plugin. These options are used during build-time by the Sentry SDK. | ||
| */ | ||
| export type SentryTanstackStartReactPluginOptions = { | ||
| /** | ||
| * The auth token to use when uploading source maps to Sentry. | ||
| * | ||
| * Instead of specifying this option, you can also set the `SENTRY_AUTH_TOKEN` environment variable. | ||
| * | ||
| * To create an auth token, follow this guide: | ||
| * @see https://docs.sentry.io/product/accounts/auth-tokens/#organization-auth-tokens | ||
| */ | ||
| authToken?: string; | ||
|
|
||
| /** | ||
| * The organization slug of your Sentry organization. | ||
| * Instead of specifying this option, you can also set the `SENTRY_ORG` environment variable. | ||
| */ | ||
| org?: string; | ||
|
|
||
| /** | ||
| * The project slug of your Sentry project. | ||
| * Instead of specifying this option, you can also set the `SENTRY_PROJECT` environment variable. | ||
| */ | ||
| project?: string; | ||
|
|
||
| /** | ||
| * Options for the Sentry Vite plugin to customize the source maps upload process. | ||
| */ | ||
| sourceMapsUploadOptions?: SourceMapsOptions; | ||
|
|
||
| /** | ||
| * Options for the Sentry Vite plugin to customize bundle size optimizations. | ||
| */ | ||
| bundleSizeOptimizations?: BundleSizeOptimizationOptions; | ||
|
|
||
| /** | ||
| * Enable debug functionality of the SDK during build-time. | ||
| * Enabling this will give you, for example logs about source maps. | ||
| */ | ||
| debug?: boolean; | ||
| }; | ||
45 changes: 45 additions & 0 deletions
45
packages/tanstackstart-react/src/config/wrapConfigWithSentry.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,45 @@ | ||
| import type { UserConfig } from 'vite'; | ||
| import { addSentryPlugins } from '../vite'; | ||
| import type { SentryTanstackStartReactPluginOptions } from './types'; | ||
|
|
||
| /** | ||
| * Wraps a Vite configuration object with Sentry build-time enhancements such as | ||
| * automatic source maps upload. | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * // vite.config.ts | ||
| * import { defineConfig } from 'vite'; | ||
| * import { wrapConfigWithSentry } from '@sentry/tanstackstart-react'; | ||
| * | ||
| * export default defineConfig( | ||
| * wrapConfigWithSentry( | ||
| * { | ||
| * // Your Vite/TanStack Start config | ||
| * plugins: [...] | ||
| * }, | ||
| * { | ||
| * // Sentry build-time options | ||
| * org: 'your-org', | ||
| * project: 'your-project', | ||
| * }, | ||
| * ), | ||
| * ); | ||
| * ``` | ||
| * | ||
| * @param config - A Vite configuration object | ||
| * @param sentryPluginOptions - Options to configure the Sentry Vite plugin | ||
| * @returns The modified Vite config to be passed to `defineConfig` | ||
| */ | ||
| export function wrapConfigWithSentry( | ||
| config: UserConfig = {}, | ||
| sentryPluginOptions: SentryTanstackStartReactPluginOptions = {}, | ||
| ): UserConfig { | ||
| const userPlugins = Array.isArray(config.plugins) ? [...config.plugins] : []; | ||
| const plugins = addSentryPlugins(userPlugins, sentryPluginOptions, config); | ||
|
|
||
| return { | ||
| ...config, | ||
| plugins, | ||
| }; | ||
| } |
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,31 @@ | ||
| import type { PluginOption, UserConfig } from 'vite'; | ||
| import type { SentryTanstackStartReactPluginOptions } from '../config/types'; | ||
| import { makeAddSentryVitePlugin, makeEnableSourceMapsVitePlugin } from './sourceMaps'; | ||
|
|
||
| /** | ||
| * Adds Sentry plugins to the given array of Vite plugins. | ||
| */ | ||
| export function addSentryPlugins( | ||
| plugins: PluginOption[], | ||
| options: SentryTanstackStartReactPluginOptions, | ||
| viteConfig: UserConfig, | ||
| ): PluginOption[] { | ||
| const sentryPlugins: PluginOption[] = []; | ||
|
|
||
| // Only add source map plugins in production builds | ||
| if (process.env.NODE_ENV !== 'development') { | ||
| // Check if source maps upload is enabled | ||
| // Default to enabled | ||
| const sourceMapsEnabled = options.sourceMapsUploadOptions?.enabled ?? true; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You probably want to use |
||
|
|
||
| if (sourceMapsEnabled) { | ||
| const sourceMapsPlugins = makeAddSentryVitePlugin(options, viteConfig); | ||
| const enableSourceMapsPlugin = makeEnableSourceMapsVitePlugin(options); | ||
|
|
||
| sentryPlugins.push(...sourceMapsPlugins, ...enableSourceMapsPlugin); | ||
| } | ||
| } | ||
|
|
||
| // Prepend Sentry plugins so they run first | ||
| return [...sentryPlugins, ...plugins]; | ||
| } | ||
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 @@ | ||
| export { addSentryPlugins } from './addSentryPlugins'; |
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,136 @@ | ||
| import { sentryVitePlugin } from '@sentry/vite-plugin'; | ||
| import type { Plugin, UserConfig } from 'vite'; | ||
| import type { SentryTanstackStartReactPluginOptions } from '../config/types'; | ||
|
|
||
| /** | ||
| * A Sentry plugin for adding the @sentry/vite-plugin to automatically upload source maps to Sentry. | ||
| */ | ||
| export function makeAddSentryVitePlugin( | ||
| options: SentryTanstackStartReactPluginOptions, | ||
| viteConfig: UserConfig, | ||
| ): Plugin[] { | ||
| const { authToken, bundleSizeOptimizations, debug, org, project, sourceMapsUploadOptions } = options; | ||
|
|
||
| let updatedFilesToDeleteAfterUpload: string[] | undefined = undefined; | ||
|
|
||
| if ( | ||
| typeof sourceMapsUploadOptions?.filesToDeleteAfterUpload === 'undefined' && | ||
| // Only if source maps were previously not set, we update the "filesToDeleteAfterUpload" (as we override the setting with "hidden") | ||
| typeof viteConfig.build?.sourcemap === 'undefined' | ||
| ) { | ||
| // For .output, .vercel, .netlify etc. | ||
| updatedFilesToDeleteAfterUpload = ['.*/**/*.map']; | ||
|
|
||
| if (debug) { | ||
| // eslint-disable-next-line no-console | ||
| console.log( | ||
| `[Sentry] Automatically setting \`sourceMapsUploadOptions.filesToDeleteAfterUpload: ${JSON.stringify( | ||
| updatedFilesToDeleteAfterUpload, | ||
| )}\` to delete generated source maps after they were uploaded to Sentry.`, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| return [ | ||
| ...sentryVitePlugin({ | ||
| authToken: authToken ?? process.env.SENTRY_AUTH_TOKEN, | ||
| bundleSizeOptimizations: bundleSizeOptimizations ?? undefined, | ||
| debug: debug ?? false, | ||
| org: org ?? process.env.SENTRY_ORG, | ||
| project: project ?? process.env.SENTRY_PROJECT, | ||
| sourcemaps: { | ||
| filesToDeleteAfterUpload: sourceMapsUploadOptions?.filesToDeleteAfterUpload ?? updatedFilesToDeleteAfterUpload, | ||
| }, | ||
| telemetry: sourceMapsUploadOptions?.telemetry ?? true, | ||
| _metaOptions: { | ||
| telemetry: { | ||
| metaFramework: 'tanstackstart-react', | ||
| }, | ||
| }, | ||
| }), | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * A Sentry plugin for TanStack Start React to enable "hidden" source maps if they are unset. | ||
| */ | ||
| export function makeEnableSourceMapsVitePlugin(options: SentryTanstackStartReactPluginOptions): Plugin[] { | ||
| return [ | ||
| { | ||
| name: 'sentry-tanstackstart-react-source-maps', | ||
| apply: 'build', | ||
| enforce: 'post', | ||
| config(viteConfig) { | ||
| return { | ||
| ...viteConfig, | ||
| build: { | ||
| ...viteConfig.build, | ||
| sourcemap: getUpdatedSourceMapSettings(viteConfig, options), | ||
| }, | ||
| }; | ||
| }, | ||
| }, | ||
| ]; | ||
| } | ||
|
|
||
| /** There are 3 ways to set up source map generation (https://github.com/getsentry/sentry-javascript/issues/13993) | ||
| * | ||
| * 1. User explicitly disabled source maps | ||
| * - keep this setting (emit a warning that errors won't be unminified in Sentry) | ||
| * - We won't upload anything | ||
| * | ||
| * 2. Users enabled source map generation (true, 'hidden', 'inline'). | ||
| * - keep this setting (don't do anything - like deletion - besides uploading) | ||
| * | ||
| * 3. Users didn't set source maps generation | ||
| * - we enable 'hidden' source maps generation | ||
| * - configure `filesToDeleteAfterUpload` to delete all .map files (we emit a log about this) | ||
| * | ||
| * --> only exported for testing | ||
| */ | ||
| export function getUpdatedSourceMapSettings( | ||
| viteConfig: UserConfig, | ||
| sentryPluginOptions?: SentryTanstackStartReactPluginOptions, | ||
| ): boolean | 'inline' | 'hidden' { | ||
| viteConfig.build = viteConfig.build || {}; | ||
|
|
||
| const viteSourceMap = viteConfig.build?.sourcemap; | ||
| let updatedSourceMapSetting = viteSourceMap; | ||
|
|
||
| const settingKey = 'vite.build.sourcemap'; | ||
| const debug = sentryPluginOptions?.debug; | ||
|
|
||
| if (viteSourceMap === false) { | ||
| updatedSourceMapSetting = viteSourceMap; | ||
|
|
||
| if (debug) { | ||
| // eslint-disable-next-line no-console | ||
| console.warn( | ||
| `[Sentry] Source map generation is currently disabled in your TanStack Start configuration (\`${settingKey}: false\`). Sentry won't override this setting. Without source maps, code snippets on the Sentry Issues page will remain minified.`, | ||
| ); | ||
| } else { | ||
| // eslint-disable-next-line no-console | ||
| console.warn('[Sentry] Source map generation is disabled in your TanStack Start configuration.'); | ||
| } | ||
| } else if (viteSourceMap && ['hidden', 'inline', true].includes(viteSourceMap)) { | ||
| updatedSourceMapSetting = viteSourceMap; | ||
|
|
||
| if (debug) { | ||
| // eslint-disable-next-line no-console | ||
| console.log( | ||
| `[Sentry] We discovered \`${settingKey}\` is set to \`${viteSourceMap.toString()}\`. Sentry will keep this source map setting.`, | ||
| ); | ||
| } | ||
| } else { | ||
| updatedSourceMapSetting = 'hidden'; | ||
|
|
||
| if (debug) { | ||
| // eslint-disable-next-line no-console | ||
| console.log( | ||
| `[Sentry] Enabled source map generation in the build options with \`${settingKey}: 'hidden'\`. The source maps will be deleted after they were uploaded to Sentry.`, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| return updatedSourceMapSetting; | ||
| } |
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.
You can/should extend from this type, so we can make sure that all build-time options are aligned:
sentry-javascript/packages/core/src/build-time-plugins/buildTimeOptionsBase.ts
Line 27 in 2c6e710