Skip to content

Commit afc53f6

Browse files
committed
updated sourcemap generation settings
1 parent 55e9775 commit afc53f6

File tree

3 files changed

+162
-102
lines changed

3 files changed

+162
-102
lines changed

packages/solidstart/src/vite/sourceMaps.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function makeAddSentryVitePlugin(options: SentrySolidStartPluginOptions,
1818
// Only if source maps were previously not set, we update the "filesToDeleteAfterUpload" (as we override the setting with "hidden")
1919
typeof viteConfig.build?.sourcemap === 'undefined'
2020
) {
21-
// This also works for adapters, as the source maps are also copied to e.g. the .vercel folder
21+
// For .output, .vercel, .netlify etc.
2222
updatedFilesToDeleteAfterUpload = ['.*/**/*.map'];
2323

2424
consoleSandbox(() => {

packages/sveltekit/src/vite/sourceMaps.ts

+96-47
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
/* eslint-disable max-lines */
12
import * as child_process from 'child_process';
23
import * as fs from 'fs';
34
import * as path from 'path';
45
import { consoleSandbox, escapeStringForRegex, uuid4 } from '@sentry/core';
56
import { getSentryRelease } from '@sentry/node';
67
import type { SentryVitePluginOptions } from '@sentry/vite-plugin';
78
import { sentryVitePlugin } from '@sentry/vite-plugin';
8-
import type { Plugin, UserConfig } from 'vite';
9+
import { type Plugin, type UserConfig, loadConfigFromFile } from 'vite';
910

1011
import MagicString from 'magic-string';
1112
import { WRAPPED_MODULE_SUFFIX } from './autoInstrument';
@@ -27,6 +28,18 @@ type Sorcery = {
2728
// and we only want to generate a uuid once in case we have to fall back to it.
2829
const releaseName = detectSentryRelease();
2930

31+
let sourceMapSetting: {
32+
updatedSourceMapSetting?: boolean | 'inline' | 'hidden';
33+
previousSourceMapSetting?: UserSourceMapSetting;
34+
} = { previousSourceMapSetting: undefined, updatedSourceMapSetting: undefined };
35+
36+
/**
37+
* For mocking the value of `sourceMapSetting` in tests
38+
*/
39+
export const __setSourceMapSettingForTest = (value: typeof sourceMapSetting): void => {
40+
sourceMapSetting = value;
41+
};
42+
3043
/**
3144
* Creates a new Vite plugin that uses the unplugin-based Sentry Vite plugin to create
3245
* releases and upload source maps to Sentry.
@@ -60,14 +73,50 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug
6073
},
6174
};
6275

76+
const filesToDeleteGlob = './.*/**/*.map';
77+
78+
if (sourceMapSetting.updatedSourceMapSetting === undefined) {
79+
const configFile = await loadConfigFromFile({ command: 'build', mode: 'production' });
80+
81+
if (configFile) {
82+
sourceMapSetting = getUpdatedSourceMapSetting(configFile.config);
83+
} else {
84+
if (options?.debug) {
85+
consoleSandbox(() => {
86+
// eslint-disable-next-line no-console
87+
console.warn(
88+
'[Source Maps Plugin] Could not load Vite config with Vite "production" mode. This is needed for Sentry to automatically update source map settings.',
89+
);
90+
});
91+
}
92+
}
93+
94+
if (options?.debug && sourceMapSetting.previousSourceMapSetting === 'unset') {
95+
consoleSandbox(() => {
96+
// eslint-disable-next-line no-console
97+
console.warn(
98+
`[Source Maps Plugin] Automatically setting \`sourceMapsUploadOptions.sourcemaps.filesToDeleteAfterUpload: ["${filesToDeleteGlob}"]\` to delete generated source maps after they were uploaded to Sentry.`,
99+
);
100+
});
101+
}
102+
}
103+
63104
const mergedOptions = {
64105
...defaultPluginOptions,
65106
...options,
66107
release: {
67108
...defaultPluginOptions.release,
68109
...options?.release,
69110
},
111+
sourcemaps: {
112+
...options?.sourcemaps,
113+
filesToDeleteAfterUpload:
114+
sourceMapSetting.previousSourceMapSetting === 'unset' && !options?.sourcemaps?.filesToDeleteAfterUpload
115+
? ['./.*/**/*.map']
116+
: options?.sourcemaps?.filesToDeleteAfterUpload,
117+
},
70118
};
119+
71120
const { debug } = mergedOptions;
72121

73122
const sentryPlugins: Plugin[] = await sentryVitePlugin(mergedOptions);
@@ -129,26 +178,48 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug
129178
__sentry_sveltekit_output_dir: outputDir,
130179
};
131180

132-
const customDebugIdUploadPlugin: Plugin = {
133-
name: 'sentry-sveltekit-debug-id-upload-plugin',
181+
const sourceMapSettingsPlugin: Plugin = {
182+
name: 'sentry-sveltekit-update-source-map-setting-plugin',
134183
apply: 'build', // only apply this plugin at build time
135-
enforce: 'post', // this needs to be set to post, otherwise we don't pick up the output from the SvelteKit adapter
136-
137-
// Modify the config to generate source maps
138184
config: (config: UserConfig) => {
139-
changeViteSourceMapSettings(config, options);
185+
const settingKey = 'build.sourcemap';
140186

141-
if (debug && !mergedOptions.sourcemaps?.filesToDeleteAfterUpload) {
187+
if (sourceMapSetting.previousSourceMapSetting === 'unset') {
142188
consoleSandbox(() => {
143-
// eslint-disable-next-line no-console
189+
// eslint-disable-next-line no-console
190+
console.log(`[Sentry] Enabled source map generation in the build options with \`${settingKey}: "hidden"\`.`);
191+
});
192+
193+
return {
194+
...config,
195+
build: { ...config.build, sourcemap: 'hidden' },
196+
};
197+
} else if (sourceMapSetting.previousSourceMapSetting === 'disabled') {
198+
consoleSandbox(() => {
199+
// eslint-disable-next-line no-console
144200
console.warn(
145-
'[Source Maps Plugin] We recommend setting the `sourceMapsUploadOptions.sourcemaps.filesToDeleteAfterUpload` option to clean up source maps after uploading. Otherwise, source maps might be deployed to production, depending on your configuration',
201+
`[Sentry] Parts of source map generation are currently disabled in your Vite configuration (\`${settingKey}: false\`). This setting is either a default setting or was explicitly set in your configuration. Sentry won't override this setting. Without source maps, code snippets on the Sentry Issues page will remain minified. To show unminified code, enable source maps in \`${settingKey}\` (e.g. by setting them to \`hidden\`).`,
146202
);
147203
});
204+
} else if (sourceMapSetting.previousSourceMapSetting === 'enabled') {
205+
if (mergedOptions?.debug) {
206+
consoleSandbox(() => {
207+
// eslint-disable-next-line no-console
208+
console.log(
209+
`[Sentry] We discovered you enabled source map generation in your Vite configuration (\`${settingKey}\`). Sentry will keep this source map setting. This will un-minify the code snippet on the Sentry Issue page.`,
210+
);
211+
});
212+
}
148213
}
214+
149215
return config;
150216
},
217+
};
151218

219+
const customDebugIdUploadPlugin: Plugin = {
220+
name: 'sentry-sveltekit-debug-id-upload-plugin',
221+
apply: 'build', // only apply this plugin at build time
222+
enforce: 'post', // this needs to be set to post, otherwise we don't pick up the output from the SvelteKit adapter
152223
resolveId: (id, _importer, _ref) => {
153224
if (id === VIRTUAL_GLOBAL_VALUES_FILE) {
154225
return {
@@ -318,6 +389,7 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug
318389

319390
return [
320391
...unchangedSentryVitePlugins,
392+
sourceMapSettingsPlugin,
321393
customReleaseManagementPlugin,
322394
customDebugIdUploadPlugin,
323395
customFileDeletionPlugin,
@@ -344,53 +416,30 @@ export type UserSourceMapSetting = 'enabled' | 'disabled' | 'unset' | undefined;
344416
*
345417
* --> only exported for testing
346418
*/
347-
export function changeViteSourceMapSettings(
348-
viteConfig: {
349-
build?: {
350-
sourcemap?: boolean | 'inline' | 'hidden';
351-
};
352-
},
353-
sentryPluginOptions?: CustomSentryVitePluginOptions,
354-
): UserSourceMapSetting {
355-
let previousUserSourceMapSetting: UserSourceMapSetting = undefined;
419+
export function getUpdatedSourceMapSetting(viteConfig: {
420+
build?: {
421+
sourcemap?: boolean | 'inline' | 'hidden';
422+
};
423+
}): { updatedSourceMapSetting: boolean | 'inline' | 'hidden'; previousSourceMapSetting: UserSourceMapSetting } {
424+
let previousSourceMapSetting: UserSourceMapSetting;
425+
let updatedSourceMapSetting: boolean | 'inline' | 'hidden' | undefined;
356426

357427
viteConfig.build = viteConfig.build || {};
358428

359429
const viteSourceMap = viteConfig.build.sourcemap;
360430

361-
const settingKey = 'vite.build.sourcemap';
362-
363431
if (viteSourceMap === false) {
364-
previousUserSourceMapSetting = 'disabled';
365-
366-
consoleSandbox(() => {
367-
// eslint-disable-next-line no-console
368-
console.warn(
369-
`[Sentry] Parts of source map generation are currently disabled in your Vite configuration (\`${settingKey}: false\`). This setting is either a default setting or was explicitly set in your configuration. Sentry won't override this setting. Without source maps, code snippets on the Sentry Issues page will remain minified. To show unminified code, enable source maps in \`${settingKey}\` (e.g. by setting them to \`hidden\`).`,
370-
);
371-
});
432+
previousSourceMapSetting = 'disabled';
433+
updatedSourceMapSetting = viteSourceMap;
372434
} else if (viteSourceMap && ['hidden', 'inline', true].includes(viteSourceMap)) {
373-
previousUserSourceMapSetting = 'enabled';
374-
375-
if (sentryPluginOptions?.debug) {
376-
consoleSandbox(() => {
377-
// eslint-disable-next-line no-console
378-
console.log(
379-
`[Sentry] We discovered \`${settingKey}\` is set to \`${viteSourceMap.toString()}\`. Sentry will keep this source map setting. This will un-minify the code snippet on the Sentry Issue page.`,
380-
);
381-
});
382-
}
435+
previousSourceMapSetting = 'enabled';
436+
updatedSourceMapSetting = viteSourceMap;
383437
} else {
384-
previousUserSourceMapSetting = 'unset';
385-
viteConfig.build.sourcemap = 'hidden';
386-
387-
consoleSandbox(() => {
388-
// eslint-disable-next-line no-console
389-
console.log(`[Sentry] Enabled source map generation in the build options with \`${settingKey}: 'hidden'\`.`);
390-
});
438+
previousSourceMapSetting = 'unset';
439+
updatedSourceMapSetting = 'hidden';
391440
}
392441

393-
return previousUserSourceMapSetting;
442+
return { previousSourceMapSetting, updatedSourceMapSetting };
394443
}
395444

396445
function getFiles(dir: string): string[] {

0 commit comments

Comments
 (0)