Skip to content

Commit 76dbc77

Browse files
committed
fix: call JSON.parse only when needed
1 parent 700af5b commit 76dbc77

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

packages/common/refresh-utils.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ export function addRefreshWrapper<M extends { mappings: string }>(
2121
map: M | string | undefined | typeof avoidSourceMapOption,
2222
pluginName: string,
2323
id: string,
24-
): { code: string; map: M | undefined | string } {
24+
): { code: string; map: M | null | undefined | string } {
2525
const hasRefresh = refreshContentRE.test(code)
2626
const onlyReactComp = !hasRefresh && reactCompRE.test(code)
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 }
27+
const normalizedMap = map === avoidSourceMapOption ? null : map
28+
29+
if (!hasRefresh && !onlyReactComp) return { code, map: normalizedMap }
3430

3531
const avoidSourceMap = map === avoidSourceMapOption
32+
const newMap =
33+
typeof normalizedMap === 'string'
34+
? (JSON.parse(normalizedMap) as M)
35+
: normalizedMap
3636

3737
let newCode = code
3838
if (hasRefresh) {

0 commit comments

Comments
 (0)