fix strict mode sourcemap handling

This commit is contained in:
Percs 2025-03-15 00:50:57 -05:00
parent a47e416193
commit 3cd17070ab

View file

@ -366,9 +366,16 @@ async function rewriteBody(
workertype === "module" workertype === "module"
); );
if (flagEnabled("sourcemaps", meta.base) && map) { if (flagEnabled("sourcemaps", meta.base) && map) {
js = if (js instanceof Uint8Array) {
`${globalThis.$scramjet.config.globals.pushsourcemapfn}([${map.join(",")}], "${tag}");` + js = new TextDecoder().decode(js);
(js instanceof Uint8Array ? new TextDecoder().decode(js) : js); }
const sourcemapfn = `${globalThis.$scramjet.config.globals.pushsourcemapfn}([${map.join(",")}], "${tag}");`;
const strictMode = /^\s*(['"])use strict\1;?/;
if (strictMode.test(js)) {
js = js.replace(strictMode, `$&\n${sourcemapfn}`);
} else {
js = `${sourcemapfn}\n${js}`;
}
} }
return js as unknown as ArrayBuffer; return js as unknown as ArrayBuffer;