Skip to content

Commit 4a581f5

Browse files
committed
feat(nuxt): Add sourcemaps from Nitro
1 parent ee09a02 commit 4a581f5

File tree

3 files changed

+123
-42
lines changed

3 files changed

+123
-42
lines changed

packages/nuxt/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@
4747
"@sentry/core": "8.25.0",
4848
"@sentry/node": "8.25.0",
4949
"@sentry/opentelemetry": "8.25.0",
50+
"@sentry/rollup-plugin": "2.22.1",
5051
"@sentry/types": "8.25.0",
5152
"@sentry/utils": "8.25.0",
52-
"@sentry/vite-plugin": "2.20.1",
53+
"@sentry/vite-plugin": "2.22.1",
5354
"@sentry/vue": "8.25.0"
5455
},
5556
"devDependencies": {

packages/nuxt/src/vite/sourceMaps.ts

Lines changed: 78 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,97 @@
11
import type { Nuxt } from '@nuxt/schema';
2+
import { sentryRollupPlugin } from '@sentry/rollup-plugin';
23
import { sentryVitePlugin } from '@sentry/vite-plugin';
4+
import type { NitroConfig } from 'nitropack';
35
import type { SentryNuxtModuleOptions } from '../common/types';
46

57
/**
68
* Setup source maps for Sentry inside the Nuxt module during build time.
79
*/
810
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;
1113

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));
3017

18+
// Add Sentry plugin
3119
viteInlineConfig.plugins = viteInlineConfig.plugins || [];
3220
viteInlineConfig.plugins.push(sentryPlugin);
3321

34-
const sourceMapsPreviouslyEnabled = viteInlineConfig.build?.sourcemap;
22+
// Enable source maps
23+
viteInlineConfig.build = viteInlineConfig.build || {};
24+
viteInlineConfig.build.sourcemap = true;
3525

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] : [];
4538
}
46-
}
39+
nitroConfig.rollupConfig.plugins.push(sentryPlugin);
4740

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+
}
5048
}
5149
});
5250
}
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+
}

yarn.lock

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8195,6 +8195,11 @@
81958195
resolved "https://registry.yarnpkg.com/@sentry/babel-plugin-component-annotate/-/babel-plugin-component-annotate-2.22.0.tgz#a7e1cc99d1a738d1eb17757341dff4db3a93c2dc"
81968196
integrity sha512-UzH+NNhgnOo6UFku3C4TEz+pO/yDcIA5FKTJvLbJ7lQwAjsqLs3DZWm4cCA08skICb8mULArF6S/dn5/butVCA==
81978197

8198+
8199+
version "2.22.1"
8200+
resolved "https://registry.yarnpkg.com/@sentry/babel-plugin-component-annotate/-/babel-plugin-component-annotate-2.22.1.tgz#2877af31f1d0358202593fe6d14141827a2f1e42"
8201+
integrity sha512-rQEk8EeCIBQKivWNONllQhd/wGbfuK/WXJRM6TkjeikM3wrqJf4AmIBXoA6eg089DwBFzBaxGjjLWJNHKY440w==
8202+
81988203
81998204
version "2.16.0"
82008205
resolved "https://registry.yarnpkg.com/@sentry/bundler-plugin-core/-/bundler-plugin-core-2.16.0.tgz#0c33e7a054fb56e43bd160ac141f71dfebf6dda5"
@@ -8251,6 +8256,20 @@
82518256
magic-string "0.30.8"
82528257
unplugin "1.0.1"
82538258

8259+
8260+
version "2.22.1"
8261+
resolved "https://registry.yarnpkg.com/@sentry/bundler-plugin-core/-/bundler-plugin-core-2.22.1.tgz#a3c984fd0e6a7bc3977cd74a635f6222e69c4ef2"
8262+
integrity sha512-RFbS57zfPvUBe4DL/pjt6BWCEyGFkk/n4gLNZQ9Cf2gRdUVW80AtAMZwrlEELrpW1D8kONy6/kvWf0leicHRMg==
8263+
dependencies:
8264+
"@babel/core" "^7.18.5"
8265+
"@sentry/babel-plugin-component-annotate" "2.22.1"
8266+
"@sentry/cli" "^2.33.1"
8267+
dotenv "^16.3.1"
8268+
find-up "^5.0.0"
8269+
glob "^9.3.2"
8270+
magic-string "0.30.8"
8271+
unplugin "1.0.1"
8272+
82548273
82558274
version "2.33.0"
82568275
resolved "https://registry.yarnpkg.com/@sentry/cli-darwin/-/cli-darwin-2.33.0.tgz#c0f3352a9e58e4f02deca52f0d5a9bd14b3e4a32"
@@ -8359,6 +8378,14 @@
83598378
"@sentry/cli-win32-i686" "2.33.1"
83608379
"@sentry/cli-win32-x64" "2.33.1"
83618380

