add Shell phase

This commit is contained in:
Seth Trowbridge 2023-06-07 17:09:40 -04:00
parent 0aa0bb2955
commit 0b0396b58d
4 changed files with 59 additions and 35 deletions

8
deno.json Normal file
View File

@ -0,0 +1,8 @@
{
"compilerOptions": { "lib": ["deno.window", "dom"]},
"imports":
{
"react":"https://esm.sh/preact@10.13.2/compat",
"app": "./app.tsx"
}
}

View File

@ -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"
}
}

View File

@ -1,4 +1,4 @@
import Configure, {Transpile, Extension} from "./serve.tsx";
import {Configure, Transpile, Extension} from "./serve.tsx";
const SocketsLive:Set<WebSocket> = 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
(`<!doctype html>
<html>
<head>
</head>
<body>
<div id="app"></div>
<script type="importmap">${JSON.stringify(inMap)}</script>
<script type="module">
import App from "app";
import React from "react";
React.render(React.createElement(App), document.querySelector("#app"))
</script>
</body>
</html>
`, {status:200, headers:{"content-type":"text/html"}});
return false;
}
return false;
}
});

View File

@ -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<string, string>})=>false|Response|Promise<Response|false>;
type CustomRemapper = (inImports:Record<string, string>)=>Record<string, string>;
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(
`<!doctype html>
<html>
<head>
</head>
<body>
<div id="app"></div>
<script type="importmap">${JSON.stringify(inMap)}</script>
<script type="module">
import App from "app";
import React from "react";
React.render(React.createElement(App), document.querySelector("#app"))
</script>
</body>
</html>`, {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||""}});
}