Skip to content

Commit 3e25ccd

Browse files
committed
fix(nuxt): Keep sourcesContent in Nuxt 5 server source maps
Nitro's source map minification runs after the bundler writes the maps. On Nitro 3 it deletes sourcesContent, which overrides the module's sourcemapExcludeSources: false. Set nitro.experimental.sourcemapMinify to false so Sentry can show the server source. Fixes #25322
1 parent b9cd12a commit 3e25ccd

3 files changed

Lines changed: 17 additions & 5 deletions

File tree

‎dev-packages/e2e-tests/test-applications/nuxt-5-sourcemaps/assert-build.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,9 @@ const containsMarker = (marker: string): boolean =>
162162
// Vite builds the client and Nitro builds the server. Counting bundles would still pass with either
163163
// plugin dropped, so each side is pinned to a marker only that side's source supplies.
164164
assert.ok(containsMarker(CLIENT_MARKER), 'Expected an uploaded sourcemap carrying the client source (Vite plugin)');
165-
// TODO: Nitro 3 builds the server with Rolldown, and the uploaded server maps carry no `sourcesContent`: the
166-
// module's `sourcemapExcludeSources: false` does not reach that build. Assert the server marker when that is fixed:
167-
// assert.ok(containsMarker(SERVER_MARKER), 'Expected an uploaded sourcemap carrying the server source (Rollup plugin)');
165+
// Nitro 3 deletes `sourcesContent` from the server maps unless `experimental.sourcemapMinify` is `false`. The module
166+
// sets it to `false`, which is the only reason this marker survives into `sourcesContent`.
167+
assert.ok(containsMarker(SERVER_MARKER), 'Expected an uploaded sourcemap carrying the server source (Rollup plugin)');
168168

169169
// `rewriteSources` normalizes `../../../foo` to `./foo` so paths stay resolvable in Sentry.
170170
const unnormalizedSources = [...new Set(sourcemaps.flatMap(map => map.sourcemap.sources ?? []))].filter(

‎packages/nuxt/src/vite/sourceMaps.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ export function changeNuxtSourceMapSettings(
324324
}
325325

326326
/** Logs warnings about potentially conflicting source map settings.
327-
* Configures `sourcemapExcludeSources` in Nitro to make source maps usable in Sentry.
327+
* Configures `sourcemapExcludeSources` and `sourcemapMinify` in Nitro to make source maps usable in Sentry.
328328
*
329329
* only exported for testing
330330
*/
@@ -364,10 +364,13 @@ export function validateNitroSourceMapSettings(
364364
}
365365

366366
nitroConfig.rollupConfig.output.sourcemapExcludeSources = false;
367+
// Nitro's source map minification runs after the bundler wrote the maps: it clears `mappings` of maps with
368+
// `node_modules` sources, and Nitro 3 also deletes `sourcesContent`, which overrides `sourcemapExcludeSources`.
369+
nitroConfig.experimental = { ...nitroConfig.experimental, sourcemapMinify: false };
367370
if (isDebug) {
368371
// eslint-disable-next-line no-console
369372
console.log(
370-
'[Sentry] Set `sourcemapExcludeSources: false` in the Nuxt config (`nitro.rollupConfig.output`). Source maps will now include the actual code to be able to un-minify code snippets in Sentry.',
373+
'[Sentry] Set `sourcemapExcludeSources: false` (`nitro.rollupConfig.output`) and `sourcemapMinify: false` (`nitro.experimental`) in the Nuxt config. Source maps will now include the actual code to be able to un-minify code snippets in Sentry.',
371374
);
372375
}
373376
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ describe('validate sourcemap settings', () => {
328328
rollupConfig?: {
329329
output?: { sourcemap?: SourceMapSetting; sourcemapExcludeSources?: boolean };
330330
};
331+
experimental?: { asyncContext?: boolean; sourcemapMinify?: false };
331332
};
332333
type MinimalNuxtConfig = {
333334
options: { sourcemap?: SourceMapSetting | { server?: SourceMapSetting; client?: SourceMapSetting } };
@@ -363,6 +364,14 @@ describe('validate sourcemap settings', () => {
363364
expect(nitroConfig?.rollupConfig?.output?.sourcemapExcludeSources).toBe(false);
364365
});
365366

367+
it("should disable Nitro's source map minification and keep other experimental options", () => {
368+
const nitroConfig = { ...getNitroConfig(true), experimental: { asyncContext: true } };
369+
370+
validateNitroSourceMapSettings(getNuxtConfig(true), nitroConfig, { debug: true });
371+
372+
expect(nitroConfig.experimental).toEqual({ asyncContext: true, sourcemapMinify: false });
373+
});
374+
366375
it('should not show console.warn when rollup sourcemap is undefined', () => {
367376
const nitroConfig = getNitroConfig(true);
368377

0 commit comments

Comments
 (0)