Skip to content

Commit a1efb5e

Browse files
authored
feat(react-router): Streamline build logs (#17303)
Streamlines react router build logs. Those are mostly already gated behind `debug`, so smaller changes here: 1. Streamline with style from other parts of the SDK 2. Log compact message when source maps are disabled without `debug: true` 3. Add some tests for this
1 parent c21df26 commit a1efb5e

File tree

3 files changed

+37
-25
lines changed

3 files changed

+37
-25
lines changed

packages/react-router/src/vite/buildEnd/handleOnBuildEnd.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,13 @@ export const sentryOnBuildEnd: BuildEndHook = async ({ reactRouterConfig, viteCo
8484
// set a default value no option was set
8585
if (typeof sourceMapsUploadOptions?.filesToDeleteAfterUpload === 'undefined') {
8686
updatedFilesToDeleteAfterUpload = [`${reactRouterConfig.buildDirectory}/**/*.map`];
87-
if (debug) {
87+
debug &&
8888
// eslint-disable-next-line no-console
8989
console.info(
9090
`[Sentry] Automatically setting \`sourceMapsUploadOptions.filesToDeleteAfterUpload: ${JSON.stringify(
9191
updatedFilesToDeleteAfterUpload,
9292
)}\` to delete generated source maps after they were uploaded to Sentry.`,
9393
);
94-
}
9594
}
9695
if (updatedFilesToDeleteAfterUpload) {
9796
try {
@@ -108,11 +107,10 @@ export const sentryOnBuildEnd: BuildEndHook = async ({ reactRouterConfig, viteCo
108107
await Promise.all(
109108
filePathsToDelete.map(filePathToDelete =>
110109
rm(filePathToDelete, { force: true }).catch((e: unknown) => {
111-
if (debug) {
112-
// This is allowed to fail - we just don't do anything
110+
// This is allowed to fail - we just don't do anything
111+
debug &&
113112
// eslint-disable-next-line no-console
114113
console.debug(`An error occurred while attempting to delete asset: ${filePathToDelete}`, e);
115-
}
116114
}),
117115
),
118116
);

packages/react-router/src/vite/makeEnableSourceMapsPlugin.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { consoleSandbox } from '@sentry/core';
21
import type { Plugin, UserConfig } from 'vite';
32
import type { SentryReactRouterBuildOptions } from './types';
43

@@ -47,36 +46,35 @@ export function getUpdatedSourceMapSettings(
4746
let updatedSourceMapSetting = viteSourceMap;
4847

4948
const settingKey = 'vite.build.sourcemap';
49+
const debug = sentryPluginOptions?.debug;
5050

5151
if (viteSourceMap === false) {
5252
updatedSourceMapSetting = viteSourceMap;
5353

54-
consoleSandbox(() => {
55-
// eslint-disable-next-line no-console
54+
if (debug) {
55+
// Longer debug message with more details
56+
// eslint-disable-next-line no-console
5657
console.warn(
5758
`[Sentry] Source map generation is 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\`).`,
5859
);
59-
});
60+
} else {
61+
// eslint-disable-next-line no-console
62+
console.warn('[Sentry] Source map generation is disabled in your Vite configuration.');
63+
}
6064
} else if (viteSourceMap && ['hidden', 'inline', true].includes(viteSourceMap)) {
6165
updatedSourceMapSetting = viteSourceMap;
6266

63-
if (sentryPluginOptions?.debug) {
64-
consoleSandbox(() => {
65-
// eslint-disable-next-line no-console
66-
console.log(
67-
`[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.`,
68-
);
69-
});
70-
}
67+
debug &&
68+
// eslint-disable-next-line no-console
69+
console.log(
70+
`[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.`,
71+
);
7172
} else {
7273
updatedSourceMapSetting = 'hidden';
73-
74-
consoleSandbox(() => {
75-
// eslint-disable-next-line no-console
74+
debug && // eslint-disable-next-line no-console
7675
console.log(
7776
`[Sentry] Enabled source map generation in the build options with \`${settingKey}: 'hidden'\`. The source maps will be deleted after they were uploaded to Sentry.`,
7877
);
79-
});
8078
}
8179

8280
return updatedSourceMapSetting;

packages/react-router/test/vite/sourceMaps.test.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,29 @@ describe('getUpdatedSourceMapSettings', () => {
4141
});
4242

4343
describe('when sourcemap is false', () => {
44-
it('should keep sourcemap as false and show warning', () => {
44+
it('should keep sourcemap as false and show short warning when debug is disabled', () => {
4545
const result = getUpdatedSourceMapSettings({ build: { sourcemap: false } });
4646

4747
expect(result).toBe(false);
4848
// eslint-disable-next-line no-console
4949
expect(console.warn).toHaveBeenCalledWith(
50-
expect.stringContaining('[Sentry] Source map generation is currently disabled'),
50+
'[Sentry] Source map generation is disabled in your Vite configuration.',
51+
);
52+
});
53+
54+
it('should keep sourcemap as false and show long warning when debug is enabled', () => {
55+
const result = getUpdatedSourceMapSettings({ build: { sourcemap: false } }, { debug: true });
56+
57+
expect(result).toBe(false);
58+
// eslint-disable-next-line no-console
59+
expect(console.warn).toHaveBeenCalledWith(
60+
expect.stringContaining('[Sentry] Source map generation is currently disabled in your Vite configuration'),
61+
);
62+
// eslint-disable-next-line no-console
63+
expect(console.warn).toHaveBeenCalledWith(
64+
expect.stringContaining(
65+
'This setting is either a default setting or was explicitly set in your configuration.',
66+
),
5167
);
5268
});
5369
});
@@ -72,7 +88,7 @@ describe('getUpdatedSourceMapSettings', () => {
7288
it.each([[undefined], ['invalid'], ['something'], [null]])(
7389
'should set sourcemap to hidden when value is %s',
7490
input => {
75-
const result = getUpdatedSourceMapSettings({ build: { sourcemap: input as any } });
91+
const result = getUpdatedSourceMapSettings({ build: { sourcemap: input as any } }, { debug: true });
7692

7793
expect(result).toBe('hidden');
7894
// eslint-disable-next-line no-console
@@ -85,7 +101,7 @@ describe('getUpdatedSourceMapSettings', () => {
85101
);
86102

87103
it('should set sourcemap to hidden when build config is empty', () => {
88-
const result = getUpdatedSourceMapSettings({});
104+
const result = getUpdatedSourceMapSettings({}, { debug: true });
89105

90106
expect(result).toBe('hidden');
91107
// eslint-disable-next-line no-console

0 commit comments

Comments
 (0)