diff --git a/example/deep/component.tsx b/example/deep/component.tsx index 637cedd..22970d5 100644 --- a/example/deep/component.tsx +++ b/example/deep/component.tsx @@ -6,6 +6,7 @@ export default ()=> const [countGet, countSet] = React.useState(1); return
+ Component!!!
; }; \ No newline at end of file diff --git a/example/deno.json b/example/deno.json index bb0e33d..ed849e6 100644 --- a/example/deno.json +++ b/example/deno.json @@ -8,6 +8,7 @@ "@eno/iso": "http://localhost:4507/lib/iso.tsx" }, "tasks": { + "host": "deno run -A --unstable https://deno.land/std@0.181.0/http/file_server.ts", "dev": "deno run -A --unstable --reload=http://localhost:4507/ --no-lock --config=deno.json 'http://localhost:4507/server.tsx?reload=1'" } } \ No newline at end of file diff --git a/server.tsx b/server.tsx index f07399e..f668358 100644 --- a/server.tsx +++ b/server.tsx @@ -6,6 +6,13 @@ import * as Twind from "https://esm.sh/@twind/core@1.1.3"; import React, {JSX, createElement as h} from "react"; import * as Iso from "@eno/iso"; +let hosted = import.meta.resolve("./"); +const Path = { + Hosted:hosted.substring(0, hosted.length-1), + Active:`file://${Deno.cwd().replaceAll("\\", "/")}` +}; +console.log(Path); + const Transpiled = new Map(); const Transpileable =(inFilePath:string):boolean=> { @@ -33,19 +40,6 @@ const Transpile =async(inCode:string, inKey:string):Promise=> return transpile.code; }; type Transpiler = (inPath:string, inKey:string, inCheck?:boolean)=>Promise; -const TranspileFS:Transpiler =async(inPath, inKey, inCheck)=> -{ - if(inCheck) - { - const cached = Transpiled.get(inKey); - if(cached) - { - return cached; - } - } - const body = await Deno.readTextFile(inPath); - return Transpile(body, inKey); -}; const TranspileURL:Transpiler =async(inPath, inKey, inCheck)=> { if(inCheck) @@ -56,8 +50,7 @@ const TranspileURL:Transpiler =async(inPath, inKey, inCheck)=> return cached; } } - const path = import.meta.resolve(`.${inPath}`); - let body = await fetch(path); + let body = await fetch(inPath); let text = await body.text(); return Transpile(text, inKey); }; @@ -68,8 +61,8 @@ type ImportMap = {imports?:Record, importMap?:string}; let ImportObject:ImportMap = {}; try { - const confDeno = await Deno.readTextFile(Deno.cwd()+"/deno.json"); - const confDenoParsed:ImportMap = JSON.parse(confDeno); + const confDeno = await fetch(`${Path.Active}/deno.json`); + const confDenoParsed:ImportMap = await confDeno.json(); if(confDenoParsed.importMap) { try @@ -147,7 +140,6 @@ catch(e) Deno.serve({ port: Deno.args[0]||3000 }, async(_req:Request) => { const url:URL = new URL(_req.url); - const fsPath = Deno.cwd()+url.pathname; const pathParts = url.pathname.substring(1, url.pathname.endsWith("/") ? url.pathname.length-1 : url.pathname.length).split("/"); const pathLast = pathParts[pathParts.length-1]; const pathExt:string|undefined = pathLast.split(".")[1]; @@ -194,12 +186,11 @@ Deno.serve({ port: Deno.args[0]||3000 }, async(_req:Request) => type = `application/javascript`; if(isLib) { - body = await TranspileURL(url.pathname, url.pathname, true); + body = await TranspileURL(Path.Hosted+url.pathname, url.pathname, true); } else if(!url.searchParams.get("reload")) { - const path = `file://${Deno.cwd().replaceAll("\\", "/")+url.pathname}`; - const imp = await import(path); + const imp = await import(Path.Active+url.pathname); const members = []; for( const key in imp ) { members.push(key); } body = @@ -218,22 +209,15 @@ FileListen("${url.pathname}", reloadHandler);`; } else { - body = await TranspileFS(fsPath, url.pathname, true); + body = await TranspileURL(Path.Active+url.pathname, url.pathname, true); } } // serve static media else if( pathExt ) { type = MIME.typeByExtension(pathExt) || "text/html"; - if(isLib) - { - const _fetch = await fetch(import.meta.resolve(`.${url.pathname}`)); - body = await _fetch.text(); - } - else - { - body = await Deno.readFile(fsPath); - } + const _fetch = await fetch((isLib ? Path.Hosted : Path.Active)+url.pathname); + body = await _fetch.text(); } else { @@ -289,7 +273,7 @@ const ProcessFiles =debounce(async()=> if(action != "remove") { - await TranspileFS(path, key, false); + await TranspileURL(Path.Active+key, key, false); console.log(` ...cached "${key}"`); SocketsBroadcast(key); }