diff --git a/.vscode/settings.json b/.vscode/settings.json index 8675ad5..b003be2 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,4 +1,5 @@ { "deno.enable": true, - "deno.unstable": true + "deno.unstable": true, + "deno.config": "./deno.json" } \ No newline at end of file diff --git a/deno.json b/deno.json index a5c402c..9a94b43 100644 --- a/deno.json +++ b/deno.json @@ -1,8 +1,11 @@ { - "compilerOptions": { "lib": ["deno.window", "dom"]}, + "compilerOptions": { "lib": ["deno.window", "dom"], + "jsx": "react-jsx", + "jsxImportSource": "https://esm.sh/preact@10.15.1" + }, "imports": { - "react":"https://esm.sh/preact@10.13.2/compat", + "react":"https://esm.sh/preact@10.15.1/compat", "app": "./app.tsx" } } \ No newline at end of file diff --git a/example/app.tsx b/example/app.tsx index 03c2ce0..ed9b503 100644 --- a/example/app.tsx +++ b/example/app.tsx @@ -1,4 +1,5 @@ -import React from "react"; + + export default ()=> { return

hey!

diff --git a/example/deno.json b/example/deno.json index 9a61d59..8256efb 100644 --- a/example/deno.json +++ b/example/deno.json @@ -2,7 +2,7 @@ "compilerOptions": { "lib": ["deno.window", "dom"] }, "imports": { - "react":"https://esm.sh/preact@10.13.2/compat", + "react":"https://esm.sh/preact@10.15.1/compat", "app": "./app.tsx" } } \ No newline at end of file diff --git a/example/deno.lock b/example/deno.lock new file mode 100644 index 0000000..b84dd19 --- /dev/null +++ b/example/deno.lock @@ -0,0 +1,11 @@ +{ + "version": "2", + "remote": { + "https://esm.sh/preact@10.13.2/compat/jsx-runtime": "cd2ac8b136b917d804161f394f0838e5b1643cd9a1f4ba7edc52e50caaa419d2", + "https://esm.sh/stable/preact@10.13.2/deno/compat.js": "3151a948abd84aa75dfc9e57733da7e1a45fff7a25de58c7b6025b923874b508", + "https://esm.sh/stable/preact@10.13.2/deno/compat/jsx-runtime.js": "e042cc9c6a59023f70840c4e6f9854fce0486241583e0e471c67d2b6cecf849f", + "https://esm.sh/stable/preact@10.13.2/deno/hooks.js": "c7a8e703bcbc6a05949f329b618c33d5d1ea5fee113ddcea44ff0f527af8556f", + "https://esm.sh/stable/preact@10.13.2/deno/jsx-runtime.js": "dd40c5bdfe7b277bf51009fb950c550dfb554b6d56a1b3a4cb9bc12bde84bcd1", + "https://esm.sh/stable/preact@10.13.2/deno/preact.mjs": "365fab897381f4f403f859c5d12939084560545567108cc90dae901bbe892578" + } +} diff --git a/local.tsx b/local.tsx index 9affb83..08d7706 100644 --- a/local.tsx +++ b/local.tsx @@ -20,7 +20,17 @@ Configure({ } } }, - + Remap: (inImports)=> + { + Object.entries(inImports).forEach(([key, value])=> + { + if(value.startsWith("./")) + { + inImports[key] = value.substring(1); + } + }); + return inImports; + }, Serve(inReq, inURL, inExt, inMap) { if(inReq.headers.get("upgrade") == "websocket") @@ -36,13 +46,8 @@ Configure({ } catch(e) { - return new Response(e); } } - else - { - return false; - } } }); diff --git a/serve.tsx b/serve.tsx index a7dbacd..4e08888 100644 --- a/serve.tsx +++ b/serve.tsx @@ -2,22 +2,28 @@ import * as MIME from "https://deno.land/std@0.180.0/media_types/mod.ts"; import * as HTTP from "https://deno.land/std@0.177.0/http/server.ts"; import * as SWCW from "https://esm.sh/@swc/wasm-web@1.3.62"; -const ImportMap = {imports:{}}; +type DenoConfig = {imports:Record}; +const ImportMap:DenoConfig = {imports:{}}; const ImportMapReload =async()=> { - let confText; + let json:DenoConfig; + const path = Configuration.Proxy+"/deno.json"; try { - confText = await Deno.readTextFile(Deno.cwd()+"\\deno.json"); + const resp = await fetch(path); + json = await resp.json(); + if(!json?.imports) + { throw new Error("imports not specified in deno.json") } } catch(e) { - console.log(`No "deno.json" file found at "${Deno.cwd()}"`); + console.log(`error reading deno config "${path}" message:"${e}"`); + return; } - confText && (ImportMap.imports = Configuration.Remap(JSON.parse(confText)?.imports || {})); + ImportMap.imports = Configuration.Remap(json.imports); }; -type CustomHTTPHandler = (inReq:Request, inURL:URL, inExt:string|false, inMap:{imports:Record})=>false|Response|Promise; +type CustomHTTPHandler = (inReq:Request, inURL:URL, inExt:string|false, inMap:{imports:Record})=>void|false|Response|Promise; type CustomRemapper = (inImports:Record)=>Record; type Configuration = {Proxy:string, Allow:string, Reset:string, SWCOp:SWCW.Options, Serve:CustomHTTPHandler, Shell:CustomHTTPHandler, Remap:CustomRemapper}; type ConfigurationArgs = {Proxy?:string, Allow?:string, Reset?:string, SWCOp?:SWCW.Options, Serve?:CustomHTTPHandler, Shell?:CustomHTTPHandler, Remap?:CustomRemapper}; @@ -26,10 +32,7 @@ let Configuration:Configuration = Proxy: `file://${Deno.cwd().replaceAll("\\", "/")}`, Allow: "*", Reset: "/clear-cache", - Serve(inReq, inURL, inExt, inMap) - { - return false; - }, + Serve(inReq, inURL, inExt, inMap){}, Remap: (inImports)=> { Object.entries(inImports).forEach(([key, value])=> @@ -39,7 +42,13 @@ let Configuration:Configuration = inImports[key] = value.substring(1); } }); - Configuration.SWCOp.jsc.transform.react.importSource = inImports["react"]; + const reactURL = inImports["react"] ?? console.log("React is not defined in imports"); + const setting = Configuration.SWCOp?.jsc?.transform?.react; + if(setting) + { + setting.importSource = reactURL; + } + console.log(inImports); return inImports; }, Shell(inReq, inURL, inExt, inMap) @@ -119,7 +128,7 @@ export const Transpile = } catch(e) { - //console.log(`Transpile.Fetch error. Key:"${inKey}" Path:"${inPath}" Error:"${e}"`); + console.log(`Transpile.Fetch error. Key:"${inKey}" Path:"${inPath}" Error:"${e}"`); return null; } }