-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
feat(nuxt): Upload sourcemaps generated by Nitro #13382
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d16753d
feat(nuxt): Add sourcemaps from Nitro
s1gr1d 8765634
review comments
s1gr1d 09f5f13
Merge branch 'develop' into sig/nuxt-nitro-sourcemaps
s1gr1d 28efd99
one line comment
s1gr1d e4fe965
Merge branch 'develop' into sig/nuxt-nitro-sourcemaps
s1gr1d 6a94108
Merge branch 'refs/heads/develop' into sig/nuxt-nitro-sourcemaps
s1gr1d a1f3890
add type checks
s1gr1d 3cf9f85
Merge branch 'refs/heads/develop' into sig/nuxt-nitro-sourcemaps
s1gr1d 2b5444a
review comments
s1gr1d cc7713f
Merge branch 'develop' into sig/nuxt-nitro-sourcemaps
s1gr1d File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,107 @@ | ||
import type { Nuxt } from '@nuxt/schema'; | ||
import { sentryVitePlugin } from '@sentry/vite-plugin'; | ||
import { type SentryRollupPluginOptions, sentryRollupPlugin } from '@sentry/rollup-plugin'; | ||
import { type SentryVitePluginOptions, sentryVitePlugin } from '@sentry/vite-plugin'; | ||
import type { NitroConfig } from 'nitropack'; | ||
import type { NullValue } from 'rollup'; | ||
import type { SentryNuxtModuleOptions } from '../common/types'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
function isNullValue(value: any): value is NullValue { | ||
return value === null || value === undefined; | ||
} | ||
|
||
/** | ||
* Setup source maps for Sentry inside the Nuxt module during build time. | ||
* 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 { | ||
nuxt.hook('vite:extendConfig', async (viteInlineConfig, _env) => { | ||
const sourceMapsUploadOptions = moduleOptions.sourceMapsUploadOptions || {}; | ||
|
||
if ((sourceMapsUploadOptions.enabled ?? true) && viteInlineConfig.mode !== 'development') { | ||
const sentryPlugin = sentryVitePlugin({ | ||
org: sourceMapsUploadOptions.org ?? process.env.SENTRY_ORG, | ||
project: sourceMapsUploadOptions.project ?? process.env.SENTRY_PROJECT, | ||
authToken: sourceMapsUploadOptions.authToken ?? process.env.SENTRY_AUTH_TOKEN, | ||
telemetry: sourceMapsUploadOptions.telemetry ?? true, | ||
sourcemaps: { | ||
assets: sourceMapsUploadOptions.sourcemaps?.assets ?? undefined, | ||
ignore: sourceMapsUploadOptions.sourcemaps?.ignore ?? undefined, | ||
filesToDeleteAfterUpload: sourceMapsUploadOptions.sourcemaps?.filesToDeleteAfterUpload ?? undefined, | ||
}, | ||
_metaOptions: { | ||
telemetry: { | ||
metaFramework: 'nuxt', | ||
}, | ||
}, | ||
debug: moduleOptions.debug ?? false, | ||
}); | ||
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(sentryPlugin); | ||
|
||
const sourceMapsPreviouslyEnabled = viteInlineConfig.build?.sourcemap; | ||
|
||
if (moduleOptions.debug && !sourceMapsPreviouslyEnabled) { | ||
// eslint-disable-next-line no-console | ||
console.log('[Sentry]: Enabled source maps generation in the Vite build options.'); | ||
if (!moduleOptions.sourceMapsUploadOptions?.sourcemaps?.filesToDeleteAfterUpload) { | ||
// eslint-disable-next-line no-console | ||
console.warn( | ||
`[Sentry] We recommend setting the \`sourceMapsUploadOptions.sourcemaps.filesToDeleteAfterUpload\` option to clean up source maps after uploading. | ||
[Sentry] Otherwise, source maps might be deployed to production, depending on your configuration`, | ||
); | ||
} | ||
} | ||
viteInlineConfig.plugins.push(sentryVitePlugin(getPluginOptions(moduleOptions))); | ||
|
||
// Enable source maps | ||
viteInlineConfig.build = viteInlineConfig.build || {}; | ||
viteInlineConfig.build.sourcemap = true; | ||
|
||
logDebugInfo(moduleOptions, viteInlineConfig.build?.sourcemap); | ||
} | ||
}); | ||
|
||
nuxt.hook('nitro:config', (nitroConfig: NitroConfig) => { | ||
if (sourceMapsEnabled && !nitroConfig.dev) { | ||
if (!nitroConfig.rollupConfig) { | ||
nitroConfig.rollupConfig = {}; | ||
} | ||
|
||
if (isNullValue(nitroConfig.rollupConfig.plugins)) { | ||
nitroConfig.rollupConfig.plugins = []; | ||
} else if (!Array.isArray(nitroConfig.rollupConfig.plugins)) { | ||
nitroConfig.rollupConfig.plugins = [nitroConfig.rollupConfig.plugins]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. l: Maybe we can leave a comment saying something along the lines of " |
||
} | ||
|
||
// Add Sentry plugin | ||
nitroConfig.rollupConfig.plugins.push(sentryRollupPlugin(getPluginOptions(moduleOptions, true))); | ||
|
||
// 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`) | ||
|
||
logDebugInfo(moduleOptions, nitroConfig.rollupConfig.output?.sourcemap); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Normalizes the beginning of a path from e.g. ../../../ to ./ | ||
*/ | ||
function normalizePath(path: string): string { | ||
return path.replace(/^(\.\.\/)+/, './'); | ||
} | ||
|
||
function getPluginOptions( | ||
moduleOptions: SentryNuxtModuleOptions, | ||
isNitro = false, | ||
): SentryVitePluginOptions | SentryRollupPluginOptions { | ||
const sourceMapsUploadOptions = moduleOptions.sourceMapsUploadOptions || {}; | ||
|
||
return { | ||
org: sourceMapsUploadOptions.org ?? process.env.SENTRY_ORG, | ||
project: sourceMapsUploadOptions.project ?? process.env.SENTRY_PROJECT, | ||
authToken: sourceMapsUploadOptions.authToken ?? process.env.SENTRY_AUTH_TOKEN, | ||
telemetry: sourceMapsUploadOptions.telemetry ?? true, | ||
sourcemaps: { | ||
assets: | ||
sourceMapsUploadOptions.sourcemaps?.assets ?? isNitro ? ['./.output/server/**/*'] : ['./.output/public/**/*'], | ||
ignore: sourceMapsUploadOptions.sourcemaps?.ignore ?? undefined, | ||
filesToDeleteAfterUpload: sourceMapsUploadOptions.sourcemaps?.filesToDeleteAfterUpload ?? undefined, | ||
rewriteSources: (source: string) => normalizePath(source), | ||
}, | ||
_metaOptions: { | ||
telemetry: { | ||
metaFramework: 'nuxt', | ||
}, | ||
}, | ||
debug: moduleOptions.debug ?? false, | ||
}; | ||
} | ||
|
||
function logDebugInfo(moduleOptions: SentryNuxtModuleOptions, sourceMapsPreviouslyEnabled: boolean): void { | ||
if (moduleOptions.debug && !sourceMapsPreviouslyEnabled) { | ||
// eslint-disable-next-line no-console | ||
console.log('[Sentry]: Enabled source maps generation in the Vite build options.'); | ||
|
||
const sourceMapsUploadOptions = moduleOptions.sourceMapsUploadOptions || {}; | ||
|
||
if (!sourceMapsUploadOptions.sourcemaps?.filesToDeleteAfterUpload) { | ||
// 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', | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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: Personal preference, I would just do
!== null
etc instead of the helper because it is easier to parse mentally but I'll leave it up to you.