Skip to content

Commit 4126c26

Browse files
committed
use new api in types
1 parent 8c86faf commit 4126c26

File tree

8 files changed

+80
-2
lines changed

8 files changed

+80
-2
lines changed

packages/core/src/build-time-plugins/buildTimeOptionsBase.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,19 @@ interface SourceMapsOptions {
241241
* The globbing patterns must follow the implementation of the `glob` package: https://www.npmjs.com/package/glob#glob-primer
242242
*/
243243
filesToDeleteAfterUpload?: string | Array<string>;
244+
245+
/**
246+
* Hook to rewrite the `sources` field inside the source map before being uploaded to Sentry. Does not modify the actual source map.
247+
*
248+
* The hook receives the following arguments:
249+
* - `source` - the source file path from the source map's `sources` field
250+
* - `map` - the source map object
251+
* - `context` - an optional object containing `mapDir`, the absolute path to the directory of the source map file
252+
*
253+
* Defaults to making all sources relative to `process.cwd()` while building.
254+
*/
255+
// oxlint-disable-next-line typescript-eslint/no-explicit-any -- matches the bundler plugin's RewriteSourcesHook type
256+
rewriteSources?: (source: string, map: any, context?: { mapDir: string }) => string;
244257
}
245258

246259
type AutoSetCommitsOptions = {

packages/nextjs/src/config/getBuildPluginOptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ export function getBuildPluginOptions({
326326
url: sentryBuildOptions.sentryUrl,
327327
sourcemaps: {
328328
disable: skipSourcemapsUpload ? true : (sentryBuildOptions.sourcemaps?.disable ?? false),
329-
rewriteSources: rewriteWebpackSources,
329+
rewriteSources: sentryBuildOptions.sourcemaps?.rewriteSources ?? rewriteWebpackSources,
330330
assets: sentryBuildOptions.sourcemaps?.assets ?? sourcemapUploadAssets,
331331
ignore: finalIgnorePatterns,
332332
filesToDeleteAfterUpload,

packages/nextjs/src/config/types.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,21 @@ export type SentryBuildOptions = {
294294
* ```
295295
*/
296296
filesToDeleteAfterUpload?: string | string[];
297+
298+
/**
299+
* Hook to rewrite the `sources` field inside the source map before being uploaded to Sentry. Does not modify the actual source map.
300+
*
301+
* The hook receives the following arguments:
302+
* - `source` - the source file path from the source map's `sources` field
303+
* - `map` - the source map object
304+
* - `context` - an optional object containing `mapDir`, the absolute path to the directory of the source map file
305+
*
306+
* If not provided, the SDK defaults to stripping webpack-specific prefixes (`webpack://_N_E/`).
307+
*
308+
* Defaults to making all sources relative to `process.cwd()` while building.
309+
*/
310+
// oxlint-disable-next-line typescript-eslint/no-explicit-any -- matches the bundler plugin's RewriteSourcesHook type
311+
rewriteSources?: (source: string, map: any, context?: { mapDir: string }) => string;
297312
};
298313

299314
/**

packages/nextjs/test/config/getBuildPluginOptions.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,26 @@ describe('getBuildPluginOptions', () => {
692692
expect(rewriteSources('./components/Layout.tsx', {})).toBe('./components/Layout.tsx');
693693
}
694694
});
695+
696+
it('allows user to override rewriteSources', () => {
697+
const customRewrite = (source: string) => source.replace(/^custom\//, '');
698+
const sentryBuildOptions: SentryBuildOptions = {
699+
org: 'test-org',
700+
project: 'test-project',
701+
sourcemaps: {
702+
rewriteSources: customRewrite,
703+
},
704+
};
705+
706+
const result = getBuildPluginOptions({
707+
sentryBuildOptions,
708+
releaseName: mockReleaseName,
709+
distDirAbsPath: mockDistDirAbsPath,
710+
buildTool: 'webpack-client',
711+
});
712+
713+
expect(result.sourcemaps?.rewriteSources).toBe(customRewrite);
714+
});
695715
});
696716

697717
describe('release configuration', () => {

packages/nuxt/src/vite/sourceMaps.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ export function getPluginOptions(
216216
: shouldDeleteFilesFallback?.server || shouldDeleteFilesFallback?.client
217217
? fallbackFilesToDelete
218218
: undefined,
219-
rewriteSources: (source: string) => normalizePath(source),
219+
rewriteSources: sourcemapsOptions.rewriteSources ?? ((source: string) => normalizePath(source)),
220220
...moduleOptions?.unstable_sentryBundlerPluginOptions?.sourcemaps,
221221
},
222222
};

packages/nuxt/test/vite/sourceMaps.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,16 @@ describe('getPluginOptions', () => {
118118
expect(rewrite!('./local')).toBe('./local');
119119
});
120120

121+
it('allows user to override rewriteSources', () => {
122+
const customRewrite = (source: string) => source.replace(/^src\//, 'custom/');
123+
const options = getPluginOptions({
124+
sourcemaps: {
125+
rewriteSources: customRewrite,
126+
},
127+
} as SentryNuxtModuleOptions);
128+
expect(options.sourcemaps?.rewriteSources).toBe(customRewrite);
129+
});
130+
121131
it('prioritizes new BuildTimeOptionsBase options over deprecated ones', () => {
122132
const options: SentryNuxtModuleOptions = {
123133
// New options

packages/tanstackstart-react/src/vite/sourceMaps.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export function makeAddSentryVitePlugin(options: BuildTimeOptionsBase): Plugin[]
6565
assets: sourcemaps?.assets,
6666
disable: sourcemaps?.disable,
6767
ignore: sourcemaps?.ignore,
68+
rewriteSources: sourcemaps?.rewriteSources,
6869
filesToDeleteAfterUpload: filesToDeleteAfterUploadPromise,
6970
},
7071
telemetry: telemetry ?? true,

packages/tanstackstart-react/test/vite/sourceMaps.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,25 @@ describe('makeAddSentryVitePlugin()', () => {
195195
consoleSpy.mockRestore();
196196
});
197197

198+
it('passes rewriteSources to the vite plugin', () => {
199+
const customRewrite = (source: string) => source.replace(/^src\//, '');
200+
makeAddSentryVitePlugin({
201+
org: 'my-org',
202+
authToken: 'my-token',
203+
sourcemaps: {
204+
rewriteSources: customRewrite,
205+
},
206+
});
207+
208+
expect(sentryVitePluginSpy).toHaveBeenCalledWith(
209+
expect.objectContaining({
210+
sourcemaps: expect.objectContaining({
211+
rewriteSources: customRewrite,
212+
}),
213+
}),
214+
);
215+
});
216+
198217
it('sets the correct metaFramework in telemetry options', () => {
199218
makeAddSentryVitePlugin({
200219
org: 'my-org',

0 commit comments

Comments
 (0)