From 0b0396b58dc608668792f7f3a41379c2822e6992 Mon Sep 17 00:00:00 2001 From: Seth Trowbridge Date: Wed, 7 Jun 2023 17:09:40 -0400 Subject: [PATCH] add Shell phase --- deno.json | 8 +++++++ example/deno.json | 3 ++- local.tsx | 25 ++++---------------- serve.tsx | 58 ++++++++++++++++++++++++++++++++++++----------- 4 files changed, 59 insertions(+), 35 deletions(-) create mode 100644 deno.json diff --git a/deno.json b/deno.json new file mode 100644 index 0000000..a5c402c --- /dev/null +++ b/deno.json @@ -0,0 +1,8 @@ +{ + "compilerOptions": { "lib": ["deno.window", "dom"]}, + "imports": + { + "react":"https://esm.sh/preact@10.13.2/compat", + "app": "./app.tsx" + } +} \ No newline at end of file diff --git a/example/deno.json b/example/deno.json index 4d1522b..9a61d59 100644 --- a/example/deno.json +++ b/example/deno.json @@ -1,7 +1,8 @@ { + "compilerOptions": { "lib": ["deno.window", "dom"] }, "imports": { - "react":"https://esm.sh/preact/compat", + "react":"https://esm.sh/preact@10.13.2/compat", "app": "./app.tsx" } } \ No newline at end of file diff --git a/local.tsx b/local.tsx index cbafb77..9affb83 100644 --- a/local.tsx +++ b/local.tsx @@ -1,4 +1,4 @@ -import Configure, {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); } } @@ -20,6 +20,7 @@ Configure({ } } }, + Serve(inReq, inURL, inExt, inMap) { if(inReq.headers.get("upgrade") == "websocket") @@ -38,28 +39,10 @@ Configure({ return new Response(e); } } - - if(!inExt) + else { - return new Response -(` - - - - -
- - - - -`, {status:200, headers:{"content-type":"text/html"}}); + return false; } - - return false; } }); diff --git a/serve.tsx b/serve.tsx index 3a09418..a7dbacd 100644 --- a/serve.tsx +++ b/serve.tsx @@ -14,19 +14,22 @@ const ImportMapReload =async()=> { console.log(`No "deno.json" file found at "${Deno.cwd()}"`); } - confText && (ImportMap.imports = Configure.Remap(JSON.parse(confText)?.imports || {})); + confText && (ImportMap.imports = Configuration.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 = +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}; +let Configuration:Configuration = { - Proxy: "", + Proxy: `file://${Deno.cwd().replaceAll("\\", "/")}`, Allow: "*", Reset: "/clear-cache", - Serve: ()=>false, + Serve(inReq, inURL, inExt, inMap) + { + return false; + }, Remap: (inImports)=> { Object.entries(inImports).forEach(([key, value])=> @@ -36,8 +39,27 @@ let Configure:Configuration = inImports[key] = value.substring(1); } }); + Configuration.SWCOp.jsc.transform.react.importSource = inImports["react"]; return inImports; }, + Shell(inReq, inURL, inExt, inMap) + { + return new Response( + ` + + + + +
+ + + + `, {status:200, headers:{"content-type":"text/html"}}); + }, SWCOp: { sourceMaps: false, @@ -91,7 +113,7 @@ export const Transpile = { const resp = await fetch(inPath); const text = await resp.text(); - const {code} = await SWCW.transform(text, { ...Configure.SWCOp, filename:inKey}); + const {code} = await SWCW.transform(text, { ...Configuration.SWCOp, filename:inKey}); this.Cache.set(inKey, code); return code; } @@ -111,7 +133,7 @@ export const Extension =(inPath:string)=> return posDot > posSlash ? inPath.substring(posDot+1).toLowerCase() : false; }; -export default (config:ConfigurationArgs)=> Configure = {...Configure, ...config}; +export const Configure =(config:ConfigurationArgs)=> Configuration = {...Configuration, ...config}; await ImportMapReload(); await SWCW.default(); @@ -119,25 +141,35 @@ 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"}; + const headers = {"content-type":"application/json", "Access-Control-Allow-Origin": Configuration.Allow, "charset":"UTF-8"}; // cache-reset route - if(url.pathname === Configure.Reset) + if(url.pathname === Configuration.Reset) { return new Response(`{"cleared":${Transpile.Clear()}}`, {headers}); } // allow for custom handler - const custom = await Configure.Serve(req, url, ext, ImportMap); + const custom = await Configuration.Serve(req, url, ext, ImportMap); if(custom) { return custom; } + // custom page html + if(!ext) + { + const shell = await Configuration.Shell(req, url, ext, ImportMap); + if(shell) + { + return shell; + } + } + // transpileable files if(Transpile.Check(ext)) { - const lookup = await Transpile.Fetch(Configure.Proxy + url.pathname, url.pathname); + const lookup = await Transpile.Fetch(Configuration.Proxy + url.pathname, url.pathname); return new Response(lookup, {status:lookup?200:404, headers:{...headers, "content-type":"application/javascript"}} ); } @@ -147,7 +179,7 @@ HTTP.serve(async(req: Request)=> try { const type = MIME.typeByExtension(ext); - const file = await fetch(Configure.Proxy + url.pathname); + const file = await fetch(Configuration.Proxy + url.pathname); const text = await file.text(); return new Response(text, {headers:{...headers, "content-type":type||""}}); }