From ffbb686e0799f1e9266e84aeb88a5b5757dee236 Mon Sep 17 00:00:00 2001 From: Josh Handley Date: Sat, 8 Apr 2023 19:08:37 -0400 Subject: [PATCH] Load sourceContents if missing In the case where the sourceContents in the source map are empty (null) in order to keep source maps small, try to fill in the sourceContents by reading the contents of the corresponding files in the sources array. --- src/getCodeAndMap.ts | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/getCodeAndMap.ts b/src/getCodeAndMap.ts index b7a6cf2..ccfae19 100644 --- a/src/getCodeAndMap.ts +++ b/src/getCodeAndMap.ts @@ -25,6 +25,21 @@ export async function getCodeAndMap(): Promise<{ code: string; map: string } | u } } + const addMissingSourceContent = async (map: string) => { + const json = JSON.parse(map) + const { sources, sourcesContent } = json + let addedSourceContent = false + for (let i = 0; i < sources.length; i++) { + if (!sourcesContent[i] && sources[i]) { + addedSourceContent = true + sourcesContent[i] = await readTextFile(sources[i]) + } + } + if (addedSourceContent) + map = JSON.stringify(json) + return map + } + const mapUrl = getSourceMapUrl(code) if (mapUrl) { if (mapUrl.startsWith('data:')) { @@ -57,9 +72,11 @@ export async function getCodeAndMap(): Promise<{ code: string; map: string } | u = mapFiles.find(f => f === `${fileName}.map`) || mapFiles.find(f => fileName.startsWith(f.split('.')[0])) if (mapFile) { - const map = await readTextFile(mapFile) - if (map) + let map = await readTextFile(mapFile) + if (map) { + map = await addMissingSourceContent(map) return { code, map } + } } } }