Skip to content

Commit 4e50246

Browse files
committed
Set sourcemapexcludesources to false from vite plugin
1 parent 766eb98 commit 4e50246

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import type { BuildTimeOptionsBase } from '@sentry/core';
22
import type { Plugin } from 'vite';
33
import { makeAutoInstrumentMiddlewarePlugin } from './autoInstrumentMiddleware';
4-
import { makeAddSentryVitePlugin, makeEnableSourceMapsVitePlugin } from './sourceMaps';
4+
import {
5+
makeAddSentryVitePlugin,
6+
makeEnableSourceMapsVitePlugin,
7+
makeNitroSourcemapExcludeSourcesPlugin,
8+
} from './sourceMaps';
59

610
/**
711
* Build-time options for the Sentry TanStack Start SDK.
@@ -62,6 +66,7 @@ export function sentryTanstackStart(options: SentryTanstackStartOptions = {}): P
6266
const sourceMapsDisabled = options.sourcemaps?.disable === true || options.sourcemaps?.disable === 'disable-upload';
6367
if (!sourceMapsDisabled) {
6468
plugins.push(...makeEnableSourceMapsVitePlugin(options));
69+
plugins.push(makeNitroSourcemapExcludeSourcesPlugin(options));
6570
}
6671

6772
return plugins;

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,31 @@ export function makeEnableSourceMapsVitePlugin(options: BuildTimeOptionsBase): P
101101
];
102102
}
103103

104+
/**
105+
* A Sentry plugin for TanStack Start to set `sourcemapExcludeSources: false` in the Nitro config.
106+
*
107+
* By default, Nitro sets `sourcemapExcludeSources: true`, which means the original source code is not included
108+
* in the source maps. This makes it impossible for Sentry to display un-minified code snippets on the Issues page.
109+
*/
110+
export function makeNitroSourcemapExcludeSourcesPlugin(_options: BuildTimeOptionsBase): Plugin {
111+
return {
112+
name: 'sentry-tanstackstart-nitro-sourcemap-exclude-sources',
113+
apply: 'build',
114+
enforce: 'post',
115+
config() {
116+
return {
117+
nitro: {
118+
rollupConfig: {
119+
output: {
120+
sourcemapExcludeSources: false,
121+
},
122+
},
123+
},
124+
} as UserConfig;
125+
},
126+
};
127+
}
128+
104129
/** There are 3 ways to set up source map generation (https://github.com/getsentry/sentry-javascript/issues/13993)
105130
*
106131
* 1. User explicitly disabled source maps

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ const mockEnableSourceMapsPlugin: Plugin = {
2222
config: vi.fn(),
2323
};
2424

25+
const mockNitroSourcemapExcludeSourcesPlugin: Plugin = {
26+
name: 'sentry-tanstackstart-nitro-sourcemap-exclude-sources',
27+
apply: 'build',
28+
enforce: 'post',
29+
config: vi.fn(),
30+
};
31+
2532
const mockMiddlewarePlugin: Plugin = {
2633
name: 'sentry-tanstack-middleware-auto-instrument',
2734
apply: 'build',
@@ -31,6 +38,7 @@ const mockMiddlewarePlugin: Plugin = {
3138
vi.mock('../../src/vite/sourceMaps', () => ({
3239
makeAddSentryVitePlugin: vi.fn(() => [mockSourceMapsConfigPlugin, mockSentryVitePlugin]),
3340
makeEnableSourceMapsVitePlugin: vi.fn(() => [mockEnableSourceMapsPlugin]),
41+
makeNitroSourcemapExcludeSourcesPlugin: vi.fn(() => mockNitroSourcemapExcludeSourcesPlugin),
3442
}));
3543

3644
vi.mock('../../src/vite/autoInstrumentMiddleware', () => ({
@@ -51,7 +59,12 @@ describe('sentryTanstackStart()', () => {
5159
it('returns source maps plugins in production mode', () => {
5260
const plugins = sentryTanstackStart({ autoInstrumentMiddleware: false });
5361

54-
expect(plugins).toEqual([mockSourceMapsConfigPlugin, mockSentryVitePlugin, mockEnableSourceMapsPlugin]);
62+
expect(plugins).toEqual([
63+
mockSourceMapsConfigPlugin,
64+
mockSentryVitePlugin,
65+
mockEnableSourceMapsPlugin,
66+
mockNitroSourcemapExcludeSourcesPlugin,
67+
]);
5568
});
5669

5770
it('returns no plugins in development mode', () => {
@@ -86,7 +99,12 @@ describe('sentryTanstackStart()', () => {
8699
sourcemaps: { disable: false },
87100
});
88101

89-
expect(plugins).toEqual([mockSourceMapsConfigPlugin, mockSentryVitePlugin, mockEnableSourceMapsPlugin]);
102+
expect(plugins).toEqual([
103+
mockSourceMapsConfigPlugin,
104+
mockSentryVitePlugin,
105+
mockEnableSourceMapsPlugin,
106+
mockNitroSourcemapExcludeSourcesPlugin,
107+
]);
90108
});
91109
});
92110

0 commit comments

Comments
 (0)