Skip to content

Commit 5a2f9ac

Browse files
committed
chore: use source-map-js
1 parent 95ce815 commit 5a2f9ac

File tree

8 files changed

+39
-32
lines changed

8 files changed

+39
-32
lines changed

e2e/cases/server/overlay/index.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ test('should show overlay correctly', async ({ page }) => {
3939
fse.readFileSync(appPath, 'utf-8').replace('</div>', '</aaaaa>'),
4040
);
4141

42-
await expect(errorOverlay.locator('.title')).toHaveText('Compilation failed');
42+
await expect(errorOverlay.locator('.title')).toHaveText('Failed to compile');
4343

4444
await rsbuild.close();
4545

packages/core/modern.config.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const externals = [
1818
...commonExternals,
1919
'@rsbuild/core/client/hmr',
2020
'@rsbuild/core/client/overlay',
21+
'@rsbuild/core/client/runtimeErrors',
2122
];
2223

2324
// Since the relative paths of bundle and compiled have changed,
@@ -69,10 +70,11 @@ export default defineConfig({
6970
input: {
7071
hmr: 'src/client/hmr.ts',
7172
overlay: 'src/client/overlay.ts',
73+
runtimeErrors: 'src/client/runtimeErrors.ts',
7274
},
7375
target: BUILD_TARGET.client,
7476
dts: false,
75-
externals: ['./hmr'],
77+
externals: ['./hmr', './overlay'],
7678
outDir: './dist/client',
7779
autoExtension: true,
7880
externalHelpers: true,

packages/core/package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
"types": "./dist-types/client/overlay.d.ts",
2828
"default": "./dist/client/overlay.js"
2929
},
30+
"./client/runtimeErrors": {
31+
"types": "./dist/client/runtimeErrors.d.ts",
32+
"default": "./dist/client/runtimeErrors.js"
33+
},
3034
"./types": {
3135
"types": "./types.d.ts"
3236
},
@@ -57,7 +61,7 @@
5761
"core-js": "~3.36.0",
5862
"html-webpack-plugin": "npm:[email protected]",
5963
"postcss": "^8.4.38",
60-
"source-map": "0.5.7"
64+
"source-map-js": "^1.2.0"
6165
},
6266
"devDependencies": {
6367
"@types/fs-extra": "^11.0.4",

packages/core/src/client/findSourceMap.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// @ts-expect-error
2-
import { SourceMapConsumer } from 'source-map';
3-
41
const fetchContent = (url: string) => fetch(url).then((r) => r.text());
52

63
const findSourceMap = async (fileSource: string, filename: string) => {
@@ -69,6 +66,8 @@ export const findSourceCode = async (sourceInfo: any) => {
6966
if (!smContent) return;
7067
const rawSourceMap = JSON.parse(smContent);
7168

69+
const { SourceMapConsumer } = await import('source-map-js');
70+
7271
const consumer = await new SourceMapConsumer(rawSourceMap);
7372

7473
// Use sourcemap to find the source code location
@@ -81,6 +80,8 @@ export const findSourceCode = async (sourceInfo: any) => {
8180
const sourceCode = consumer.sourceContentFor(pos.source);
8281
return {
8382
sourceCode: formatSourceCode(sourceCode, pos),
83+
// Please use an absolute path in order to open it in vscode.
84+
// Take webpack as an example. Please configure it correctly for [output.devtoolModuleFilenameTemplate](https://www.webpackjs.com/configuration/output/#outputdevtoolmodulefilenametemplate)
8485
sourceFile: url,
8586
};
8687
};

packages/core/src/client/overlay.ts

-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { OverlayError } from '../types';
2-
import { formatRuntimeErrors } from './format';
32
import { registerOverlay } from './hmr';
43

54
function stripAnsi(content: string) {
@@ -339,19 +338,3 @@ if (typeof document !== 'undefined') {
339338
'[Rsbuild] Failed to display error overlay as document is not available, you can disable the `dev.client.overlay` option.',
340339
);
341340
}
342-
343-
const config = RSBUILD_CLIENT_CONFIG;
344-
345-
if (config.overlay.runtimeErrors) {
346-
window.addEventListener('error', async (event) => {
347-
const formatted = await formatRuntimeErrors(event, false);
348-
createOverlay(formatted);
349-
});
350-
351-
window.addEventListener('unhandledrejection', async (event) => {
352-
if (event.reason?.stack) {
353-
const formatted = await formatRuntimeErrors(event, true);
354-
createOverlay(formatted);
355-
}
356-
});
357-
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { formatRuntimeErrors } from './format';
2+
import { createOverlay } from './overlay';
3+
4+
window.addEventListener('error', async (event) => {
5+
const formatted = await formatRuntimeErrors(event, false);
6+
createOverlay(formatted);
7+
});
8+
9+
window.addEventListener('unhandledrejection', async (event) => {
10+
if (event.reason?.stack) {
11+
const formatted = await formatRuntimeErrors(event, true);
12+
createOverlay(formatted);
13+
}
14+
});

packages/core/src/server/compilerDevMiddleware.ts

+9
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ function getClientPaths(devConfig: DevConfig) {
3131
clientPaths.push(`${require.resolve('@rsbuild/core/client/overlay')}`);
3232
}
3333

34+
if (
35+
typeof devConfig?.client?.overlay === 'object' &&
36+
devConfig.client.overlay.runtimeErrors
37+
) {
38+
clientPaths.push(
39+
`${require.resolve('@rsbuild/core/client/runtimeErrors')}`,
40+
);
41+
}
42+
3443
return clientPaths;
3544
}
3645

pnpm-lock.yaml

+3-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)