|
1 | 1 | import * as child_process from 'child_process';
|
2 | 2 | import * as fs from 'fs';
|
3 | 3 | import * as path from 'path';
|
4 |
| -import { escapeStringForRegex, uuid4 } from '@sentry/core'; |
| 4 | +import { consoleSandbox, escapeStringForRegex, uuid4 } from '@sentry/core'; |
5 | 5 | import { getSentryRelease } from '@sentry/node';
|
6 | 6 | import type { SentryVitePluginOptions } from '@sentry/vite-plugin';
|
7 | 7 | import { sentryVitePlugin } from '@sentry/vite-plugin';
|
8 |
| -import type { Plugin } from 'vite'; |
| 8 | +import type { Plugin, UserConfig } from 'vite'; |
9 | 9 |
|
10 | 10 | import MagicString from 'magic-string';
|
11 | 11 | import { WRAPPED_MODULE_SUFFIX } from './autoInstrument';
|
@@ -135,26 +135,18 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug
|
135 | 135 | enforce: 'post', // this needs to be set to post, otherwise we don't pick up the output from the SvelteKit adapter
|
136 | 136 |
|
137 | 137 | // Modify the config to generate source maps
|
138 |
| - config: config => { |
139 |
| - const sourceMapsPreviouslyNotEnabled = !config.build?.sourcemap; |
140 |
| - if (debug && sourceMapsPreviouslyNotEnabled) { |
141 |
| - // eslint-disable-next-line no-console |
142 |
| - console.log('[Source Maps Plugin] Enabling source map generation'); |
143 |
| - if (!mergedOptions.sourcemaps?.filesToDeleteAfterUpload) { |
| 138 | + config: (config: UserConfig) => { |
| 139 | + changeViteSourceMapSettings(config, options); |
| 140 | + |
| 141 | + if (debug && !mergedOptions.sourcemaps?.filesToDeleteAfterUpload) { |
| 142 | + consoleSandbox(() => { |
144 | 143 | // eslint-disable-next-line no-console
|
145 | 144 | console.warn(
|
146 |
| - `[Source Maps Plugin] We recommend setting the \`sourceMapsUploadOptions.sourcemaps.filesToDeleteAfterUpload\` option to clean up source maps after uploading. |
147 |
| -[Source Maps Plugin] Otherwise, source maps might be deployed to production, depending on your configuration`, |
| 145 | + '[Source Maps Plugin] We recommend setting the `sourceMapsUploadOptions.sourcemaps.filesToDeleteAfterUpload` option to clean up source maps after uploading. Otherwise, source maps might be deployed to production, depending on your configuration', |
148 | 146 | );
|
149 |
| - } |
| 147 | + }); |
150 | 148 | }
|
151 |
| - return { |
152 |
| - ...config, |
153 |
| - build: { |
154 |
| - ...config.build, |
155 |
| - sourcemap: true, |
156 |
| - }, |
157 |
| - }; |
| 149 | + return config; |
158 | 150 | },
|
159 | 151 |
|
160 | 152 | resolveId: (id, _importer, _ref) => {
|
@@ -332,6 +324,86 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug
|
332 | 324 | ];
|
333 | 325 | }
|
334 | 326 |
|
| 327 | +/** |
| 328 | + * Whether the user enabled (true, 'hidden', 'inline') or disabled (false) source maps |
| 329 | + */ |
| 330 | +export type UserSourceMapSetting = 'enabled' | 'disabled' | 'unset' | undefined; |
| 331 | + |
| 332 | +/** There are 3 ways to set up source maps (https://github.com/getsentry/sentry-javascript/issues/13993) |
| 333 | + * |
| 334 | + * 1. User explicitly disabled source maps |
| 335 | + * - keep this setting (emit a warning that errors won't be unminified in Sentry) |
| 336 | + * - We won't upload anything |
| 337 | + * |
| 338 | + * 2. Users enabled source map generation (true, 'hidden', 'inline'). |
| 339 | + * - keep this setting (don't do anything - like deletion - besides uploading) |
| 340 | + * |
| 341 | + * 3. Users didn't set source maps generation |
| 342 | + * - we enable 'hidden' source maps generation |
| 343 | + * - configure `filesToDeleteAfterUpload` to delete all .map files (we emit a log about this) |
| 344 | + * |
| 345 | + * --> only exported for testing |
| 346 | + */ |
| 347 | +export function changeViteSourceMapSettings( |
| 348 | + viteConfig: { |
| 349 | + build?: { |
| 350 | + sourcemap?: boolean | 'inline' | 'hidden'; |
| 351 | + }; |
| 352 | + }, |
| 353 | + sentryPluginOptions?: CustomSentryVitePluginOptions, |
| 354 | +): UserSourceMapSetting { |
| 355 | + let previousUserSourceMapSetting: UserSourceMapSetting = undefined; |
| 356 | + |
| 357 | + viteConfig.build = viteConfig.build || {}; |
| 358 | + |
| 359 | + const viteSourceMap = viteConfig.build.sourcemap; |
| 360 | + |
| 361 | + if (viteSourceMap === false) { |
| 362 | + warnExplicitlyDisabledSourceMap('vite.build.sourcemap'); |
| 363 | + previousUserSourceMapSetting = 'disabled'; |
| 364 | + } else if (viteSourceMap && ['hidden', 'inline', true].includes(viteSourceMap)) { |
| 365 | + logKeepSourceMapSetting('vite.build.sourcemap', viteSourceMap.toString(), sentryPluginOptions); |
| 366 | + previousUserSourceMapSetting = 'enabled'; |
| 367 | + } else { |
| 368 | + viteConfig.build.sourcemap = 'hidden'; |
| 369 | + logSentryEnablesSourceMap('vite.build.sourcemap', 'hidden'); |
| 370 | + previousUserSourceMapSetting = 'unset'; |
| 371 | + } |
| 372 | + |
| 373 | + return previousUserSourceMapSetting; |
| 374 | +} |
| 375 | + |
| 376 | +function logKeepSourceMapSetting( |
| 377 | + settingKey: string, |
| 378 | + settingValue: string, |
| 379 | + sentryPluginOptions?: CustomSentryVitePluginOptions, |
| 380 | +): void { |
| 381 | + if (sentryPluginOptions?.debug) { |
| 382 | + consoleSandbox(() => { |
| 383 | + // eslint-disable-next-line no-console |
| 384 | + console.log( |
| 385 | + `[Sentry] We discovered \`${settingKey}\` is set to \`${settingValue}\`. Sentry will keep this source map setting. This will un-minify the code snippet on the Sentry Issue page.`, |
| 386 | + ); |
| 387 | + }); |
| 388 | + } |
| 389 | +} |
| 390 | + |
| 391 | +function warnExplicitlyDisabledSourceMap(settingKey: string): void { |
| 392 | + consoleSandbox(() => { |
| 393 | + // eslint-disable-next-line no-console |
| 394 | + console.warn( |
| 395 | + `[Sentry] Parts of source map generation are currently disabled in your Nuxt configuration (\`${settingKey}: false\`). This setting is either a default setting or was explicitly set in your configuration. Sentry won't override this setting. Without source maps, code snippets on the Sentry Issues page will remain minified. To show unminified code, enable source maps in \`${settingKey}\` (e.g. by setting them to \`hidden\`).`, |
| 396 | + ); |
| 397 | + }); |
| 398 | +} |
| 399 | + |
| 400 | +function logSentryEnablesSourceMap(settingKey: string, settingValue: string): void { |
| 401 | + consoleSandbox(() => { |
| 402 | + // eslint-disable-next-line no-console |
| 403 | + console.log(`[Sentry] Enabled source map generation in the build options with \`${settingKey}: ${settingValue}\`.`); |
| 404 | + }); |
| 405 | +} |
| 406 | + |
335 | 407 | function getFiles(dir: string): string[] {
|
336 | 408 | if (!fs.existsSync(dir)) {
|
337 | 409 | return [];
|
|
0 commit comments