Skip to content

Commit

Permalink
feat: add support for injecting debug IDs (#18763)
Browse files Browse the repository at this point in the history
  • Loading branch information
timfish authored Feb 13, 2025
1 parent d444d6a commit 0ff556a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/vite/src/node/plugins/importAnalysisBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,8 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
chunk.map as RawSourceMap,
]) as SourceMap
map.toUrl = () => genSourceMapUrl(map)

const originalDebugId = chunk.map.debugId
chunk.map = map

if (buildSourcemap === 'inline') {
Expand All @@ -706,6 +708,9 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
)
chunk.code += `\n//# sourceMappingURL=${genSourceMapUrl(map)}`
} else {
if (originalDebugId) {
map.debugId = originalDebugId
}
const mapAsset = bundle[chunk.fileName + '.map']
if (mapAsset && mapAsset.type === 'asset') {
mapAsset.source = map.toString()
Expand Down
34 changes: 34 additions & 0 deletions playground/js-sourcemap/__tests__/js-sourcemap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import {
findAssetFile,
formatSourcemapForSnapshot,
isBuild,
listAssets,
page,
readFile,
serverLogs,
} from '~utils'

Expand Down Expand Up @@ -139,6 +141,7 @@ describe.runIf(isBuild)('build tests', () => {
const map = findAssetFile(/after-preload-dynamic-[-\w]{8}\.js\.map/)
expect(formatSourcemapForSnapshot(JSON.parse(map))).toMatchInlineSnapshot(`
{
"debugId": "c3dabf82-954a-4c41-ba03-767350e274b5",
"ignoreList": [],
"mappings": ";+8BAAA,OAAO,2BAAuB,0BAE9B,QAAQ,IAAI,uBAAuB",
"sources": [
Expand Down Expand Up @@ -177,6 +180,7 @@ describe.runIf(isBuild)('build tests', () => {
const map = findAssetFile(/with-define-object.*\.js\.map/)
expect(formatSourcemapForSnapshot(JSON.parse(map))).toMatchInlineSnapshot(`
{
"debugId": "bd3962fc-edb5-4a6d-a5da-f27a1e5f3268",
"mappings": "qBAEA,SAASA,GAAO,CACJC,EAAA,CACZ,CAEA,SAASA,GAAY,CAEX,QAAA,MAAM,qBAAsBC,CAAkB,CACxD,CAEAF,EAAK",
"sources": [
"../../with-define-object.ts",
Expand Down Expand Up @@ -207,4 +211,34 @@ describe.runIf(isBuild)('build tests', () => {
cwd: fileURLToPath(new URL('..', import.meta.url)),
})
})

test('source and sourcemap contain matching debug IDs', () => {
function getDebugIdFromString(input: string): string | undefined {
const match = input.match(/\/\/# debugId=([a-fA-F0-9-]+)/)
return match ? match[1] : undefined
}

const assets = listAssets().map((asset) => `dist/assets/${asset}`)
const jsAssets = assets.filter((asset) => asset.endsWith('.js'))

for (const jsAsset of jsAssets) {
const jsContent = readFile(jsAsset)
const sourceDebugId = getDebugIdFromString(jsContent)
expect(
sourceDebugId,
`Asset '${jsAsset}' did not contain a debug ID`,
).toBeDefined()

const mapFile = jsAsset + '.map'
const mapContent = readFile(mapFile)

const mapObj = JSON.parse(mapContent)
const mapDebugId = mapObj.debugId

expect(
sourceDebugId,
'Debug ID in source didnt match debug ID in sourcemap',
).toEqual(mapDebugId)
}
})
})
1 change: 1 addition & 0 deletions playground/js-sourcemap/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default defineConfig({
return '#!/usr/bin/env node'
}
},
sourcemapDebugIds: true,
},
},
},
Expand Down

0 comments on commit 0ff556a

Please sign in to comment.