Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions e2e/cases/browser-logs/stack-trace-none/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { rspackTest } from '@e2e/helper';

const EXPECTED_LOG = 'error [browser] Uncaught Error: test';

rspackTest(
'should hide stack trace when stackTrace is none',
async ({ dev }) => {
const rsbuild = await dev();
await rsbuild.expectLog(EXPECTED_LOG, { posix: true, strict: true });
},
);
9 changes: 9 additions & 0 deletions e2e/cases/browser-logs/stack-trace-none/rsbuild.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from '@rsbuild/core';

export default defineConfig({
dev: {
browserLogs: {
stackTrace: 'none',
},
},
});
1 change: 1 addition & 0 deletions e2e/cases/browser-logs/stack-trace-none/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw new Error('test');
4 changes: 3 additions & 1 deletion packages/core/src/defaultConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ const require = createRequire(import.meta.url);
const getDefaultDevConfig = (): NormalizedDevConfig => ({
hmr: true,
liveReload: true,
browserLogs: true,
browserLogs: {
stackTrace: 'summary',
},
watchFiles: [],
// Temporary placeholder, default: `${server.base}`
assetPrefix: DEFAULT_ASSET_PREFIX,
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/server/browserLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { SCRIPT_REGEX } from '../constants';
import { color } from '../helpers';
import { requireCompiledPackage } from '../helpers/vendors';
import { logger } from '../logger';
import type { InternalContext, Rspack } from '../types';
import type { BrowserLogsStackTrace, InternalContext, Rspack } from '../types';
import { getFileFromUrl } from './assets-middleware/getFileFromUrl';
import type { OutputFileSystem } from './assets-middleware/index';
import type { ClientMessageError } from './socketServer';
Expand Down Expand Up @@ -131,10 +131,11 @@ export const formatBrowserErrorLog = async (
message: ClientMessageError,
context: InternalContext,
fs: Rspack.OutputFileSystem,
stackTrace: BrowserLogsStackTrace,
): Promise<string> => {
let log = `${color.cyan('[browser]')} ${color.red(message.message)}`;

if (message.stack) {
if (message.stack && stackTrace !== 'none') {
const rawLocation = await formatErrorLocation(message.stack, context, fs);
if (rawLocation) {
log += color.dim(` (${rawLocation})`);
Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/server/socketServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { IncomingMessage } from 'node:http';
import type { Socket } from 'node:net';
import type Ws from '../../compiled/ws/index.js';
import { formatStatsError } from '../helpers/format';
import { isObject } from '../helpers/index';
import { getStatsErrors, getStatsWarnings } from '../helpers/stats';
import { requireCompiledPackage } from '../helpers/vendors';
import { logger } from '../logger';
Expand Down Expand Up @@ -290,17 +291,22 @@ export class SocketServer {
typeof data === 'string' ? data : data.toString(),
);

const { browserLogs } = this.context.normalizedConfig?.dev || {};
if (
message.type === 'client-error' &&
// Do not report browser error when using webpack
this.context.bundlerType === 'rspack' &&
// Do not report browser error when build failed
!this.context.buildState.hasErrors
!this.context.buildState.hasErrors &&
browserLogs
) {
const stackTrace =
(isObject(browserLogs) && browserLogs.stackTrace) || 'summary';
const log = await formatBrowserErrorLog(
message,
this.context,
this.getOutputFileSystem(),
stackTrace,
);

if (!this.reportedBrowserLogs.has(log)) {
Expand Down
17 changes: 15 additions & 2 deletions packages/core/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1712,14 +1712,27 @@ export type CliShortcut = {

export type WriteToDisk = boolean | ((filename: string) => boolean);

export type BrowserLogsStackTrace = 'summary' | 'none';

export interface DevConfig {
/**
* Controls whether to forward browser runtime errors to the terminal. When `true`, the dev
* client listens for window `error` events in the browser and send them to the dev server,
* where they are printed in the terminal (prefixed with `[browser]`).
* @default true
* @default { stackTrace: 'summary' }
*/
browserLogs?: boolean;
browserLogs?:
| boolean
| {
/**
* Controls how the error stack trace is displayed in the terminal when forwarding
* browser errors.
* - `'summary'` – Show only the first frame (e.g. `(src/App.jsx:3:0)`).
* - `'none'` – Hide stack traces.
* @default 'summary'
*/
stackTrace?: BrowserLogsStackTrace;
};
/**
* Whether to enable Hot Module Replacement.
* @default true
Expand Down
36 changes: 27 additions & 9 deletions packages/core/tests/__snapshots__/environments.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ exports[`environment config > should normalize environment config correctly 1`]
{
"dev": {
"assetPrefix": "/",
"browserLogs": true,
"browserLogs": {
"stackTrace": "summary",
},
"cliShortcuts": false,
"client": {
"host": "",
Expand Down Expand Up @@ -158,7 +160,9 @@ exports[`environment config > should normalize environment config correctly 2`]
{
"dev": {
"assetPrefix": "/foo",
"browserLogs": true,
"browserLogs": {
"stackTrace": "summary",
},
"cliShortcuts": false,
"client": {
"host": "",
Expand Down Expand Up @@ -313,7 +317,9 @@ exports[`environment config > should print environment config when inspect confi
"ssr": {
"dev": {
"assetPrefix": "/",
"browserLogs": true,
"browserLogs": {
"stackTrace": "summary",
},
"cliShortcuts": false,
"client": {
"host": "",
Expand Down Expand Up @@ -467,7 +473,9 @@ exports[`environment config > should print environment config when inspect confi
"web": {
"dev": {
"assetPrefix": "/",
"browserLogs": true,
"browserLogs": {
"stackTrace": "summary",
},
"cliShortcuts": false,
"client": {
"host": "",
Expand Down Expand Up @@ -641,7 +649,9 @@ exports[`environment config > should support modify environment config by api.mo
"ssr": {
"dev": {
"assetPrefix": "/",
"browserLogs": true,
"browserLogs": {
"stackTrace": "summary",
},
"cliShortcuts": false,
"client": {
"host": "",
Expand Down Expand Up @@ -795,7 +805,9 @@ exports[`environment config > should support modify environment config by api.mo
"web": {
"dev": {
"assetPrefix": "/",
"browserLogs": true,
"browserLogs": {
"stackTrace": "summary",
},
"cliShortcuts": false,
"client": {
"host": "",
Expand Down Expand Up @@ -950,7 +962,9 @@ exports[`environment config > should support modify environment config by api.mo
"web1": {
"dev": {
"assetPrefix": "/",
"browserLogs": true,
"browserLogs": {
"stackTrace": "summary",
},
"cliShortcuts": false,
"client": {
"host": "",
Expand Down Expand Up @@ -1108,7 +1122,9 @@ exports[`environment config > should support modify single environment config by
"ssr": {
"dev": {
"assetPrefix": "/",
"browserLogs": true,
"browserLogs": {
"stackTrace": "summary",
},
"cliShortcuts": false,
"client": {
"host": "",
Expand Down Expand Up @@ -1262,7 +1278,9 @@ exports[`environment config > should support modify single environment config by
"web": {
"dev": {
"assetPrefix": "/",
"browserLogs": true,
"browserLogs": {
"stackTrace": "summary",
},
"cliShortcuts": false,
"client": {
"host": "",
Expand Down
Loading