Skip to content

Commit b9a884e

Browse files
committed
fix: fix sourcemap for oxc plugin
1 parent 45a177f commit b9a884e

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

packages/common/refresh-utils.ts

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,30 @@ window.$RefreshSig$ = () => (type) => type;`
1414
export const getPreambleCode = (base: string): string =>
1515
preambleCode.replace('__BASE__', base)
1616

17-
export function addRefreshWrapper<M extends { mappings: string } | undefined>(
17+
export const avoidSourceMapOption = Symbol()
18+
19+
export function addRefreshWrapper<M extends { mappings: string }>(
1820
code: string,
19-
map: M | string,
21+
map: M | string | undefined | typeof avoidSourceMapOption,
2022
pluginName: string,
2123
id: string,
22-
): { code: string; map: M | string } {
24+
): { code: string; map: M | undefined | string } {
2325
const hasRefresh = refreshContentRE.test(code)
2426
const onlyReactComp = !hasRefresh && reactCompRE.test(code)
25-
if (!hasRefresh && !onlyReactComp) return { code, map }
27+
const newMap =
28+
map === avoidSourceMapOption
29+
? undefined
30+
: typeof map === 'string'
31+
? (JSON.parse(map) as M)
32+
: map
33+
if (!hasRefresh && !onlyReactComp) return { code, map: newMap }
34+
35+
const avoidSourceMap = map === avoidSourceMapOption
2636

27-
const newMap = typeof map === 'string' ? (JSON.parse(map) as M) : map
2837
let newCode = code
2938
if (hasRefresh) {
30-
newCode = `let prevRefreshReg;
39+
const refreshHead = removeLineBreaksIfNeeded(
40+
`let prevRefreshReg;
3141
let prevRefreshSig;
3242
3343
if (import.meta.hot && !inWebWorker) {
@@ -43,7 +53,11 @@ if (import.meta.hot && !inWebWorker) {
4353
window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
4454
}
4555
46-
${newCode}
56+
`,
57+
avoidSourceMap,
58+
)
59+
60+
newCode = `${refreshHead}${newCode}
4761
4862
if (import.meta.hot && !inWebWorker) {
4963
window.$RefreshReg$ = prevRefreshReg;
@@ -55,10 +69,15 @@ if (import.meta.hot && !inWebWorker) {
5569
}
5670
}
5771

58-
newCode = `import * as RefreshRuntime from "${runtimePublicPath}";
72+
const sharedHead = removeLineBreaksIfNeeded(
73+
`import * as RefreshRuntime from "${runtimePublicPath}";
5974
const inWebWorker = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope;
6075
61-
${newCode}
76+
`,
77+
avoidSourceMap,
78+
)
79+
80+
newCode = `${sharedHead}${newCode}
6281
6382
if (import.meta.hot && !inWebWorker) {
6483
RefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => {
@@ -81,3 +100,7 @@ if (import.meta.hot && !inWebWorker) {
81100

82101
return { code: newCode, map: newMap }
83102
}
103+
104+
function removeLineBreaksIfNeeded(code: string, enabled: boolean): string {
105+
return enabled ? code.replace(/\n/g, '') : code
106+
}

packages/plugin-react-oxc/src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { readFileSync } from 'node:fs'
44
import type { BuildOptions, Plugin, PluginOption } from 'vite'
55
import {
66
addRefreshWrapper,
7+
avoidSourceMapOption,
78
getPreambleCode,
89
runtimePublicPath,
910
silenceUseClientWarning,
@@ -123,12 +124,13 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
123124
code.includes(jsxImportRuntime))
124125
if (!useFastRefresh) return
125126

126-
return addRefreshWrapper(
127+
const { code: newCode } = addRefreshWrapper(
127128
code,
128-
undefined,
129+
avoidSourceMapOption,
129130
'@vitejs/plugin-react-oxc',
130131
id,
131132
)
133+
return { code: newCode, map: null }
132134
},
133135
},
134136
transformIndexHtml(_, config) {

0 commit comments

Comments
 (0)