Skip to content

Commit

Permalink
beauty
Browse files Browse the repository at this point in the history
  • Loading branch information
unit-404 committed Oct 13, 2024
1 parent e82f391 commit 12e4469
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
10 changes: 8 additions & 2 deletions src/Atomic/WorkerLib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@
export const loadWorker = (WX: any): Worker|null =>{
if (WX instanceof Worker) { return WX; } else
if (typeof WX == "function") { return new WX(); } else
if (typeof WX == "string") { return new Worker(WX, {type: "module"}); }
return null;
if (typeof WX == "string") {
if (URL.canParse(WX)) { return new Worker(WX, {type: "module"}); };
return new Worker(URL.createObjectURL(new Blob([WX], {type: "application/javascript"})), {type: "module"});
} else
if (WX instanceof Blob || WX instanceof File) {
return new Worker(URL.createObjectURL(WX), {type: "module"});
}
return (typeof self != "undefined" ? self : null) as unknown as Worker;
}

//
Expand Down
9 changes: 6 additions & 3 deletions src/ESM/ModuleLoader.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// deno-lint-ignore-file no-explicit-any
// just compressed base64 encoded string, we currently doesn't supports native wrappers from custom vite
// `inline` will also ignored in custom vite bundle, prior of `compress`
import $raw$ from "../Workers/ModuleWorker.ts?worker&compress&inline"; const IW = $raw$; // put into start of code
//import $raw$ from "../Workers/ModuleWorker.ts?worker&compress&inline"; const IW = $raw$; // put into start of code
import $raw$ from "../Workers/ModuleWorker.ts?worker&compress"; const IW = $raw$;

//
const loadCompressed = async (b64c: string): Promise<string|null> => {
Expand All @@ -11,15 +12,17 @@ const loadCompressed = async (b64c: string): Promise<string|null> => {
const response = await (new Response(decompressedStream, {headers: new Headers({"Content-Type": "application/javascript" })})).blob();
return URL.createObjectURL(response);
}
const PRELOAD = (IW as unknown as string)?.length >= 1024 ? loadCompressed(IW as unknown as string) : IW;

//
const PRELOAD = !URL.canParse(IW) ? loadCompressed(IW as unknown as string) : IW;

//
const $moduleLoader = async <T extends unknown>(moduleSource: string): Promise<T> => {
if (!moduleSource || typeof moduleSource != "string") throw new Error("Invalid module source");

// if url too long, un-compress code
const {loadWorker} = await import("../Atomic/WorkerLib.ts");
const uWorker = loadWorker(await PRELOAD);
const uWorker = loadWorker(await PRELOAD);
const EXChanger = (await import("../Library/FLOW/ExChanger.ts")).default;
const exChanger = new EXChanger(uWorker)?.initialize?.();
const module = await (await exChanger?.access?.("!!import!!") as any)?.(moduleSource);
Expand Down

0 comments on commit 12e4469

Please sign in to comment.