-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(tanstackstart-react): Add sentryTanstackStart vite plugin to manage automatic 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
Merged
+572
−7
Merged
Changes from all commits
Commits
Show all changes
31 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 6eb1c8b
use buildTimeOptionsBase instead of defining my own type
nicohrubec 4dcdabf
Merge branch 'develop' into nh/tss-vite-config-wrapper
nicohrubec 9aaabd9
.
nicohrubec bfa238a
switch to sentry vite plugin
nicohrubec 086b9f6
add changelog entry and pass down all options
nicohrubec 05e08f1
clean
nicohrubec bc0acdd
always add sentry vite plugin and pass down disable option
nicohrubec 8e60de8
Merge branch 'develop' into nh/tss-vite-config-wrapper
nicohrubec 9bbd0b0
readability
nicohrubec 283a545
update tests
nicohrubec 1e736b4
Merge branch 'develop' into nh/tss-vite-config-wrapper
nicohrubec 841a311
bump bundler plugins
nicohrubec 9ad7a8d
update
nicohrubec 286f624
update bundler plugins fr this time
nicohrubec 505c92c
Revert "update bundler plugins fr this time"
nicohrubec 07d5e77
Merge branch 'develop' into nh/tss-vite-config-wrapper
nicohrubec 073e352
Merge branch 'develop' into nh/tss-vite-config-wrapper
nicohrubec 8ce5358
update sentry vite plugin
nicohrubec 76c746c
address some pr comments
nicohrubec bbf7be4
use post for config plugin
nicohrubec 63a6660
fix files to delete after upload settings
nicohrubec c99e1a1
Merge branch 'develop' into nh/tss-vite-config-wrapper
nicohrubec 32ed325
update changelog
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
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 |
|---|---|---|
|
|
@@ -3,3 +3,4 @@ | |
| export * from './config'; | ||
| export * from './server'; | ||
| export * from './common'; | ||
| export * from './vite'; | ||
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 @@ | ||
| export { sentryTanstackStart } from './sentryTanstackStart'; |
43 changes: 43 additions & 0 deletions
43
packages/tanstackstart-react/src/vite/sentryTanstackStart.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,43 @@ | ||
| import type { BuildTimeOptionsBase } from '@sentry/core'; | ||
| import type { Plugin } from 'vite'; | ||
| import { makeAddSentryVitePlugin, makeEnableSourceMapsVitePlugin } from './sourceMaps'; | ||
|
|
||
| /** | ||
| * Vite plugins for the Sentry TanStack Start SDK. | ||
| * | ||
| * @example | ||
| * ```typescript | ||
| * // vite.config.ts | ||
| * import { defineConfig } from 'vite'; | ||
| * import { sentryTanstackStart } from '@sentry/tanstackstart-react'; | ||
| * import { tanstackStart } from '@tanstack/react-start/plugin/vite'; | ||
| * | ||
| * export default defineConfig({ | ||
| * plugins: [ | ||
| * sentryTanstackStart({ | ||
| * org: 'your-org', | ||
| * project: 'your-project', | ||
| * }), | ||
| * tanstackStart(), | ||
| * ], | ||
| * }); | ||
| * ``` | ||
| * | ||
| * @param options - Options to configure the Sentry Vite plugins | ||
| * @returns An array of Vite plugins | ||
| */ | ||
| export function sentryTanstackStart(options: BuildTimeOptionsBase = {}): Plugin[] { | ||
| // Only add plugins in production builds | ||
| if (process.env.NODE_ENV === 'development') { | ||
| return []; | ||
| } | ||
|
|
||
| const plugins: Plugin[] = [...makeAddSentryVitePlugin(options)]; | ||
|
|
||
| const sourceMapsDisabled = options.sourcemaps?.disable === true || options.sourcemaps?.disable === 'disable-upload'; | ||
| if (!sourceMapsDisabled) { | ||
| plugins.push(...makeEnableSourceMapsVitePlugin(options)); | ||
| } | ||
|
|
||
| return 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,162 @@ | ||
| import type { BuildTimeOptionsBase } from '@sentry/core'; | ||
| import { sentryVitePlugin } from '@sentry/vite-plugin'; | ||
| import type { Plugin, UserConfig } from 'vite'; | ||
|
|
||
| type FilesToDeleteAfterUpload = string | string[] | undefined; | ||
|
|
||
| /** | ||
| * A Sentry plugin for adding the @sentry/vite-plugin to automatically upload source maps to Sentry. | ||
| */ | ||
| export function makeAddSentryVitePlugin(options: BuildTimeOptionsBase): Plugin[] { | ||
| const { | ||
| authToken, | ||
| bundleSizeOptimizations, | ||
| debug, | ||
| errorHandler, | ||
| headers, | ||
| org, | ||
| project, | ||
| release, | ||
| sentryUrl, | ||
| silent, | ||
| sourcemaps, | ||
| telemetry, | ||
| } = options; | ||
|
|
||
| // defer resolving the filesToDeleteAfterUpload until we got access to the Vite config | ||
| let resolveFilesToDeleteAfterUpload: ((value: FilesToDeleteAfterUpload) => void) | undefined; | ||
| const filesToDeleteAfterUploadPromise = new Promise<FilesToDeleteAfterUpload>(resolve => { | ||
| resolveFilesToDeleteAfterUpload = resolve; | ||
| }); | ||
|
|
||
| const configPlugin: Plugin = { | ||
| name: 'sentry-tanstackstart-files-to-delete-after-upload-plugin', | ||
| apply: 'build', | ||
| enforce: 'post', | ||
| config(config) { | ||
| const userFilesToDelete = sourcemaps?.filesToDeleteAfterUpload; | ||
|
|
||
| // Only auto-delete source maps if the user didn't configure sourcemaps at all | ||
| if (typeof userFilesToDelete === 'undefined' && typeof config.build?.sourcemap === 'undefined') { | ||
| if (debug) { | ||
| // eslint-disable-next-line no-console | ||
| console.log( | ||
| '[Sentry] Automatically setting `sourcemaps.filesToDeleteAfterUpload: ["./**/*.map"]` to delete generated source maps after they were uploaded to Sentry.', | ||
| ); | ||
| } | ||
| resolveFilesToDeleteAfterUpload?.(['./**/*.map']); | ||
| } else { | ||
| resolveFilesToDeleteAfterUpload?.(userFilesToDelete); | ||
| } | ||
| }, | ||
| }; | ||
|
|
||
| const sentryPlugins = sentryVitePlugin({ | ||
| authToken: authToken ?? process.env.SENTRY_AUTH_TOKEN, | ||
| bundleSizeOptimizations: bundleSizeOptimizations ?? undefined, | ||
| debug: debug ?? false, | ||
| errorHandler, | ||
| headers, | ||
| org: org ?? process.env.SENTRY_ORG, | ||
| project: project ?? process.env.SENTRY_PROJECT, | ||
| release, | ||
| silent, | ||
| sourcemaps: { | ||
| assets: sourcemaps?.assets, | ||
| disable: sourcemaps?.disable, | ||
| ignore: sourcemaps?.ignore, | ||
| filesToDeleteAfterUpload: filesToDeleteAfterUploadPromise, | ||
| }, | ||
| telemetry: telemetry ?? true, | ||
| url: sentryUrl, | ||
| _metaOptions: { | ||
| telemetry: { | ||
| metaFramework: 'tanstackstart-react', | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| return [configPlugin, ...sentryPlugins]; | ||
| } | ||
|
|
||
| /** | ||
| * A Sentry plugin for TanStack Start React to enable "hidden" source maps if they are unset. | ||
| */ | ||
| export function makeEnableSourceMapsVitePlugin(options: BuildTimeOptionsBase): 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?: BuildTimeOptionsBase, | ||
| ): boolean | 'inline' | 'hidden' { | ||
| viteConfig.build = viteConfig.build || {}; | ||
|
|
||
| const viteUserSourceMapSetting = viteConfig.build?.sourcemap; | ||
| const settingKey = 'vite.build.sourcemap'; | ||
| const debug = sentryPluginOptions?.debug; | ||
|
|
||
| // Respect user source map setting if it is explicitly set | ||
| if (viteUserSourceMapSetting === false) { | ||
| 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.'); | ||
| } | ||
|
|
||
| return viteUserSourceMapSetting; | ||
| } else if (viteUserSourceMapSetting && ['hidden', 'inline', true].includes(viteUserSourceMapSetting)) { | ||
| if (debug) { | ||
| // eslint-disable-next-line no-console | ||
| console.log( | ||
| `[Sentry] We discovered \`${settingKey}\` is set to \`${viteUserSourceMapSetting.toString()}\`. Sentry will keep this source map setting.`, | ||
| ); | ||
| } | ||
|
|
||
| return viteUserSourceMapSetting; | ||
| } | ||
|
|
||
| // If the user did not specify a source map setting, we enable 'hidden' by default | ||
| 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 'hidden'; | ||
| } |
82 changes: 82 additions & 0 deletions
82
packages/tanstackstart-react/test/vite/sentryTanstackStart.test.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,82 @@ | ||
| import type { Plugin } from 'vite'; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { sentryTanstackStart } from '../../src/vite/sentryTanstackStart'; | ||
|
|
||
| const mockSourceMapsConfigPlugin: Plugin = { | ||
| name: 'sentry-tanstackstart-files-to-delete-after-upload-plugin', | ||
| apply: 'build', | ||
| enforce: 'pre', | ||
| config: vi.fn(), | ||
| }; | ||
|
|
||
| const mockSentryVitePlugin: Plugin = { | ||
| name: 'sentry-vite-debug-id-upload-plugin', | ||
| writeBundle: vi.fn(), | ||
| }; | ||
|
|
||
| const mockEnableSourceMapsPlugin: Plugin = { | ||
| name: 'sentry-tanstackstart-react-source-maps', | ||
| apply: 'build', | ||
| enforce: 'post', | ||
| config: vi.fn(), | ||
| }; | ||
|
|
||
| vi.mock('../../src/vite/sourceMaps', () => ({ | ||
| makeAddSentryVitePlugin: vi.fn(() => [mockSourceMapsConfigPlugin, mockSentryVitePlugin]), | ||
| makeEnableSourceMapsVitePlugin: vi.fn(() => [mockEnableSourceMapsPlugin]), | ||
| })); | ||
|
|
||
| describe('sentryTanstackStart()', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| process.env.NODE_ENV = 'production'; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| process.env.NODE_ENV = 'production'; | ||
| }); | ||
|
|
||
| it('returns plugins in production mode', () => { | ||
| const plugins = sentryTanstackStart({ org: 'test-org' }); | ||
|
|
||
| expect(plugins).toEqual([mockSourceMapsConfigPlugin, mockSentryVitePlugin, mockEnableSourceMapsPlugin]); | ||
| }); | ||
|
|
||
| it('returns no plugins in development mode', () => { | ||
| process.env.NODE_ENV = 'development'; | ||
|
|
||
| const plugins = sentryTanstackStart({ org: 'test-org' }); | ||
|
|
||
| expect(plugins).toEqual([]); | ||
| }); | ||
|
|
||
| it('returns Sentry Vite plugins but not enable source maps plugin when sourcemaps.disable is true', () => { | ||
| const plugins = sentryTanstackStart({ | ||
| sourcemaps: { disable: true }, | ||
| }); | ||
|
|
||
| expect(plugins).toEqual([mockSourceMapsConfigPlugin, mockSentryVitePlugin]); | ||
| }); | ||
|
|
||
| it('returns Sentry Vite plugins but not enable source maps plugin when sourcemaps.disable is "disable-upload"', () => { | ||
| const plugins = sentryTanstackStart({ | ||
| sourcemaps: { disable: 'disable-upload' }, | ||
| }); | ||
|
|
||
| expect(plugins).toEqual([mockSourceMapsConfigPlugin, mockSentryVitePlugin]); | ||
| }); | ||
|
|
||
| it('returns Sentry Vite plugins and enable source maps plugin when sourcemaps.disable is false', () => { | ||
| const plugins = sentryTanstackStart({ | ||
| sourcemaps: { disable: false }, | ||
| }); | ||
|
|
||
| expect(plugins).toEqual([mockSourceMapsConfigPlugin, mockSentryVitePlugin, mockEnableSourceMapsPlugin]); | ||
| }); | ||
|
|
||
| it('returns Sentry Vite Plugins and enable source maps plugin by default when sourcemaps is not specified', () => { | ||
| const plugins = sentryTanstackStart({}); | ||
|
|
||
| expect(plugins).toEqual([mockSourceMapsConfigPlugin, mockSentryVitePlugin, mockEnableSourceMapsPlugin]); | ||
| }); | ||
| }); |
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.
l: Nice changelog entry! Just for completeness I suggest we mention auth token here as well. Alternatively, we can just mention it in text (smthg like "Make sure to set the
SENTRY_AUTH_TOKENenv var") since the env var is picked up automatically.