11import type { Nuxt } from '@nuxt/schema' ;
22import { type SentryRollupPluginOptions , sentryRollupPlugin } from '@sentry/rollup-plugin' ;
3+ import { consoleSandbox } from '@sentry/utils' ;
34import { type SentryVitePluginOptions , sentryVitePlugin } from '@sentry/vite-plugin' ;
45import type { NitroConfig } from 'nitropack' ;
6+ import type { OutputOptions } from 'rollup' ;
57import type { SentryNuxtModuleOptions } from '../common/types' ;
68
79/**
8- * Setup source maps for Sentry inside the Nuxt module during build time (in Vite for Nuxt and Rollup for Nitro).
10+ * Whether the user enabled (true, 'hidden', 'inline') or disabled (false) sourcemaps
11+ */
12+ export type UserSourcemapSetting = 'enabled' | 'disabled' | 'unset' | undefined ;
13+
14+ /**
15+ * Setup source maps for Sentry inside the Nuxt module during build time (in Vite for Nuxt and Rollup for N itr/**
16+ *
917 */
1018export function setupSourceMaps ( moduleOptions : SentryNuxtModuleOptions , nuxt : Nuxt ) : void {
1119 const sourceMapsUploadOptions = moduleOptions . sourceMapsUploadOptions || { } ;
1220 const sourceMapsEnabled = sourceMapsUploadOptions . enabled ?? true ;
1321
14- nuxt . hook ( 'vite:extendConfig ' , async ( viteInlineConfig , _env ) => {
15- if ( sourceMapsEnabled && viteInlineConfig . mode !== 'development' ) {
16- // Add Sentry plugin
17- viteInlineConfig . plugins = viteInlineConfig . plugins || [ ] ;
18- viteInlineConfig . plugins . push ( sentryVitePlugin ( getPluginOptions ( moduleOptions ) ) ) ;
22+ nuxt . hook ( 'modules:done ' , ( ) => {
23+ if ( sourceMapsEnabled && ! nuxt . options . dev ) {
24+ changeNuxtSourcemapSettings ( nuxt , moduleOptions ) ;
25+ }
26+ } ) ;
1927
20- // Enable source maps
21- viteInlineConfig . build = viteInlineConfig . build || { } ;
22- viteInlineConfig . build . sourcemap = true ;
28+ nuxt . hook ( 'vite:extendConfig' , async ( viteConfig , _env ) => {
29+ if ( sourceMapsEnabled && viteConfig . mode !== 'development' ) {
30+ const previousUserSourcemapSetting = changeViteSourcemapSettings ( viteConfig , moduleOptions ) ;
2331
24- logDebugInfo ( moduleOptions , viteInlineConfig . build ?. sourcemap ) ;
32+ // Add Sentry plugin
33+ viteConfig . plugins = viteConfig . plugins || [ ] ;
34+ viteConfig . plugins . push (
35+ sentryVitePlugin ( getPluginOptions ( moduleOptions , previousUserSourcemapSetting === 'unset' ) ) ,
36+ ) ;
2537 }
2638 } ) ;
2739
@@ -38,15 +50,12 @@ export function setupSourceMaps(moduleOptions: SentryNuxtModuleOptions, nuxt: Nu
3850 nitroConfig . rollupConfig . plugins = [ nitroConfig . rollupConfig . plugins ] ;
3951 }
4052
41- // Add Sentry plugin
42- nitroConfig . rollupConfig . plugins . push ( sentryRollupPlugin ( getPluginOptions ( moduleOptions ) ) ) ;
43-
44- // Enable source maps
45- nitroConfig . rollupConfig . output = nitroConfig ?. rollupConfig ?. output || { } ;
46- nitroConfig . rollupConfig . output . sourcemap = true ;
47- nitroConfig . rollupConfig . output . sourcemapExcludeSources = false ; // Adding "sourcesContent" to the source map (Nitro sets this eto `true`)
53+ const previousUserSourcemapSetting = changeRollupSourcemapSettings ( nitroConfig , moduleOptions ) ;
4854
49- logDebugInfo ( moduleOptions , nitroConfig . rollupConfig . output ?. sourcemap ) ;
55+ // Add Sentry plugin
56+ nitroConfig . rollupConfig . plugins . push (
57+ sentryRollupPlugin ( getPluginOptions ( moduleOptions , previousUserSourcemapSetting === 'unset' ) ) ,
58+ ) ;
5059 }
5160 } ) ;
5261}
@@ -65,9 +74,19 @@ function normalizePath(path: string): string {
6574 */
6675export function getPluginOptions (
6776 moduleOptions : SentryNuxtModuleOptions ,
77+ deleteFilesAfterUpload : boolean ,
6878) : SentryVitePluginOptions | SentryRollupPluginOptions {
6979 const sourceMapsUploadOptions = moduleOptions . sourceMapsUploadOptions || { } ;
7080
81+ if ( typeof sourceMapsUploadOptions . sourcemaps ?. filesToDeleteAfterUpload === 'undefined' && deleteFilesAfterUpload ) {
82+ consoleSandbox ( ( ) => {
83+ // eslint-disable-next-line no-console
84+ console . log (
85+ '[Sentry] Setting `sentry.sourceMapsUploadOptions.sourcemaps.filesToDeleteAfterUpload: [".*/**/*.map"]` to delete generated source maps after they were uploaded to Sentry.' ,
86+ ) ;
87+ } ) ;
88+ }
89+
7190 return {
7291 org : sourceMapsUploadOptions . org ?? process . env . SENTRY_ORG ,
7392 project : sourceMapsUploadOptions . project ?? process . env . SENTRY_PROJECT ,
@@ -87,25 +106,189 @@ export function getPluginOptions(
87106 // 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/**/*'],
88107 assets : sourceMapsUploadOptions . sourcemaps ?. assets ?? undefined ,
89108 ignore : sourceMapsUploadOptions . sourcemaps ?. ignore ?? undefined ,
90- filesToDeleteAfterUpload : sourceMapsUploadOptions . sourcemaps ?. filesToDeleteAfterUpload ?? undefined ,
109+ filesToDeleteAfterUpload : sourceMapsUploadOptions . sourcemaps ?. filesToDeleteAfterUpload
110+ ? sourceMapsUploadOptions . sourcemaps ?. filesToDeleteAfterUpload
111+ : deleteFilesAfterUpload
112+ ? [ '.*/**/*.map' ]
113+ : undefined ,
91114 rewriteSources : ( source : string ) => normalizePath ( source ) ,
92115 ...moduleOptions ?. unstable_sentryBundlerPluginOptions ?. sourcemaps ,
93116 } ,
94117 } ;
95118}
96119
97- function logDebugInfo ( moduleOptions : SentryNuxtModuleOptions , sourceMapsPreviouslyEnabled : boolean ) : void {
98- if ( moduleOptions . debug && ! sourceMapsPreviouslyEnabled ) {
120+ /* There are 3 ways to set up sourcemaps (https://github.com/getsentry/sentry-javascript/issues/13993)
121+ 1. User explicitly disabled sourcemaps
122+ - keep this setting (emit a warning that errors won't be unminified in Sentry)
123+ - We will not upload anything
124+ 2. users enabled source map generation (true, hidden, inline).
125+ - keep this setting this and
126+ - don't do anything (i.e. deletion) besides uploading.
127+ 3. users did not set source maps generation
128+ - we enable 'hidden' source maps generation
129+ - configure `filesToDeleteAfterUpload` to delete all .map files (we emit a log about this)
130+
131+ Nuxt has 3 places to set sourcemaps: vite options, rollup options, nuxt itself
132+ Ideally, all 3 are enabled to get all sourcemaps.
133+ */
134+
135+ /** only exported for testing */
136+ export function changeNuxtSourcemapSettings (
137+ nuxt : Nuxt ,
138+ sentryModuleOptions : SentryNuxtModuleOptions ,
139+ ) : { client : UserSourcemapSetting ; server : UserSourcemapSetting } {
140+ nuxt . options = nuxt . options || { } ;
141+ nuxt . options . sourcemap = nuxt . options . sourcemap ?? { server : undefined , client : undefined } ;
142+
143+ let previousUserSourceMapSetting : { client : UserSourcemapSetting ; server : UserSourcemapSetting } = {
144+ client : undefined ,
145+ server : undefined ,
146+ } ;
147+
148+ const nuxtSourcemap = nuxt . options . sourcemap ;
149+
150+ if ( typeof nuxtSourcemap === 'string' || typeof nuxtSourcemap === 'boolean' || typeof nuxtSourcemap === 'undefined' ) {
151+ switch ( nuxtSourcemap ) {
152+ case false :
153+ warnExplicitlyDisabledSourcemap ( 'sourcemap' ) ;
154+ previousUserSourceMapSetting = { client : 'disabled' , server : 'disabled' } ;
155+ break ;
156+
157+ case 'hidden' :
158+ case true :
159+ logKeepSourcemapSetting ( sentryModuleOptions , 'sourcemap' , ( nuxtSourcemap as true ) . toString ( ) ) ;
160+ previousUserSourceMapSetting = { client : 'enabled' , server : 'enabled' } ;
161+ break ;
162+ case undefined :
163+ nuxt . options . sourcemap = { server : 'hidden' , client : 'hidden' } ;
164+ logSentryEnablesSourcemap ( 'sourcemap.client' , 'hidden' ) ;
165+ logSentryEnablesSourcemap ( 'sourcemap.server' , 'hidden' ) ;
166+ previousUserSourceMapSetting = { client : 'unset' , server : 'unset' } ;
167+ break ;
168+ }
169+ } else {
170+ if ( nuxtSourcemap . client === false ) {
171+ warnExplicitlyDisabledSourcemap ( 'sourcemap.client' ) ;
172+ previousUserSourceMapSetting . client = 'disabled' ;
173+ } else if ( [ 'hidden' , true ] . includes ( nuxtSourcemap . client ) ) {
174+ logKeepSourcemapSetting ( sentryModuleOptions , 'sourcemap.client' , nuxtSourcemap . client . toString ( ) ) ;
175+ previousUserSourceMapSetting . client = 'enabled' ;
176+ } else {
177+ nuxt . options . sourcemap . client = 'hidden' ;
178+ logSentryEnablesSourcemap ( 'sourcemap.client' , 'hidden' ) ;
179+ previousUserSourceMapSetting . client = 'unset' ;
180+ }
181+
182+ if ( nuxtSourcemap . server === false ) {
183+ warnExplicitlyDisabledSourcemap ( 'sourcemap.server' ) ;
184+ previousUserSourceMapSetting . server = 'disabled' ;
185+ } else if ( [ 'hidden' , true ] . includes ( nuxtSourcemap . server ) ) {
186+ logKeepSourcemapSetting ( sentryModuleOptions , 'sourcemap.server' , nuxtSourcemap . server . toString ( ) ) ;
187+ previousUserSourceMapSetting . server = 'enabled' ;
188+ } else {
189+ nuxt . options . sourcemap . server = 'hidden' ;
190+ logSentryEnablesSourcemap ( 'sourcemap.server' , 'hidden' ) ;
191+ previousUserSourceMapSetting . server = 'unset' ;
192+ }
193+ }
194+
195+ return previousUserSourceMapSetting ;
196+ }
197+
198+ /** only exported for testing */
199+ export function changeViteSourcemapSettings (
200+ viteConfig : { build ?: { sourcemap ?: boolean | 'inline' | 'hidden' } } ,
201+ sentryModuleOptions : SentryNuxtModuleOptions ,
202+ ) : UserSourcemapSetting {
203+ viteConfig . build = viteConfig . build || { } ;
204+ const viteSourcemap = viteConfig . build . sourcemap ;
205+
206+ let previousUserSourceMapSetting : UserSourcemapSetting ;
207+
208+ if ( viteSourcemap === false ) {
209+ warnExplicitlyDisabledSourcemap ( 'vite.build.sourcemap' ) ;
210+ previousUserSourceMapSetting = 'disabled' ;
211+ } else if ( viteSourcemap && [ 'hidden' , 'inline' , true ] . includes ( viteSourcemap ) ) {
212+ logKeepSourcemapSetting ( sentryModuleOptions , 'vite.build.sourcemap' , viteSourcemap . toString ( ) ) ;
213+ previousUserSourceMapSetting = 'enabled' ;
214+ } else {
215+ viteConfig . build . sourcemap = 'hidden' ;
216+ logSentryEnablesSourcemap ( 'vite.build.sourcemap' , 'hidden' ) ;
217+ previousUserSourceMapSetting = 'unset' ;
218+ }
219+
220+ return previousUserSourceMapSetting ;
221+ }
222+
223+ /** only exported for testing */
224+ export function changeRollupSourcemapSettings (
225+ nitroConfig : {
226+ rollupConfig ?: {
227+ output ?: {
228+ sourcemap ?: OutputOptions [ 'sourcemap' ] ;
229+ sourcemapExcludeSources ?: OutputOptions [ 'sourcemapExcludeSources' ] ;
230+ } ;
231+ } ;
232+ } ,
233+ sentryModuleOptions : SentryNuxtModuleOptions ,
234+ ) : UserSourcemapSetting {
235+ nitroConfig . rollupConfig = nitroConfig . rollupConfig || { } ;
236+ nitroConfig . rollupConfig . output = nitroConfig . rollupConfig . output || { sourcemap : undefined } ;
237+
238+ let previousUserSourceMapSetting : UserSourcemapSetting ;
239+
240+ const nitroSourcemap = nitroConfig . rollupConfig . output . sourcemap ;
241+
242+ if ( nitroSourcemap === false ) {
243+ warnExplicitlyDisabledSourcemap ( 'nitro.rollupConfig.output.sourcemap' ) ;
244+ previousUserSourceMapSetting = 'disabled' ;
245+ } else if ( nitroSourcemap && [ 'hidden' , 'inline' , true ] . includes ( nitroSourcemap ) ) {
246+ logKeepSourcemapSetting ( sentryModuleOptions , 'nitro.rollupConfig.output.sourcemap' , nitroSourcemap . toString ( ) ) ;
247+ previousUserSourceMapSetting = 'enabled' ;
248+ } else {
249+ nitroConfig . rollupConfig . output . sourcemap = 'hidden' ;
250+ logSentryEnablesSourcemap ( 'nitro.rollupConfig.output.sourcemap' , 'hidden' ) ;
251+ previousUserSourceMapSetting = 'unset' ;
252+ }
253+
254+ nitroConfig . rollupConfig . output . sourcemapExcludeSources = false ;
255+ consoleSandbox ( ( ) => {
99256 // eslint-disable-next-line no-console
100- console . log ( '[Sentry]: Enabled source maps generation in the Vite build options.' ) ;
257+ console . log (
258+ '[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.' ,
259+ ) ;
260+ } ) ;
101261
102- const sourceMapsUploadOptions = moduleOptions . sourceMapsUploadOptions || { } ;
262+ return previousUserSourceMapSetting ;
263+ }
103264
104- if ( ! sourceMapsUploadOptions . sourcemaps ?. filesToDeleteAfterUpload ) {
265+ function logKeepSourcemapSetting (
266+ sentryNuxtModuleOptions : SentryNuxtModuleOptions ,
267+ settingKey : string ,
268+ settingValue : string ,
269+ ) : void {
270+ if ( sentryNuxtModuleOptions . debug ) {
271+ consoleSandbox ( ( ) => {
105272 // eslint-disable-next-line no-console
106- console . warn (
107- ' [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' ,
273+ console . log (
274+ ` [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.` ,
108275 ) ;
109- }
276+ } ) ;
110277 }
111278}
279+
280+ function warnExplicitlyDisabledSourcemap ( settingKey : string ) : void {
281+ consoleSandbox ( ( ) => {
282+ // eslint-disable-next-line no-console
283+ console . warn (
284+ `[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 } \`.` ,
285+ ) ;
286+ } ) ;
287+ }
288+
289+ function logSentryEnablesSourcemap ( settingKey : string , settingValue : string ) : void {
290+ consoleSandbox ( ( ) => {
291+ // eslint-disable-next-line no-console
292+ console . log ( `[Sentry] Enabled sourcemap generation in the build options with \`${ settingKey } : ${ settingValue } \`.` ) ;
293+ } ) ;
294+ }
0 commit comments