Skip to content

Commit aa4547c

Browse files
authored
feat(nuxt): Align build-time options to follow bundler plugins structure (#17255)
Aligns the build-time options of the Nuxt SDK and implements the missing options. Closes #17064
1 parent 52be861 commit aa4547c

File tree

10 files changed

+409
-71
lines changed

10 files changed

+409
-71
lines changed

packages/core/src/build-time-plugins/buildTimeOptionsBase.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,23 @@ export interface BuildTimeOptionsBase {
8484
*/
8585
silent?: boolean;
8686

87+
/**
88+
* When an error occurs during release creation or sourcemaps upload, the plugin will call this function.
89+
*
90+
* By default, the plugin will simply throw an error, thereby stopping the bundling process.
91+
* If an `errorHandler` callback is provided, compilation will continue unless an error is
92+
* thrown in the provided callback.
93+
*
94+
* To allow compilation to continue but still emit a warning, set this option to the following:
95+
*
96+
* ```js
97+
* (err) => {
98+
* console.warn(err);
99+
* }
100+
* ```
101+
*/
102+
errorHandler?: (err: Error) => void;
103+
87104
/**
88105
* Enable debug information logs about the SDK during build-time.
89106
* Enabling this will give you, for example, logs about source maps.
@@ -184,7 +201,9 @@ export type UnstableRollupPluginOptions<PluginOptionsType> = {
184201

185202
interface SourceMapsOptions {
186203
/**
187-
* If this flag is `true`, any functionality related to source maps will be disabled.
204+
* If this flag is `true`, any functionality related to source maps will be disabled. This includes the automatic upload of source maps.
205+
*
206+
* By default (`false`), the plugin automatically uploads source maps during a production build if a Sentry auth token is detected.
188207
*
189208
* @default false
190209
*/

packages/nuxt/.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module.exports = {
77
{
88
files: ['vite.config.ts'],
99
parserOptions: {
10-
project: ['tsconfig.test.json'],
10+
project: ['tsconfig.vite.json'],
1111
},
1212
},
1313
],

packages/nuxt/src/common/types.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { BuildTimeOptionsBase } from '@sentry/core';
12
import type { init as initNode } from '@sentry/node';
23
import type { SentryRollupPluginOptions } from '@sentry/rollup-plugin';
34
import type { SentryVitePluginOptions } from '@sentry/vite-plugin';
@@ -26,6 +27,7 @@ type SourceMapsOptions = {
2627
* Suppresses all logs.
2728
*
2829
* @default false
30+
* @deprecated Use option `silent` instead of `sourceMapsUploadOptions.silent`
2931
*/
3032
silent?: boolean;
3133

@@ -43,13 +45,17 @@ type SourceMapsOptions = {
4345
* console.warn(err);
4446
* }
4547
* ```
48+
*
49+
* @deprecated Use option `errorHandler` instead of `sourceMapsUploadOptions.errorHandler`
4650
*/
4751
errorHandler?: (err: Error) => void;
4852

4953
/**
5054
* Options related to managing the Sentry releases for a build.
5155
*
5256
* More info: https://docs.sentry.io/product/releases/
57+
*
58+
* @deprecated Use option `release` instead of `sourceMapsUploadOptions.release`
5359
*/
5460
release?: {
5561
/**
@@ -62,6 +68,8 @@ type SourceMapsOptions = {
6268
* (the latter requires access to git CLI and for the root directory to be a valid repository)
6369
*
6470
* If you didn't provide a value and the plugin can't automatically detect one, no release will be created.
71+
*
72+
* @deprecated Use `release.name` instead of `sourceMapsUploadOptions.release.name`
6573
*/
6674
name?: string;
6775
};
@@ -71,6 +79,7 @@ type SourceMapsOptions = {
7179
* automatically generate and upload source maps to Sentry during a production build.
7280
*
7381
* @default true
82+
* @deprecated Use option `sourcemaps.disable` instead of `sourceMapsUploadOptions.enabled`
7483
*/
7584
enabled?: boolean;
7685

@@ -81,25 +90,30 @@ type SourceMapsOptions = {
8190
*
8291
* To create an auth token, follow this guide:
8392
* @see https://docs.sentry.io/product/accounts/auth-tokens/#organization-auth-tokens
93+
* @deprecated Use option `authToken` instead of `sourceMapsUploadOptions.authToken`
8494
*/
8595
authToken?: string;
8696

8797
/**
8898
* The organization slug of your Sentry organization.
8999
* Instead of specifying this option, you can also set the `SENTRY_ORG` environment variable.
100+
* @deprecated Use option `org` instead of `sourceMapsUploadOptions.org`
90101
*/
91102
org?: string;
92103

93104
/**
94105
* The URL of your Sentry instance if you're using self-hosted Sentry.
95106
*
96107
* @default https://sentry.io by default the plugin will point towards the Sentry SaaS URL
108+
* @deprecated Use `sentryUrl` instead of `sourceMapsUploadOptions.url`
97109
*/
98110
url?: string;
99111

100112
/**
101113
* The project slug of your Sentry project.
102114
* Instead of specifying this option, you can also set the `SENTRY_PROJECT` environment variable.
115+
*
116+
* @deprecated Use option `project` instead of `sourceMapsUploadOptions.project`
103117
*/
104118
project?: string;
105119

@@ -108,11 +122,14 @@ type SourceMapsOptions = {
108122
* It will not collect any sensitive or user-specific data.
109123
*
110124
* @default true
125+
* @deprecated Use option `telemetry` instead of `sourceMapsUploadOptions.telemetry`
111126
*/
112127
telemetry?: boolean;
113128

114129
/**
115130
* Options related to sourcemaps
131+
*
132+
* @deprecated Use option `sourcemaps` instead of `sourceMapsUploadOptions.sourcemaps`
116133
*/
117134
sourcemaps?: {
118135
/**
@@ -124,6 +141,8 @@ type SourceMapsOptions = {
124141
*
125142
* The globbing patterns must follow the implementation of the `glob` package.
126143
* @see https://www.npmjs.com/package/glob#glob-primer
144+
*
145+
* @deprecated Use option `sourcemaps.assets` instead of `sourceMapsUploadOptions.sourcemaps.assets`
127146
*/
128147
assets?: string | Array<string>;
129148

@@ -134,6 +153,8 @@ type SourceMapsOptions = {
134153
* or the default value for `assets` are uploaded.
135154
*
136155
* The globbing patterns follow the implementation of the glob package. (https://www.npmjs.com/package/glob)
156+
*
157+
* @deprecated Use option `sourcemaps.ignore` instead of `sourceMapsUploadOptions.sourcemaps.ignore`
137158
*/
138159
ignore?: string | Array<string>;
139160

@@ -144,6 +165,8 @@ type SourceMapsOptions = {
144165
* @default [] - By default no files are deleted.
145166
*
146167
* The globbing patterns follow the implementation of the glob package. (https://www.npmjs.com/package/glob)
168+
*
169+
* @deprecated Use option `sourcemaps.filesToDeleteAfterUpload` instead of `sourceMapsUploadOptions.sourcemaps.filesToDeleteAfterUpload`
147170
*/
148171
filesToDeleteAfterUpload?: string | Array<string>;
149172
};
@@ -152,7 +175,7 @@ type SourceMapsOptions = {
152175
/**
153176
* Build options for the Sentry module. These options are used during build-time by the Sentry SDK.
154177
*/
155-
export type SentryNuxtModuleOptions = {
178+
export type SentryNuxtModuleOptions = BuildTimeOptionsBase & {
156179
/**
157180
* Enable the Sentry Nuxt Module.
158181
*
@@ -165,15 +188,12 @@ export type SentryNuxtModuleOptions = {
165188
*
166189
* These options are always read from the `sentry` module options in the `nuxt.config.(js|ts).
167190
* Do not define them in the `sentry.client.config.(js|ts)` or `sentry.server.config.(js|ts)` files.
191+
*
192+
* @deprecated This option was deprecated as it adds unnecessary nesting.
193+
* Put the options one level higher to the root-level of the `sentry` module options.
168194
*/
169195
sourceMapsUploadOptions?: SourceMapsOptions;
170196

171-
/**
172-
* Enable debug functionality of the SDK during build-time.
173-
* Enabling this will give you, for example, logs about source maps.
174-
*/
175-
debug?: boolean;
176-
177197
/**
178198
*
179199
* Enables (partial) server tracing by automatically injecting Sentry for environments where modifying the node option `--import` is not possible.

packages/nuxt/src/vite/sourceMaps.ts

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,20 @@ export type SourceMapSetting = boolean | 'hidden' | 'inline';
1717
* Setup source maps for Sentry inside the Nuxt module during build time (in Vite for Nuxt and Rollup for Nitro).
1818
*/
1919
export function setupSourceMaps(moduleOptions: SentryNuxtModuleOptions, nuxt: Nuxt): void {
20+
// TODO(v11): remove deprecated options (also from SentryNuxtModuleOptions type)
21+
2022
const isDebug = moduleOptions.debug;
2123

24+
// eslint-disable-next-line deprecation/deprecation
2225
const sourceMapsUploadOptions = moduleOptions.sourceMapsUploadOptions || {};
23-
const sourceMapsEnabled = sourceMapsUploadOptions.enabled ?? true;
26+
27+
const sourceMapsEnabled =
28+
moduleOptions.sourcemaps?.disable === true
29+
? false
30+
: moduleOptions.sourcemaps?.disable === false
31+
? true
32+
: // eslint-disable-next-line deprecation/deprecation
33+
sourceMapsUploadOptions.enabled ?? true;
2434

2535
// In case we overwrite the source map settings, we default to deleting the files
2636
let shouldDeleteFilesFallback = { client: true, server: true };
@@ -42,6 +52,8 @@ export function setupSourceMaps(moduleOptions: SentryNuxtModuleOptions, nuxt: Nu
4252

4353
if (
4454
isDebug &&
55+
!moduleOptions.sourcemaps?.filesToDeleteAfterUpload &&
56+
// eslint-disable-next-line deprecation/deprecation
4557
!sourceMapsUploadOptions.sourcemaps?.filesToDeleteAfterUpload &&
4658
(shouldDeleteFilesFallback.client || shouldDeleteFilesFallback.server)
4759
) {
@@ -134,10 +146,13 @@ function normalizePath(path: string): string {
134146
*
135147
* Only exported for Testing purposes.
136148
*/
149+
// todo(v11): This "eslint-disable" can be removed again once we remove deprecated options.
150+
// eslint-disable-next-line complexity
137151
export function getPluginOptions(
138152
moduleOptions: SentryNuxtModuleOptions,
139153
shouldDeleteFilesFallback?: { client: boolean; server: boolean },
140154
): SentryVitePluginOptions | SentryRollupPluginOptions {
155+
// eslint-disable-next-line deprecation/deprecation
141156
const sourceMapsUploadOptions = moduleOptions.sourceMapsUploadOptions || {};
142157

143158
const shouldDeleteFilesAfterUpload = shouldDeleteFilesFallback?.client || shouldDeleteFilesFallback?.server;
@@ -148,10 +163,17 @@ export function getPluginOptions(
148163
: []),
149164
];
150165

151-
if (
152-
typeof sourceMapsUploadOptions.sourcemaps?.filesToDeleteAfterUpload === 'undefined' &&
153-
shouldDeleteFilesAfterUpload
154-
) {
166+
// Check for filesToDeleteAfterUpload in new location first, then deprecated location
167+
const sourcemapsOptions = moduleOptions.sourcemaps || {};
168+
// eslint-disable-next-line deprecation/deprecation
169+
const deprecatedSourcemapsOptions = sourceMapsUploadOptions.sourcemaps || {};
170+
171+
const filesToDeleteAfterUpload =
172+
sourcemapsOptions.filesToDeleteAfterUpload ??
173+
// eslint-disable-next-line deprecation/deprecation
174+
deprecatedSourcemapsOptions.filesToDeleteAfterUpload;
175+
176+
if (typeof filesToDeleteAfterUpload === 'undefined' && shouldDeleteFilesAfterUpload) {
155177
consoleSandbox(() => {
156178
// eslint-disable-next-line no-console
157179
console.log(
@@ -164,16 +186,28 @@ export function getPluginOptions(
164186
}
165187

166188
return {
167-
org: sourceMapsUploadOptions.org ?? process.env.SENTRY_ORG,
168-
project: sourceMapsUploadOptions.project ?? process.env.SENTRY_PROJECT,
169-
authToken: sourceMapsUploadOptions.authToken ?? process.env.SENTRY_AUTH_TOKEN,
170-
telemetry: sourceMapsUploadOptions.telemetry ?? true,
171-
url: sourceMapsUploadOptions.url ?? process.env.SENTRY_URL,
189+
// eslint-disable-next-line deprecation/deprecation
190+
org: moduleOptions.org ?? sourceMapsUploadOptions.org ?? process.env.SENTRY_ORG,
191+
// eslint-disable-next-line deprecation/deprecation
192+
project: moduleOptions.project ?? sourceMapsUploadOptions.project ?? process.env.SENTRY_PROJECT,
193+
// eslint-disable-next-line deprecation/deprecation
194+
authToken: moduleOptions.authToken ?? sourceMapsUploadOptions.authToken ?? process.env.SENTRY_AUTH_TOKEN,
195+
// eslint-disable-next-line deprecation/deprecation
196+
telemetry: moduleOptions.telemetry ?? sourceMapsUploadOptions.telemetry ?? true,
197+
// eslint-disable-next-line deprecation/deprecation
198+
url: moduleOptions.sentryUrl ?? sourceMapsUploadOptions.url ?? process.env.SENTRY_URL,
199+
headers: moduleOptions.headers,
172200
debug: moduleOptions.debug ?? false,
173-
silent: sourceMapsUploadOptions.silent ?? false,
174-
errorHandler: sourceMapsUploadOptions.errorHandler,
201+
// eslint-disable-next-line deprecation/deprecation
202+
silent: moduleOptions.silent ?? sourceMapsUploadOptions.silent ?? false,
203+
// eslint-disable-next-line deprecation/deprecation
204+
errorHandler: moduleOptions.errorHandler ?? sourceMapsUploadOptions.errorHandler,
205+
bundleSizeOptimizations: moduleOptions.bundleSizeOptimizations, // todo: test if this can be overridden by the user
175206
release: {
176-
name: sourceMapsUploadOptions.release?.name,
207+
// eslint-disable-next-line deprecation/deprecation
208+
name: moduleOptions.release?.name ?? sourceMapsUploadOptions.release?.name,
209+
// Support all release options from BuildTimeOptionsBase
210+
...moduleOptions.release,
177211
...moduleOptions?.unstable_sentryBundlerPluginOptions?.release,
178212
},
179213
_metaOptions: {
@@ -184,13 +218,16 @@ export function getPluginOptions(
184218
...moduleOptions?.unstable_sentryBundlerPluginOptions,
185219

186220
sourcemaps: {
221+
disable: moduleOptions.sourcemaps?.disable,
187222
// The server/client files are in different places depending on the nitro preset (e.g. '.output/server' or '.netlify/functions-internal/server')
188223
// We cannot determine automatically how the build folder looks like (depends on the preset), so we have to accept that source maps are uploaded multiple times (with the vitePlugin for Nuxt and the rollupPlugin for Nitro).
189224
// 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/**/*'],
190-
assets: sourceMapsUploadOptions.sourcemaps?.assets ?? undefined,
191-
ignore: sourceMapsUploadOptions.sourcemaps?.ignore ?? undefined,
192-
filesToDeleteAfterUpload: sourceMapsUploadOptions.sourcemaps?.filesToDeleteAfterUpload
193-
? sourceMapsUploadOptions.sourcemaps?.filesToDeleteAfterUpload
225+
// eslint-disable-next-line deprecation/deprecation
226+
assets: sourcemapsOptions.assets ?? deprecatedSourcemapsOptions.assets ?? undefined,
227+
// eslint-disable-next-line deprecation/deprecation
228+
ignore: sourcemapsOptions.ignore ?? deprecatedSourcemapsOptions.ignore ?? undefined,
229+
filesToDeleteAfterUpload: filesToDeleteAfterUpload
230+
? filesToDeleteAfterUpload
194231
: shouldDeleteFilesFallback?.server || shouldDeleteFilesFallback?.client
195232
? fallbackFilesToDelete
196233
: undefined,

0 commit comments

Comments
 (0)