Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
13 changes: 13 additions & 0 deletions packages/core/src/build-time-plugins/buildTimeOptionsBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,19 @@ interface SourceMapsOptions {
* The globbing patterns must follow the implementation of the `glob` package: https://www.npmjs.com/package/glob#glob-primer
*/
filesToDeleteAfterUpload?: string | Array<string>;

/**
* Hook to rewrite the `sources` field inside the source map before being uploaded to Sentry. Does not modify the actual source map.
*
* The hook receives the following arguments:
* - `source` - the source file path from the source map's `sources` field
* - `map` - the source map object
* - `context` - an optional object containing `mapDir`, the absolute path to the directory of the source map file
*
* Defaults to making all sources relative to `process.cwd()` while building.
*/
// oxlint-disable-next-line typescript-eslint/no-explicit-any -- matches the bundler plugin's RewriteSourcesHook type
rewriteSources?: (source: string, map: any, context?: { mapDir: string }) => string;
}

type AutoSetCommitsOptions = {
Expand Down
2 changes: 1 addition & 1 deletion packages/nextjs/src/config/getBuildPluginOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ export function getBuildPluginOptions({
url: sentryBuildOptions.sentryUrl,
sourcemaps: {
disable: skipSourcemapsUpload ? true : (sentryBuildOptions.sourcemaps?.disable ?? false),
rewriteSources: rewriteWebpackSources,
rewriteSources: sentryBuildOptions.sourcemaps?.rewriteSources ?? rewriteWebpackSources,
assets: sentryBuildOptions.sourcemaps?.assets ?? sourcemapUploadAssets,
ignore: finalIgnorePatterns,
filesToDeleteAfterUpload,
Expand Down
15 changes: 15 additions & 0 deletions packages/nextjs/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,21 @@ export type SentryBuildOptions = {
* ```
*/
filesToDeleteAfterUpload?: string | string[];

/**
* Hook to rewrite the `sources` field inside the source map before being uploaded to Sentry. Does not modify the actual source map.
*
* The hook receives the following arguments:
* - `source` - the source file path from the source map's `sources` field
* - `map` - the source map object
* - `context` - an optional object containing `mapDir`, the absolute path to the directory of the source map file
*
* If not provided, the SDK defaults to stripping webpack-specific prefixes (`webpack://_N_E/`).
*
* Defaults to making all sources relative to `process.cwd()` while building.
*/
// oxlint-disable-next-line typescript-eslint/no-explicit-any -- matches the bundler plugin's RewriteSourcesHook type
rewriteSources?: (source: string, map: any, context?: { mapDir: string }) => string;
};

/**
Expand Down
20 changes: 20 additions & 0 deletions packages/nextjs/test/config/getBuildPluginOptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,26 @@ describe('getBuildPluginOptions', () => {
expect(rewriteSources('./components/Layout.tsx', {})).toBe('./components/Layout.tsx');
}
});

it('allows user to override rewriteSources', () => {
const customRewrite = (source: string) => source.replace(/^custom\//, '');
const sentryBuildOptions: SentryBuildOptions = {
org: 'test-org',
project: 'test-project',
sourcemaps: {
rewriteSources: customRewrite,
},
};

const result = getBuildPluginOptions({
sentryBuildOptions,
releaseName: mockReleaseName,
distDirAbsPath: mockDistDirAbsPath,
buildTool: 'webpack-client',
});

expect(result.sourcemaps?.rewriteSources).toBe(customRewrite);
});
});

describe('release configuration', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt/src/vite/sourceMaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export function getPluginOptions(
: shouldDeleteFilesFallback?.server || shouldDeleteFilesFallback?.client
? fallbackFilesToDelete
: undefined,
rewriteSources: (source: string) => normalizePath(source),
rewriteSources: sourcemapsOptions.rewriteSources ?? ((source: string) => normalizePath(source)),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Super-L: Just because I see the pattern here now:

Suggested change
rewriteSources: sourcemapsOptions.rewriteSources ?? ((source: string) => normalizePath(source)),
rewriteSources: sourcemapsOptions.rewriteSources ?? normalizePath

This should have the same outcome

...moduleOptions?.unstable_sentryBundlerPluginOptions?.sourcemaps,
},
};
Expand Down
10 changes: 10 additions & 0 deletions packages/nuxt/test/vite/sourceMaps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ describe('getPluginOptions', () => {
expect(rewrite!('./local')).toBe('./local');
});

it('allows user to override rewriteSources', () => {
const customRewrite = (source: string) => source.replace(/^src\//, 'custom/');
const options = getPluginOptions({
sourcemaps: {
rewriteSources: customRewrite,
},
} as SentryNuxtModuleOptions);
expect(options.sourcemaps?.rewriteSources).toBe(customRewrite);
});

it('prioritizes new BuildTimeOptionsBase options over deprecated ones', () => {
const options: SentryNuxtModuleOptions = {
// New options
Expand Down
1 change: 1 addition & 0 deletions packages/tanstackstart-react/src/vite/sourceMaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export function makeAddSentryVitePlugin(options: BuildTimeOptionsBase): Plugin[]
assets: sourcemaps?.assets,
disable: sourcemaps?.disable,
ignore: sourcemaps?.ignore,
rewriteSources: sourcemaps?.rewriteSources,
filesToDeleteAfterUpload: filesToDeleteAfterUploadPromise,
},
telemetry: telemetry ?? true,
Expand Down
19 changes: 19 additions & 0 deletions packages/tanstackstart-react/test/vite/sourceMaps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,25 @@ describe('makeAddSentryVitePlugin()', () => {
consoleSpy.mockRestore();
});

it('passes rewriteSources to the vite plugin', () => {
const customRewrite = (source: string) => source.replace(/^src\//, '');
makeAddSentryVitePlugin({
org: 'my-org',
authToken: 'my-token',
sourcemaps: {
rewriteSources: customRewrite,
},
});

expect(sentryVitePluginSpy).toHaveBeenCalledWith(
expect.objectContaining({
sourcemaps: expect.objectContaining({
rewriteSources: customRewrite,
}),
}),
);
});

it('sets the correct metaFramework in telemetry options', () => {
makeAddSentryVitePlugin({
org: 'my-org',
Expand Down
Loading