Skip to content

Commit 4e2479b

Browse files
committed
feat(astro): Streamline astro build logs
1 parent ffa8dd8 commit 4e2479b

File tree

6 files changed

+65
-72
lines changed

6 files changed

+65
-72
lines changed

packages/astro/src/integration/index.ts

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { consoleSandbox } from '@sentry/core';
21
import { sentryVitePlugin } from '@sentry/vite-plugin';
3-
import type { AstroConfig, AstroIntegration } from 'astro';
2+
import type { AstroConfig, AstroIntegration, AstroIntegrationLogger } from 'astro';
43
import * as fs from 'fs';
54
import * as path from 'path';
65
import { buildClientSnippet, buildSdkInitFileImportSnippet, buildServerSnippet } from './snippets';
@@ -36,14 +35,11 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => {
3635

3736
const otherOptionsKeys = Object.keys(otherOptions);
3837
if (otherOptionsKeys.length > 0) {
39-
consoleSandbox(() => {
40-
// eslint-disable-next-line no-console
41-
console.warn(
42-
`[Sentry] You passed in additional options (${otherOptionsKeys.join(
43-
', ',
44-
)}) to the Sentry integration. This is deprecated and will stop working in a future version. Instead, configure the Sentry SDK in your \`sentry.client.config.(js|ts)\` or \`sentry.server.config.(js|ts)\` files.`,
45-
);
46-
});
38+
logger.warn(
39+
`You passed in additional options (${otherOptionsKeys.join(
40+
', ',
41+
)}) to the Sentry integration. This is deprecated and will stop working in a future version. Instead, configure the Sentry SDK in your \`sentry.client.config.(js|ts)\` or \`sentry.server.config.(js|ts)\` files.`,
42+
);
4743
}
4844

4945
const sdkEnabled = {
@@ -57,7 +53,7 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => {
5753

5854
// We don't need to check for AUTH_TOKEN here, because the plugin will pick it up from the env
5955
if (shouldUploadSourcemaps && command !== 'dev') {
60-
const computedSourceMapSettings = getUpdatedSourceMapSettings(config, options);
56+
const computedSourceMapSettings = _getUpdatedSourceMapSettings(config, options, logger);
6157

6258
let updatedFilesToDeleteAfterUpload: string[] | undefined = undefined;
6359

@@ -68,14 +64,12 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => {
6864
// This also works for adapters, as the source maps are also copied to e.g. the .vercel folder
6965
updatedFilesToDeleteAfterUpload = ['./dist/**/client/**/*.map', './dist/**/server/**/*.map'];
7066

71-
consoleSandbox(() => {
72-
// eslint-disable-next-line no-console
73-
console.log(
67+
debug &&
68+
logger.info(
7469
`[Sentry] Automatically setting \`sourceMapsUploadOptions.filesToDeleteAfterUpload: ${JSON.stringify(
7570
updatedFilesToDeleteAfterUpload,
7671
)}\` to delete generated source maps after they were uploaded to Sentry.`,
7772
);
78-
});
7973
}
8074

8175
updateConfig({
@@ -222,9 +216,10 @@ export type UserSourceMapSetting = 'enabled' | 'disabled' | 'unset' | undefined;
222216
*
223217
* --> only exported for testing
224218
*/
225-
export function getUpdatedSourceMapSettings(
219+
export function _getUpdatedSourceMapSettings(
226220
astroConfig: AstroConfig,
227-
sentryOptions?: SentryOptions,
221+
sentryOptions: SentryOptions | undefined,
222+
logger: AstroIntegrationLogger,
228223
): { previousUserSourceMapSetting: UserSourceMapSetting; updatedSourceMapSetting: boolean | 'inline' | 'hidden' } {
229224
let previousUserSourceMapSetting: UserSourceMapSetting = undefined;
230225

@@ -234,39 +229,32 @@ export function getUpdatedSourceMapSettings(
234229
let updatedSourceMapSetting = viteSourceMap;
235230

236231
const settingKey = 'vite.build.sourcemap';
232+
const debug = sentryOptions?.debug;
237233

238234
if (viteSourceMap === false) {
239235
previousUserSourceMapSetting = 'disabled';
240236
updatedSourceMapSetting = viteSourceMap;
241237

242-
consoleSandbox(() => {
243-
// eslint-disable-next-line no-console
244-
console.warn(
245-
`[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\`).`,
238+
debug &&
239+
logger.warn(
240+
`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\`).`,
246241
);
247-
});
248242
} else if (viteSourceMap && ['hidden', 'inline', true].includes(viteSourceMap)) {
249243
previousUserSourceMapSetting = 'enabled';
250244
updatedSourceMapSetting = viteSourceMap;
251245

252-
if (sentryOptions?.debug) {
253-
consoleSandbox(() => {
254-
// eslint-disable-next-line no-console
255-
console.log(
256-
`[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.`,
257-
);
258-
});
259-
}
246+
debug &&
247+
logger.info(
248+
`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.`,
249+
);
260250
} else {
261251
previousUserSourceMapSetting = 'unset';
262252
updatedSourceMapSetting = 'hidden';
263253

264-
consoleSandbox(() => {
265-
// eslint-disable-next-line no-console
266-
console.log(
267-
`[Sentry] Enabled source map generation in the build options with \`${settingKey}: 'hidden'\`. The source maps will be deleted after they were uploaded to Sentry.`,
254+
debug &&
255+
logger.info(
256+
`Enabled source map generation in the build options with \`${settingKey}: 'hidden'\`. The source maps will be deleted after they were uploaded to Sentry.`,
268257
);
269-
});
270258
}
271259

272260
return { previousUserSourceMapSetting, updatedSourceMapSetting };

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

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { AstroConfig } from 'astro';
1+
import type { AstroConfig, AstroIntegrationLogger } from 'astro';
22
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
3-
import { getUpdatedSourceMapSettings, sentryAstro } from '../../src/integration';
3+
import { _getUpdatedSourceMapSettings, sentryAstro } from '../../src/integration';
44
import type { SentryOptions } from '../../src/integration/types';
55

66
const sentryVitePluginSpy = vi.fn(() => 'sentryVitePlugin');
@@ -305,21 +305,25 @@ describe('sentryAstro integration', () => {
305305
});
306306

307307
it('injects runtime config into client and server init scripts and warns about deprecation', async () => {
308-
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
309308
const integration = sentryAstro({
310309
environment: 'test',
311310
release: '1.0.0',
312311
dsn: 'https://test.sentry.io/123',
313-
debug: true,
314312
bundleSizeOptimizations: {},
313+
// this also warns when debug is not enabled
315314
});
316315

316+
const logger = {
317+
warn: vi.fn(),
318+
info: vi.fn(),
319+
};
320+
317321
expect(integration.hooks['astro:config:setup']).toBeDefined();
318322
// @ts-expect-error - the hook exists and we only need to pass what we actually use
319-
await integration.hooks['astro:config:setup']({ updateConfig, injectScript, config, logger: { info: vi.fn() } });
323+
await integration.hooks['astro:config:setup']({ updateConfig, injectScript, config, logger });
320324

321-
expect(consoleWarnSpy).toHaveBeenCalledWith(
322-
'[Sentry] You passed in additional options (environment, release, dsn) to the Sentry integration. This is deprecated and will stop working in a future version. Instead, configure the Sentry SDK in your `sentry.client.config.(js|ts)` or `sentry.server.config.(js|ts)` files.',
325+
expect(logger.warn).toHaveBeenCalledWith(
326+
'You passed in additional options (environment, release, dsn) to the Sentry integration. This is deprecated and will stop working in a future version. Instead, configure the Sentry SDK in your `sentry.client.config.(js|ts)` or `sentry.server.config.(js|ts)` files.',
323327
);
324328

325329
expect(injectScript).toHaveBeenCalledTimes(2);
@@ -490,18 +494,23 @@ describe('sentryAstro integration', () => {
490494
});
491495
});
492496

493-
describe('getUpdatedSourceMapSettings', () => {
497+
describe('_getUpdatedSourceMapSettings', () => {
494498
let astroConfig: Omit<AstroConfig, 'vite'> & { vite: { build: { sourcemap?: any } } };
495499
let sentryOptions: SentryOptions;
500+
let logger: AstroIntegrationLogger;
496501

497502
beforeEach(() => {
498503
astroConfig = { vite: { build: {} } } as Omit<AstroConfig, 'vite'> & { vite: { build: { sourcemap?: any } } };
499504
sentryOptions = {};
505+
logger = {
506+
info: vi.fn(),
507+
warn: vi.fn(),
508+
} as unknown as AstroIntegrationLogger;
500509
});
501510

502511
it('should keep explicitly disabled source maps disabled', () => {
503512
astroConfig.vite.build.sourcemap = false;
504-
const result = getUpdatedSourceMapSettings(astroConfig, sentryOptions);
513+
const result = _getUpdatedSourceMapSettings(astroConfig, sentryOptions, logger);
505514
expect(result.previousUserSourceMapSetting).toBe('disabled');
506515
expect(result.updatedSourceMapSetting).toBe(false);
507516
});
@@ -515,34 +524,30 @@ describe('getUpdatedSourceMapSettings', () => {
515524

516525
cases.forEach(({ sourcemap, expected }) => {
517526
astroConfig.vite.build.sourcemap = sourcemap;
518-
const result = getUpdatedSourceMapSettings(astroConfig, sentryOptions);
527+
const result = _getUpdatedSourceMapSettings(astroConfig, sentryOptions, logger);
519528
expect(result.previousUserSourceMapSetting).toBe('enabled');
520529
expect(result.updatedSourceMapSetting).toBe(expected);
521530
});
522531
});
523532

524533
it('should enable "hidden" source maps when unset', () => {
525534
astroConfig.vite.build.sourcemap = undefined;
526-
const result = getUpdatedSourceMapSettings(astroConfig, sentryOptions);
535+
const result = _getUpdatedSourceMapSettings(astroConfig, sentryOptions, logger);
527536
expect(result.previousUserSourceMapSetting).toBe('unset');
528537
expect(result.updatedSourceMapSetting).toBe('hidden');
529538
});
530539

531540
it('should log warnings and messages when debug is enabled', () => {
532-
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
533-
const consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
534-
535541
sentryOptions = { debug: true };
536542

537543
astroConfig.vite.build.sourcemap = false;
538-
getUpdatedSourceMapSettings(astroConfig, sentryOptions);
539-
expect(consoleWarnSpy).toHaveBeenCalledWith(expect.stringContaining('Source map generation is currently disabled'));
544+
_getUpdatedSourceMapSettings(astroConfig, sentryOptions, logger);
545+
// eslint-disable-next-line @typescript-eslint/unbound-method
546+
expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('Source map generation is currently disabled'));
540547

541548
astroConfig.vite.build.sourcemap = 'hidden';
542-
getUpdatedSourceMapSettings(astroConfig, sentryOptions);
543-
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('Sentry will keep this source map setting'));
544-
545-
consoleWarnSpy.mockRestore();
546-
consoleLogSpy.mockRestore();
549+
_getUpdatedSourceMapSettings(astroConfig, sentryOptions, logger);
550+
// eslint-disable-next-line @typescript-eslint/unbound-method
551+
expect(logger.info).toHaveBeenCalledWith(expect.stringContaining('Sentry will keep this source map setting'));
547552
});
548553
});

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function makeEnableSourceMapsPlugin(options: SentryReactRouterBuildOption
1515
...viteConfig,
1616
build: {
1717
...viteConfig.build,
18-
sourcemap: getUpdatedSourceMapSettings(viteConfig, options),
18+
sourcemap: _getUpdatedSourceMapSettings(viteConfig, options),
1919
},
2020
};
2121
},
@@ -37,7 +37,7 @@ export function makeEnableSourceMapsPlugin(options: SentryReactRouterBuildOption
3737
*
3838
* --> only exported for testing
3939
*/
40-
export function getUpdatedSourceMapSettings(
40+
export function _getUpdatedSourceMapSettings(
4141
viteConfig: UserConfig,
4242
sentryPluginOptions?: SentryReactRouterBuildOptions,
4343
): boolean | 'inline' | 'hidden' {

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { SentryVitePluginOptions } from '@sentry/vite-plugin';
22
import { beforeEach, describe, expect, it, vi } from 'vitest';
3-
import { getUpdatedSourceMapSettings, makeEnableSourceMapsPlugin } from '../../src/vite/makeEnableSourceMapsPlugin';
3+
import { _getUpdatedSourceMapSettings, makeEnableSourceMapsPlugin } from '../../src/vite/makeEnableSourceMapsPlugin';
44

55
const mockedSentryVitePlugin = {
66
name: 'sentry-vite-debug-id-upload-plugin',
@@ -33,7 +33,7 @@ describe('makeEnableSourceMapsPlugin()', () => {
3333
});
3434
});
3535

36-
describe('getUpdatedSourceMapSettings', () => {
36+
describe('_getUpdatedSourceMapSettings', () => {
3737
beforeEach(() => {
3838
vi.clearAllMocks();
3939
vi.spyOn(console, 'warn').mockImplementation(() => {});
@@ -42,7 +42,7 @@ describe('getUpdatedSourceMapSettings', () => {
4242

4343
describe('when sourcemap is false', () => {
4444
it('should keep sourcemap as false and show warning', () => {
45-
const result = getUpdatedSourceMapSettings({ build: { sourcemap: false } });
45+
const result = _getUpdatedSourceMapSettings({ build: { sourcemap: false } });
4646

4747
expect(result).toBe(false);
4848
// eslint-disable-next-line no-console
@@ -58,7 +58,7 @@ describe('getUpdatedSourceMapSettings', () => {
5858
['inline', 'inline'],
5959
[true, true],
6060
] as ('inline' | 'hidden' | boolean)[][])('should keep sourcemap as %s when set to %s', (input, expected) => {
61-
const result = getUpdatedSourceMapSettings({ build: { sourcemap: input } }, { debug: true });
61+
const result = _getUpdatedSourceMapSettings({ build: { sourcemap: input } }, { debug: true });
6262

6363
expect(result).toBe(expected);
6464
// eslint-disable-next-line no-console
@@ -72,7 +72,7 @@ describe('getUpdatedSourceMapSettings', () => {
7272
it.each([[undefined], ['invalid'], ['something'], [null]])(
7373
'should set sourcemap to hidden when value is %s',
7474
input => {
75-
const result = getUpdatedSourceMapSettings({ build: { sourcemap: input as any } });
75+
const result = _getUpdatedSourceMapSettings({ build: { sourcemap: input as any } });
7676

7777
expect(result).toBe('hidden');
7878
// eslint-disable-next-line no-console
@@ -85,7 +85,7 @@ describe('getUpdatedSourceMapSettings', () => {
8585
);
8686

8787
it('should set sourcemap to hidden when build config is empty', () => {
88-
const result = getUpdatedSourceMapSettings({});
88+
const result = _getUpdatedSourceMapSettings({});
8989

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

packages/solidstart/src/vite/sourceMaps.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export function makeEnableSourceMapsVitePlugin(options: SentrySolidStartPluginOp
7070
...viteConfig,
7171
build: {
7272
...viteConfig.build,
73-
sourcemap: getUpdatedSourceMapSettings(viteConfig, options),
73+
sourcemap: _getUpdatedSourceMapSettings(viteConfig, options),
7474
},
7575
};
7676
},
@@ -93,7 +93,7 @@ export function makeEnableSourceMapsVitePlugin(options: SentrySolidStartPluginOp
9393
*
9494
* --> only exported for testing
9595
*/
96-
export function getUpdatedSourceMapSettings(
96+
export function _getUpdatedSourceMapSettings(
9797
viteConfig: UserConfig,
9898
sentryPluginOptions?: SentrySolidStartPluginOptions,
9999
): boolean | 'inline' | 'hidden' {

packages/solidstart/test/vite/sourceMaps.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { SentryVitePluginOptions } from '@sentry/vite-plugin';
22
import { beforeEach, describe, expect, it, vi } from 'vitest';
33
import {
4-
getUpdatedSourceMapSettings,
4+
_getUpdatedSourceMapSettings,
55
makeAddSentryVitePlugin,
66
makeEnableSourceMapsVitePlugin,
77
} from '../../src/vite/sourceMaps';
@@ -171,7 +171,7 @@ describe('makeAddSentryVitePlugin()', () => {
171171
});
172172
});
173173

174-
describe('getUpdatedSourceMapSettings', () => {
174+
describe('_getUpdatedSourceMapSettings', () => {
175175
beforeEach(() => {
176176
vi.clearAllMocks();
177177
vi.spyOn(console, 'warn').mockImplementation(() => {});
@@ -180,7 +180,7 @@ describe('getUpdatedSourceMapSettings', () => {
180180

181181
describe('when sourcemap is false', () => {
182182
it('should keep sourcemap as false and show warning', () => {
183-
const result = getUpdatedSourceMapSettings({ build: { sourcemap: false } });
183+
const result = _getUpdatedSourceMapSettings({ build: { sourcemap: false } });
184184

185185
expect(result).toBe(false);
186186
// eslint-disable-next-line no-console
@@ -196,7 +196,7 @@ describe('getUpdatedSourceMapSettings', () => {
196196
['inline', 'inline'],
197197
[true, true],
198198
] as ('inline' | 'hidden' | boolean)[][])('should keep sourcemap as %s when set to %s', (input, expected) => {
199-
const result = getUpdatedSourceMapSettings({ build: { sourcemap: input } }, { debug: true });
199+
const result = _getUpdatedSourceMapSettings({ build: { sourcemap: input } }, { debug: true });
200200

201201
expect(result).toBe(expected);
202202
// eslint-disable-next-line no-console
@@ -210,7 +210,7 @@ describe('getUpdatedSourceMapSettings', () => {
210210
it.each([[undefined], ['invalid'], ['something'], [null]])(
211211
'should set sourcemap to hidden when value is %s',
212212
input => {
213-
const result = getUpdatedSourceMapSettings({ build: { sourcemap: input as any } });
213+
const result = _getUpdatedSourceMapSettings({ build: { sourcemap: input as any } });
214214

215215
expect(result).toBe('hidden');
216216
// eslint-disable-next-line no-console
@@ -223,7 +223,7 @@ describe('getUpdatedSourceMapSettings', () => {
223223
);
224224

225225
it('should set sourcemap to hidden when build config is empty', () => {
226-
const result = getUpdatedSourceMapSettings({});
226+
const result = _getUpdatedSourceMapSettings({});
227227

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

0 commit comments

Comments
 (0)