Skip to content
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

feat: add support for injecting debug IDs #18763

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"playwright-chromium": "^1.49.1",
"premove": "^4.0.0",
"prettier": "3.4.2",
"rollup": "^4.23.0",
"rollup": "^4.28.1",
"rollup-plugin-esbuild": "^6.1.1",
"simple-git-hooks": "^2.11.1",
"tslib": "^2.8.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"dependencies": {
"esbuild": "^0.24.2",
"postcss": "^8.4.49",
"rollup": "^4.23.0"
"rollup": "^4.28.1"
},
"optionalDependencies": {
"fsevents": "~2.3.3"
Expand Down
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
32 changes: 32 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 @@ -207,4 +209,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
4 changes: 2 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading