diff --git a/rspack.config.js b/rspack.config.js index 847e8af..d38e176 100644 --- a/rspack.config.js +++ b/rspack.config.js @@ -6,6 +6,8 @@ import { fileURLToPath } from "url"; const __dirname = fileURLToPath(new URL(".", import.meta.url)); export default defineConfig({ + // change to production when needed + mode: "development", entry: { shared: join(__dirname, "src/shared/index.ts"), worker: join(__dirname, "src/worker/index.ts"), @@ -35,7 +37,9 @@ export default defineConfig({ }, output: { filename: "scramjet.[name].js", - path: join(__dirname, "dist") + path: join(__dirname, "dist"), + iife: true, + clean: true }, plugins: [ // new RsdoctorRspackPlugin({ diff --git a/src/client/beacon.ts b/src/client/beacon.ts index e48d209..b28a6f4 100644 --- a/src/client/beacon.ts +++ b/src/client/beacon.ts @@ -1,4 +1,4 @@ -import { encodeUrl } from "./index"; +import { encodeUrl } from "./shared"; navigator.sendBeacon = new Proxy(navigator.sendBeacon, { apply(target, thisArg, argArray) { diff --git a/src/client/css.ts b/src/client/css.ts index d507706..4c14178 100644 --- a/src/client/css.ts +++ b/src/client/css.ts @@ -1,4 +1,4 @@ -import { rewriteCss } from "./index"; +import { rewriteCss } from "./shared"; const cssProperties = ["background", "background-image", "mask", "mask-image", "list-style", "list-style-image", "border-image", "border-image-source", "cursor"]; const jsProperties = ["background", "backgroundImage", "mask", "maskImage", "listStyle", "listStyleImage", "borderImage", "borderImageSource", "cursor"]; diff --git a/src/client/element.ts b/src/client/element.ts index 2c400f8..bbb6ae9 100644 --- a/src/client/element.ts +++ b/src/client/element.ts @@ -1,4 +1,4 @@ -import { encodeUrl, rewriteCss, rewriteHtml, rewriteJs, rewriteSrcset } from "./index"; +import { encodeUrl, rewriteCss, rewriteHtml, rewriteJs, rewriteSrcset } from "./shared"; const attrObject = { "nonce": [HTMLElement], diff --git a/src/client/history.ts b/src/client/history.ts index 6ec6e96..c36e0c0 100644 --- a/src/client/history.ts +++ b/src/client/history.ts @@ -1,5 +1,5 @@ -import { encodeUrl } from "./index"; +import { encodeUrl } from "./shared"; window.history.pushState = new Proxy(window.history.pushState, { apply(target, thisArg, argArray) { diff --git a/src/client/index.ts b/src/client/index.ts index 10f3c3e..32d7f79 100644 --- a/src/client/index.ts +++ b/src/client/index.ts @@ -1,3 +1,4 @@ +import "./scope.ts"; import "./window.ts"; import "./event.ts"; import "./native/eval.ts"; @@ -11,23 +12,9 @@ import "./storage.ts"; import "./css.ts"; import "./history.ts" import "./worker.ts"; -import "./scope.ts"; declare global { interface Window { $s: any; } -} - -export const { - encodeUrl, - decodeUrl, - rewriteHeaders, - rewriteHtml, - rewriteSrcset, - rewriteJs, - rewriteCss, - rewriteWorkers, - isScramjetFile, - BareClient -} = self.$scramjet.bundle; \ No newline at end of file +} \ No newline at end of file diff --git a/src/client/location.ts b/src/client/location.ts index 257cacd..089738e 100644 --- a/src/client/location.ts +++ b/src/client/location.ts @@ -1,5 +1,5 @@ // @ts-nocheck -import { encodeUrl, decodeUrl } from "./index"; +import { encodeUrl, decodeUrl } from "./shared"; function createLocation() { const loc = new URL(decodeUrl(location.href)); diff --git a/src/client/native/eval.ts b/src/client/native/eval.ts index 574f2c6..5c7edcb 100644 --- a/src/client/native/eval.ts +++ b/src/client/native/eval.ts @@ -1,4 +1,4 @@ -import { rewriteJs } from "../index"; +import { rewriteJs } from "../shared"; const FunctionProxy = new Proxy(Function, { construct(target, argArray) { diff --git a/src/client/requests/fetch.ts b/src/client/requests/fetch.ts index 70131d6..2547f53 100644 --- a/src/client/requests/fetch.ts +++ b/src/client/requests/fetch.ts @@ -1,6 +1,6 @@ // ts throws an error if you dont do window.fetch -import { encodeUrl, rewriteHeaders } from "../index"; +import { encodeUrl, rewriteHeaders } from "../shared"; window.fetch = new Proxy(window.fetch, { apply(target, thisArg, argArray) { diff --git a/src/client/requests/websocket.ts b/src/client/requests/websocket.ts index 5a19937..35a2bb5 100644 --- a/src/client/requests/websocket.ts +++ b/src/client/requests/websocket.ts @@ -1,4 +1,4 @@ -import { BareClient } from "../index"; +import { BareClient } from "../shared"; const client = new BareClient(); WebSocket = new Proxy(WebSocket, { diff --git a/src/client/requests/xmlhttprequest.ts b/src/client/requests/xmlhttprequest.ts index ea42a49..ca426ce 100644 --- a/src/client/requests/xmlhttprequest.ts +++ b/src/client/requests/xmlhttprequest.ts @@ -1,4 +1,4 @@ -import { encodeUrl, rewriteHeaders } from "../index"; +import { encodeUrl, rewriteHeaders } from "../shared"; XMLHttpRequest.prototype.open = new Proxy(XMLHttpRequest.prototype.open, { apply(target, thisArg, argArray) { diff --git a/src/client/shared.ts b/src/client/shared.ts new file mode 100644 index 0000000..b6dce8e --- /dev/null +++ b/src/client/shared.ts @@ -0,0 +1,12 @@ +export const { + encodeUrl, + decodeUrl, + rewriteHeaders, + rewriteHtml, + rewriteSrcset, + rewriteJs, + rewriteCss, + rewriteWorkers, + isScramjetFile, + BareClient +} = self.$scramjet.shared; \ No newline at end of file diff --git a/src/client/trustedTypes.ts b/src/client/trustedTypes.ts index d767c0d..6856a85 100644 --- a/src/client/trustedTypes.ts +++ b/src/client/trustedTypes.ts @@ -1,4 +1,4 @@ -import { rewriteHtml, rewriteJs, encodeUrl } from "./index"; +import { rewriteHtml, rewriteJs, encodeUrl } from "./shared"; // @ts-expect-error trustedTypes.createPolicy = new Proxy(trustedTypes.createPolicy, { diff --git a/src/client/worker.ts b/src/client/worker.ts index cd3c465..42e85b6 100644 --- a/src/client/worker.ts +++ b/src/client/worker.ts @@ -1,4 +1,4 @@ -import { encodeUrl } from "./index"; +import { encodeUrl } from "./shared"; Worker = new Proxy(Worker, { construct(target, argArray) { @@ -20,12 +20,15 @@ Worklet.prototype.addModule = new Proxy(Worklet.prototype.addModule, { }, }); -window.importScripts = new Proxy(window.importScripts, { - apply(target, thisArg, argArray) { - for (const i in argArray) { - argArray[i] = encodeUrl(argArray[i]); - } - return Reflect.apply(target, thisArg, argArray); - }, -}); \ No newline at end of file +// broken + +// window.importScripts = new Proxy(window.importScripts, { +// apply(target, thisArg, argArray) { +// for (const i in argArray) { +// argArray[i] = encodeUrl(argArray[i]); +// } + +// return Reflect.apply(target, thisArg, argArray); +// }, +// }); \ No newline at end of file diff --git a/src/shared/index.ts b/src/shared/index.ts index 9b92585..c00c4e9 100644 --- a/src/shared/index.ts +++ b/src/shared/index.ts @@ -11,7 +11,7 @@ if (!self.$scramjet) { //@ts-expect-error really dumb workaround self.$scramjet = {} } -self.$scramjet.bundle = { +self.$scramjet.shared = { encodeUrl, decodeUrl, rewriteCss, diff --git a/src/types.d.ts b/src/types.d.ts index b36d34d..88388d2 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -11,7 +11,7 @@ import { BareClient } from "@mercuryworkshop/bare-mux"; declare global { interface Window { $scramjet: { - bundle: { + shared: { encodeUrl: typeof encodeUrl, decodeUrl: typeof decodeUrl, rewriteCss: typeof rewriteCss, diff --git a/src/worker/index.ts b/src/worker/index.ts index 88a2264..110d6d2 100644 --- a/src/worker/index.ts +++ b/src/worker/index.ts @@ -7,10 +7,10 @@ declare global { } self.ScramjetServiceWorker = class ScramjetServiceWorker { - client: typeof self.$scramjet.bundle.BareClient.prototype; + client: typeof self.$scramjet.shared.BareClient.prototype; config: typeof self.$scramjet.config; constructor(config = self.$scramjet.config) { - this.client = new self.$scramjet.bundle.BareClient(); + this.client = new self.$scramjet.shared.BareClient(); if (!config.prefix) config.prefix = "/scramjet/"; this.config = config; } @@ -22,7 +22,7 @@ self.ScramjetServiceWorker = class ScramjetServiceWorker { async fetch({ request }: FetchEvent) { const urlParam = new URLSearchParams(new URL(request.url).search); - const { encodeUrl, decodeUrl, rewriteHeaders, rewriteHtml, rewriteJs, rewriteCss, rewriteWorkers } = self.$scramjet.bundle; + const { encodeUrl, decodeUrl, rewriteHeaders, rewriteHtml, rewriteJs, rewriteCss, rewriteWorkers } = self.$scramjet.shared; if (urlParam.has("url")) { return Response.redirect(encodeUrl(urlParam.get("url"), new URL(urlParam.get("url"))))