Skip to content

Commit

Permalink
chore: use source-map-js
Browse files Browse the repository at this point in the history
  • Loading branch information
nanianlisao committed May 20, 2024
1 parent 95ce815 commit ab644c0
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 31 deletions.
13 changes: 13 additions & 0 deletions examples/react/rsbuild.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,17 @@ import { pluginReact } from '@rsbuild/plugin-react';

export default defineConfig({
plugins: [pluginReact()],
dev: {
client: {
overlay: {
runtimeErrors: true,
},
},
},
tools: {
bundlerChain(config) {
config.devtool('source-map');
return config;
},
},
});
4 changes: 4 additions & 0 deletions examples/react/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import './App.css';
const App = () => {
return (
<div className="content">
<div onClick={() => console.log('xxs', d2z)} onKeyUp={() => {}}>
throw error
</div>

<h1>Rsbuild with React</h1>
<p>Start building amazing things with Rsbuild.</p>
</div>
Expand Down
4 changes: 3 additions & 1 deletion packages/core/modern.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const externals = [
...commonExternals,
'@rsbuild/core/client/hmr',
'@rsbuild/core/client/overlay',
'@rsbuild/core/client/runtimeErrors',
];

// Since the relative paths of bundle and compiled have changed,
Expand Down Expand Up @@ -69,10 +70,11 @@ export default defineConfig({
input: {
hmr: 'src/client/hmr.ts',
overlay: 'src/client/overlay.ts',
runtimeErrors: 'src/client/runtimeErrors.ts',
},
target: BUILD_TARGET.client,
dts: false,
externals: ['./hmr'],
externals: ['./hmr', './overlay'],
outDir: './dist/client',
autoExtension: true,
externalHelpers: true,
Expand Down
6 changes: 5 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
"types": "./dist-types/client/overlay.d.ts",
"default": "./dist/client/overlay.js"
},
"./client/runtimeErrors": {
"types": "./dist/client/runtimeErrors.d.ts",
"default": "./dist/client/runtimeErrors.js"
},
"./types": {
"types": "./types.d.ts"
},
Expand Down Expand Up @@ -57,7 +61,7 @@
"core-js": "~3.36.0",
"html-webpack-plugin": "npm:[email protected]",
"postcss": "^8.4.38",
"source-map": "0.5.7"
"source-map-js": "^1.2.0"
},
"devDependencies": {
"@types/fs-extra": "^11.0.4",
Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/client/findSourceMap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// @ts-expect-error
import { SourceMapConsumer } from 'source-map';

const fetchContent = (url: string) => fetch(url).then((r) => r.text());

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

const { SourceMapConsumer } = await import('source-map-js');

const consumer = await new SourceMapConsumer(rawSourceMap);

// Use sourcemap to find the source code location
Expand All @@ -81,6 +80,8 @@ export const findSourceCode = async (sourceInfo: any) => {
const sourceCode = consumer.sourceContentFor(pos.source);
return {
sourceCode: formatSourceCode(sourceCode, pos),
// Please use an absolute path in order to open it in vscode.
// Take webpack as an example. Please configure it correctly for [output.devtoolModuleFilenameTemplate](https://www.webpackjs.com/configuration/output/#outputdevtoolmodulefilenametemplate)
sourceFile: url,
};
};
17 changes: 0 additions & 17 deletions packages/core/src/client/overlay.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { OverlayError } from '../types';
import { formatRuntimeErrors } from './format';
import { registerOverlay } from './hmr';

function stripAnsi(content: string) {
Expand Down Expand Up @@ -339,19 +338,3 @@ if (typeof document !== 'undefined') {
'[Rsbuild] Failed to display error overlay as document is not available, you can disable the `dev.client.overlay` option.',
);
}

const config = RSBUILD_CLIENT_CONFIG;

if (config.overlay.runtimeErrors) {
window.addEventListener('error', async (event) => {
const formatted = await formatRuntimeErrors(event, false);
createOverlay(formatted);
});

window.addEventListener('unhandledrejection', async (event) => {
if (event.reason?.stack) {
const formatted = await formatRuntimeErrors(event, true);
createOverlay(formatted);
}
});
}
14 changes: 14 additions & 0 deletions packages/core/src/client/runtimeErrors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { formatRuntimeErrors } from './format';
import { createOverlay } from './overlay';

window.addEventListener('error', async (event) => {
const formatted = await formatRuntimeErrors(event, false);
createOverlay(formatted);
});

window.addEventListener('unhandledrejection', async (event) => {
if (event.reason?.stack) {
const formatted = await formatRuntimeErrors(event, true);
createOverlay(formatted);
}
});
9 changes: 9 additions & 0 deletions packages/core/src/server/compilerDevMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ function getClientPaths(devConfig: DevConfig) {
clientPaths.push(`${require.resolve('@rsbuild/core/client/overlay')}`);
}

if (
typeof devConfig?.client?.overlay === 'object' &&
devConfig.client.overlay.runtimeErrors
) {
clientPaths.push(
`${require.resolve('@rsbuild/core/client/runtimeErrors')}`,
);
}

return clientPaths;
}

Expand Down
12 changes: 3 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ab644c0

Please sign in to comment.