|
| 1 | +import type { BuildTimeOptionsBase } from '@sentry/core'; |
| 2 | +import { sentryVitePlugin } from '@sentry/vite-plugin'; |
| 3 | +import type { Plugin, UserConfig } from 'vite'; |
| 4 | + |
| 5 | +type FilesToDeleteAfterUpload = string | string[] | undefined; |
| 6 | + |
| 7 | +/** |
| 8 | + * A Sentry plugin for adding the @sentry/vite-plugin to automatically upload source maps to Sentry. |
| 9 | + */ |
| 10 | +export function makeAddSentryVitePlugin(options: BuildTimeOptionsBase): Plugin[] { |
| 11 | + const { |
| 12 | + authToken, |
| 13 | + bundleSizeOptimizations, |
| 14 | + debug, |
| 15 | + errorHandler, |
| 16 | + headers, |
| 17 | + org, |
| 18 | + project, |
| 19 | + release, |
| 20 | + sentryUrl, |
| 21 | + silent, |
| 22 | + sourcemaps, |
| 23 | + telemetry, |
| 24 | + } = options; |
| 25 | + |
| 26 | + // defer resolving the filesToDeleteAfterUpload until we got access to the Vite config |
| 27 | + let resolveFilesToDeleteAfterUpload: ((value: FilesToDeleteAfterUpload) => void) | undefined; |
| 28 | + const filesToDeleteAfterUploadPromise = new Promise<FilesToDeleteAfterUpload>(resolve => { |
| 29 | + resolveFilesToDeleteAfterUpload = resolve; |
| 30 | + }); |
| 31 | + |
| 32 | + const configPlugin: Plugin = { |
| 33 | + name: 'sentry-tanstackstart-files-to-delete-after-upload-plugin', |
| 34 | + apply: 'build', |
| 35 | + enforce: 'post', |
| 36 | + config(config) { |
| 37 | + const userFilesToDelete = sourcemaps?.filesToDeleteAfterUpload; |
| 38 | + |
| 39 | + // Only auto-delete source maps if the user didn't configure sourcemaps at all |
| 40 | + if (typeof userFilesToDelete === 'undefined' && typeof config.build?.sourcemap === 'undefined') { |
| 41 | + if (debug) { |
| 42 | + // eslint-disable-next-line no-console |
| 43 | + console.log( |
| 44 | + '[Sentry] Automatically setting `sourcemaps.filesToDeleteAfterUpload: ["./**/*.map"]` to delete generated source maps after they were uploaded to Sentry.', |
| 45 | + ); |
| 46 | + } |
| 47 | + resolveFilesToDeleteAfterUpload?.(['./**/*.map']); |
| 48 | + } else { |
| 49 | + resolveFilesToDeleteAfterUpload?.(userFilesToDelete); |
| 50 | + } |
| 51 | + }, |
| 52 | + }; |
| 53 | + |
| 54 | + const sentryPlugins = sentryVitePlugin({ |
| 55 | + authToken: authToken ?? process.env.SENTRY_AUTH_TOKEN, |
| 56 | + bundleSizeOptimizations: bundleSizeOptimizations ?? undefined, |
| 57 | + debug: debug ?? false, |
| 58 | + errorHandler, |
| 59 | + headers, |
| 60 | + org: org ?? process.env.SENTRY_ORG, |
| 61 | + project: project ?? process.env.SENTRY_PROJECT, |
| 62 | + release, |
| 63 | + silent, |
| 64 | + sourcemaps: { |
| 65 | + assets: sourcemaps?.assets, |
| 66 | + disable: sourcemaps?.disable, |
| 67 | + ignore: sourcemaps?.ignore, |
| 68 | + filesToDeleteAfterUpload: filesToDeleteAfterUploadPromise, |
| 69 | + }, |
| 70 | + telemetry: telemetry ?? true, |
| 71 | + url: sentryUrl, |
| 72 | + _metaOptions: { |
| 73 | + telemetry: { |
| 74 | + metaFramework: 'tanstackstart-react', |
| 75 | + }, |
| 76 | + }, |
| 77 | + }); |
| 78 | + |
| 79 | + return [configPlugin, ...sentryPlugins]; |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * A Sentry plugin for TanStack Start React to enable "hidden" source maps if they are unset. |
| 84 | + */ |
| 85 | +export function makeEnableSourceMapsVitePlugin(options: BuildTimeOptionsBase): Plugin[] { |
| 86 | + return [ |
| 87 | + { |
| 88 | + name: 'sentry-tanstackstart-react-source-maps', |
| 89 | + apply: 'build', |
| 90 | + enforce: 'post', |
| 91 | + config(viteConfig) { |
| 92 | + return { |
| 93 | + ...viteConfig, |
| 94 | + build: { |
| 95 | + ...viteConfig.build, |
| 96 | + sourcemap: getUpdatedSourceMapSettings(viteConfig, options), |
| 97 | + }, |
| 98 | + }; |
| 99 | + }, |
| 100 | + }, |
| 101 | + ]; |
| 102 | +} |
| 103 | + |
| 104 | +/** There are 3 ways to set up source map generation (https://github.com/getsentry/sentry-javascript/issues/13993) |
| 105 | + * |
| 106 | + * 1. User explicitly disabled source maps |
| 107 | + * - keep this setting (emit a warning that errors won't be unminified in Sentry) |
| 108 | + * - We won't upload anything |
| 109 | + * |
| 110 | + * 2. Users enabled source map generation (true, 'hidden', 'inline'). |
| 111 | + * - keep this setting (don't do anything - like deletion - besides uploading) |
| 112 | + * |
| 113 | + * 3. Users didn't set source maps generation |
| 114 | + * - we enable 'hidden' source maps generation |
| 115 | + * - configure `filesToDeleteAfterUpload` to delete all .map files (we emit a log about this) |
| 116 | + * |
| 117 | + * --> only exported for testing |
| 118 | + */ |
| 119 | +export function getUpdatedSourceMapSettings( |
| 120 | + viteConfig: UserConfig, |
| 121 | + sentryPluginOptions?: BuildTimeOptionsBase, |
| 122 | +): boolean | 'inline' | 'hidden' { |
| 123 | + viteConfig.build = viteConfig.build || {}; |
| 124 | + |
| 125 | + const viteUserSourceMapSetting = viteConfig.build?.sourcemap; |
| 126 | + const settingKey = 'vite.build.sourcemap'; |
| 127 | + const debug = sentryPluginOptions?.debug; |
| 128 | + |
| 129 | + // Respect user source map setting if it is explicitly set |
| 130 | + if (viteUserSourceMapSetting === false) { |
| 131 | + if (debug) { |
| 132 | + // eslint-disable-next-line no-console |
| 133 | + console.warn( |
| 134 | + `[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.`, |
| 135 | + ); |
| 136 | + } else { |
| 137 | + // eslint-disable-next-line no-console |
| 138 | + console.warn('[Sentry] Source map generation is disabled in your TanStack Start configuration.'); |
| 139 | + } |
| 140 | + |
| 141 | + return viteUserSourceMapSetting; |
| 142 | + } else if (viteUserSourceMapSetting && ['hidden', 'inline', true].includes(viteUserSourceMapSetting)) { |
| 143 | + if (debug) { |
| 144 | + // eslint-disable-next-line no-console |
| 145 | + console.log( |
| 146 | + `[Sentry] We discovered \`${settingKey}\` is set to \`${viteUserSourceMapSetting.toString()}\`. Sentry will keep this source map setting.`, |
| 147 | + ); |
| 148 | + } |
| 149 | + |
| 150 | + return viteUserSourceMapSetting; |
| 151 | + } |
| 152 | + |
| 153 | + // If the user did not specify a source map setting, we enable 'hidden' by default |
| 154 | + if (debug) { |
| 155 | + // eslint-disable-next-line no-console |
| 156 | + console.log( |
| 157 | + `[Sentry] Enabled source map generation in the build options with \`${settingKey}: 'hidden'\`. The source maps will be deleted after they were uploaded to Sentry.`, |
| 158 | + ); |
| 159 | + } |
| 160 | + |
| 161 | + return 'hidden'; |
| 162 | +} |
0 commit comments