Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nuxt): Add unstable_sentryBundlerPluginOptions to module options #13811

Merged
merged 4 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used Options from @sentry/bundler-plugin-core first (as this is the type the other plugins inherit from) but this gave me the following build error:

 ERROR  The keyword 'interface' is reserved (4:0) in /[folders...]/sentry-javascript/node_modules/@sentry/bundler-plugin-core/dist/types/index.d.ts

The Options are defined as interface, but Rollup defines interface as reserved name. I think this is related but I couldn't figure out how to fix that.

};
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',
}),
}),
);
});
});
Loading