Skip to content

Commit ea07ec7

Browse files
authored
feat(nuxt): Setup source maps with vite config (#13018)
Closes #13017
1 parent aaaedbc commit ea07ec7

File tree

8 files changed

+193
-34
lines changed

8 files changed

+193
-34
lines changed

packages/astro/src/integration/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ type SourceMapsOptions = {
7070
telemetry?: boolean;
7171

7272
/**
73-
* A glob or an array of globs that specify the build artifacts and source maps that will uploaded to Sentry.
73+
* A glob or an array of globs that specify the build artifacts and source maps that will be uploaded to Sentry.
7474
*
7575
* If this option is not specified, sensible defaults based on your `outDir`, `rootDir` and `adapter`
7676
* config will be used. Use this option to override these defaults, for instance if you have a

packages/nuxt/.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module.exports = {
1414
files: ['src/vite/**', 'src/server/**'],
1515
rules: {
1616
'@sentry-internal/sdk/no-optional-chaining': 'off',
17+
'@sentry-internal/sdk/no-nullish-coalescing': 'off',
1718
},
1819
},
1920
],

packages/nuxt/README.md

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,22 @@ dependency to your `package.json`
138138
}
139139
```
140140

141-
### 5. Vite Setup
142-
143-
todo: add vite setup
144-
145-
---
146-
147141
## Uploading Source Maps
148142

149-
todo: add source maps instructions
143+
To upload source maps, you can use the `sourceMapsUploadOptions` option inside the `sentry` options of your
144+
`nuxt.config.ts`:
145+
146+
```javascript
147+
// nuxt.config.ts
148+
export default defineNuxtConfig({
149+
modules: ['@sentry/nuxt/module'],
150+
sentry: {
151+
debug: true,
152+
sourceMapsUploadOptions: {
153+
org: 'your-org-slug',
154+
project: 'your-project-slug',
155+
authToken: process.env.SENTRY_AUTH_TOKEN,
156+
},
157+
},
158+
});
159+
```

packages/nuxt/src/common/types.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,99 @@ import type { init } from '@sentry/vue';
22

33
// Omitting 'app' as the Nuxt SDK will add the app instance in the client plugin (users do not have to provide this)
44
export type SentryNuxtOptions = Omit<Parameters<typeof init>[0] & object, 'app'>;
5+
6+
type SourceMapsOptions = {
7+
/**
8+
* Options for the Sentry Vite plugin to customize the source maps upload process.
9+
*
10+
* These options are always read from the `sentry` module options in the `nuxt.config.(js|ts).
11+
* Do not define them in the `sentry.client.config.(js|ts)` or `sentry.server.config.(js|ts)` files.
12+
*/
13+
sourceMapsUploadOptions?: {
14+
/**
15+
* If this flag is `true`, and an auth token is detected, the Sentry integration will
16+
* automatically generate and upload source maps to Sentry during a production build.
17+
*
18+
* @default true
19+
*/
20+
enabled?: boolean;
21+
22+
/**
23+
* The auth token to use when uploading source maps to Sentry.
24+
*
25+
* Instead of specifying this option, you can also set the `SENTRY_AUTH_TOKEN` environment variable.
26+
*
27+
* To create an auth token, follow this guide:
28+
* @see https://docs.sentry.io/product/accounts/auth-tokens/#organization-auth-tokens
29+
*/
30+
authToken?: string;
31+
32+
/**
33+
* The organization slug of your Sentry organization.
34+
* Instead of specifying this option, you can also set the `SENTRY_ORG` environment variable.
35+
*/
36+
org?: string;
37+
38+
/**
39+
* The project slug of your Sentry project.
40+
* Instead of specifying this option, you can also set the `SENTRY_PROJECT` environment variable.
41+
*/
42+
project?: string;
43+
44+
/**
45+
* If this flag is `true`, the Sentry plugin will collect some telemetry data and send it to Sentry.
46+
* It will not collect any sensitive or user-specific data.
47+
*
48+
* @default true
49+
*/
50+
telemetry?: boolean;
51+
52+
/**
53+
* Options related to sourcemaps
54+
*/
55+
sourcemaps?: {
56+
/**
57+
* A glob or an array of globs that specify the build artifacts and source maps that will be uploaded to Sentry.
58+
*
59+
* If this option is not specified, sensible defaults based on your adapter and nuxt.config.js
60+
* setup will be used. Use this option to override these defaults, for instance if you have a
61+
* customized build setup that diverges from Nuxt's defaults.
62+
*
63+
* The globbing patterns must follow the implementation of the `glob` package.
64+
* @see https://www.npmjs.com/package/glob#glob-primer
65+
*/
66+
assets?: string | Array<string>;
67+
68+
/**
69+
* A glob or an array of globs that specifies which build artifacts should not be uploaded to Sentry.
70+
*
71+
* @default [] - By default no files are ignored. Thus, all files matching the `assets` glob
72+
* or the default value for `assets` are uploaded.
73+
*
74+
* The globbing patterns follow the implementation of the glob package. (https://www.npmjs.com/package/glob)
75+
*/
76+
ignore?: string | Array<string>;
77+
78+
/**
79+
* A glob or an array of globs that specifies the build artifacts that should be deleted after the artifact
80+
* upload to Sentry has been completed.
81+
*
82+
* @default [] - By default no files are deleted.
83+
*
84+
* The globbing patterns follow the implementation of the glob package. (https://www.npmjs.com/package/glob)
85+
*/
86+
filesToDeleteAfterUpload?: string | Array<string>;
87+
};
88+
};
89+
};
90+
91+
/**
92+
* Build options for the Sentry module. These options are used during build-time by the Sentry SDK.
93+
*/
94+
export type SentryNuxtModuleOptions = SourceMapsOptions & {
95+
/**
96+
* Enable debug functionality of the SDK during build-time.
97+
* Enabling this will give you, for example, logs about source maps.
98+
*/
99+
debug?: boolean;
100+
};

packages/nuxt/src/module.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import * as fs from 'fs';
22
import * as path from 'path';
33
import { addPlugin, addPluginTemplate, addServerPlugin, createResolver, defineNuxtModule } from '@nuxt/kit';
4-
import type { SentryNuxtOptions } from './common/types';
4+
import type { SentryNuxtModuleOptions } from './common/types';
5+
import { setupSourceMaps } from './vite/sourceMaps';
56

6-
export type ModuleOptions = SentryNuxtOptions;
7+
export type ModuleOptions = SentryNuxtModuleOptions;
78

89
export default defineNuxtModule<ModuleOptions>({
910
meta: {
@@ -14,7 +15,7 @@ export default defineNuxtModule<ModuleOptions>({
1415
},
1516
},
1617
defaults: {},
17-
setup(_moduleOptions, nuxt) {
18+
setup(moduleOptions, nuxt) {
1819
const moduleDirResolver = createResolver(import.meta.url);
1920
const buildDirResolver = createResolver(nuxt.options.buildDir);
2021

@@ -47,6 +48,10 @@ export default defineNuxtModule<ModuleOptions>({
4748

4849
addServerPlugin(moduleDirResolver.resolve('./runtime/plugins/sentry.server'));
4950
}
51+
52+
if (clientConfigFile || serverConfigFile) {
53+
setupSourceMaps(moduleOptions, nuxt);
54+
}
5055
},
5156
});
5257

packages/nuxt/src/vite/sourceMaps.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import type { Nuxt } from '@nuxt/schema';
2+
import { sentryVitePlugin } from '@sentry/vite-plugin';
3+
import type { SentryNuxtModuleOptions } from '../common/types';
4+
5+
/**
6+
* Setup source maps for Sentry inside the Nuxt module during build time.
7+
*/
8+
export function setupSourceMaps(moduleOptions: SentryNuxtModuleOptions, nuxt: Nuxt): void {
9+
nuxt.hook('vite:extendConfig', async (viteInlineConfig, _env) => {
10+
const sourceMapsUploadOptions = moduleOptions.sourceMapsUploadOptions || {};
11+
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+
});
30+
31+
viteInlineConfig.plugins = viteInlineConfig.plugins || [];
32+
viteInlineConfig.plugins.push(sentryPlugin);
33+
34+
const sourceMapsPreviouslyEnabled = viteInlineConfig.build?.sourcemap;
35+
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+
);
45+
}
46+
}
47+
48+
viteInlineConfig.build = viteInlineConfig.build || {};
49+
viteInlineConfig.build.sourcemap = true;
50+
}
51+
});
52+
}

