2023-06-06 12:50:50 -04:00
|
|
|
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";
|
2023-06-06 22:24:43 -04:00
|
|
|
import * as SWCW from "https://esm.sh/@swc/wasm-web@1.3.62";
|
2023-06-06 12:50:50 -04:00
|
|
|
|
2023-06-07 23:35:00 -04:00
|
|
|
type DenoConfig = {imports:Record<string, string>};
|
|
|
|
const ImportMap:DenoConfig = {imports:{}};
|
2023-06-07 14:15:57 -04:00
|
|
|
const ImportMapReload =async()=>
|
|
|
|
{
|
2023-06-07 23:35:00 -04:00
|
|
|
let json:DenoConfig;
|
|
|
|
const path = Configuration.Proxy+"/deno.json";
|
2023-06-07 14:15:57 -04:00
|
|
|
try
|
|
|
|
{
|
2023-06-07 23:35:00 -04:00
|
|
|
const resp = await fetch(path);
|
|
|
|
json = await resp.json();
|
|
|
|
if(!json?.imports)
|
|
|
|
{ throw new Error("imports not specified in deno.json") }
|
2023-06-07 14:15:57 -04:00
|
|
|
}
|
|
|
|
catch(e)
|
|
|
|
{
|
2023-06-07 23:35:00 -04:00
|
|
|
console.log(`error reading deno config "${path}" message:"${e}"`);
|
|
|
|
return;
|
2023-06-07 14:15:57 -04:00
|
|
|
}
|
2023-06-20 22:56:20 -04:00
|
|
|
|
|
|
|
Object.entries(json.imports).forEach(([key, value])=>
|
|
|
|
{
|
|
|
|
if(value.startsWith("./"))
|
|
|
|
{
|
|
|
|
json.imports[key] = value.substring(1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if(!json.imports["@able/"])
|
|
|
|
{
|
|
|
|
console.log(`"@able/" specifier not defined in import map`);
|
|
|
|
}
|
|
|
|
json.imports["@able/"] = "/_lib_/";
|
|
|
|
|
|
|
|
if(!json.imports["react"])
|
|
|
|
{
|
|
|
|
console.log(`"react" specifier not defined in import map`);
|
|
|
|
}
|
|
|
|
|
2023-06-21 21:20:24 -04:00
|
|
|
ImportMap.imports = Configuration.Remap(json.imports, Configuration);
|
2023-06-20 22:56:20 -04:00
|
|
|
console.log(ImportMap.imports);
|
2023-06-07 14:15:57 -04:00
|
|
|
};
|
|
|
|
|
2023-06-21 21:20:24 -04:00
|
|
|
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>;
|
2023-06-07 17:09:40 -04:00
|
|
|
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 =
|
2023-06-06 12:50:50 -04:00
|
|
|
{
|
2023-06-20 21:40:49 -04:00
|
|
|
Proxy: new URL(`file://${Deno.cwd().replaceAll("\\", "/")}`).toString(),
|
2023-06-06 12:50:50 -04:00
|
|
|
Allow: "*",
|
2023-06-06 17:22:14 -04:00
|
|
|
Reset: "/clear-cache",
|
2023-06-20 21:40:49 -04:00
|
|
|
Serve(inReq, inURL, inExt, inMap, inProxy){},
|
2023-06-21 21:20:24 -04:00
|
|
|
Remap: (inImports, inConfig)=>
|
2023-06-07 14:15:57 -04:00
|
|
|
{
|
2023-06-20 22:56:20 -04:00
|
|
|
const reactURL = inImports["react"];
|
2023-06-07 23:35:00 -04:00
|
|
|
const setting = Configuration.SWCOp?.jsc?.transform?.react;
|
2023-06-20 22:56:20 -04:00
|
|
|
if(setting && reactURL)
|
2023-06-07 23:35:00 -04:00
|
|
|
{
|
|
|
|
setting.importSource = reactURL;
|
|
|
|
}
|
2023-06-07 14:15:57 -04:00
|
|
|
return inImports;
|
|
|
|
},
|
2023-06-21 21:20:24 -04:00
|
|
|
Shell(inReq, inURL, inExt, inMap, inConfig)
|
2023-06-07 17:09:40 -04:00
|
|
|
{
|
2023-06-21 21:20:24 -04:00
|
|
|
console.log("Start app:", Deno.mainModule, "start dir", inConfig.Proxy);
|
|
|
|
console.log("Split:", Deno.mainModule.split(inConfig.Proxy) );
|
2023-06-20 21:40:49 -04:00
|
|
|
|
2023-06-21 21:20:24 -04:00
|
|
|
const parts = Deno.mainModule.split(inConfig.Proxy);
|
2023-06-20 21:40:49 -04:00
|
|
|
|
2023-06-07 17:09:40 -04:00
|
|
|
return new Response(
|
|
|
|
`<!doctype html>
|
|
|
|
<html>
|
|
|
|
<head>
|
2023-06-21 17:21:36 -04:00
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
|
|
|
|
<meta charset="utf-8"/>
|
2023-06-07 17:09:40 -04:00
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div id="app"></div>
|
|
|
|
<script type="importmap">${JSON.stringify(inMap)}</script>
|
|
|
|
<script type="module">
|
2023-06-21 17:21:36 -04:00
|
|
|
import Mount from "/_lib_/boot-browser.tsx";
|
2023-06-20 21:40:49 -04:00
|
|
|
Mount("#app", "${parts[1]??"/app.tsx"}");
|
2023-06-07 17:09:40 -04:00
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>`, {status:200, headers:{"content-type":"text/html"}});
|
|
|
|
},
|
2023-06-07 11:22:49 -04:00
|
|
|
SWCOp:
|
2023-06-06 22:24:43 -04:00
|
|
|
{
|
|
|
|
sourceMaps: false,
|
|
|
|
minify: true,
|
|
|
|
jsc:
|
|
|
|
{
|
2023-06-20 09:50:14 -04:00
|
|
|
target:"es2022",
|
2023-06-06 22:24:43 -04:00
|
|
|
minify:
|
|
|
|
{
|
|
|
|
compress: { unused: true },
|
|
|
|
mangle: true
|
|
|
|
},
|
|
|
|
parser:
|
|
|
|
{
|
|
|
|
syntax: "typescript",
|
|
|
|
tsx: true,
|
|
|
|
},
|
|
|
|
transform:
|
|
|
|
{
|
|
|
|
react: { runtime: "automatic" }
|
|
|
|
}
|
2023-06-07 11:22:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const Transpile =
|
|
|
|
{
|
2023-06-06 22:24:43 -04:00
|
|
|
Cache: new Map() as Map<string, string>,
|
2023-06-07 11:22:49 -04:00
|
|
|
Files: ["tsx", "jsx", "ts", "js", "mjs"],
|
|
|
|
Check(inExtension:string|false)
|
|
|
|
{
|
|
|
|
return inExtension ? this.Files.includes(inExtension) : false;
|
|
|
|
},
|
2023-06-06 22:24:43 -04:00
|
|
|
Clear()
|
|
|
|
{
|
|
|
|
const size = this.Cache.size;
|
|
|
|
this.Cache.clear();
|
2023-06-07 14:15:57 -04:00
|
|
|
ImportMapReload();
|
2023-06-06 22:24:43 -04:00
|
|
|
return size;
|
|
|
|
},
|
|
|
|
Fetch: async function(inPath:string, inKey:string, inCheckCache=true)
|
|
|
|
{
|
2023-06-07 11:22:49 -04:00
|
|
|
const check = this.Cache.get(inPath);
|
|
|
|
if(check && inCheckCache)
|
2023-06-06 22:24:43 -04:00
|
|
|
{
|
2023-06-07 11:22:49 -04:00
|
|
|
return check;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
try
|
2023-06-06 22:24:43 -04:00
|
|
|
{
|
2023-06-07 11:22:49 -04:00
|
|
|
const resp = await fetch(inPath);
|
|
|
|
const text = await resp.text();
|
2023-06-07 17:09:40 -04:00
|
|
|
const {code} = await SWCW.transform(text, { ...Configuration.SWCOp, filename:inKey});
|
2023-06-07 11:22:49 -04:00
|
|
|
this.Cache.set(inKey, code);
|
|
|
|
return code;
|
2023-06-06 22:24:43 -04:00
|
|
|
}
|
2023-06-07 11:22:49 -04:00
|
|
|
catch(e)
|
2023-06-06 22:24:43 -04:00
|
|
|
{
|
2023-06-07 23:35:00 -04:00
|
|
|
console.log(`Transpile.Fetch error. Key:"${inKey}" Path:"${inPath}" Error:"${e}"`);
|
2023-06-07 11:22:49 -04:00
|
|
|
return null;
|
2023-06-06 22:24:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2023-06-06 12:50:50 -04:00
|
|
|
|
2023-06-07 11:22:49 -04:00
|
|
|
export const Extension =(inPath:string)=>
|
|
|
|
{
|
|
|
|
const posSlash = inPath.lastIndexOf("/");
|
|
|
|
const posDot = inPath.lastIndexOf(".");
|
|
|
|
return posDot > posSlash ? inPath.substring(posDot+1).toLowerCase() : false;
|
|
|
|
};
|
|
|
|
|
2023-06-15 17:13:51 -04:00
|
|
|
export const Configure =(config:ConfigurationArgs)=>
|
|
|
|
{
|
|
|
|
Configuration = {...Configuration, ...config};
|
|
|
|
ImportMapReload();
|
|
|
|
}
|
2023-06-07 14:15:57 -04:00
|
|
|
|
|
|
|
await ImportMapReload();
|
|
|
|
await SWCW.default();
|
2023-06-06 12:50:50 -04:00
|
|
|
HTTP.serve(async(req: Request)=>
|
|
|
|
{
|
|
|
|
const url:URL = new URL(req.url);
|
2023-06-07 11:22:49 -04:00
|
|
|
const ext = Extension(url.pathname);
|
2023-06-07 17:09:40 -04:00
|
|
|
const headers = {"content-type":"application/json", "Access-Control-Allow-Origin": Configuration.Allow, "charset":"UTF-8"};
|
2023-06-07 11:22:49 -04:00
|
|
|
|
|
|
|
// cache-reset route
|
2023-06-07 17:09:40 -04:00
|
|
|
if(url.pathname === Configuration.Reset)
|
2023-06-06 12:50:50 -04:00
|
|
|
{
|
2023-06-07 11:22:49 -04:00
|
|
|
return new Response(`{"cleared":${Transpile.Clear()}}`, {headers});
|
2023-06-06 12:50:50 -04:00
|
|
|
}
|
|
|
|
|
2023-06-07 14:15:57 -04:00
|
|
|
// allow for custom handler
|
2023-06-21 21:20:24 -04:00
|
|
|
const custom = await Configuration.Serve(req, url, ext, ImportMap, Configuration);
|
2023-06-07 14:15:57 -04:00
|
|
|
if(custom)
|
|
|
|
{
|
|
|
|
return custom;
|
|
|
|
}
|
|
|
|
|
2023-06-07 11:22:49 -04:00
|
|
|
// transpileable files
|
|
|
|
if(Transpile.Check(ext))
|
2023-06-06 12:50:50 -04:00
|
|
|
{
|
2023-06-20 21:40:49 -04:00
|
|
|
let code;
|
|
|
|
let path;
|
2023-06-08 16:59:56 -04:00
|
|
|
if(url.pathname.startsWith("/_lib_/"))
|
|
|
|
{
|
2023-06-21 17:32:10 -04:00
|
|
|
const clipRoot = import.meta.url.substring(0, import.meta.url.lastIndexOf("/"));
|
|
|
|
const clipPath = url.pathname.substring(url.pathname.indexOf("/", 1));
|
|
|
|
if(clipPath.startsWith("/boot-"))
|
2023-06-20 21:40:49 -04:00
|
|
|
{
|
2023-06-21 17:32:10 -04:00
|
|
|
path = clipRoot+"/boot-browser.tsx";
|
2023-06-20 21:40:49 -04:00
|
|
|
}
|
|
|
|
else
|
2023-06-08 16:59:56 -04:00
|
|
|
{
|
2023-06-21 17:32:10 -04:00
|
|
|
path = clipRoot + clipPath;
|
2023-06-08 16:59:56 -04:00
|
|
|
}
|
2023-06-21 17:32:10 -04:00
|
|
|
console.log("transpiling", path);
|
2023-06-20 21:40:49 -04:00
|
|
|
code = await Transpile.Fetch(path, url.pathname, true);
|
2023-06-08 16:59:56 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-06-20 21:40:49 -04:00
|
|
|
path = Configuration.Proxy + url.pathname;
|
|
|
|
code = await Transpile.Fetch(path, url.pathname);
|
2023-06-08 16:59:56 -04:00
|
|
|
}
|
2023-06-20 21:40:49 -04:00
|
|
|
|
|
|
|
if(code)
|
|
|
|
{
|
|
|
|
return new Response(code, {headers:{...headers, "content-type":"application/javascript"}} );
|
|
|
|
}
|
2023-06-06 12:50:50 -04:00
|
|
|
}
|
2023-06-07 11:22:49 -04:00
|
|
|
|
2023-06-08 23:19:03 -04:00
|
|
|
// custom page html
|
|
|
|
if(!ext)
|
|
|
|
{
|
2023-06-21 21:20:24 -04:00
|
|
|
const shell = await Configuration.Shell(req, url, ext, ImportMap, Configuration);
|
2023-06-08 23:19:03 -04:00
|
|
|
if(shell)
|
|
|
|
{
|
|
|
|
return shell;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-07 11:22:49 -04:00
|
|
|
// all other static files
|
2023-06-07 14:15:57 -04:00
|
|
|
if(ext)
|
2023-06-06 12:50:50 -04:00
|
|
|
{
|
2023-06-07 14:15:57 -04:00
|
|
|
try
|
|
|
|
{
|
|
|
|
const type = MIME.typeByExtension(ext);
|
2023-06-07 17:09:40 -04:00
|
|
|
const file = await fetch(Configuration.Proxy + url.pathname);
|
2023-06-07 14:15:57 -04:00
|
|
|
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});
|
|
|
|
}
|
2023-06-06 12:50:50 -04:00
|
|
|
}
|
2023-06-07 11:22:49 -04:00
|
|
|
|
2023-06-07 14:15:57 -04:00
|
|
|
return new Response(`{"error":"unmatched route", "path":"${url.pathname}"}`, {status:404, headers});
|
|
|
|
|
2023-06-06 12:50:50 -04:00
|
|
|
});
|