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
48 changes: 48 additions & 0 deletions e2e/cases/browser-logs/custom-source-map/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { join, relative } from 'node:path';
import { rspackTest, toPosixPath } from '@e2e/helper';

const EXPECTED_LOG =
'error [browser] Uncaught Error: test (src/index.js:1:0)';

rspackTest(
'should parse source map correctly if source path is absolute',
async ({ dev }) => {
const rsbuild = await dev({
config: {
tools: {
rspack: {
output: {
devtoolModuleFilenameTemplate(info) {
return toPosixPath(info.absoluteResourcePath);
},
},
},
},
},
});
await rsbuild.expectLog(EXPECTED_LOG, { posix: true });
},
);

rspackTest(
'should parse source map correctly if source path is relative',
async ({ dev }) => {
const distPath = join(__dirname, 'dist');
const rsbuild = await dev({
config: {
tools: {
rspack: {
output: {
devtoolModuleFilenameTemplate(info) {
return toPosixPath(
relative(distPath, info.absoluteResourcePath),
);
},
},
},
},
},
});
await rsbuild.expectLog(EXPECTED_LOG, { posix: true });
},
);
1 change: 1 addition & 0 deletions e2e/cases/browser-logs/custom-source-map/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw new Error('test');
13 changes: 12 additions & 1 deletion packages/core/src/server/browserLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,17 @@ const resolveOriginalLocation = async (
};
};

/**
* Get relative source path that is relative to the project root.
* By default, the source path is relative to the dist path or is absolute.
*/
const getRelativeSourcePath = (source: string, context: InternalContext) => {
const absoluteSourcePath = path.isAbsolute(source)
? source
: path.join(context.distPath, source);
return path.relative(context.rootPath, absoluteSourcePath);
};

const formatOriginalLocation = (
originalMapping: OriginalMapping | InvalidOriginalMapping,
context: InternalContext,
Expand All @@ -129,7 +140,7 @@ const formatOriginalLocation = (
return;
}

let result = path.relative(context.rootPath, source);
let result = getRelativeSourcePath(source, context);
if (line !== null) {
result += column === null ? `:${line}` : `:${line}:${column}`;
}
Expand Down
Loading