diff --git a/example/app.tsx b/example/app.tsx new file mode 100644 index 0000000..03c2ce0 --- /dev/null +++ b/example/app.tsx @@ -0,0 +1,5 @@ +import React from "react"; +export default ()=> +{ + return

hey!

+} \ No newline at end of file diff --git a/example/deno.json b/example/deno.json index 16d929c..4d1522b 100644 --- a/example/deno.json +++ b/example/deno.json @@ -1,6 +1,7 @@ { "imports": { - "react":"https://esm.sh/preact/compat" + "react":"https://esm.sh/preact/compat", + "app": "./app.tsx" } } \ No newline at end of file diff --git a/local.tsx b/local.tsx index dbd2d56..cbafb77 100644 --- a/local.tsx +++ b/local.tsx @@ -1,10 +1,10 @@ -import Setup, {Transpile, Extension} from "./serve.tsx"; +import Configure, {Transpile, Extension} from "./serve.tsx"; const SocketsLive:Set = new Set(); const SocketsSend =(inData:string)=>{ console.log(inData); for (const socket of SocketsLive){ socket.send(inData); } } const Directory = `file://${Deno.cwd().replaceAll("\\", "/")}`; -Setup({ +Configure({ Proxy:Directory, SWCOp: { @@ -20,7 +20,7 @@ Setup({ } } }, - async Serve(inReq, inURL, inExt) + Serve(inReq, inURL, inExt, inMap) { if(inReq.headers.get("upgrade") == "websocket") { @@ -38,31 +38,27 @@ Setup({ return new Response(e); } } - + if(!inExt) { - await fetch(Directory+"/deno.json"); - - return new Response( -` + return new Response +(`
- + - -`, {headers:{"content-type":"text/html"}}); + +`, {status:200, headers:{"content-type":"text/html"}}); } - + return false; } }); @@ -84,7 +80,7 @@ for await (const event of Deno.watchFs(Deno.cwd())) const key = path.substring(Deno.cwd().length).replaceAll("\\", "/"); if(action != "remove") { - const tsx = await Transpile.Fetch(Directory+key, key, true, true); + const tsx = await Transpile.Fetch(Directory+key, key, true); tsx && SocketsSend(key); } else diff --git a/serve.tsx b/serve.tsx index 8c69e79..3a09418 100644 --- a/serve.tsx +++ b/serve.tsx @@ -2,15 +2,42 @@ 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"; -type CustomHTTPHandler = (inReq:Request, inURL:URL, inExtension:string|false)=>false|Response|Promise -type Configuration = {Proxy:string, Allow:string, Reset:string, SWCOp:SWCW.Options, Serve:CustomHTTPHandler}; -type ConfigurationArgs = {Proxy?:string, Allow?:string, Reset?:string, SWCOp?:SWCW.Options, Serve?:CustomHTTPHandler}; +const ImportMap = {imports:{}}; +const ImportMapReload =async()=> +{ + let confText; + try + { + confText = await Deno.readTextFile(Deno.cwd()+"\\deno.json"); + } + catch(e) + { + console.log(`No "deno.json" file found at "${Deno.cwd()}"`); + } + confText && (ImportMap.imports = Configure.Remap(JSON.parse(confText)?.imports || {})); +}; + +type CustomHTTPHandler = (inReq:Request, inURL:URL, inExt:string|false, inMap:{imports:Record})=>false|Response|Promise; +type CustomRemapper = (inImports:Record)=>Record; +type Configuration = {Proxy:string, Allow:string, Reset:string, SWCOp:SWCW.Options, Serve:CustomHTTPHandler, Remap:CustomRemapper}; +type ConfigurationArgs = {Proxy?:string, Allow?:string, Reset?:string, SWCOp?:SWCW.Options, Serve?:CustomHTTPHandler, Remap?:CustomRemapper}; let Configure:Configuration = { Proxy: "", Allow: "*", Reset: "/clear-cache", Serve: ()=>false, + Remap: (inImports)=> + { + Object.entries(inImports).forEach(([key, value])=> + { + if(value.startsWith("./")) + { + inImports[key] = value.substring(1); + } + }); + return inImports; + }, SWCOp: { sourceMaps: false, @@ -35,7 +62,6 @@ let Configure:Configuration = } } }; -export default (config:ConfigurationArgs)=> Configure = {...Configure, ...config}; export const Transpile = { @@ -49,6 +75,7 @@ export const Transpile = { const size = this.Cache.size; this.Cache.clear(); + ImportMapReload(); return size; }, Fetch: async function(inPath:string, inKey:string, inCheckCache=true) @@ -84,40 +111,29 @@ export const Extension =(inPath:string)=> return posDot > posSlash ? inPath.substring(posDot+1).toLowerCase() : false; }; -SWCW.default(); +export default (config:ConfigurationArgs)=> Configure = {...Configure, ...config}; + +await ImportMapReload(); +await SWCW.default(); HTTP.serve(async(req: Request)=> { const url:URL = new URL(req.url); const ext = Extension(url.pathname); const headers = {"content-type":"application/json", "Access-Control-Allow-Origin": Configure.Allow, "charset":"UTF-8"}; - // allow for custom handler - const custom = await Configure.Serve(req, url, ext); - if(custom) - { - return custom; - } - - // implied index.html files - if(!ext) - { - return new Response(` - - - - - - - -`, {headers:{...headers, "content-type":"text/html"}}); - } - // cache-reset route if(url.pathname === Configure.Reset) { return new Response(`{"cleared":${Transpile.Clear()}}`, {headers}); } + // allow for custom handler + const custom = await Configure.Serve(req, url, ext, ImportMap); + if(custom) + { + return custom; + } + // transpileable files if(Transpile.Check(ext)) { @@ -126,16 +142,21 @@ HTTP.serve(async(req: Request)=> } // all other static files - try + if(ext) { - const type = MIME.typeByExtension(ext); - const file = await fetch(Configure.Proxy + url.pathname); - const text = await file.text(); - return new Response(text, {headers:{...headers, "content-type":type||""}}); - } - catch(e) - { - return new Response(`{"error":"${e}", "path":"${url.pathname}"}`, {status:404, headers}); + try + { + const type = MIME.typeByExtension(ext); + const file = await fetch(Configure.Proxy + url.pathname); + const text = await file.text(); + return new Response(text, {headers:{...headers, "content-type":type||""}}); + } + catch(e) + { + return new Response(`{"error":"${e}", "path":"${url.pathname}"}`, {status:404, headers}); + } } + return new Response(`{"error":"unmatched route", "path":"${url.pathname}"}`, {status:404, headers}); + }); \ No newline at end of file