From 613af1680e1e7e7fcc2a15647a916962ef38f111 Mon Sep 17 00:00:00 2001 From: velzie Date: Sat, 20 Jul 2024 18:36:11 -0400 Subject: [PATCH] rewriteJsNaiive --- src/shared/rewriters/js.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/shared/rewriters/js.ts b/src/shared/rewriters/js.ts index 865caf2..b84ca78 100644 --- a/src/shared/rewriters/js.ts +++ b/src/shared/rewriters/js.ts @@ -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} + + } + `; +}