Skip to content

Commit 5777d18

Browse files
committed
fix it...?
1 parent 7000bdf commit 5777d18

File tree

2 files changed

+194
-36
lines changed

2 files changed

+194
-36
lines changed

packages/sveltekit/src/vite/sourceMaps.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable max-lines */
2-
import { consoleSandbox, escapeStringForRegex, uuid4 } from '@sentry/core';
2+
import { escapeStringForRegex, uuid4 } from '@sentry/core';
33
import { getSentryRelease } from '@sentry/node';
44
import type { SentryVitePluginOptions } from '@sentry/vite-plugin';
55
import { sentryVitePlugin } from '@sentry/vite-plugin';
@@ -27,6 +27,8 @@ type Sorcery = {
2727
// and we only want to generate a uuid once in case we have to fall back to it.
2828
const releaseName = detectSentryRelease();
2929

30+
type FilesToDeleteAfterUpload = string | string[] | undefined;
31+
3032
/**
3133
* Creates a new Vite plugin that uses the unplugin-based Sentry Vite plugin to create
3234
* releases and upload source maps to Sentry.
@@ -60,6 +62,13 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug
6062
},
6163
};
6264

65+
let _resolveFilesToDeleteAfterUpload:
66+
| undefined
67+
| ((value: FilesToDeleteAfterUpload | Promise<FilesToDeleteAfterUpload>) => void);
68+
const filesToDeleteAfterUploadPromise = new Promise<FilesToDeleteAfterUpload>(resolve => {
69+
_resolveFilesToDeleteAfterUpload = resolve;
70+
});
71+
6372
const mergedOptions = {
6473
...defaultPluginOptions,
6574
...options,
@@ -69,6 +78,7 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug
6978
},
7079
sourcemaps: {
7180
...options?.sourcemaps,
81+
filesToDeleteAfterUpload: filesToDeleteAfterUploadPromise,
7282
},
7383
};
7484

@@ -153,36 +163,26 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug
153163
const filesToDeleteAfterUploadConfigPlugin: Plugin = {
154164
name: 'sentry-sveltekit-files-to-delete-after-upload-setting-plugin',
155165
apply: 'build', // only apply this plugin at build time
156-
config: async (config: UserConfig) => {
157-
const filesToDeleteAfterUpload = mergedOptions.sourcemaps?.filesToDeleteAfterUpload;
166+
config: (config: UserConfig) => {
167+
const originalFilesToDeleteAfterUpload = options?.sourcemaps?.filesToDeleteAfterUpload;
158168

159-
if (
160-
typeof filesToDeleteAfterUpload === 'undefined' &&
161-
// Only if source maps were previously not set, we update the "filesToDeleteAfterUpload" (as we override the setting with "hidden")
162-
typeof config.build?.sourcemap === 'undefined'
163-
) {
169+
if (typeof originalFilesToDeleteAfterUpload === 'undefined' && typeof config.build?.sourcemap === 'undefined') {
164170
// Including all hidden (`.*`) directories by default so that folders like .vercel,
165171
// .netlify, etc are also cleaned up. Additionally, we include the adapter output
166172
// dir which could be a non-hidden directory, like `build` for the Node adapter.
167173
const defaultFileDeletionGlob = ['./.*/**/*.map', `./${adapterOutputDir}/**/*.map`];
168174

169-
consoleSandbox(() => {
170-
debug &&
171-
// eslint-disable-next-line no-console
172-
console.info(
173-
`[Sentry] Automatically setting \`sourceMapsUploadOptions.sourcemaps.filesToDeleteAfterUpload: [${defaultFileDeletionGlob
174-
.map(file => `"${file}"`)
175-
.join(', ')}]\` to delete generated source maps after they were uploaded to Sentry.`,
176-
);
177-
});
175+
debug &&
176+
// eslint-disable-next-line no-console
177+
console.info(
178+
`[Sentry] Automatically setting \`sourceMapsUploadOptions.sourcemaps.filesToDeleteAfterUpload: [${defaultFileDeletionGlob
179+
.map(file => `"${file}"`)
180+
.join(', ')}]\` to delete generated source maps after they were uploaded to Sentry.`,
181+
);
178182

179-
return {
180-
...config,
181-
build: {
182-
...config.build,
183-
filesToDeleteAfterUpload: defaultFileDeletionGlob,
184-
},
185-
};
183+
_resolveFilesToDeleteAfterUpload?.(defaultFileDeletionGlob);
184+
} else {
185+
_resolveFilesToDeleteAfterUpload?.(originalFilesToDeleteAfterUpload);
186186
}
187187

188188
return config;

packages/sveltekit/test/vite/sourceMaps.test.ts