packages/sveltekit/src/vite/sourceMaps.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug
108108

109109
// Modify the config to generate source maps
110110
config: config => {
111-
const sourceMapsPreviouslyEnabled = !config.build?.sourcemap;
112-
if (debug && sourceMapsPreviouslyEnabled) {
111+
const sourceMapsPreviouslyNotEnabled = !config.build?.sourcemap;
112+
if (debug && sourceMapsPreviouslyNotEnabled) {
113113
// eslint-disable-next-line no-console
114114
console.log('[Source Maps Plugin] Enabeling source map generation');
115115
if (!mergedOptions.sourcemaps?.filesToDeleteAfterUpload) {

yarn.lock

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5973,15 +5973,6 @@
59735973
semver "^7.3.5"
59745974
tar "^6.1.11"
59755975

5976-
"@nestjs/common@^10.3.10":
5977-
version "10.3.10"
5978-
resolved "https://registry.yarnpkg.com/@nestjs/common/-/common-10.3.10.tgz#d8825d55a50a04e33080c9188e6a5b03235d19f2"
5979-
integrity sha512-H8k0jZtxk1IdtErGDmxFRy0PfcOAUg41Prrqpx76DQusGGJjsaovs1zjXVD1rZWaVYchfT1uczJ6L4Kio10VNg==
5980-
dependencies:
5981-
uid "2.0.2"
5982-
iterare "1.2.1"
5983-
tslib "2.6.3"
5984-
59855976
"@nestjs/common@^10.3.7":
59865977
version "10.3.7"
59875978
resolved "https://registry.yarnpkg.com/@nestjs/common/-/common-10.3.7.tgz#38ab5ff92277cf1f26f4749c264524e76962cfff"
@@ -5991,16 +5982,13 @@
59915982
iterare "1.2.1"
59925983
tslib "2.6.2"
59935984

5994-
"@nestjs/core@^10.3.10":
5985+
"@nestjs/common@^8.0.0 || ^9.0.0 || ^10.0.0":
59955986
version "10.3.10"
5996-
resolved "https://registry.yarnpkg.com/@nestjs/core/-/core-10.3.10.tgz#508090c3ca36488a8e24a9e5939c2f37426e48f4"
5997-
integrity sha512-ZbQ4jovQyzHtCGCrzK5NdtW1SYO2fHSsgSY1+/9WdruYCUra+JDkWEXgZ4M3Hv480Dl3OXehAmY1wCOojeMyMQ==
5987+
resolved "https://registry.yarnpkg.com/@nestjs/common/-/common-10.3.10.tgz#d8825d55a50a04e33080c9188e6a5b03235d19f2"
5988+
integrity sha512-H8k0jZtxk1IdtErGDmxFRy0PfcOAUg41Prrqpx76DQusGGJjsaovs1zjXVD1rZWaVYchfT1uczJ6L4Kio10VNg==
59985989
dependencies:
59995990
uid "2.0.2"
6000-
"@nuxtjs/opencollective" "0.3.2"
6001-
fast-safe-stringify "2.1.1"
60025991
iterare "1.2.1"
6003-
path-to-regexp "3.2.0"
60045992
tslib "2.6.3"
60055993

60065994
"@nestjs/core@^10.3.3":
@@ -6015,6 +6003,18 @@
60156003
path-to-regexp "3.2.0"
60166004
tslib "2.6.2"
60176005

6006+
"@nestjs/core@^8.0.0 || ^9.0.0 || ^10.0.0":
6007+
version "10.3.10"
6008+
resolved "https://registry.yarnpkg.com/@nestjs/core/-/core-10.3.10.tgz#508090c3ca36488a8e24a9e5939c2f37426e48f4"
6009+
integrity sha512-ZbQ4jovQyzHtCGCrzK5NdtW1SYO2fHSsgSY1+/9WdruYCUra+JDkWEXgZ4M3Hv480Dl3OXehAmY1wCOojeMyMQ==
6010+
dependencies:
6011+
uid "2.0.2"
6012+
"@nuxtjs/opencollective" "0.3.2"
6013+
fast-safe-stringify "2.1.1"
6014+
iterare "1.2.1"
6015+
path-to-regexp "3.2.0"
6016+
tslib "2.6.3"
6017+
60186018
"@nestjs/platform-express@^10.3.3":
60196019
version "10.3.3"
60206020
resolved "https://registry.yarnpkg.com/@nestjs/platform-express/-/platform-express-10.3.3.tgz#c1484d30d1e7666c4c8d0d7cde31cfc0b9d166d7"
@@ -31918,7 +31918,7 @@ [email protected], tslib@^2.6.2:
3191831918
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.6.2.tgz#703ac29425e7b37cd6fd456e92404d46d1f3e4ae"
3191931919
integrity sha512-AEYxH93jGFPn/a2iVAwW87VuUIkR1FVUKB77NwMF7nBTDkDrrT/Hpt/IrCJ0QXhW27jTBDcf5ZY7w6RiqTMw2Q==
3192031920

31921-
31921+
[email protected], tslib@^2.2.0:
3192231922
version "2.6.3"
3192331923
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.6.3.tgz#0438f810ad7a9edcde7a241c3d80db693c8cbfe0"
3192431924
integrity sha512-xNvxJEOUiWPGhUuUdQgAJPKOOJfGnIyKySOc09XkKsgdUV/3E2zvwZYdejjmRgPCgcym1juLH3226yA7sEFJKQ==
@@ -31933,11 +31933,6 @@ tslib@^2.0.0, tslib@^2.0.1, tslib@^2.0.3, tslib@^2.1.0, tslib@^2.3.0, tslib@^2.3
3193331933
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.5.2.tgz#1b6f07185c881557b0ffa84b111a0106989e8338"
3193431934
integrity sha512-5svOrSA2w3iGFDs1HibEVBGbDrAY82bFQ3HZ3ixB+88nsbsWQoKqDRb5UBYAUPEzbBn6dAp5gRNXglySbx1MlA==
3193531935

31936-
tslib@^2.2.0:
31937-
version "2.6.3"
31938-
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.6.3.tgz#0438f810ad7a9edcde7a241c3d80db693c8cbfe0"
31939-
integrity sha512-xNvxJEOUiWPGhUuUdQgAJPKOOJfGnIyKySOc09XkKsgdUV/3E2zvwZYdejjmRgPCgcym1juLH3226yA7sEFJKQ==
31940-
3194131936
tsutils@^3.21.0:
3194231937
version "3.21.0"
3194331938
resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-3.21.0.tgz#b48717d394cea6c1e096983eed58e9d61715b623"

0 commit comments

Comments
 (0)