rewriteJsNaiive

This commit is contained in:
velzie 2024-07-20 18:36:11 -04:00
parent b2afb9c2ca
commit 613af1680e
No known key found for this signature in database
GPG key ID: 048413F95F0DDE1F

View file

@ -35,3 +35,26 @@ export function rewriteJs(js: string | ArrayBuffer, origin?: URL) {
return js;
}
// 1. does not work with modules
// 2. cannot proxy import()
// 3. disables "use strict" optimizations
// 4. `location = ...` will fail
// 5. i think the global state can get clobbered somehow
//
// if you can ensure all the preconditions are met this is faster than full rewrites
export function rewriteJsNaiive(js: string | ArrayBuffer, origin?: URL) {
if ("window" in globalThis) origin ??= new URL(decodeUrl(location.href));
if (typeof js !== "string") {
js = new TextDecoder().decode(js);
}
return `
with ($s(window)) {
${js}
}
`;
}