Skip to content

Commit

Permalink
enhance: make source maps work
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelgerber committed Feb 5, 2025
1 parent d052b21 commit ccb9ac1
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions baker/buildLocalArchivalBake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,34 @@ const bakeAssets = async () => {
const filename = await hashAndCopyFile(srcFile, targetDir)
staticAssetMap[dirent.name] = `/${filename}`
}

// Attempt to make JS source maps work, by replacing the content of the //# sourceMappingURL comment
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 }
}

Expand Down

0 comments on commit ccb9ac1

Please sign in to comment.