From 0ff556a6d9b55bff7cac17396ce7d4397becacaa Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Thu, 13 Feb 2025 10:48:04 +0000 Subject: [PATCH] feat: add support for injecting debug IDs (#18763) --- .../src/node/plugins/importAnalysisBuild.ts | 5 +++ .../__tests__/js-sourcemap.spec.ts | 34 +++++++++++++++++++ playground/js-sourcemap/vite.config.js | 1 + 3 files changed, 40 insertions(+) diff --git a/packages/vite/src/node/plugins/importAnalysisBuild.ts b/packages/vite/src/node/plugins/importAnalysisBuild.ts index 98e4fcd32d3e21..615bf6ad722b95 100644 --- a/packages/vite/src/node/plugins/importAnalysisBuild.ts +++ b/packages/vite/src/node/plugins/importAnalysisBuild.ts @@ -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') { @@ -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() diff --git a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts index 6b8d02a407fe63..ccba59deb722fa 100644 --- a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts +++ b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts @@ -9,7 +9,9 @@ import { findAssetFile, formatSourcemapForSnapshot, isBuild, + listAssets, page, + readFile, serverLogs, } from '~utils' @@ -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": [ @@ -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", @@ -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) + } + }) }) diff --git a/playground/js-sourcemap/vite.config.js b/playground/js-sourcemap/vite.config.js index f47c89eff07ebf..4d3d5818b5dde9 100644 --- a/playground/js-sourcemap/vite.config.js +++ b/playground/js-sourcemap/vite.config.js @@ -30,6 +30,7 @@ export default defineConfig({ return '#!/usr/bin/env node' } }, + sourcemapDebugIds: true, }, }, },