|
1 | 1 | import type { Nuxt } from '@nuxt/schema'; |
| 2 | +import { sentryRollupPlugin } from '@sentry/rollup-plugin'; |
2 | 3 | import { sentryVitePlugin } from '@sentry/vite-plugin'; |
| 4 | +import type { NitroConfig } from 'nitropack'; |
3 | 5 | import type { SentryNuxtModuleOptions } from '../common/types'; |
4 | 6 |
|
5 | 7 | /** |
6 | 8 | * Setup source maps for Sentry inside the Nuxt module during build time. |
7 | 9 | */ |
8 | 10 | export function setupSourceMaps(moduleOptions: SentryNuxtModuleOptions, nuxt: Nuxt): void { |
9 | | - nuxt.hook('vite:extendConfig', async (viteInlineConfig, _env) => { |
10 | | - const sourceMapsUploadOptions = moduleOptions.sourceMapsUploadOptions || {}; |
| 11 | + const sourceMapsUploadOptions = moduleOptions.sourceMapsUploadOptions || {}; |
| 12 | + const sourceMapsEnabled = sourceMapsUploadOptions.enabled ?? true; |
11 | 13 |
|
12 | | - if ((sourceMapsUploadOptions.enabled ?? true) && viteInlineConfig.mode !== 'development') { |
13 | | - const sentryPlugin = sentryVitePlugin({ |
14 | | - org: sourceMapsUploadOptions.org ?? process.env.SENTRY_ORG, |
15 | | - project: sourceMapsUploadOptions.project ?? process.env.SENTRY_PROJECT, |
16 | | - authToken: sourceMapsUploadOptions.authToken ?? process.env.SENTRY_AUTH_TOKEN, |
17 | | - telemetry: sourceMapsUploadOptions.telemetry ?? true, |
18 | | - sourcemaps: { |
19 | | - assets: sourceMapsUploadOptions.sourcemaps?.assets ?? undefined, |
20 | | - ignore: sourceMapsUploadOptions.sourcemaps?.ignore ?? undefined, |
21 | | - filesToDeleteAfterUpload: sourceMapsUploadOptions.sourcemaps?.filesToDeleteAfterUpload ?? undefined, |
22 | | - }, |
23 | | - _metaOptions: { |
24 | | - telemetry: { |
25 | | - metaFramework: 'nuxt', |
26 | | - }, |
27 | | - }, |
28 | | - debug: moduleOptions.debug ?? false, |
29 | | - }); |
| 14 | + nuxt.hook('vite:extendConfig', async (viteInlineConfig, _env) => { |
| 15 | + if (sourceMapsEnabled && viteInlineConfig.mode !== 'development') { |
| 16 | + const sentryPlugin = sentryVitePlugin(getPluginOptions(moduleOptions)); |
30 | 17 |
|
| 18 | + // Add Sentry plugin |
31 | 19 | viteInlineConfig.plugins = viteInlineConfig.plugins || []; |
32 | 20 | viteInlineConfig.plugins.push(sentryPlugin); |
33 | 21 |
|
34 | | - const sourceMapsPreviouslyEnabled = viteInlineConfig.build?.sourcemap; |
| 22 | + // Enable source maps |
| 23 | + viteInlineConfig.build = viteInlineConfig.build || {}; |
| 24 | + viteInlineConfig.build.sourcemap = true; |
35 | 25 |
|
36 | | - if (moduleOptions.debug && !sourceMapsPreviouslyEnabled) { |
37 | | - // eslint-disable-next-line no-console |
38 | | - console.log('[Sentry]: Enabled source maps generation in the Vite build options.'); |
39 | | - if (!moduleOptions.sourceMapsUploadOptions?.sourcemaps?.filesToDeleteAfterUpload) { |
40 | | - // eslint-disable-next-line no-console |
41 | | - console.warn( |
42 | | - `[Sentry] We recommend setting the \`sourceMapsUploadOptions.sourcemaps.filesToDeleteAfterUpload\` option to clean up source maps after uploading. |
43 | | -[Sentry] Otherwise, source maps might be deployed to production, depending on your configuration`, |
44 | | - ); |
| 26 | + logDebugInfo(moduleOptions, viteInlineConfig.build?.sourcemap); |
| 27 | + } |
| 28 | + }); |
| 29 | + |
| 30 | + nuxt.hook('nitro:config', (nitroConfig: NitroConfig) => { |
| 31 | + if (sourceMapsEnabled && !nitroConfig.dev) { |
| 32 | + const sentryPlugin = sentryRollupPlugin(getPluginOptions(moduleOptions)); |
| 33 | + |
| 34 | + if (nitroConfig.rollupConfig) { |
| 35 | + // Add Sentry plugin |
| 36 | + if (!Array.isArray(nitroConfig.rollupConfig.plugins)) { |
| 37 | + nitroConfig.rollupConfig.plugins = nitroConfig.rollupConfig.plugins ? [nitroConfig.rollupConfig.plugins] : []; |
45 | 38 | } |
46 | | - } |
| 39 | + nitroConfig.rollupConfig.plugins.push(sentryPlugin); |
47 | 40 |
|
48 | | - viteInlineConfig.build = viteInlineConfig.build || {}; |
49 | | - viteInlineConfig.build.sourcemap = true; |
| 41 | + // Enable source maps |
| 42 | + nitroConfig.rollupConfig.output = nitroConfig?.rollupConfig?.output || {}; |
| 43 | + nitroConfig.rollupConfig.output.sourcemap = true; |
| 44 | + nitroConfig.rollupConfig.output.sourcemapExcludeSources = false; // Adding "sourcesContent" to the source map (Nitro sets this eto `true`) |
| 45 | + |
| 46 | + logDebugInfo(moduleOptions, nitroConfig.rollupConfig.output?.sourcemap); |
| 47 | + } |
50 | 48 | } |
51 | 49 | }); |
52 | 50 | } |
| 51 | + |
| 52 | +/** |
| 53 | + * Normalizes the beginning of a path from e.g. ../../../ to ./ |
| 54 | + */ |
| 55 | +function normalizePath(path: string): string { |
| 56 | + return path.replace(/^(\.\.\/)+/, './'); |
| 57 | +} |
| 58 | + |
| 59 | +function getPluginOptions(moduleOptions: SentryNuxtModuleOptions): object { |
| 60 | + const sourceMapsUploadOptions = moduleOptions.sourceMapsUploadOptions || {}; |
| 61 | + |
| 62 | + return { |
| 63 | + org: sourceMapsUploadOptions.org ?? process.env.SENTRY_ORG, |
| 64 | + project: sourceMapsUploadOptions.project ?? process.env.SENTRY_PROJECT, |
| 65 | + authToken: sourceMapsUploadOptions.authToken ?? process.env.SENTRY_AUTH_TOKEN, |
| 66 | + telemetry: sourceMapsUploadOptions.telemetry ?? true, |
| 67 | + sourcemaps: { |
| 68 | + assets: sourceMapsUploadOptions.sourcemaps?.assets ?? ['./.output/public/**/*', './.output/server/**/*'], |
| 69 | + ignore: sourceMapsUploadOptions.sourcemaps?.ignore ?? undefined, |
| 70 | + filesToDeleteAfterUpload: sourceMapsUploadOptions.sourcemaps?.filesToDeleteAfterUpload ?? undefined, |
| 71 | + rewriteSources: (source: string) => normalizePath(source), |
| 72 | + }, |
| 73 | + _metaOptions: { |
| 74 | + telemetry: { |
| 75 | + metaFramework: 'nuxt', |
| 76 | + }, |
| 77 | + }, |
| 78 | + debug: moduleOptions.debug ?? false, |
| 79 | + }; |
| 80 | +} |
| 81 | + |
| 82 | +function logDebugInfo(moduleOptions: SentryNuxtModuleOptions, sourceMapsPreviouslyEnabled: boolean): void { |
| 83 | + if (moduleOptions.debug && !sourceMapsPreviouslyEnabled) { |
| 84 | + // eslint-disable-next-line no-console |
| 85 | + console.log('[Sentry]: Enabled source maps generation in the Vite build options.'); |
| 86 | + |
| 87 | + const sourceMapsUploadOptions = moduleOptions.sourceMapsUploadOptions || {}; |
| 88 | + |
| 89 | + if (!sourceMapsUploadOptions.sourcemaps?.filesToDeleteAfterUpload) { |
| 90 | + // eslint-disable-next-line no-console |
| 91 | + console.warn( |
| 92 | + `[Sentry] We recommend setting the \`sourceMapsUploadOptions.sourcemaps.filesToDeleteAfterUpload\` option to clean up source maps after uploading. |
| 93 | +[Sentry] Otherwise, source maps might be deployed to production, depending on your configuration`, |
| 94 | + ); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments