diff --git a/e2e/cases/browser-logs/stack-trace-full/index.test.ts b/e2e/cases/browser-logs/stack-trace-full/index.test.ts index 03fdb9c723..fd0613d319 100644 --- a/e2e/cases/browser-logs/stack-trace-full/index.test.ts +++ b/e2e/cases/browser-logs/stack-trace-full/index.test.ts @@ -3,7 +3,7 @@ import { rspackTest } from '@e2e/helper'; // Omitted some parts of the stack trace as they are not static const EXPECTED_LOG = `error [browser] Uncaught Error: foo at foo (src/foo.js:2:0) - at ./src/index.js (src/index.js:3:0) + at src/index.js:3:0 at __webpack_require__ (http://localhost`; rspackTest('should display formatted full stack trace', async ({ dev }) => { diff --git a/packages/core/src/server/browserLogs.ts b/packages/core/src/server/browserLogs.ts index cd98bcb00e..6afdfbbfac 100644 --- a/packages/core/src/server/browserLogs.ts +++ b/packages/core/src/server/browserLogs.ts @@ -14,6 +14,15 @@ import { getFileFromUrl } from './assets-middleware/getFileFromUrl'; import type { OutputFileSystem } from './assets-middleware/index'; import type { ClientMessageError } from './socketServer'; +/** + * Determines whether a given string is a valid method name + * extracted from a browser error stack trace. + * Excludes file paths such as "./src/App.tsx" + */ +const isValidMethodName = (methodName: string) => { + return methodName !== '' && !/[\\/]/.test(methodName); +}; + /** * Maps a position in compiled code to its original source position using * source maps. @@ -176,7 +185,7 @@ const formatFullStack = async ( const { methodName } = frame; const parts: (string | undefined)[] = []; - if (methodName !== '') { + if (isValidMethodName(methodName)) { parts.push(methodName); } @@ -238,8 +247,7 @@ export const formatBrowserErrorLog = async ( let suffix = ''; - // exclude unknown method name and file path like `./src/App.tsx` - if (methodName !== '' && !/[\\/]/.test(methodName)) { + if (isValidMethodName(methodName)) { suffix += ` at ${methodName}`; } if (location) {