more configuration
This commit is contained in:
parent
c758f66b27
commit
0aa0bb2955
5
example/app.tsx
Normal file
5
example/app.tsx
Normal file
@ -0,0 +1,5 @@
|
||||
import React from "react";
|
||||
export default ()=>
|
||||
{
|
||||
return <div><h1>hey!</h1></div>
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
{
|
||||
"imports":
|
||||
{
|
||||
"react":"https://esm.sh/preact/compat"
|
||||
"react":"https://esm.sh/preact/compat",
|
||||
"app": "./app.tsx"
|
||||
}
|
||||
}
|
34
local.tsx
34
local.tsx
@ -1,10 +1,10 @@
|
||||
import Setup, {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); } }
|
||||
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(
|
||||
`<!doctype html>
|
||||
return new Response
|
||||
(`<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
</head>
|
||||
<body>
|
||||
<div id="app"></div>
|
||||
<script type="importmap">
|
||||
{
|
||||
"imports":
|
||||
{
|
||||
"react":"https://esm.sh/preact/compat"
|
||||
}
|
||||
}
|
||||
<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>
|
||||
<script type="module"></script>
|
||||
</body>
|
||||
</html>`, {headers:{"content-type":"text/html"}});
|
||||
</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
|
||||
|
91
serve.tsx
91
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<Response|false>
|
||||
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<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 =
|
||||
{
|
||||
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(`<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
</head>
|
||||
<body>
|
||||
<script type="importmap"></script>
|
||||
<script type="module"></script>
|
||||
</body>
|
||||
</html>`, {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});
|
||||
|
||||
});
|
Loading…
Reference in New Issue
Block a user