api #3
@ -7,7 +7,9 @@
|
||||
{
|
||||
"react":"https://esm.sh/preact@10.15.1/compat",
|
||||
"react-original":"https://esm.sh/preact@10.15.1/compat",
|
||||
"able/": "http://localhost:1234/"
|
||||
"able/": "http://localhost:1234/",
|
||||
"able:app": "./example/app.tsx",
|
||||
">able/": "https://gitea.hptrow.me/SethTrowbridge/able-baker/raw/branch/master/"
|
||||
},
|
||||
"tasks":
|
||||
{
|
||||
|
@ -1,4 +1,4 @@
|
||||
import {Configure, Transpile, Extension} from "./run-serve.tsx";
|
||||
import {Configure, Transpile, Extension, Root} from "./run-serve.tsx";
|
||||
import * as Collect from "./hmr-static.tsx";
|
||||
|
||||
const SocketsLive:Set<WebSocket> = new Set();
|
||||
@ -30,21 +30,8 @@ Configure({
|
||||
if(Transpile.Check(inExt) && !inURL.searchParams.get("reload"))
|
||||
{
|
||||
|
||||
// const imp = await import(inConfig.Proxy+inURL.pathname);
|
||||
// const members = [];
|
||||
// for( const key in imp ) { members.push(key); }
|
||||
//
|
||||
// const code =`
|
||||
//import {FileListen} from "${import.meta.resolve(`./hmr-listen.tsx`)}";
|
||||
//import * as Import from "${inURL.pathname}?reload=0";
|
||||
//${ members.map(m=>`let proxy_${m} = Import.${m}; export { proxy_${m} as ${m} };`).join("\n") }
|
||||
//FileListen("${inURL.pathname}", (updatedModule)=>
|
||||
//{
|
||||
// ${ members.map(m=>`proxy_${m} = updatedModule.${m};`).join("\n") }
|
||||
//});`
|
||||
|
||||
// we dont need to add ?reload= because this fetch is by way the file system not the hosted url
|
||||
const [local, foreign] = await Collect.FileExports(inConfig.Proxy+inURL.pathname);
|
||||
const [local, foreign] = await Collect.FileExports(Root+inURL.pathname);
|
||||
const code =`
|
||||
import {FileListen} from "${import.meta.resolve(`./hmr-listen.tsx`)}";
|
||||
import * as Import from "${inURL.pathname}?reload=0";
|
||||
|
@ -1,6 +1,7 @@
|
||||
import * as MIME from "https://deno.land/std@0.180.0/media_types/mod.ts";
|
||||
import * as SWCW from "https://esm.sh/@swc/wasm-web@1.3.62";
|
||||
|
||||
export const Root = new URL(`file://${Deno.cwd().replaceAll("\\", "/")}`).toString();
|
||||
|
||||
Deno.args.forEach(arg=>
|
||||
{
|
||||
@ -14,10 +15,11 @@ Deno.args.forEach(arg=>
|
||||
type DenoConfig = {imports:Record<string, string>};
|
||||
const ImportMap:DenoConfig = {imports:{}};
|
||||
let ImportMapOriginal = {};
|
||||
let ImportMapProxies:Record<string, string> = {};
|
||||
const ImportMapReload =async()=>
|
||||
{
|
||||
let json:DenoConfig;
|
||||
const path = Configuration.Proxy+"/deno.json";
|
||||
const path = Root+"/deno.json";
|
||||
try
|
||||
{
|
||||
const resp = await fetch(path);
|
||||
@ -41,17 +43,24 @@ const ImportMapReload =async()=>
|
||||
json.imports["react/"] = json.imports["react"]+"/";
|
||||
}
|
||||
|
||||
if(!json.imports["entry"])
|
||||
if(!json.imports["able:app"])
|
||||
{
|
||||
console.log(`"entry" specifier not defined in import map.`);
|
||||
console.log(`"able:app" specifier not defined in import map.`);
|
||||
}
|
||||
|
||||
|
||||
ImportMapProxies = {};
|
||||
Object.entries(json.imports).forEach(([key, value])=>
|
||||
{
|
||||
if(value.startsWith("./"))
|
||||
{
|
||||
json.imports[key] = value.substring(1);
|
||||
}
|
||||
if(key.startsWith(">")||key.startsWith("able/"))
|
||||
{
|
||||
ImportMapProxies["/"+encodeURI(key)] = value;
|
||||
json.imports[key] = "/"+key;
|
||||
}
|
||||
});
|
||||
|
||||
ImportMap.imports = Configuration.Remap(json.imports, Configuration);
|
||||
@ -59,11 +68,10 @@ const ImportMapReload =async()=>
|
||||
|
||||
type CustomHTTPHandler = (inReq:Request, inURL:URL, inExt:string|false, inMap:{imports:Record<string, string>}, inConfig:Configuration)=>void|false|Response|Promise<Response|void|false>;
|
||||
type CustomRemapper = (inImports:Record<string, string>, inConfig:Configuration)=>Record<string, string>;
|
||||
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};
|
||||
type Configuration = {Allow:string, Reset:string, SWCOp:SWCW.Options, Serve:CustomHTTPHandler, Shell:CustomHTTPHandler, Remap:CustomRemapper};
|
||||
type ConfigurationArgs = {Allow?:string, Reset?:string, SWCOp?:SWCW.Options, Serve?:CustomHTTPHandler, Shell?:CustomHTTPHandler, Remap?:CustomRemapper};
|
||||
let Configuration:Configuration =
|
||||
{
|
||||
Proxy: new URL(`file://${Deno.cwd().replaceAll("\\", "/")}`).toString(),
|
||||
Allow: "*",
|
||||
Reset: "/clear-cache",
|
||||
async Serve(inReq, inURL, inExt, inMap, inConfig){},
|
||||
@ -73,8 +81,6 @@ let Configuration:Configuration =
|
||||
},
|
||||
Shell(inReq, inURL, inExt, inMap, inConfig)
|
||||
{
|
||||
const parts = Deno.mainModule.split(inConfig.Proxy);
|
||||
|
||||
return new Response(
|
||||
`<!doctype html>
|
||||
<html>
|
||||
@ -87,7 +93,7 @@ let Configuration:Configuration =
|
||||
<script type="importmap">${JSON.stringify(inMap)}</script>
|
||||
<script type="module">
|
||||
import Mount from "${import.meta.resolve("./run-browser.tsx")}";
|
||||
Mount("#app", "entry");
|
||||
Mount("#app", "able:app");
|
||||
</script>
|
||||
</body>
|
||||
</html>`, {status:200, headers:{"content-type":"text/html"}});
|
||||
@ -133,6 +139,7 @@ export const Transpile =
|
||||
return size;
|
||||
},
|
||||
/**
|
||||
* DONT USE
|
||||
* Converts dynamic module imports in to static, also can resolve paths with an import map
|
||||
*/
|
||||
async Patch(inPath:string, inKey:string, inMap?:DenoConfig)
|
||||
@ -269,12 +276,30 @@ const server = Deno.serve({port:parseInt(Deno.env.get("port")||"8000")}, async(r
|
||||
const url:URL = new URL(req.url);
|
||||
const ext = Extension(url.pathname);
|
||||
const headers = {"content-type":"application/json", "Access-Control-Allow-Origin": Configuration.Allow, "charset":"UTF-8"};
|
||||
let proxy = Root + url.pathname;
|
||||
|
||||
if(url.pathname.includes("/__"))
|
||||
{
|
||||
return new Response(`{"error":"unmatched route", "path":"${url.pathname}"}`, {status:404, headers});
|
||||
}
|
||||
|
||||
// proxy imports
|
||||
if(url.pathname.startsWith(encodeURI("/>")))
|
||||
{
|
||||
let bestMatch="";
|
||||
for(let key in ImportMapProxies)
|
||||
{
|
||||
if(url.pathname.startsWith(key) && key.length > bestMatch.length)
|
||||
{
|
||||
bestMatch = key;
|
||||
}
|
||||
}
|
||||
if(bestMatch.length)
|
||||
{
|
||||
proxy = ImportMapProxies[bestMatch] + url.pathname.substring(bestMatch.length);
|
||||
}
|
||||
}
|
||||
|
||||
// allow for custom handler
|
||||
const custom = await Configuration.Serve(req, url, ext, ImportMap, Configuration);
|
||||
if(custom)
|
||||
@ -285,39 +310,7 @@ const server = Deno.serve({port:parseInt(Deno.env.get("port")||"8000")}, async(r
|
||||
// transpileable files
|
||||
if(Transpile.Check(ext))
|
||||
{
|
||||
let code;
|
||||
let path;
|
||||
let file;
|
||||
if( req.headers.get("user-agent")?.startsWith("Deno") || url.searchParams.has("deno") )
|
||||
{
|
||||
try
|
||||
{
|
||||
path = Configuration.Proxy + ExtensionPrefix(url.pathname, "deno");
|
||||
console.log("looking for", path);
|
||||
file = await fetch(path) as Response;
|
||||
if(file.ok)
|
||||
{
|
||||
code = await file.text();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Error("404");
|
||||
}
|
||||
}
|
||||
catch(e)
|
||||
{
|
||||
path = Configuration.Proxy + url.pathname;
|
||||
console.log("falling back to", path);
|
||||
file = await fetch(path);
|
||||
code = await file.text();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
path = Configuration.Proxy + url.pathname;
|
||||
code = await Transpile.Fetch(path, url.pathname);
|
||||
}
|
||||
|
||||
const code = await Transpile.Fetch(proxy, url.pathname);
|
||||
if(code)
|
||||
{
|
||||
return new Response(code, {headers:{...headers, "content-type":"application/javascript"}} );
|
||||
@ -346,7 +339,7 @@ const server = Deno.serve({port:parseInt(Deno.env.get("port")||"8000")}, async(r
|
||||
try
|
||||
{
|
||||
const type = MIME.typeByExtension(ext);
|
||||
const file = await fetch(Configuration.Proxy + url.pathname);
|
||||
const file = await fetch(proxy);
|
||||
return new Response(file.body, {headers:{...headers, "content-type":type||""}});
|
||||
}
|
||||
catch(e)
|
||||
|
Loading…
Reference in New Issue
Block a user