Lines changed: 170 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import type { Plugin } from 'vite';
22
import * as vite from 'vite';
33
import { beforeEach, describe, expect, it, vi } from 'vitest';
44
import { _getUpdatedSourceMapSettings, makeCustomSentryVitePlugins } from '../../src/vite/sourceMaps';
5+
import { ViteUserConfig } from 'vitest/config';
6+
import { sentryVitePlugin } from '@sentry/vite-plugin';
57

68
const mockedViteDebugIdUploadPlugin = {
79
name: 'sentry-vite-debug-id-upload-plugin',
@@ -18,25 +20,21 @@ const mockedFileDeletionPlugin = {
1820
writeBundle: vi.fn(),
1921
};
2022

21-
vi.mock('vite', async () => {
22-
const original = (await vi.importActual('vite')) as any;
23+
vi.mock('@sentry/vite-plugin', async () => {
24+
const original = (await vi.importActual('@sentry/vite-plugin')) as any;
2325

2426
return {
2527
...original,
26-
loadConfigFromFile: vi.fn(),
28+
sentryVitePlugin: vi.fn(),
2729
};
2830
});
2931

30-
vi.mock('@sentry/vite-plugin', async () => {
31-
const original = (await vi.importActual('@sentry/vite-plugin')) as any;
32+
vi.mock('vite', async () => {
33+
const original = (await vi.importActual('vite')) as any;
3234

3335
return {
3436
...original,
35-
sentryVitePlugin: () => [
36-
mockedViteReleaseManagementPlugin,
37-
mockedViteDebugIdUploadPlugin,
38-
mockedFileDeletionPlugin,
39-
],
37+
loadConfigFromFile: vi.fn(),
4038
};
4139
});
4240

@@ -65,6 +63,15 @@ async function getSentryViteSubPlugin(name: string): Promise<Plugin | undefined>
6563
}
6664

6765
describe('makeCustomSentryVitePlugins()', () => {
66+
beforeEach(() => {
67+
// @ts-expect-error - this function exists!
68+
sentryVitePlugin.mockReturnValue([
69+
mockedViteReleaseManagementPlugin,
70+
mockedViteDebugIdUploadPlugin,
71+
mockedFileDeletionPlugin,
72+
]);
73+
});
74+
6875
it('returns the custom sentry source maps plugin', async () => {
6976
const plugin = await getSentryViteSubPlugin('sentry-sveltekit-debug-id-upload-plugin');
7077

@@ -87,6 +94,7 @@ describe('makeCustomSentryVitePlugins()', () => {
8794
// @ts-expect-error - this global variable is set/accessed in src/vite/sourceMaps.ts
8895
globalThis._sentry_sourceMapSetting = undefined;
8996
});
97+
9098
it('returns the custom sentry source maps plugin', async () => {
9199
const plugin = await getSentryViteSubPlugin('sentry-sveltekit-update-source-map-setting-plugin');
92100

@@ -117,6 +125,8 @@ describe('makeCustomSentryVitePlugins()', () => {
117125
});
118126

119127
it('keeps source map generation settings when previously disabled', async () => {
128+
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementationOnce(() => {});
129+
120130
const originalConfig = {
121131
build: { sourcemap: false, assetsDir: 'assets' },
122132
};
@@ -138,6 +148,10 @@ describe('makeCustomSentryVitePlugins()', () => {
138148
sourcemap: false,
139149
},
140150
});
151+
152+
expect(consoleWarnSpy).toHaveBeenCalledWith(
153+
'[Sentry] Source map generation is disabled in your Vite configuration.',
154+
);
141155
});
142156

143157
it('enables source map generation with "hidden" when unset', async () => {
@@ -201,8 +215,8 @@ describe('makeCustomSentryVitePlugins()', () => {
201215
throw new Error('test error');
202216
});
203217

204-
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementationOnce(() => {});
205-
const consoleLogSpy = vi.spyOn(console, 'log').mockImplementationOnce(() => {});
218+
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
219+
const consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
206220

207221
const plugin = await getSentryViteSubPlugin('sentry-sveltekit-debug-id-upload-plugin');
208222

@@ -395,3 +409,147 @@ describe('_getUpdatedSourceMapSettings', () => {
395409
});
396410
});
397411
});
412+
413+
describe('deleteFilesAfterUpload', () => {
414+
it('works with defauts', async () => {
415+
const viteConfig: ViteUserConfig = {};
416+
417+
vi.mock('@sentry/vite-plugin', async () => {
418+
const original = (await vi.importActual('@sentry/vite-plugin')) as any;
419+
420+
return {
421+
...original,
422+
sentryVitePlugin: vi.fn(original.sentryVitePlugin),
423+
};
424+
});
425+
426+
const plugins = await makeCustomSentryVitePlugins({
427+
authToken: 'token',
428+
org: 'org',
429+
project: 'project',
430+
adapter: 'other',
431+
});
432+
433+
// @ts-expect-error this function exists!
434+
const mergedOptions = sentryVitePlugin.mock.calls[0][0];
435+
436+
expect(mergedOptions).toEqual({
437+
_metaOptions: {
438+
telemetry: {
439+
metaFramework: 'sveltekit',
440+
},
441+
},
442+
authToken: 'token',
443+
org: 'org',
444+
project: 'project',
445+
adapter: 'other',
446+
release: {
447+
name: expect.any(String),
448+
},
449+
sourcemaps: {
450+
filesToDeleteAfterUpload: expect.any(Promise),
451+
},
452+
});
453+
454+
const sourceMapSettingPlugin = plugins.find(
455+
plugin => plugin.name === 'sentry-sveltekit-update-source-map-setting-plugin',
456+
)!;
457+
458+
// @ts-expect-error this function exists!
459+
const sourceMapSettingConfig = await sourceMapSettingPlugin.config(viteConfig);
460+
expect(sourceMapSettingConfig).toEqual({ build: { sourcemap: 'hidden' } });
461+
462+
const filesToDeleteAfterUploadSettingPlugin = plugins.find(
463+
plugin => plugin.name === 'sentry-sveltekit-files-to-delete-after-upload-setting-plugin',
464+
)!;
465+
466+
// call this to ensure the filesToDeleteAfterUpload setting is resolved
467+
// @ts-expect-error this function exists!
468+
await filesToDeleteAfterUploadSettingPlugin.config(viteConfig);
469+
470+
await expect(mergedOptions.sourcemaps.filesToDeleteAfterUpload).resolves.toEqual([
471+
'./.*/**/*.map',
472+
'./.svelte-kit/output/**/*.map',
473+
]);
474+
});
475+
476+
it.each([
477+
[['blub/'], undefined, 'hidden', ['blub/']],
478+
[['blub/'], false, false, ['blub/']],
479+
[undefined, 'hidden' as const, 'hidden', undefined],
480+
[undefined, false, false, undefined],
481+
[undefined, true, true, undefined],
482+
[['/blub/'], true, true, ['/blub/']],
483+
])(
484+
'works with filesToDeleteAfterUpload: %j & sourcemap: %s',
485+
async (filesToDeleteAfterUpload, sourcemap, sourcemapExpected, filesToDeleteAfterUploadExpected) => {
486+
const viteConfig: ViteUserConfig = {
487+
build: {
488+
sourcemap,
489+
},
490+
};
491+
492+
vi.mock('@sentry/vite-plugin', async () => {
493+
const original = (await vi.importActual('@sentry/vite-plugin')) as any;
494+
495+
return {
496+
...original,
497+
sentryVitePlugin: vi.fn(original.sentryVitePlugin),
498+
};
499+
});
500+
501+
const plugins = await makeCustomSentryVitePlugins({
502+
authToken: 'token',
503+
org: 'org',
504+
project: 'project',
505+
adapter: 'other',
506+
sourcemaps: {
507+
filesToDeleteAfterUpload,
508+
},
509+
});
510+
511+
// @ts-expect-error this function exists!
512+
const mergedOptions = sentryVitePlugin.mock.calls[0][0];
513+
514+
expect(mergedOptions).toEqual({
515+
_metaOptions: {
516+
telemetry: {
517+
metaFramework: 'sveltekit',
518+
},
519+
},
520+
authToken: 'token',
521+
org: 'org',
522+
project: 'project',
523+
adapter: 'other',
524+
release: {
525+
name: expect.any(String),
526+
},
527+
sourcemaps: {
528+
filesToDeleteAfterUpload: expect.any(Promise),
529+
},
530+
});
531+
532+
const sourceMapSettingPlugin = plugins.find(
533+
plugin => plugin.name === 'sentry-sveltekit-update-source-map-setting-plugin',
534+
)!;
535+
536+
// @ts-expect-error this function exists!
537+
const sourceMapSettingConfig = await sourceMapSettingPlugin.config(viteConfig);
538+
expect(sourceMapSettingConfig).toEqual({ build: { sourcemap: sourcemapExpected } });
539+
540+
const filesToDeleteAfterUploadSettingPlugin = plugins.find(
541+
plugin => plugin.name === 'sentry-sveltekit-files-to-delete-after-upload-setting-plugin',
542+
)!;
543+
544+
console.log(filesToDeleteAfterUploadSettingPlugin);
545+
546+
// call this to ensure the filesToDeleteAfterUpload setting is resolved
547+
// @ts-expect-error this function exists!
548+
await filesToDeleteAfterUploadSettingPlugin.config(viteConfig);
549+
550+
await expect(mergedOptions.sourcemaps.filesToDeleteAfterUpload).resolves.toEqual(
551+
filesToDeleteAfterUploadExpected,
552+
);
553+
},
554+
);
555+
});

0 commit comments

Comments
 (0)