Skip to content

Commit cc13c28

Browse files
authoredDec 6, 2022
fix: move sourcemapping source out of the sourcemap (#2443)
1 parent 2e7892c commit cc13c28

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed
 

‎packages/vite-node/src/source-map.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,24 @@ interface InstallSourceMapSupportOptions {
99
let SOURCEMAPPING_URL = 'sourceMa'
1010
SOURCEMAPPING_URL += 'ppingURL'
1111

12-
const VITE_NODE_SOURCEMAPPING_URL = `${SOURCEMAPPING_URL}=data:application/json;charset=utf-8;source=vite-node`
12+
const VITE_NODE_SOURCEMAPPING_SOURCE = '//# sourceMappingSource=vite-node'
13+
const VITE_NODE_SOURCEMAPPING_URL = `${SOURCEMAPPING_URL}=data:application/json;charset=utf-8`
1314
const VITE_NODE_SOURCEMAPPING_REGEXP = new RegExp(`//# ${VITE_NODE_SOURCEMAPPING_URL};base64,(.+)`)
14-
const OTHER_SOURCE_MAP_REGEXP = new RegExp(`//# ${SOURCEMAPPING_URL}=data:application/json[^,]+base64,(.+)`)
1515

1616
export async function withInlineSourcemap(result: TransformResult) {
17-
const { code, map } = result
17+
const map = result.map
18+
let code = result.code
1819

19-
if (!map || code.includes(VITE_NODE_SOURCEMAPPING_URL))
20+
if (!map || code.includes(VITE_NODE_SOURCEMAPPING_SOURCE))
2021
return result
2122

2223
// to reduce the payload size, we only inline vite node source map, because it's also the only one we use
23-
if (OTHER_SOURCE_MAP_REGEXP.test(code))
24-
result.code = code.replace(OTHER_SOURCE_MAP_REGEXP, '')
24+
const OTHER_SOURCE_MAP_REGEXP = new RegExp(`//# ${SOURCEMAPPING_URL}=data:application/json[^,]+base64,(.+)`, 'g')
25+
while (OTHER_SOURCE_MAP_REGEXP.test(code))
26+
code = code.replace(OTHER_SOURCE_MAP_REGEXP, '')
2527

26-
result.code = `${code}\n\n//# ${VITE_NODE_SOURCEMAPPING_URL};base64,${Buffer.from(JSON.stringify(map), 'utf-8').toString('base64')}\n`
28+
const sourceMap = Buffer.from(JSON.stringify(map), 'utf-8').toString('base64')
29+
result.code = `${code.trimEnd()}\n\n${VITE_NODE_SOURCEMAPPING_SOURCE}\n//# ${VITE_NODE_SOURCEMAPPING_URL};base64,${sourceMap}\n`
2730

2831
return result
2932
}

0 commit comments

Comments
 (0)
Please sign in to comment.