-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(nuxt): Respect user-provided source map generation settings #14020
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -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 | ||||||
|
- keep this setting this and | |
- keep this setting and |
Outdated
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: some vars use sourcemap
camel-cased, others not
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.
Changed all variables/comments to "source maps" and sourceMap
according to https://tc39.es/source-map/
Outdated
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: the logic in here is basically the same for vite rollup and nuxt, maybe this could be moved into a function instead
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.
I thought about that as it would make the code eventually shorter. But I kept the three different functions (and so the duplication) because I think it adds more complexity if those functions are combined into one. The log outputs and the sourcmap option source are different for vite, rollup and nuxt and a function abstraction would make it very difficult to read. I think it's better to have some duplication here for the purpose of readability.
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.
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.
oh noo that is left from biome (in WebStorm), it continuously messed up this file 😢 Especially in JSDoc and function declarations.