From 5a6a0de0933fb016e6dc7a97bc4bfa0213ceb29c Mon Sep 17 00:00:00 2001 From: s1gr1d Date: Thu, 26 Sep 2024 13:11:37 +0200 Subject: [PATCH 1/3] feat(nuxt): Add `unstable_sentryBundlerPluginOptions` to module options --- packages/nuxt/src/common/types.ts | 8 ++ packages/nuxt/src/vite/sourceMaps.ts | 24 ++-- packages/nuxt/test/vite/sourceMaps.test.ts | 139 +++++++++++++++++++++ 3 files changed, 164 insertions(+), 7 deletions(-) create mode 100644 packages/nuxt/test/vite/sourceMaps.test.ts diff --git a/packages/nuxt/src/common/types.ts b/packages/nuxt/src/common/types.ts index 5fbe68bd89cb..540b5b7e6cf0 100644 --- a/packages/nuxt/src/common/types.ts +++ b/packages/nuxt/src/common/types.ts @@ -111,4 +111,12 @@ export type SentryNuxtModuleOptions = { * @default false */ experimental_basicServerTracing?: boolean; + + /** + * Options to be passed directly to the Sentry Rollup Plugin (`@sentry/rollup-plugin`) and Sentry Vite Plugin (`@sentry/vite-plugin`) that ship with the Sentry Nuxt SDK. + * You can use this option to override any options the SDK passes to the Vite (for Nuxt) and Rollup (for Nitro) plugin. + * + * Please note that this option is unstable and may change in a breaking way in any release. + */ + unstable_sentryBundlerPluginOptions?: SentryBundlerPluginOptions; }; diff --git a/packages/nuxt/src/vite/sourceMaps.ts b/packages/nuxt/src/vite/sourceMaps.ts index 801d16ab71cf..9abbfe8eaf08 100644 --- a/packages/nuxt/src/vite/sourceMaps.ts +++ b/packages/nuxt/src/vite/sourceMaps.ts @@ -58,7 +58,14 @@ function normalizePath(path: string): string { return path.replace(/^(\.\.\/)+/, './'); } -function getPluginOptions(moduleOptions: SentryNuxtModuleOptions): SentryVitePluginOptions | SentryRollupPluginOptions { +/** + * Generates source maps upload options for the Sentry Vite and Rollup plugin. + * + * Only exported for Testing purposes. + */ +export function getPluginOptions( + moduleOptions: SentryNuxtModuleOptions, +): SentryVitePluginOptions | SentryRollupPluginOptions { const sourceMapsUploadOptions = moduleOptions.sourceMapsUploadOptions || {}; return { @@ -66,6 +73,14 @@ function getPluginOptions(moduleOptions: SentryNuxtModuleOptions): SentryVitePlu project: sourceMapsUploadOptions.project ?? process.env.SENTRY_PROJECT, authToken: sourceMapsUploadOptions.authToken ?? process.env.SENTRY_AUTH_TOKEN, telemetry: sourceMapsUploadOptions.telemetry ?? true, + debug: moduleOptions.debug ?? false, + _metaOptions: { + telemetry: { + metaFramework: 'nuxt', + }, + }, + ...moduleOptions?.unstable_sentryBundlerPluginOptions, + sourcemaps: { // The server/client files are in different places depending on the nitro preset (e.g. '.output/server' or '.netlify/functions-internal/server') // We cannot determine automatically how the build folder looks like (depends on the preset), so we have to accept that sourcemaps are uploaded multiple times (with the vitePlugin for Nuxt and the rollupPlugin for Nitro). @@ -74,13 +89,8 @@ function getPluginOptions(moduleOptions: SentryNuxtModuleOptions): SentryVitePlu ignore: sourceMapsUploadOptions.sourcemaps?.ignore ?? undefined, filesToDeleteAfterUpload: sourceMapsUploadOptions.sourcemaps?.filesToDeleteAfterUpload ?? undefined, rewriteSources: (source: string) => normalizePath(source), + ...moduleOptions?.unstable_sentryBundlerPluginOptions?.sourcemaps, }, - _metaOptions: { - telemetry: { - metaFramework: 'nuxt', - }, - }, - debug: moduleOptions.debug ?? false, }; } diff --git a/packages/nuxt/test/vite/sourceMaps.test.ts b/packages/nuxt/test/vite/sourceMaps.test.ts new file mode 100644 index 000000000000..34c520b96d83 --- /dev/null +++ b/packages/nuxt/test/vite/sourceMaps.test.ts @@ -0,0 +1,139 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest'; +import type { SentryNuxtModuleOptions } from '../../src/common/types'; +import { getPluginOptions } from '../../src/vite/sourceMaps'; + +describe('getPluginOptions', () => { + beforeEach(() => { + vi.resetModules(); + process.env = {}; + }); + + it('uses environment variables when no moduleOptions are provided', () => { + const defaultEnv = { + SENTRY_ORG: 'default-org', + SENTRY_PROJECT: 'default-project', + SENTRY_AUTH_TOKEN: 'default-token', + }; + + process.env = { ...defaultEnv }; + + const options = getPluginOptions({} as SentryNuxtModuleOptions); + + expect(options).toEqual( + expect.objectContaining({ + org: 'default-org', + project: 'default-project', + authToken: 'default-token', + telemetry: true, + sourcemaps: expect.objectContaining({ + rewriteSources: expect.any(Function), + }), + _metaOptions: expect.objectContaining({ + telemetry: expect.objectContaining({ + metaFramework: 'nuxt', + }), + }), + debug: false, + }), + ); + }); + + it('returns default options when no moduleOptions are provided', () => { + const options = getPluginOptions({} as SentryNuxtModuleOptions); + + expect(options.org).toBeUndefined(); + expect(options.project).toBeUndefined(); + expect(options.authToken).toBeUndefined(); + expect(options).toEqual( + expect.objectContaining({ + telemetry: true, + sourcemaps: expect.objectContaining({ + rewriteSources: expect.any(Function), + }), + _metaOptions: expect.objectContaining({ + telemetry: expect.objectContaining({ + metaFramework: 'nuxt', + }), + }), + debug: false, + }), + ); + }); + + it('merges custom moduleOptions with default options', () => { + const customOptions: SentryNuxtModuleOptions = { + sourceMapsUploadOptions: { + org: 'custom-org', + project: 'custom-project', + authToken: 'custom-token', + telemetry: false, + sourcemaps: { + assets: ['custom-assets/**/*'], + ignore: ['ignore-this.js'], + filesToDeleteAfterUpload: ['delete-this.js'], + }, + }, + debug: true, + }; + const options = getPluginOptions(customOptions); + expect(options).toEqual( + expect.objectContaining({ + org: 'custom-org', + project: 'custom-project', + authToken: 'custom-token', + telemetry: false, + sourcemaps: expect.objectContaining({ + assets: ['custom-assets/**/*'], + ignore: ['ignore-this.js'], + filesToDeleteAfterUpload: ['delete-this.js'], + rewriteSources: expect.any(Function), + }), + _metaOptions: expect.objectContaining({ + telemetry: expect.objectContaining({ + metaFramework: 'nuxt', + }), + }), + debug: true, + }), + ); + }); + + it('overrides options that were undefined with options from unstable_sentryRollupPluginOptions', () => { + const customOptions: SentryNuxtModuleOptions = { + sourceMapsUploadOptions: { + org: 'custom-org', + project: 'custom-project', + sourcemaps: { + assets: ['custom-assets/**/*'], + filesToDeleteAfterUpload: ['delete-this.js'], + }, + }, + debug: true, + unstable_sentryBundlerPluginOptions: { + org: 'unstable-org', + sourcemaps: { + assets: ['unstable-assets/**/*'], + }, + release: { + name: 'test-release', + }, + }, + }; + const options = getPluginOptions(customOptions); + expect(options).toEqual( + expect.objectContaining({ + debug: true, + org: 'unstable-org', + project: 'custom-project', + sourcemaps: expect.objectContaining({ + assets: ['unstable-assets/**/*'], + filesToDeleteAfterUpload: ['delete-this.js'], + rewriteSources: expect.any(Function), + }), + release: expect.objectContaining({ + name: 'test-release', + }), + }), + ); + }); +}); From fd615c2c2877c439c7fa0d44182812481f93e090 Mon Sep 17 00:00:00 2001 From: s1gr1d Date: Thu, 26 Sep 2024 13:21:56 +0200 Subject: [PATCH 2/3] add missing type --- packages/nuxt/src/common/types.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/nuxt/src/common/types.ts b/packages/nuxt/src/common/types.ts index 540b5b7e6cf0..ae73299832ba 100644 --- a/packages/nuxt/src/common/types.ts +++ b/packages/nuxt/src/common/types.ts @@ -1,3 +1,4 @@ +import type { Options as SentryBundlerPluginOptions } from '@sentry/bundler-plugin-core'; import type { init as initNode } from '@sentry/node'; import type { init as initVue } from '@sentry/vue'; From 2ef3f77144101ae2c240d37319a096db5f40cdef Mon Sep 17 00:00:00 2001 From: s1gr1d Date: Thu, 26 Sep 2024 14:38:10 +0200 Subject: [PATCH 3/3] fix types --- packages/nuxt/src/common/types.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/nuxt/src/common/types.ts b/packages/nuxt/src/common/types.ts index ae73299832ba..bcc14ad1d307 100644 --- a/packages/nuxt/src/common/types.ts +++ b/packages/nuxt/src/common/types.ts @@ -1,5 +1,6 @@ -import type { Options as SentryBundlerPluginOptions } from '@sentry/bundler-plugin-core'; import type { init as initNode } from '@sentry/node'; +import type { SentryRollupPluginOptions } from '@sentry/rollup-plugin'; +import type { SentryVitePluginOptions } from '@sentry/vite-plugin'; import type { init as initVue } from '@sentry/vue'; // Omitting 'app' as the Nuxt SDK will add the app instance in the client plugin (users do not have to provide this) @@ -119,5 +120,5 @@ export type SentryNuxtModuleOptions = { * * Please note that this option is unstable and may change in a breaking way in any release. */ - unstable_sentryBundlerPluginOptions?: SentryBundlerPluginOptions; + unstable_sentryBundlerPluginOptions?: SentryRollupPluginOptions & SentryVitePluginOptions; };