From 8f3e5b25ede4e11779fa045df407685fda56a6fd Mon Sep 17 00:00:00 2001 From: Sigrid Huemer <32902192+s1gr1d@users.noreply.github.com> Date: Wed, 2 Oct 2024 14:26:35 +0200 Subject: [PATCH] feat(nuxt): Add `unstable_sentryBundlerPluginOptions` to module options (#13811) Allows to pass other options from the bundler plugins (vite and rollup). closes https://github.com/getsentry/sentry-javascript/issues/13380 --- packages/nuxt/src/common/types.ts | 10 ++ packages/nuxt/src/vite/sourceMaps.ts | 24 ++-- packages/nuxt/test/vite/sourceMaps.test.ts | 139 +++++++++++++++++++++ 3 files changed, 166 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..bcc14ad1d307 100644 --- a/packages/nuxt/src/common/types.ts +++ b/packages/nuxt/src/common/types.ts @@ -1,4 +1,6 @@ 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) @@ -111,4 +113,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?: SentryRollupPluginOptions & SentryVitePluginOptions; }; 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', + }), + }), + ); + }); +});