From 12e44692fef4f9f7540e2f73d30faa1e333bbf52 Mon Sep 17 00:00:00 2001 From: unit-404 Date: Sun, 13 Oct 2024 15:09:14 +0700 Subject: [PATCH] beauty --- src/Atomic/WorkerLib.ts | 10 ++++++++-- src/ESM/ModuleLoader.ts | 9 ++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Atomic/WorkerLib.ts b/src/Atomic/WorkerLib.ts index 138e8b9..7353609 100644 --- a/src/Atomic/WorkerLib.ts +++ b/src/Atomic/WorkerLib.ts @@ -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; } // diff --git a/src/ESM/ModuleLoader.ts b/src/ESM/ModuleLoader.ts index e9fc335..2adc686 100644 --- a/src/ESM/ModuleLoader.ts +++ b/src/ESM/ModuleLoader.ts @@ -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 => { @@ -11,7 +12,9 @@ const loadCompressed = async (b64c: string): Promise => { 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 (moduleSource: string): Promise => { @@ -19,7 +22,7 @@ const $moduleLoader = async (moduleSource: string): Promise