8381+
8382+
version "2.22.1"
8383+
resolved "https://registry.yarnpkg.com/@sentry/rollup-plugin/-/rollup-plugin-2.22.1.tgz#644e6e47d4130bca31a578782813835ca243a484"
8384+
integrity sha512-ytioqf5e9lyuDc4G8cK0kMtl6DUUuPaE2ob583esnzEw/X15fpvdJOkBDNbyCtOrArm8me8o5lM47vi6FTv1vA==
8385+
dependencies:
8386+
"@sentry/bundler-plugin-core" "2.22.1"
8387+
unplugin "1.0.1"
8388+
83628389
83638390
version "2.19.0"
83648391
resolved "https://registry.yarnpkg.com/@sentry/vite-plugin/-/vite-plugin-2.19.0.tgz#c7938fb13eee15036963b87d7b12c4fc851e488b"
@@ -8367,14 +8394,6 @@
83678394
"@sentry/bundler-plugin-core" "2.19.0"
83688395
unplugin "1.0.1"
83698396

8370-
"@sentry/[email protected]", "@sentry/vite-plugin@^2.20.1":
8371-
version "2.20.1"
8372-
resolved "https://registry.yarnpkg.com/@sentry/vite-plugin/-/vite-plugin-2.20.1.tgz#80d5639fca3f68fbf81c316883272ffb34dbc544"
8373-
integrity sha512-IOYAJRcV+Uqn0EL8rxcoCvE77PbtGzKjP+B6iIgDZ229AWbpfbpWY8zHCcufwdDzb5PtgOhWWHT74iAsNyij/A==
8374-
dependencies:
8375-
"@sentry/bundler-plugin-core" "2.20.1"
8376-
unplugin "1.0.1"
8377-
83788397
83798398
version "2.22.0"
83808399
resolved "https://registry.yarnpkg.com/@sentry/vite-plugin/-/vite-plugin-2.22.0.tgz#09743ac390cf8c1609f2fa6d5424548d0b6f7928"
@@ -8383,6 +8402,22 @@
83838402
"@sentry/bundler-plugin-core" "2.22.0"
83848403
unplugin "1.0.1"
83858404

8405+
8406+
version "2.22.1"
8407+
resolved "https://registry.yarnpkg.com/@sentry/vite-plugin/-/vite-plugin-2.22.1.tgz#66192485440fd7f3726279a6bda2de4e59e6fd13"
8408+
integrity sha512-TC+3RIcu0rnxaEPV8ZHjYlRHLx5M9eRDI2il1lFkSguTc0N89n7Tt/aDnFAwtaPG4yqPkhIMpxi7LfXx+YDJ1Q==
8409+
dependencies:
8410+
"@sentry/bundler-plugin-core" "2.22.1"
8411+
unplugin "1.0.1"
8412+
8413+
"@sentry/vite-plugin@^2.20.1":
8414+
version "2.20.1"
8415+
resolved "https://registry.yarnpkg.com/@sentry/vite-plugin/-/vite-plugin-2.20.1.tgz#80d5639fca3f68fbf81c316883272ffb34dbc544"
8416+
integrity sha512-IOYAJRcV+Uqn0EL8rxcoCvE77PbtGzKjP+B6iIgDZ229AWbpfbpWY8zHCcufwdDzb5PtgOhWWHT74iAsNyij/A==
8417+
dependencies:
8418+
"@sentry/bundler-plugin-core" "2.20.1"
8419+
unplugin "1.0.1"
8420+
83868421
83878422
version "2.16.0"
83888423
resolved "https://registry.yarnpkg.com/@sentry/webpack-plugin/-/webpack-plugin-2.16.0.tgz#4764577edb10c9575a8b4ce03135493f995f56b9"

0 commit comments

Comments
 (0)