Skip to content

Commit 4e12f13

Browse files
committed
fix overwrite object
1 parent cc11b08 commit 4e12f13

2 files changed

Lines changed: 67 additions & 8 deletions

File tree

packages/nuxt/src/vite/sourceMaps.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function setupSourceMaps(
3838
(sourceMapsUploadOptions.enabled ?? true);
3939

4040
// In case we overwrite the source map settings, we default to deleting the files
41-
let shouldDeleteFilesFallback = { client: true, server: true };
41+
const shouldDeleteFilesFallback = { client: true, server: true };
4242

4343
nuxt.hook('modules:done', () => {
4444
if (sourceMapsEnabled && !nuxt.options.dev && !nuxt.options?._prepare) {
@@ -47,13 +47,12 @@ export function setupSourceMaps(
4747
// - for server to viteConfig.build.sourceMap and nitro.sourceMap
4848
// On server, nitro.rollupConfig.output.sourcemap remains unaffected from this change.
4949

50-
// ONLY THIS nuxt.sourcemap.(server/client) setting is the one Sentry will eventually overwrite with 'hidden'
50+
// ONLY THIS nuxt.sourcemap.(server/client) setting is the one Sentry will overwrite with 'hidden', if needed.
5151
const previousSourceMapSettings = changeNuxtSourceMapSettings(nuxt, moduleOptions);
5252

53-
shouldDeleteFilesFallback = {
54-
client: previousSourceMapSettings.client === 'unset',
55-
server: previousSourceMapSettings.server === 'unset',
56-
};
53+
// Mutate in place so the Vite plugin (which captured this object at registration time) sees the updated values
54+
shouldDeleteFilesFallback.client = previousSourceMapSettings.client === 'unset';
55+
shouldDeleteFilesFallback.server = previousSourceMapSettings.server === 'unset';
5756

5857
if (isDebug && (shouldDeleteFilesFallback.client || shouldDeleteFilesFallback.server)) {
5958
const enabledDeleteFallbacks =

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

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,17 @@ describe('setupSourceMaps hooks', () => {
7676
});
7777

7878
describe('vite plugin registration', () => {
79-
it('registers a vite plugin after modules:done hook', async () => {
79+
it('calls `addVitePlugin` when setupSourceMaps is called', async () => {
8080
const { setupSourceMaps } = await import('../../src/vite/sourceMaps');
8181
const mockNuxt = createMockNuxt({ _prepare: false, dev: false });
8282
const { mockAddVitePlugin, getCapturedPlugin } = createMockAddVitePlugin();
8383

8484
setupSourceMaps({ debug: true }, mockNuxt as unknown as Nuxt, mockAddVitePlugin);
85-
await mockNuxt.triggerHook('modules:done');
8685

8786
const plugin = getCapturedPlugin();
8887
expect(plugin).not.toBeNull();
8988
expect(plugin?.name).toBe('sentry-nuxt-vite-config');
89+
// modules:done is called afterward. Later, the plugin is actually added
9090
});
9191

9292
it.each([
@@ -179,6 +179,66 @@ describe('setupSourceMaps hooks', () => {
179179
});
180180
});
181181

182+
describe('shouldDeleteFilesFallback passed to getPluginOptions in Vite plugin', () => {
183+
const defaultFilesToDeleteAfterUpload = [
184+
'.*/**/public/**/*.map',
185+
'.*/**/server/**/*.map',
186+
'.*/**/output/**/*.map',
187+
'.*/**/function/**/*.map',
188+
];
189+
190+
it('uses mutated shouldDeleteFilesFallback (unset → true): plugin.config() after modules:done gets fallback filesToDeleteAfterUpload', async () => {
191+
const { setupSourceMaps } = await import('../../src/vite/sourceMaps');
192+
const mockNuxt = createMockNuxt({
193+
_prepare: false,
194+
dev: false,
195+
sourcemap: { client: undefined, server: undefined },
196+
});
197+
const { mockAddVitePlugin, getCapturedPlugin } = createMockAddVitePlugin();
198+
199+
setupSourceMaps({ debug: false }, mockNuxt as unknown as Nuxt, mockAddVitePlugin);
200+
await mockNuxt.triggerHook('modules:done');
201+
202+
const plugin = getCapturedPlugin();
203+
expect(plugin).not.toBeNull();
204+
if (plugin && typeof plugin.config === 'function') {
205+
plugin.config({ build: { ssr: false }, plugins: [] }, { mode: 'production', command: 'build' });
206+
}
207+
208+
expect(mockSentryVitePlugin).toHaveBeenCalledWith(
209+
expect.objectContaining({
210+
sourcemaps: expect.objectContaining({
211+
filesToDeleteAfterUpload: defaultFilesToDeleteAfterUpload,
212+
}),
213+
}),
214+
);
215+
});
216+
217+
it('uses mutated shouldDeleteFilesFallback (explicitly enabled → false): plugin.config() after modules:done gets no filesToDeleteAfterUpload', async () => {
218+
const { setupSourceMaps } = await import('../../src/vite/sourceMaps');
219+
const mockNuxt = createMockNuxt({
220+
_prepare: false,
221+
dev: false,
222+
sourcemap: { client: true, server: true },
223+
});
224+
const { mockAddVitePlugin, getCapturedPlugin } = createMockAddVitePlugin();
225+
226+
setupSourceMaps({ debug: false }, mockNuxt as unknown as Nuxt, mockAddVitePlugin);
227+
await mockNuxt.triggerHook('modules:done');
228+
229+
const plugin = getCapturedPlugin();
230+
expect(plugin).not.toBeNull();
231+
if (plugin && typeof plugin.config === 'function') {
232+
plugin.config({ build: { ssr: false }, plugins: [] }, { mode: 'production', command: 'build' });
233+
}
234+
235+
const pluginOptions = (mockSentryVitePlugin?.mock?.calls?.[0] as unknown[])?.[0] as {
236+
sourcemaps?: { filesToDeleteAfterUpload?: string[] };
237+
};
238+
expect(pluginOptions?.sourcemaps?.filesToDeleteAfterUpload).toBeUndefined();
239+
});
240+
});
241+
182242
describe('nitro:config hook', () => {
183243
it('adds sentryRollupPlugin to nitro rollup config in production mode', async () => {
184244
const { setupSourceMaps } = await import('../../src/vite/sourceMaps');

0 commit comments

Comments
 (0)