Skip to content

Commit 0ff556a

Browse files
authoredFeb 13, 2025··
feat: add support for injecting debug IDs (#18763)
1 parent d444d6a commit 0ff556a

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed
 

‎packages/vite/src/node/plugins/importAnalysisBuild.ts

+5
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,8 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
697697
chunk.map as RawSourceMap,
698698
]) as SourceMap
699699
map.toUrl = () => genSourceMapUrl(map)
700+
701+
const originalDebugId = chunk.map.debugId
700702
chunk.map = map
701703

702704
if (buildSourcemap === 'inline') {
@@ -706,6 +708,9 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
706708
)
707709
chunk.code += `\n//# sourceMappingURL=${genSourceMapUrl(map)}`
708710
} else {
711+
if (originalDebugId) {
712+
map.debugId = originalDebugId
713+
}
709714
const mapAsset = bundle[chunk.fileName + '.map']
710715
if (mapAsset && mapAsset.type === 'asset') {
711716
mapAsset.source = map.toString()

‎playground/js-sourcemap/__tests__/js-sourcemap.spec.ts

+34
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import {
99
findAssetFile,
1010
formatSourcemapForSnapshot,
1111
isBuild,
12+
listAssets,
1213
page,
14+
readFile,
1315
serverLogs,
1416
} from '~utils'
1517

@@ -139,6 +141,7 @@ describe.runIf(isBuild)('build tests', () => {
139141
const map = findAssetFile(/after-preload-dynamic-[-\w]{8}\.js\.map/)
140142
expect(formatSourcemapForSnapshot(JSON.parse(map))).toMatchInlineSnapshot(`
141143
{
144+
"debugId": "c3dabf82-954a-4c41-ba03-767350e274b5",
142145
"ignoreList": [],
143146
"mappings": ";+8BAAA,OAAO,2BAAuB,0BAE9B,QAAQ,IAAI,uBAAuB",
144147
"sources": [
@@ -177,6 +180,7 @@ describe.runIf(isBuild)('build tests', () => {
177180
const map = findAssetFile(/with-define-object.*\.js\.map/)
178181
expect(formatSourcemapForSnapshot(JSON.parse(map))).toMatchInlineSnapshot(`
179182
{
183+
"debugId": "bd3962fc-edb5-4a6d-a5da-f27a1e5f3268",
180184
"mappings": "qBAEA,SAASA,GAAO,CACJC,EAAA,CACZ,CAEA,SAASA,GAAY,CAEX,QAAA,MAAM,qBAAsBC,CAAkB,CACxD,CAEAF,EAAK",
181185
"sources": [
182186
"../../with-define-object.ts",
@@ -207,4 +211,34 @@ describe.runIf(isBuild)('build tests', () => {
207211
cwd: fileURLToPath(new URL('..', import.meta.url)),
208212
})
209213
})
214+
215+
test('source and sourcemap contain matching debug IDs', () => {
216+
function getDebugIdFromString(input: string): string | undefined {
217+
const match = input.match(/\/\/# debugId=([a-fA-F0-9-]+)/)
218+
return match ? match[1] : undefined
219+
}
220+
221+
const assets = listAssets().map((asset) => `dist/assets/${asset}`)
222+
const jsAssets = assets.filter((asset) => asset.endsWith('.js'))
223+
224+
for (const jsAsset of jsAssets) {
225+
const jsContent = readFile(jsAsset)
226+
const sourceDebugId = getDebugIdFromString(jsContent)
227+
expect(
228+
sourceDebugId,
229+
`Asset '${jsAsset}' did not contain a debug ID`,
230+
).toBeDefined()
231+
232+
const mapFile = jsAsset + '.map'
233+
const mapContent = readFile(mapFile)
234+
235+
const mapObj = JSON.parse(mapContent)
236+
const mapDebugId = mapObj.debugId
237+
238+
expect(
239+
sourceDebugId,
240+
'Debug ID in source didnt match debug ID in sourcemap',
241+
).toEqual(mapDebugId)
242+
}
243+
})
210244
})

‎playground/js-sourcemap/vite.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export default defineConfig({
3030
return '#!/usr/bin/env node'
3131
}
3232
},
33+
sourcemapDebugIds: true,
3334
},
3435
},
3536
},

0 commit comments

Comments
 (0)
Please sign in to comment.