From fe665abb74d8b1afd73eeada09d961d61e94de8c Mon Sep 17 00:00:00 2001 From: s1gr1d Date: Fri, 18 Oct 2024 11:48:29 +0200 Subject: [PATCH 1/2] feat(nuxt): Respect user-provided sourcemap generation settings --- packages/nuxt/src/vite/sourceMaps.ts | 237 ++++++++++++++++++--- packages/nuxt/test/vite/sourceMaps.test.ts | 159 +++++++++++++- 2 files changed, 364 insertions(+), 32 deletions(-) diff --git a/packages/nuxt/src/vite/sourceMaps.ts b/packages/nuxt/src/vite/sourceMaps.ts index 9abbfe8eaf08..5721ef4369a2 100644 --- a/packages/nuxt/src/vite/sourceMaps.ts +++ b/packages/nuxt/src/vite/sourceMaps.ts @@ -1,27 +1,39 @@ import type { Nuxt } from '@nuxt/schema'; import { type SentryRollupPluginOptions, sentryRollupPlugin } from '@sentry/rollup-plugin'; +import { consoleSandbox } from '@sentry/utils'; import { type SentryVitePluginOptions, sentryVitePlugin } from '@sentry/vite-plugin'; import type { NitroConfig } from 'nitropack'; +import type { OutputOptions } from 'rollup'; import type { SentryNuxtModuleOptions } from '../common/types'; /** - * Setup source maps for Sentry inside the Nuxt module during build time (in Vite for Nuxt and Rollup for Nitro). + * Whether the user enabled (true, 'hidden', 'inline') or disabled (false) sourcemaps + */ +export type UserSourcemapSetting = 'enabled' | 'disabled' | 'unset' | undefined; + +/** + * Setup source maps for Sentry inside the Nuxt module during build time (in Vite for Nuxt and Rollup for N itr/** + * */ export function setupSourceMaps(moduleOptions: SentryNuxtModuleOptions, nuxt: Nuxt): void { const sourceMapsUploadOptions = moduleOptions.sourceMapsUploadOptions || {}; const sourceMapsEnabled = sourceMapsUploadOptions.enabled ?? true; - nuxt.hook('vite:extendConfig', async (viteInlineConfig, _env) => { - if (sourceMapsEnabled && viteInlineConfig.mode !== 'development') { - // Add Sentry plugin - viteInlineConfig.plugins = viteInlineConfig.plugins || []; - viteInlineConfig.plugins.push(sentryVitePlugin(getPluginOptions(moduleOptions))); + nuxt.hook('modules:done', () => { + if (sourceMapsEnabled && !nuxt.options.dev) { + changeNuxtSourcemapSettings(nuxt, moduleOptions); + } + }); - // Enable source maps - viteInlineConfig.build = viteInlineConfig.build || {}; - viteInlineConfig.build.sourcemap = true; + nuxt.hook('vite:extendConfig', async (viteConfig, _env) => { + if (sourceMapsEnabled && viteConfig.mode !== 'development') { + const previousUserSourcemapSetting = changeViteSourcemapSettings(viteConfig, moduleOptions); - logDebugInfo(moduleOptions, viteInlineConfig.build?.sourcemap); + // Add Sentry plugin + viteConfig.plugins = viteConfig.plugins || []; + viteConfig.plugins.push( + sentryVitePlugin(getPluginOptions(moduleOptions, previousUserSourcemapSetting === 'unset')), + ); } }); @@ -38,15 +50,12 @@ export function setupSourceMaps(moduleOptions: SentryNuxtModuleOptions, nuxt: Nu nitroConfig.rollupConfig.plugins = [nitroConfig.rollupConfig.plugins]; } - // Add Sentry plugin - nitroConfig.rollupConfig.plugins.push(sentryRollupPlugin(getPluginOptions(moduleOptions))); - - // Enable source maps - nitroConfig.rollupConfig.output = nitroConfig?.rollupConfig?.output || {}; - nitroConfig.rollupConfig.output.sourcemap = true; - nitroConfig.rollupConfig.output.sourcemapExcludeSources = false; // Adding "sourcesContent" to the source map (Nitro sets this eto `true`) + const previousUserSourcemapSetting = changeRollupSourcemapSettings(nitroConfig, moduleOptions); - logDebugInfo(moduleOptions, nitroConfig.rollupConfig.output?.sourcemap); + // Add Sentry plugin + nitroConfig.rollupConfig.plugins.push( + sentryRollupPlugin(getPluginOptions(moduleOptions, previousUserSourcemapSetting === 'unset')), + ); } }); } @@ -65,9 +74,19 @@ function normalizePath(path: string): string { */ export function getPluginOptions( moduleOptions: SentryNuxtModuleOptions, + deleteFilesAfterUpload: boolean, ): SentryVitePluginOptions | SentryRollupPluginOptions { const sourceMapsUploadOptions = moduleOptions.sourceMapsUploadOptions || {}; + if (typeof sourceMapsUploadOptions.sourcemaps?.filesToDeleteAfterUpload === 'undefined' && deleteFilesAfterUpload) { + consoleSandbox(() => { + // eslint-disable-next-line no-console + console.log( + '[Sentry] Setting `sentry.sourceMapsUploadOptions.sourcemaps.filesToDeleteAfterUpload: [".*/**/*.map"]` to delete generated source maps after they were uploaded to Sentry.', + ); + }); + } + return { org: sourceMapsUploadOptions.org ?? process.env.SENTRY_ORG, project: sourceMapsUploadOptions.project ?? process.env.SENTRY_PROJECT, @@ -87,25 +106,189 @@ export function getPluginOptions( // If we could know where the server/client assets are located, we could do something like this (based on the Nitro preset): isNitro ? ['./.output/server/**/*'] : ['./.output/public/**/*'], assets: sourceMapsUploadOptions.sourcemaps?.assets ?? undefined, ignore: sourceMapsUploadOptions.sourcemaps?.ignore ?? undefined, - filesToDeleteAfterUpload: sourceMapsUploadOptions.sourcemaps?.filesToDeleteAfterUpload ?? undefined, + filesToDeleteAfterUpload: sourceMapsUploadOptions.sourcemaps?.filesToDeleteAfterUpload + ? sourceMapsUploadOptions.sourcemaps?.filesToDeleteAfterUpload + : deleteFilesAfterUpload + ? ['.*/**/*.map'] + : undefined, rewriteSources: (source: string) => normalizePath(source), ...moduleOptions?.unstable_sentryBundlerPluginOptions?.sourcemaps, }, }; } -function logDebugInfo(moduleOptions: SentryNuxtModuleOptions, sourceMapsPreviouslyEnabled: boolean): void { - if (moduleOptions.debug && !sourceMapsPreviouslyEnabled) { +/* There are 3 ways to set up sourcemaps (https://github.com/getsentry/sentry-javascript/issues/13993) + 1. User explicitly disabled sourcemaps + - keep this setting (emit a warning that errors won't be unminified in Sentry) + - We will not upload anything + 2. users enabled source map generation (true, hidden, inline). + - keep this setting this and + - don't do anything (i.e. deletion) besides uploading. + 3. users did not set source maps generation + - we enable 'hidden' source maps generation + - configure `filesToDeleteAfterUpload` to delete all .map files (we emit a log about this) + + Nuxt has 3 places to set sourcemaps: vite options, rollup options, nuxt itself + Ideally, all 3 are enabled to get all sourcemaps. + */ + +/** only exported for testing */ +export function changeNuxtSourcemapSettings( + nuxt: Nuxt, + sentryModuleOptions: SentryNuxtModuleOptions, +): { client: UserSourcemapSetting; server: UserSourcemapSetting } { + nuxt.options = nuxt.options || {}; + nuxt.options.sourcemap = nuxt.options.sourcemap ?? { server: undefined, client: undefined }; + + let previousUserSourceMapSetting: { client: UserSourcemapSetting; server: UserSourcemapSetting } = { + client: undefined, + server: undefined, + }; + + const nuxtSourcemap = nuxt.options.sourcemap; + + if (typeof nuxtSourcemap === 'string' || typeof nuxtSourcemap === 'boolean' || typeof nuxtSourcemap === 'undefined') { + switch (nuxtSourcemap) { + case false: + warnExplicitlyDisabledSourcemap('sourcemap'); + previousUserSourceMapSetting = { client: 'disabled', server: 'disabled' }; + break; + + case 'hidden': + case true: + logKeepSourcemapSetting(sentryModuleOptions, 'sourcemap', (nuxtSourcemap as true).toString()); + previousUserSourceMapSetting = { client: 'enabled', server: 'enabled' }; + break; + case undefined: + nuxt.options.sourcemap = { server: 'hidden', client: 'hidden' }; + logSentryEnablesSourcemap('sourcemap.client', 'hidden'); + logSentryEnablesSourcemap('sourcemap.server', 'hidden'); + previousUserSourceMapSetting = { client: 'unset', server: 'unset' }; + break; + } + } else { + if (nuxtSourcemap.client === false) { + warnExplicitlyDisabledSourcemap('sourcemap.client'); + previousUserSourceMapSetting.client = 'disabled'; + } else if (['hidden', true].includes(nuxtSourcemap.client)) { + logKeepSourcemapSetting(sentryModuleOptions, 'sourcemap.client', nuxtSourcemap.client.toString()); + previousUserSourceMapSetting.client = 'enabled'; + } else { + nuxt.options.sourcemap.client = 'hidden'; + logSentryEnablesSourcemap('sourcemap.client', 'hidden'); + previousUserSourceMapSetting.client = 'unset'; + } + + if (nuxtSourcemap.server === false) { + warnExplicitlyDisabledSourcemap('sourcemap.server'); + previousUserSourceMapSetting.server = 'disabled'; + } else if (['hidden', true].includes(nuxtSourcemap.server)) { + logKeepSourcemapSetting(sentryModuleOptions, 'sourcemap.server', nuxtSourcemap.server.toString()); + previousUserSourceMapSetting.server = 'enabled'; + } else { + nuxt.options.sourcemap.server = 'hidden'; + logSentryEnablesSourcemap('sourcemap.server', 'hidden'); + previousUserSourceMapSetting.server = 'unset'; + } + } + + return previousUserSourceMapSetting; +} + +/** only exported for testing */ +export function changeViteSourcemapSettings( + viteConfig: { build?: { sourcemap?: boolean | 'inline' | 'hidden' } }, + sentryModuleOptions: SentryNuxtModuleOptions, +): UserSourcemapSetting { + viteConfig.build = viteConfig.build || {}; + const viteSourcemap = viteConfig.build.sourcemap; + + let previousUserSourceMapSetting: UserSourcemapSetting; + + if (viteSourcemap === false) { + warnExplicitlyDisabledSourcemap('vite.build.sourcemap'); + previousUserSourceMapSetting = 'disabled'; + } else if (viteSourcemap && ['hidden', 'inline', true].includes(viteSourcemap)) { + logKeepSourcemapSetting(sentryModuleOptions, 'vite.build.sourcemap', viteSourcemap.toString()); + previousUserSourceMapSetting = 'enabled'; + } else { + viteConfig.build.sourcemap = 'hidden'; + logSentryEnablesSourcemap('vite.build.sourcemap', 'hidden'); + previousUserSourceMapSetting = 'unset'; + } + + return previousUserSourceMapSetting; +} + +/** only exported for testing */ +export function changeRollupSourcemapSettings( + nitroConfig: { + rollupConfig?: { + output?: { + sourcemap?: OutputOptions['sourcemap']; + sourcemapExcludeSources?: OutputOptions['sourcemapExcludeSources']; + }; + }; + }, + sentryModuleOptions: SentryNuxtModuleOptions, +): UserSourcemapSetting { + nitroConfig.rollupConfig = nitroConfig.rollupConfig || {}; + nitroConfig.rollupConfig.output = nitroConfig.rollupConfig.output || { sourcemap: undefined }; + + let previousUserSourceMapSetting: UserSourcemapSetting; + + const nitroSourcemap = nitroConfig.rollupConfig.output.sourcemap; + + if (nitroSourcemap === false) { + warnExplicitlyDisabledSourcemap('nitro.rollupConfig.output.sourcemap'); + previousUserSourceMapSetting = 'disabled'; + } else if (nitroSourcemap && ['hidden', 'inline', true].includes(nitroSourcemap)) { + logKeepSourcemapSetting(sentryModuleOptions, 'nitro.rollupConfig.output.sourcemap', nitroSourcemap.toString()); + previousUserSourceMapSetting = 'enabled'; + } else { + nitroConfig.rollupConfig.output.sourcemap = 'hidden'; + logSentryEnablesSourcemap('nitro.rollupConfig.output.sourcemap', 'hidden'); + previousUserSourceMapSetting = 'unset'; + } + + nitroConfig.rollupConfig.output.sourcemapExcludeSources = false; + consoleSandbox(() => { // eslint-disable-next-line no-console - console.log('[Sentry]: Enabled source maps generation in the Vite build options.'); + console.log( + '[Sentry] Disabled sourcemap setting in the Nuxt config: `nitro.rollupConfig.output.sourcemapExcludeSources`. Source maps will include the actual code to be able to un-minify code snippets in Sentry.', + ); + }); - const sourceMapsUploadOptions = moduleOptions.sourceMapsUploadOptions || {}; + return previousUserSourceMapSetting; +} - if (!sourceMapsUploadOptions.sourcemaps?.filesToDeleteAfterUpload) { +function logKeepSourcemapSetting( + sentryNuxtModuleOptions: SentryNuxtModuleOptions, + settingKey: string, + settingValue: string, +): void { + if (sentryNuxtModuleOptions.debug) { + consoleSandbox(() => { // eslint-disable-next-line no-console - console.warn( - '[Sentry] 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', + console.log( + `[Sentry] We discovered \`${settingKey}\` is set to \`${settingValue}\`. Sentry will keep this sourcemap setting. This will un-minify the code snippet on the Sentry Issue page.`, ); - } + }); } } + +function warnExplicitlyDisabledSourcemap(settingKey: string): void { + consoleSandbox(() => { + // eslint-disable-next-line no-console + console.warn( + `[Sentry] Parts of sourcemap 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 sourcemaps, code snippets on the Sentry Issues page will remain minified. To show unminified code, enable sourcemaps in \`${settingKey}\`.`, + ); + }); +} + +function logSentryEnablesSourcemap(settingKey: string, settingValue: string): void { + consoleSandbox(() => { + // eslint-disable-next-line no-console + console.log(`[Sentry] Enabled sourcemap generation in the build options with \`${settingKey}: ${settingValue}\`.`); + }); +} diff --git a/packages/nuxt/test/vite/sourceMaps.test.ts b/packages/nuxt/test/vite/sourceMaps.test.ts index 34c520b96d83..e912658ac657 100644 --- a/packages/nuxt/test/vite/sourceMaps.test.ts +++ b/packages/nuxt/test/vite/sourceMaps.test.ts @@ -1,6 +1,13 @@ +import type { Nuxt } from '@nuxt/schema'; import { beforeEach, describe, expect, it, vi } from 'vitest'; import type { SentryNuxtModuleOptions } from '../../src/common/types'; -import { getPluginOptions } from '../../src/vite/sourceMaps'; +import type { UserSourcemapSetting } from '../../src/vite/sourceMaps'; +import { + changeNuxtSourcemapSettings, + changeRollupSourcemapSettings, + changeViteSourcemapSettings, + getPluginOptions, +} from '../../src/vite/sourceMaps'; describe('getPluginOptions', () => { beforeEach(() => { @@ -17,7 +24,7 @@ describe('getPluginOptions', () => { process.env = { ...defaultEnv }; - const options = getPluginOptions({} as SentryNuxtModuleOptions); + const options = getPluginOptions({} as SentryNuxtModuleOptions, false); expect(options).toEqual( expect.objectContaining({ @@ -39,7 +46,7 @@ describe('getPluginOptions', () => { }); it('returns default options when no moduleOptions are provided', () => { - const options = getPluginOptions({} as SentryNuxtModuleOptions); + const options = getPluginOptions({} as SentryNuxtModuleOptions, false); expect(options.org).toBeUndefined(); expect(options.project).toBeUndefined(); @@ -75,7 +82,7 @@ describe('getPluginOptions', () => { }, debug: true, }; - const options = getPluginOptions(customOptions); + const options = getPluginOptions(customOptions, true); expect(options).toEqual( expect.objectContaining({ org: 'custom-org', @@ -119,7 +126,7 @@ describe('getPluginOptions', () => { }, }, }; - const options = getPluginOptions(customOptions); + const options = getPluginOptions(customOptions, false); expect(options).toEqual( expect.objectContaining({ debug: true, @@ -137,3 +144,145 @@ describe('getPluginOptions', () => { ); }); }); + +describe('change sourcemap settings', () => { + describe('changeViteSourcemapSettings', () => { + let viteConfig: { build?: { sourcemap?: boolean | 'inline' | 'hidden' } }; + let sentryModuleOptions: SentryNuxtModuleOptions; + + beforeEach(() => { + viteConfig = {}; + sentryModuleOptions = {}; + }); + + it('should handle viteConfig.build.sourcemap settings', () => { + const cases: { + sourcemap?: boolean | 'hidden' | 'inline'; + expectedSourcemap: boolean | string; + expectedReturn: UserSourcemapSetting; + }[] = [ + { sourcemap: false, expectedSourcemap: false, expectedReturn: 'disabled' }, + { sourcemap: 'hidden', expectedSourcemap: 'hidden', expectedReturn: 'enabled' }, + { sourcemap: 'inline', expectedSourcemap: 'inline', expectedReturn: 'enabled' }, + { sourcemap: true, expectedSourcemap: true, expectedReturn: 'enabled' }, + { sourcemap: undefined, expectedSourcemap: 'hidden', expectedReturn: 'unset' }, + ]; + + cases.forEach(({ sourcemap, expectedSourcemap, expectedReturn }) => { + viteConfig.build = { sourcemap }; + const previousUserSourcemapSetting = changeViteSourcemapSettings(viteConfig, sentryModuleOptions); + expect(viteConfig.build.sourcemap).toBe(expectedSourcemap); + expect(previousUserSourcemapSetting).toBe(expectedReturn); + }); + }); + }); + + describe('changeRollupSourcemapSettings', () => { + let nitroConfig: { + rollupConfig?: { output?: { sourcemap?: boolean | 'hidden' | 'inline'; sourcemapExcludeSources?: boolean } }; + }; + let sentryModuleOptions: SentryNuxtModuleOptions; + + beforeEach(() => { + nitroConfig = {}; + sentryModuleOptions = {}; + }); + + it('should handle nitroConfig.rollupConfig.output.sourcemap settings', () => { + const cases: { + output?: { sourcemap?: boolean | 'hidden' | 'inline' }; + expectedSourcemap: boolean | string; + expectedReturn: UserSourcemapSetting; + }[] = [ + { output: { sourcemap: false }, expectedSourcemap: false, expectedReturn: 'disabled' }, + { output: { sourcemap: 'hidden' }, expectedSourcemap: 'hidden', expectedReturn: 'enabled' }, + { output: { sourcemap: 'inline' }, expectedSourcemap: 'inline', expectedReturn: 'enabled' }, + { output: { sourcemap: true }, expectedSourcemap: true, expectedReturn: 'enabled' }, + { output: { sourcemap: undefined }, expectedSourcemap: 'hidden', expectedReturn: 'unset' }, + { output: undefined, expectedSourcemap: 'hidden', expectedReturn: 'unset' }, + ]; + + cases.forEach(({ output, expectedSourcemap, expectedReturn }) => { + nitroConfig.rollupConfig = { output }; + const previousUserSourceMapSetting = changeRollupSourcemapSettings(nitroConfig, sentryModuleOptions); + expect(nitroConfig.rollupConfig?.output?.sourcemap).toBe(expectedSourcemap); + expect(previousUserSourceMapSetting).toBe(expectedReturn); + expect(nitroConfig.rollupConfig?.output?.sourcemapExcludeSources).toBe(false); + }); + }); + }); + + describe('changeNuxtSourcemapSettings', () => { + let nuxt: { options: { sourcemap: { client: boolean | 'hidden'; server: boolean | 'hidden' } } }; + let sentryModuleOptions: SentryNuxtModuleOptions; + + beforeEach(() => { + // @ts-expect-error - Nuxt types don't accept `undefined` but we want to test this case + nuxt = { options: { sourcemap: { client: undefined } } }; + sentryModuleOptions = {}; + }); + + it('should handle nuxt.options.sourcemap.client settings', () => { + const cases = [ + // { clientSourcemap: false, expectedSourcemap: false, expectedReturn: 'disabled' }, + // { clientSourcemap: 'hidden', expectedSourcemap: 'hidden', expectedReturn: 'enabled' }, + { clientSourcemap: true, expectedSourcemap: true, expectedReturn: 'enabled' }, + { clientSourcemap: undefined, expectedSourcemap: 'hidden', expectedReturn: 'unset' }, + ]; + + cases.forEach(({ clientSourcemap, expectedSourcemap, expectedReturn }) => { + // @ts-expect-error - Nuxt types don't accept `undefined` but we want to test this case + nuxt.options.sourcemap.client = clientSourcemap; + const previousUserSourcemapSetting = changeNuxtSourcemapSettings(nuxt as Nuxt, sentryModuleOptions); + expect(nuxt.options.sourcemap.client).toBe(expectedSourcemap); + expect(previousUserSourcemapSetting.client).toBe(expectedReturn); + }); + }); + + it('should handle nuxt.options.sourcemap.server settings', () => { + const cases = [ + { serverSourcemap: false, expectedSourcemap: false, expectedReturn: 'disabled' }, + { serverSourcemap: 'hidden', expectedSourcemap: 'hidden', expectedReturn: 'enabled' }, + { serverSourcemap: true, expectedSourcemap: true, expectedReturn: 'enabled' }, + { serverSourcemap: undefined, expectedSourcemap: 'hidden', expectedReturn: 'unset' }, + ]; + + cases.forEach(({ serverSourcemap, expectedSourcemap, expectedReturn }) => { + // @ts-expect-error server available + nuxt.options.sourcemap.server = serverSourcemap; + const previousUserSourcemapSetting = changeNuxtSourcemapSettings(nuxt as Nuxt, sentryModuleOptions); + expect(nuxt.options.sourcemap.server).toBe(expectedSourcemap); + expect(previousUserSourcemapSetting.server).toBe(expectedReturn); + }); + }); + + describe('should handle nuxt.options.sourcemap as a boolean', () => { + it('keeps setting of nuxt.options.sourcemap if it is set', () => { + const cases = [ + { sourcemap: false, expectedSourcemap: false, expectedReturn: 'disabled' }, + { sourcemap: true, expectedSourcemap: true, expectedReturn: 'enabled' }, + { sourcemap: 'hidden', expectedSourcemap: 'hidden', expectedReturn: 'enabled' }, + ]; + + cases.forEach(({ sourcemap, expectedSourcemap, expectedReturn }) => { + // @ts-expect-error string type is possible in Nuxt (but type says differently) + nuxt.options.sourcemap = sourcemap; + const previousUserSourcemapSetting = changeNuxtSourcemapSettings(nuxt as Nuxt, sentryModuleOptions); + expect(nuxt.options.sourcemap).toBe(expectedSourcemap); + expect(previousUserSourcemapSetting.client).toBe(expectedReturn); + expect(previousUserSourcemapSetting.server).toBe(expectedReturn); + }); + }); + + it("sets client and server to 'hidden' if nuxt.options.sourcemap not set", () => { + // @ts-expect-error - Nuxt types don't accept `undefined` but we want to test this case + nuxt.options.sourcemap = undefined; + const previousUserSourcemapSetting = changeNuxtSourcemapSettings(nuxt as Nuxt, sentryModuleOptions); + expect(nuxt.options.sourcemap.client).toBe('hidden'); + expect(nuxt.options.sourcemap.server).toBe('hidden'); + expect(previousUserSourcemapSetting.client).toBe('unset'); + expect(previousUserSourcemapSetting.server).toBe('unset'); + }); + }); + }); +}); From 79028734770367be692c8277de7419275c43f98c Mon Sep 17 00:00:00 2001 From: s1gr1d Date: Fri, 18 Oct 2024 13:27:03 +0200 Subject: [PATCH 2/2] review comments --- packages/nuxt/src/vite/sourceMaps.ts | 120 ++++++++++----------- packages/nuxt/test/vite/sourceMaps.test.ts | 24 ++--- 2 files changed, 71 insertions(+), 73 deletions(-) diff --git a/packages/nuxt/src/vite/sourceMaps.ts b/packages/nuxt/src/vite/sourceMaps.ts index 5721ef4369a2..8fd6f0a381fd 100644 --- a/packages/nuxt/src/vite/sourceMaps.ts +++ b/packages/nuxt/src/vite/sourceMaps.ts @@ -7,13 +7,12 @@ import type { OutputOptions } from 'rollup'; import type { SentryNuxtModuleOptions } from '../common/types'; /** - * Whether the user enabled (true, 'hidden', 'inline') or disabled (false) sourcemaps + * Whether the user enabled (true, 'hidden', 'inline') or disabled (false) source maps */ -export type UserSourcemapSetting = 'enabled' | 'disabled' | 'unset' | undefined; +export type UserSourceMapSetting = 'enabled' | 'disabled' | 'unset' | undefined; /** - * Setup source maps for Sentry inside the Nuxt module during build time (in Vite for Nuxt and Rollup for N itr/** - * + * Setup source maps for Sentry inside the Nuxt module during build time (in Vite for Nuxt and Rollup for Nitro). */ export function setupSourceMaps(moduleOptions: SentryNuxtModuleOptions, nuxt: Nuxt): void { const sourceMapsUploadOptions = moduleOptions.sourceMapsUploadOptions || {}; @@ -21,18 +20,18 @@ export function setupSourceMaps(moduleOptions: SentryNuxtModuleOptions, nuxt: Nu nuxt.hook('modules:done', () => { if (sourceMapsEnabled && !nuxt.options.dev) { - changeNuxtSourcemapSettings(nuxt, moduleOptions); + changeNuxtSourceMapSettings(nuxt, moduleOptions); } }); nuxt.hook('vite:extendConfig', async (viteConfig, _env) => { if (sourceMapsEnabled && viteConfig.mode !== 'development') { - const previousUserSourcemapSetting = changeViteSourcemapSettings(viteConfig, moduleOptions); + const previousUserSourceMapSetting = changeViteSourceMapSettings(viteConfig, moduleOptions); // Add Sentry plugin viteConfig.plugins = viteConfig.plugins || []; viteConfig.plugins.push( - sentryVitePlugin(getPluginOptions(moduleOptions, previousUserSourcemapSetting === 'unset')), + sentryVitePlugin(getPluginOptions(moduleOptions, previousUserSourceMapSetting === 'unset')), ); } }); @@ -50,11 +49,11 @@ export function setupSourceMaps(moduleOptions: SentryNuxtModuleOptions, nuxt: Nu nitroConfig.rollupConfig.plugins = [nitroConfig.rollupConfig.plugins]; } - const previousUserSourcemapSetting = changeRollupSourcemapSettings(nitroConfig, moduleOptions); + const previousUserSourceMapSetting = changeRollupSourceMapSettings(nitroConfig, moduleOptions); // Add Sentry plugin nitroConfig.rollupConfig.plugins.push( - sentryRollupPlugin(getPluginOptions(moduleOptions, previousUserSourcemapSetting === 'unset')), + sentryRollupPlugin(getPluginOptions(moduleOptions, previousUserSourceMapSetting === 'unset')), ); } }); @@ -102,7 +101,7 @@ export function getPluginOptions( sourcemaps: { // The server/client files are in different places depending on the nitro preset (e.g. '.output/server' or '.netlify/functions-internal/server') - // We cannot determine automatically how the build folder looks like (depends on the preset), so we have to accept that sourcemaps are uploaded multiple times (with the vitePlugin for Nuxt and the rollupPlugin for Nitro). + // We cannot determine automatically how the build folder looks like (depends on the preset), so we have to accept that source maps are uploaded multiple times (with the vitePlugin for Nuxt and the rollupPlugin for Nitro). // If we could know where the server/client assets are located, we could do something like this (based on the Nitro preset): isNitro ? ['./.output/server/**/*'] : ['./.output/public/**/*'], assets: sourceMapsUploadOptions.sourcemaps?.assets ?? undefined, ignore: sourceMapsUploadOptions.sourcemaps?.ignore ?? undefined, @@ -117,77 +116,76 @@ export function getPluginOptions( }; } -/* There are 3 ways to set up sourcemaps (https://github.com/getsentry/sentry-javascript/issues/13993) - 1. User explicitly disabled sourcemaps +/* There are 3 ways to set up source maps (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 will not upload anything 2. users enabled source map generation (true, hidden, inline). - - keep this setting this and - - don't do anything (i.e. deletion) besides uploading. + - keep this setting (don't do anything - like deletion - besides uploading) 3. users did not set source maps generation - we enable 'hidden' source maps generation - configure `filesToDeleteAfterUpload` to delete all .map files (we emit a log about this) - Nuxt has 3 places to set sourcemaps: vite options, rollup options, nuxt itself - Ideally, all 3 are enabled to get all sourcemaps. + Nuxt has 3 places to set source maps: vite options, rollup options, nuxt itself + Ideally, all 3 are enabled to get all source maps. */ /** only exported for testing */ -export function changeNuxtSourcemapSettings( +export function changeNuxtSourceMapSettings( nuxt: Nuxt, sentryModuleOptions: SentryNuxtModuleOptions, -): { client: UserSourcemapSetting; server: UserSourcemapSetting } { +): { client: UserSourceMapSetting; server: UserSourceMapSetting } { nuxt.options = nuxt.options || {}; nuxt.options.sourcemap = nuxt.options.sourcemap ?? { server: undefined, client: undefined }; - let previousUserSourceMapSetting: { client: UserSourcemapSetting; server: UserSourcemapSetting } = { + let previousUserSourceMapSetting: { client: UserSourceMapSetting; server: UserSourceMapSetting } = { client: undefined, server: undefined, }; - const nuxtSourcemap = nuxt.options.sourcemap; + const nuxtSourceMap = nuxt.options.sourcemap; - if (typeof nuxtSourcemap === 'string' || typeof nuxtSourcemap === 'boolean' || typeof nuxtSourcemap === 'undefined') { - switch (nuxtSourcemap) { + if (typeof nuxtSourceMap === 'string' || typeof nuxtSourceMap === 'boolean' || typeof nuxtSourceMap === 'undefined') { + switch (nuxtSourceMap) { case false: - warnExplicitlyDisabledSourcemap('sourcemap'); + warnExplicitlyDisabledSourceMap('sourcemap'); previousUserSourceMapSetting = { client: 'disabled', server: 'disabled' }; break; case 'hidden': case true: - logKeepSourcemapSetting(sentryModuleOptions, 'sourcemap', (nuxtSourcemap as true).toString()); + logKeepSourceMapSetting(sentryModuleOptions, 'sourcemap', (nuxtSourceMap as true).toString()); previousUserSourceMapSetting = { client: 'enabled', server: 'enabled' }; break; case undefined: nuxt.options.sourcemap = { server: 'hidden', client: 'hidden' }; - logSentryEnablesSourcemap('sourcemap.client', 'hidden'); - logSentryEnablesSourcemap('sourcemap.server', 'hidden'); + logSentryEnablesSourceMap('sourcemap.client', 'hidden'); + logSentryEnablesSourceMap('sourcemap.server', 'hidden'); previousUserSourceMapSetting = { client: 'unset', server: 'unset' }; break; } } else { - if (nuxtSourcemap.client === false) { - warnExplicitlyDisabledSourcemap('sourcemap.client'); + if (nuxtSourceMap.client === false) { + warnExplicitlyDisabledSourceMap('sourcemap.client'); previousUserSourceMapSetting.client = 'disabled'; - } else if (['hidden', true].includes(nuxtSourcemap.client)) { - logKeepSourcemapSetting(sentryModuleOptions, 'sourcemap.client', nuxtSourcemap.client.toString()); + } else if (['hidden', true].includes(nuxtSourceMap.client)) { + logKeepSourceMapSetting(sentryModuleOptions, 'sourcemap.client', nuxtSourceMap.client.toString()); previousUserSourceMapSetting.client = 'enabled'; } else { nuxt.options.sourcemap.client = 'hidden'; - logSentryEnablesSourcemap('sourcemap.client', 'hidden'); + logSentryEnablesSourceMap('sourcemap.client', 'hidden'); previousUserSourceMapSetting.client = 'unset'; } - if (nuxtSourcemap.server === false) { - warnExplicitlyDisabledSourcemap('sourcemap.server'); + if (nuxtSourceMap.server === false) { + warnExplicitlyDisabledSourceMap('sourcemap.server'); previousUserSourceMapSetting.server = 'disabled'; - } else if (['hidden', true].includes(nuxtSourcemap.server)) { - logKeepSourcemapSetting(sentryModuleOptions, 'sourcemap.server', nuxtSourcemap.server.toString()); + } else if (['hidden', true].includes(nuxtSourceMap.server)) { + logKeepSourceMapSetting(sentryModuleOptions, 'sourcemap.server', nuxtSourceMap.server.toString()); previousUserSourceMapSetting.server = 'enabled'; } else { nuxt.options.sourcemap.server = 'hidden'; - logSentryEnablesSourcemap('sourcemap.server', 'hidden'); + logSentryEnablesSourceMap('sourcemap.server', 'hidden'); previousUserSourceMapSetting.server = 'unset'; } } @@ -196,24 +194,24 @@ export function changeNuxtSourcemapSettings( } /** only exported for testing */ -export function changeViteSourcemapSettings( +export function changeViteSourceMapSettings( viteConfig: { build?: { sourcemap?: boolean | 'inline' | 'hidden' } }, sentryModuleOptions: SentryNuxtModuleOptions, -): UserSourcemapSetting { +): UserSourceMapSetting { viteConfig.build = viteConfig.build || {}; - const viteSourcemap = viteConfig.build.sourcemap; + const viteSourceMap = viteConfig.build.sourcemap; - let previousUserSourceMapSetting: UserSourcemapSetting; + let previousUserSourceMapSetting: UserSourceMapSetting; - if (viteSourcemap === false) { - warnExplicitlyDisabledSourcemap('vite.build.sourcemap'); + if (viteSourceMap === false) { + warnExplicitlyDisabledSourceMap('vite.build.sourcemap'); previousUserSourceMapSetting = 'disabled'; - } else if (viteSourcemap && ['hidden', 'inline', true].includes(viteSourcemap)) { - logKeepSourcemapSetting(sentryModuleOptions, 'vite.build.sourcemap', viteSourcemap.toString()); + } else if (viteSourceMap && ['hidden', 'inline', true].includes(viteSourceMap)) { + logKeepSourceMapSetting(sentryModuleOptions, 'vite.build.sourcemap', viteSourceMap.toString()); previousUserSourceMapSetting = 'enabled'; } else { viteConfig.build.sourcemap = 'hidden'; - logSentryEnablesSourcemap('vite.build.sourcemap', 'hidden'); + logSentryEnablesSourceMap('vite.build.sourcemap', 'hidden'); previousUserSourceMapSetting = 'unset'; } @@ -221,7 +219,7 @@ export function changeViteSourcemapSettings( } /** only exported for testing */ -export function changeRollupSourcemapSettings( +export function changeRollupSourceMapSettings( nitroConfig: { rollupConfig?: { output?: { @@ -231,23 +229,23 @@ export function changeRollupSourcemapSettings( }; }, sentryModuleOptions: SentryNuxtModuleOptions, -): UserSourcemapSetting { +): UserSourceMapSetting { nitroConfig.rollupConfig = nitroConfig.rollupConfig || {}; nitroConfig.rollupConfig.output = nitroConfig.rollupConfig.output || { sourcemap: undefined }; - let previousUserSourceMapSetting: UserSourcemapSetting; + let previousUserSourceMapSetting: UserSourceMapSetting; - const nitroSourcemap = nitroConfig.rollupConfig.output.sourcemap; + const nitroSourceMap = nitroConfig.rollupConfig.output.sourcemap; - if (nitroSourcemap === false) { - warnExplicitlyDisabledSourcemap('nitro.rollupConfig.output.sourcemap'); + if (nitroSourceMap === false) { + warnExplicitlyDisabledSourceMap('nitro.rollupConfig.output.sourcemap'); previousUserSourceMapSetting = 'disabled'; - } else if (nitroSourcemap && ['hidden', 'inline', true].includes(nitroSourcemap)) { - logKeepSourcemapSetting(sentryModuleOptions, 'nitro.rollupConfig.output.sourcemap', nitroSourcemap.toString()); + } else if (nitroSourceMap && ['hidden', 'inline', true].includes(nitroSourceMap)) { + logKeepSourceMapSetting(sentryModuleOptions, 'nitro.rollupConfig.output.sourcemap', nitroSourceMap.toString()); previousUserSourceMapSetting = 'enabled'; } else { nitroConfig.rollupConfig.output.sourcemap = 'hidden'; - logSentryEnablesSourcemap('nitro.rollupConfig.output.sourcemap', 'hidden'); + logSentryEnablesSourceMap('nitro.rollupConfig.output.sourcemap', 'hidden'); previousUserSourceMapSetting = 'unset'; } @@ -255,14 +253,14 @@ export function changeRollupSourcemapSettings( consoleSandbox(() => { // eslint-disable-next-line no-console console.log( - '[Sentry] Disabled sourcemap setting in the Nuxt config: `nitro.rollupConfig.output.sourcemapExcludeSources`. Source maps will include the actual code to be able to un-minify code snippets in Sentry.', + '[Sentry] Disabled source map setting in the Nuxt config: `nitro.rollupConfig.output.sourcemapExcludeSources`. Source maps will include the actual code to be able to un-minify code snippets in Sentry.', ); }); return previousUserSourceMapSetting; } -function logKeepSourcemapSetting( +function logKeepSourceMapSetting( sentryNuxtModuleOptions: SentryNuxtModuleOptions, settingKey: string, settingValue: string, @@ -271,24 +269,24 @@ function logKeepSourcemapSetting( consoleSandbox(() => { // eslint-disable-next-line no-console console.log( - `[Sentry] We discovered \`${settingKey}\` is set to \`${settingValue}\`. Sentry will keep this sourcemap setting. This will un-minify the code snippet on the Sentry Issue page.`, + `[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.`, ); }); } } -function warnExplicitlyDisabledSourcemap(settingKey: string): void { +function warnExplicitlyDisabledSourceMap(settingKey: string): void { consoleSandbox(() => { // eslint-disable-next-line no-console console.warn( - `[Sentry] Parts of sourcemap 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 sourcemaps, code snippets on the Sentry Issues page will remain minified. To show unminified code, enable sourcemaps in \`${settingKey}\`.`, + `[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}\`.`, ); }); } -function logSentryEnablesSourcemap(settingKey: string, settingValue: string): void { +function logSentryEnablesSourceMap(settingKey: string, settingValue: string): void { consoleSandbox(() => { // eslint-disable-next-line no-console - console.log(`[Sentry] Enabled sourcemap generation in the build options with \`${settingKey}: ${settingValue}\`.`); + console.log(`[Sentry] Enabled source map generation in the build options with \`${settingKey}: ${settingValue}\`.`); }); } diff --git a/packages/nuxt/test/vite/sourceMaps.test.ts b/packages/nuxt/test/vite/sourceMaps.test.ts index e912658ac657..0c90429fa8d5 100644 --- a/packages/nuxt/test/vite/sourceMaps.test.ts +++ b/packages/nuxt/test/vite/sourceMaps.test.ts @@ -1,11 +1,11 @@ import type { Nuxt } from '@nuxt/schema'; import { beforeEach, describe, expect, it, vi } from 'vitest'; import type { SentryNuxtModuleOptions } from '../../src/common/types'; -import type { UserSourcemapSetting } from '../../src/vite/sourceMaps'; +import type { UserSourceMapSetting } from '../../src/vite/sourceMaps'; import { - changeNuxtSourcemapSettings, - changeRollupSourcemapSettings, - changeViteSourcemapSettings, + changeNuxtSourceMapSettings, + changeRollupSourceMapSettings, + changeViteSourceMapSettings, getPluginOptions, } from '../../src/vite/sourceMaps'; @@ -159,7 +159,7 @@ describe('change sourcemap settings', () => { const cases: { sourcemap?: boolean | 'hidden' | 'inline'; expectedSourcemap: boolean | string; - expectedReturn: UserSourcemapSetting; + expectedReturn: UserSourceMapSetting; }[] = [ { sourcemap: false, expectedSourcemap: false, expectedReturn: 'disabled' }, { sourcemap: 'hidden', expectedSourcemap: 'hidden', expectedReturn: 'enabled' }, @@ -170,7 +170,7 @@ describe('change sourcemap settings', () => { cases.forEach(({ sourcemap, expectedSourcemap, expectedReturn }) => { viteConfig.build = { sourcemap }; - const previousUserSourcemapSetting = changeViteSourcemapSettings(viteConfig, sentryModuleOptions); + const previousUserSourcemapSetting = changeViteSourceMapSettings(viteConfig, sentryModuleOptions); expect(viteConfig.build.sourcemap).toBe(expectedSourcemap); expect(previousUserSourcemapSetting).toBe(expectedReturn); }); @@ -192,7 +192,7 @@ describe('change sourcemap settings', () => { const cases: { output?: { sourcemap?: boolean | 'hidden' | 'inline' }; expectedSourcemap: boolean | string; - expectedReturn: UserSourcemapSetting; + expectedReturn: UserSourceMapSetting; }[] = [ { output: { sourcemap: false }, expectedSourcemap: false, expectedReturn: 'disabled' }, { output: { sourcemap: 'hidden' }, expectedSourcemap: 'hidden', expectedReturn: 'enabled' }, @@ -204,7 +204,7 @@ describe('change sourcemap settings', () => { cases.forEach(({ output, expectedSourcemap, expectedReturn }) => { nitroConfig.rollupConfig = { output }; - const previousUserSourceMapSetting = changeRollupSourcemapSettings(nitroConfig, sentryModuleOptions); + const previousUserSourceMapSetting = changeRollupSourceMapSettings(nitroConfig, sentryModuleOptions); expect(nitroConfig.rollupConfig?.output?.sourcemap).toBe(expectedSourcemap); expect(previousUserSourceMapSetting).toBe(expectedReturn); expect(nitroConfig.rollupConfig?.output?.sourcemapExcludeSources).toBe(false); @@ -233,7 +233,7 @@ describe('change sourcemap settings', () => { cases.forEach(({ clientSourcemap, expectedSourcemap, expectedReturn }) => { // @ts-expect-error - Nuxt types don't accept `undefined` but we want to test this case nuxt.options.sourcemap.client = clientSourcemap; - const previousUserSourcemapSetting = changeNuxtSourcemapSettings(nuxt as Nuxt, sentryModuleOptions); + const previousUserSourcemapSetting = changeNuxtSourceMapSettings(nuxt as Nuxt, sentryModuleOptions); expect(nuxt.options.sourcemap.client).toBe(expectedSourcemap); expect(previousUserSourcemapSetting.client).toBe(expectedReturn); }); @@ -250,7 +250,7 @@ describe('change sourcemap settings', () => { cases.forEach(({ serverSourcemap, expectedSourcemap, expectedReturn }) => { // @ts-expect-error server available nuxt.options.sourcemap.server = serverSourcemap; - const previousUserSourcemapSetting = changeNuxtSourcemapSettings(nuxt as Nuxt, sentryModuleOptions); + const previousUserSourcemapSetting = changeNuxtSourceMapSettings(nuxt as Nuxt, sentryModuleOptions); expect(nuxt.options.sourcemap.server).toBe(expectedSourcemap); expect(previousUserSourcemapSetting.server).toBe(expectedReturn); }); @@ -267,7 +267,7 @@ describe('change sourcemap settings', () => { cases.forEach(({ sourcemap, expectedSourcemap, expectedReturn }) => { // @ts-expect-error string type is possible in Nuxt (but type says differently) nuxt.options.sourcemap = sourcemap; - const previousUserSourcemapSetting = changeNuxtSourcemapSettings(nuxt as Nuxt, sentryModuleOptions); + const previousUserSourcemapSetting = changeNuxtSourceMapSettings(nuxt as Nuxt, sentryModuleOptions); expect(nuxt.options.sourcemap).toBe(expectedSourcemap); expect(previousUserSourcemapSetting.client).toBe(expectedReturn); expect(previousUserSourcemapSetting.server).toBe(expectedReturn); @@ -277,7 +277,7 @@ describe('change sourcemap settings', () => { it("sets client and server to 'hidden' if nuxt.options.sourcemap not set", () => { // @ts-expect-error - Nuxt types don't accept `undefined` but we want to test this case nuxt.options.sourcemap = undefined; - const previousUserSourcemapSetting = changeNuxtSourcemapSettings(nuxt as Nuxt, sentryModuleOptions); + const previousUserSourcemapSetting = changeNuxtSourceMapSettings(nuxt as Nuxt, sentryModuleOptions); expect(nuxt.options.sourcemap.client).toBe('hidden'); expect(nuxt.options.sourcemap.server).toBe('hidden'); expect(previousUserSourcemapSetting.client).toBe('unset');