Skip to content

feat: add support for injecting debug IDs #18763

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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