Skip to content

Commit

Permalink
feat(nuxt): Add unstable_sentryBundlerPluginOptions to module optio…
Browse files Browse the repository at this point in the history
…ns (#13811)

Allows to pass other options from the bundler plugins (vite and rollup).

closes #13380
  • Loading branch information
s1gr1d authored Oct 2, 2024
1 parent dafd510 commit 8f3e5b2
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 7 deletions.
10 changes: 10 additions & 0 deletions packages/nuxt/src/common/types.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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;
};
24 changes: 17 additions & 7 deletions packages/nuxt/src/vite/sourceMaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,29 @@ 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 {
org: sourceMapsUploadOptions.org ?? process.env.SENTRY_ORG,
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).
Expand All @@ -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,
};
}

Expand Down
139 changes: 139 additions & 0 deletions packages/nuxt/test/vite/sourceMaps.test.ts
Original file line number Diff line number Diff line change
@@ -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',
}),
}),
);
});
});

0 comments on commit 8f3e5b2

Please sign in to comment.