Skip to content

Commit 957878a

Browse files
authored
feat(solidstart): Respect user-provided source map setting (#14979)
Closes #13994
1 parent 360cadb commit 957878a

File tree

8 files changed

+310
-88
lines changed

8 files changed

+310
-88
lines changed

docs/migration/v8-to-v9.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ In v9, an `undefined` value will be treated the same as if the value is not defi
104104

105105
- By default, source maps will now be automatically deleted after being uploaded to Sentry for client-side builds. You can opt out of this behavior by explicitly setting `sourcemaps.deleteSourcemapsAfterUpload` to `false` in your Sentry config.
106106

107-
### All Meta-Framework SDKs (`@sentry/astro`, `@sentry/nuxt`)
107+
### All Meta-Framework SDKs (`@sentry/astro`, `@sentry/nuxt`, `@sentry/solidstart`)
108108

109109
- Updated source map generation to respect the user-provided value of your build config, such as `vite.build.sourcemap`:
110110

packages/astro/src/integration/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => {
4949
consoleSandbox(() => {
5050
// eslint-disable-next-line no-console
5151
console.log(
52-
`[Sentry] Setting \`sourceMapsUploadOptions.filesToDeleteAfterUpload: ${JSON.stringify(
52+
`[Sentry] Automatically setting \`sourceMapsUploadOptions.filesToDeleteAfterUpload: ${JSON.stringify(
5353
updatedFilesToDeleteAfterUpload,
5454
)}\` to delete generated source maps after they were uploaded to Sentry.`,
5555
);
@@ -226,7 +226,7 @@ export function getUpdatedSourceMapSettings(
226226
consoleSandbox(() => {
227227
// eslint-disable-next-line no-console
228228
console.warn(
229-
`[Sentry] Source map generation are currently disabled in your Astro 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\`).`,
229+
`[Sentry] Source map generation is currently disabled in your Astro 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\`).`,
230230
);
231231
});
232232
} else if (viteSourceMap && ['hidden', 'inline', true].includes(viteSourceMap)) {

packages/astro/test/integration/index.test.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -456,9 +456,7 @@ describe('getUpdatedSourceMapSettings', () => {
456456

457457
astroConfig.vite.build.sourcemap = false;
458458
getUpdatedSourceMapSettings(astroConfig, sentryOptions);
459-
expect(consoleWarnSpy).toHaveBeenCalledWith(
460-
expect.stringContaining('Source map generation are currently disabled'),
461-
);
459+
expect(consoleWarnSpy).toHaveBeenCalledWith(expect.stringContaining('Source map generation is currently disabled'));
462460

463461
astroConfig.vite.build.sourcemap = 'hidden';
464462
getUpdatedSourceMapSettings(astroConfig, sentryOptions);

packages/solidstart/src/vite/sentrySolidStartVite.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import type { Plugin, UserConfig } from 'vite';
22
import { makeBuildInstrumentationFilePlugin } from './buildInstrumentationFile';
3-
import { makeSourceMapsVitePlugin } from './sourceMaps';
3+
import { makeAddSentryVitePlugin, makeEnableSourceMapsVitePlugin } from './sourceMaps';
44
import type { SentrySolidStartPluginOptions } from './types';
55

66
/**
77
* Various Sentry vite plugins to be used for SolidStart.
88
*/
9-
export const sentrySolidStartVite = (options: SentrySolidStartPluginOptions = {}): Plugin[] => {
9+
export const sentrySolidStartVite = (options: SentrySolidStartPluginOptions = {}, viteConfig: UserConfig): Plugin[] => {
1010
const sentryPlugins: Plugin[] = [];
1111

1212
if (options.autoInjectServerSentry !== 'experimental_dynamic-import') {
@@ -15,7 +15,10 @@ export const sentrySolidStartVite = (options: SentrySolidStartPluginOptions = {}
1515

1616
if (process.env.NODE_ENV !== 'development') {
1717
if (options.sourceMapsUploadOptions?.enabled ?? true) {
18-
sentryPlugins.push(...makeSourceMapsVitePlugin(options));
18+
const sourceMapsPlugin = makeAddSentryVitePlugin(options, viteConfig);
19+
const enableSourceMapsPlugin = makeEnableSourceMapsVitePlugin(options);
20+
21+
sentryPlugins.push(...sourceMapsPlugin, ...enableSourceMapsPlugin);
1922
}
2023
}
2124

@@ -27,7 +30,7 @@ export const sentrySolidStartVite = (options: SentrySolidStartPluginOptions = {}
2730
*/
2831
export const addSentryPluginToVite = (config: UserConfig = {}, options: SentrySolidStartPluginOptions): UserConfig => {
2932
const plugins = Array.isArray(config.plugins) ? [...config.plugins] : [];
30-
plugins.unshift(sentrySolidStartVite(options));
33+
plugins.unshift(sentrySolidStartVite(options, config));
3134

3235
return {
3336
...config,
+113-36
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,48 @@
1+
import { consoleSandbox } from '@sentry/core';
12
import { sentryVitePlugin } from '@sentry/vite-plugin';
2-
import type { Plugin } from 'vite';
3+
import type { Plugin, UserConfig } from 'vite';
34
import type { SentrySolidStartPluginOptions } from './types';
45

56
/**
6-
* A Sentry plugin for SolidStart to enable source maps and use
7-
* @sentry/vite-plugin to automatically upload source maps to Sentry.
8-
* @param {SourceMapsOptions} options
7+
* A Sentry plugin for adding the @sentry/vite-plugin to automatically upload source maps to Sentry.
98
*/
10-
export function makeSourceMapsVitePlugin(options: SentrySolidStartPluginOptions): Plugin[] {
9+
export function makeAddSentryVitePlugin(options: SentrySolidStartPluginOptions, viteConfig: UserConfig): Plugin[] {
1110
const { authToken, debug, org, project, sourceMapsUploadOptions } = options;
12-
return [
13-
{
14-
name: 'sentry-solidstart-source-maps',
15-
apply: 'build',
16-
enforce: 'post',
17-
config(config) {
18-
// TODO(v9): Remove this warning
19-
if (config.build?.sourcemap === false) {
20-
// eslint-disable-next-line no-console
21-
console.warn(
22-
"[Sentry SolidStart Plugin] You disabled sourcemaps with the `build.sourcemap` option. Currently, the Sentry SDK will override this option to generate sourcemaps. In future versions, the Sentry SDK will not override the `build.sourcemap` option if you explicitly disable it. If you want to generate and upload sourcemaps please set the `build.sourcemap` option to 'hidden' or undefined.",
23-
);
24-
}
25-
26-
// TODO(v9): Remove this warning and print warning in case source map deletion is auto configured
27-
if (!sourceMapsUploadOptions?.filesToDeleteAfterUpload) {
28-
// eslint-disable-next-line no-console
29-
console.warn(
30-
"[Sentry SolidStart Plugin] The Sentry SDK has enabled source map generation for your SolidStart app. If you don't want to serve Source Maps to your users, either configure the `filesToDeleteAfterUpload` option with a glob to remove source maps after uploading them, or manually delete the source maps after the build. In future Sentry SDK versions source maps will be deleted automatically after uploading them.",
31-
);
32-
}
3311

34-
return {
35-
...config,
36-
build: {
37-
...config.build,
38-
sourcemap: true,
39-
},
40-
};
41-
},
42-
},
12+
let updatedFilesToDeleteAfterUpload: string[] | undefined = undefined;
13+
14+
if (
15+
typeof sourceMapsUploadOptions?.filesToDeleteAfterUpload === 'undefined' &&
16+
typeof sourceMapsUploadOptions?.unstable_sentryVitePluginOptions?.sourcemaps?.filesToDeleteAfterUpload ===
17+
'undefined' &&
18+
// Only if source maps were previously not set, we update the "filesToDeleteAfterUpload" (as we override the setting with "hidden")
19+
typeof viteConfig.build?.sourcemap === 'undefined'
20+
) {
21+
// This also works for adapters, as the source maps are also copied to e.g. the .vercel folder
22+
updatedFilesToDeleteAfterUpload = ['.*/**/*.map'];
23+
24+
consoleSandbox(() => {
25+
// eslint-disable-next-line no-console
26+
console.log(
27+
`[Sentry] Automatically setting \`sourceMapsUploadOptions.filesToDeleteAfterUpload: ${JSON.stringify(
28+
updatedFilesToDeleteAfterUpload,
29+
)}\` to delete generated source maps after they were uploaded to Sentry.`,
30+
);
31+
});
32+
}
33+
34+
return [
4335
...sentryVitePlugin({
4436
authToken: authToken ?? process.env.SENTRY_AUTH_TOKEN,
4537
bundleSizeOptimizations: options.bundleSizeOptimizations,
4638
debug: debug ?? false,
4739
org: org ?? process.env.SENTRY_ORG,
4840
project: project ?? process.env.SENTRY_PROJECT,
4941
sourcemaps: {
50-
filesToDeleteAfterUpload: sourceMapsUploadOptions?.filesToDeleteAfterUpload ?? undefined,
42+
filesToDeleteAfterUpload:
43+
(sourceMapsUploadOptions?.filesToDeleteAfterUpload ||
44+
sourceMapsUploadOptions?.unstable_sentryVitePluginOptions?.sourcemaps?.filesToDeleteAfterUpload) ??
45+
updatedFilesToDeleteAfterUpload,
5146
...sourceMapsUploadOptions?.unstable_sentryVitePluginOptions?.sourcemaps,
5247
},
5348
telemetry: sourceMapsUploadOptions?.telemetry ?? true,
@@ -60,3 +55,85 @@ export function makeSourceMapsVitePlugin(options: SentrySolidStartPluginOptions)
6055
}),
6156
];
6257
}
58+
59+
/**
60+
* A Sentry plugin for SolidStart to enable "hidden" source maps if they are unset.
61+
*/
62+
export function makeEnableSourceMapsVitePlugin(options: SentrySolidStartPluginOptions): Plugin[] {
63+
return [
64+
{
65+
name: 'sentry-solidstart-update-source-map-setting',
66+
apply: 'build',
67+
enforce: 'post',
68+
config(viteConfig) {
69+
return {
70+
...viteConfig,
71+
build: {
72+
...viteConfig.build,
73+
sourcemap: getUpdatedSourceMapSettings(viteConfig, options),
74+
},
75+
};
76+
},
77+
},
78+
];
79+
}
80+
81+
/** There are 3 ways to set up source map generation (https://github.com/getsentry/sentry-j avascript/issues/13993)
82+
*
83+
* 1. User explicitly disabled source maps
84+
* - keep this setting (emit a warning that errors won't be unminified in Sentry)
85+
* - We won't upload anything
86+
*
87+
* 2. Users enabled source map generation (true, 'hidden', 'inline').
88+
* - keep this setting (don't do anything - like deletion - besides uploading)
89+
*
90+
* 3. Users didn't set source maps generation
91+
* - we enable 'hidden' source maps generation
92+
* - configure `filesToDeleteAfterUpload` to delete all .map files (we emit a log about this)
93+
*
94+
* --> only exported for testing
95+
*/
96+
export function getUpdatedSourceMapSettings(
97+
viteConfig: UserConfig,
98+
sentryPluginOptions?: SentrySolidStartPluginOptions,
99+
): boolean | 'inline' | 'hidden' {
100+
viteConfig.build = viteConfig.build || {};
101+
102+
const viteSourceMap = viteConfig?.build?.sourcemap;
103+
let updatedSourceMapSetting = viteSourceMap;
104+
105+
const settingKey = 'vite.build.sourcemap';
106+
107+
if (viteSourceMap === false) {
108+
updatedSourceMapSetting = viteSourceMap;
109+
110+
consoleSandbox(() => {
111+
// eslint-disable-next-line no-console
112+
console.warn(
113+
`[Sentry] Source map generation is currently disabled in your SolidStart 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\`).`,
114+
);
115+
});
116+
} else if (viteSourceMap && ['hidden', 'inline', true].includes(viteSourceMap)) {
117+
updatedSourceMapSetting = viteSourceMap;
118+
119+
if (sentryPluginOptions?.debug) {
120+
consoleSandbox(() => {
121+
// eslint-disable-next-line no-console
122+
console.log(
123+
`[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.`,
124+
);
125+
});
126+
}
127+
} else {
128+
updatedSourceMapSetting = 'hidden';
129+
130+
consoleSandbox(() => {
131+
// eslint-disable-next-line no-console
132+
console.log(
133+
`[Sentry] Enabled source map generation in the build options with \`${settingKey}: 'hidden'\`. The source maps will be deleted after they were uploaded to Sentry.`,
134+
);
135+
});
136+
}
137+
138+
return updatedSourceMapSetting;
139+
}

packages/solidstart/test/config/withSentry.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ describe('withSentry()', () => {
7979
const names = config?.vite.plugins.flat().map((plugin: Plugin) => plugin.name);
8080
expect(names).toEqual([
8181
'sentry-solidstart-build-instrumentation-file',
82-
'sentry-solidstart-source-maps',
8382
'sentry-telemetry-plugin',
8483
'sentry-vite-release-injection-plugin',
8584
'sentry-debug-id-upload-plugin',
8685
'sentry-vite-debug-id-injection-plugin',
8786
'sentry-vite-debug-id-upload-plugin',
8887
'sentry-file-deletion-plugin',
88+
'sentry-solidstart-update-source-map-setting',
8989
]);
9090
});
9191

@@ -107,13 +107,13 @@ describe('withSentry()', () => {
107107
const names = config?.vite.plugins.flat().map((plugin: Plugin) => plugin.name);
108108
expect(names).toEqual([
109109
'sentry-solidstart-build-instrumentation-file',
110-
'sentry-solidstart-source-maps',
111110
'sentry-telemetry-plugin',
112111
'sentry-vite-release-injection-plugin',
113112
'sentry-debug-id-upload-plugin',
114113
'sentry-vite-debug-id-injection-plugin',
115114
'sentry-vite-debug-id-upload-plugin',
116115
'sentry-file-deletion-plugin',
116+
'sentry-solidstart-update-source-map-setting',
117117
'my-test-plugin',
118118
]);
119119
});
@@ -139,13 +139,13 @@ describe('withSentry()', () => {
139139
.map((plugin: Plugin) => plugin.name);
140140
expect(names).toEqual([
141141
'sentry-solidstart-build-instrumentation-file',
142-
'sentry-solidstart-source-maps',
143142
'sentry-telemetry-plugin',
144143
'sentry-vite-release-injection-plugin',
145144
'sentry-debug-id-upload-plugin',
146145
'sentry-vite-debug-id-injection-plugin',
147146
'sentry-vite-debug-id-upload-plugin',
148147
'sentry-file-deletion-plugin',
148+
'sentry-solidstart-update-source-map-setting',
149149
'my-test-plugin',
150150
]);
151151
});

packages/solidstart/test/vite/sentrySolidStartVite.test.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@ vi.spyOn(console, 'warn').mockImplementation(() => {
1010
});
1111

1212
function getSentrySolidStartVitePlugins(options?: Parameters<typeof sentrySolidStartVite>[0]): Plugin[] {
13-
return sentrySolidStartVite({
14-
project: 'project',
15-
org: 'org',
16-
authToken: 'token',
17-
...options,
18-
});
13+
return sentrySolidStartVite(
14+
{
15+
project: 'project',
16+
org: 'org',
17+
authToken: 'token',
18+
...options,
19+
},
20+
{},
21+
);
1922
}
2023

2124
describe('sentrySolidStartVite()', () => {
@@ -24,13 +27,13 @@ describe('sentrySolidStartVite()', () => {
2427
const names = plugins.map(plugin => plugin.name);
2528
expect(names).toEqual([
2629
'sentry-solidstart-build-instrumentation-file',
27-
'sentry-solidstart-source-maps',
2830
'sentry-telemetry-plugin',
2931
'sentry-vite-release-injection-plugin',
3032
'sentry-debug-id-upload-plugin',
3133
'sentry-vite-debug-id-injection-plugin',
3234
'sentry-vite-debug-id-upload-plugin',
3335
'sentry-file-deletion-plugin',
36+
'sentry-solidstart-update-source-map-setting',
3437
]);
3538
});
3639

0 commit comments

Comments
 (0)