From 884f154d77555c8b51e3541f46a18d6fb72ced5c Mon Sep 17 00:00:00 2001 From: Marcel Gerber Date: Wed, 5 Feb 2025 14:58:33 +0100 Subject: [PATCH] enhance: make source maps work --- baker/buildLocalArchivalBake.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/baker/buildLocalArchivalBake.ts b/baker/buildLocalArchivalBake.ts index 21357509b9..84a162d54d 100644 --- a/baker/buildLocalArchivalBake.ts +++ b/baker/buildLocalArchivalBake.ts @@ -118,6 +118,33 @@ const bakeAssets = async () => { const filename = await hashAndCopyFile(srcFile, targetDir) staticAssetMap[dirent.name] = `/${filename}` } + + if (staticAssetMap["owid.mjs"] && staticAssetMap["owid.mjs.map"]) { + const mjsFilename = path.basename(staticAssetMap["owid.mjs"]) + const mjsContents = await fs.readFile( + path.join(targetDir, mjsFilename), + "utf8" + ) + const withSourceMap = mjsContents.replace( + /\/\/# sourceMappingURL=owid.mjs.map/g, + `//# sourceMappingURL=${path.basename(staticAssetMap["owid.mjs.map"])}` + ) + + if (withSourceMap === mjsContents) { + console.error("Failed to replace sourceMappingURL in owid.mjs") + } else { + // Now that we have replaced the source map path, the content hash has slightly changed, + // but we don't need to care about that, realistically. We'll just live with the fact. + await fs.writeFile( + path.join(targetDir, mjsFilename), + withSourceMap, + "utf-8" + ) + } + } else { + console.error("Could not find owid.mjs and owid.mjs.map") + } + return { staticAssetMap } }