able-baker/run-local.tsx

115 lines
3.6 KiB
TypeScript
Raw Permalink Normal View History

import {Configure, Transpile, Extension} from "./run-serve.tsx";
import { Root } from "./checker.tsx";
2023-07-16 21:29:50 -04:00
import * as Collect from "./hmr-static.tsx";
2023-06-06 22:24:43 -04:00
const SocketsLive:Set<WebSocket> = new Set();
2023-06-21 21:31:21 -04:00
const SocketsSend =(inData:string)=>{ for (const socket of SocketsLive){ socket.send(inData); } }
2023-06-07 11:22:49 -04:00
2023-06-07 14:15:57 -04:00
Configure({
2023-06-07 11:22:49 -04:00
SWCOp:
{
sourceMaps: "inline",
minify: false,
jsc:
{
target:"es2022",
parser:
{
syntax: "typescript",
tsx: true,
2023-09-24 09:19:50 -04:00
},
transform:
{
react: { runtime: "automatic" }
2023-06-07 11:22:49 -04:00
}
}
},
2023-06-21 21:20:24 -04:00
Remap: (inImports, inConfig)=>
2023-06-07 23:35:00 -04:00
{
inImports["react-original"] = inImports["react"];
2023-07-29 16:45:41 -04:00
inImports["react"] = `/>able/hmr-react.tsx`;
2023-10-12 22:47:45 -04:00
inImports["signals-original"] = inImports["@preact/signals"];
2023-10-14 23:47:20 -04:00
inImports["@preact/signals"] = `/>able/hmr-signal.tsx`;
2023-10-12 22:47:45 -04:00
2023-06-07 23:35:00 -04:00
return inImports;
},
2023-07-30 09:16:17 -04:00
async Extra(inReq, inURL, inExt, inMap, inConfig)
2023-06-06 22:24:43 -04:00
{
2023-07-29 16:45:41 -04:00
if(!inURL.pathname.startsWith(encodeURI("/>")))
{
2023-07-29 16:45:41 -04:00
if(Transpile.Check(inExt) && !inURL.searchParams.get("reload"))
{
2023-06-15 17:50:55 -04:00
2023-07-29 16:45:41 -04:00
// 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(Root+inURL.pathname);
const code =`
import {FileListen} from ">able/hmr-listen.tsx";
import * as Import from "${inURL.pathname}?reload=0";
${ local.map(m=>`let proxy_${m} = Import.${m}; export { proxy_${m} as ${m} };`).join("\n") }
FileListen("${inURL.pathname}", (updatedModule)=>
{
${ local.map(m=>`proxy_${m} = updatedModule.${m};`).join("\n") }
});
${ foreign.join(";\n") }
`
2023-07-29 16:45:41 -04:00
return new Response(code, {headers:{"content-type":"application/javascript"}});
2023-06-07 11:22:49 -04:00
}
2023-07-29 16:45:41 -04:00
if(inReq.headers.get("upgrade") == "websocket")
{
try
{
const { response, socket } = Deno.upgradeWebSocket(inReq);
socket.onopen = () => SocketsLive.add(socket);
socket.onclose = () => SocketsLive.delete(socket);
socket.onmessage = (e) => {};
socket.onerror = (e) => console.log("Socket errored:", e);
return response;
}
catch(e){ /**/ }
}
2023-06-06 22:24:43 -04:00
}
2023-07-29 16:45:41 -04:00
2023-06-06 22:24:43 -04:00
}
2023-06-07 11:22:49 -04:00
});
2023-06-06 22:24:43 -04:00
2023-06-20 21:40:49 -04:00
const Watcher =async()=>
2023-06-06 22:24:43 -04:00
{
2023-06-20 21:40:49 -04:00
let blocking = false;
const filesChanged:Map<string, string> = new Map();
for await (const event of Deno.watchFs(Deno.cwd()))
2023-06-06 22:24:43 -04:00
{
2023-06-20 21:40:49 -04:00
event.paths.forEach( path => filesChanged.set(path, event.kind) );
if(!blocking)
2023-06-06 22:24:43 -04:00
{
2023-06-20 21:40:49 -04:00
blocking = true;
setTimeout(async()=>
2023-06-06 22:24:43 -04:00
{
2023-06-20 21:40:49 -04:00
for await (const [path, action] of filesChanged)
2023-06-06 22:24:43 -04:00
{
2023-06-20 21:40:49 -04:00
if(Transpile.Check(Extension(path)))
2023-06-07 11:22:49 -04:00
{
2023-06-20 21:40:49 -04:00
const key = path.substring(Deno.cwd().length).replaceAll("\\", "/");
if(action != "remove")
{
2023-07-29 16:45:41 -04:00
const tsx = await Transpile.Fetch(Root+key, key, true);
2023-06-20 21:40:49 -04:00
tsx && SocketsSend(key);
}
else
{
Transpile.Cache.delete(key);
}
2023-06-07 11:22:49 -04:00
}
2023-06-06 22:24:43 -04:00
}
2023-06-20 21:40:49 -04:00
filesChanged.clear();
blocking = false;
2023-06-06 22:24:43 -04:00
}
2023-06-20 21:40:49 -04:00
, 1000);
2023-06-06 22:24:43 -04:00
}
}
2023-06-20 21:40:49 -04:00
}
2023-06-21 21:31:21 -04:00
Watcher();