2023-06-07 17:09:40 -04:00
|
|
|
import {Configure, Transpile, Extension} from "./serve.tsx";
|
2023-06-06 22:24:43 -04:00
|
|
|
|
|
|
|
const SocketsLive:Set<WebSocket> = new Set();
|
|
|
|
const SocketsSend =(inData:string)=>{ console.log(inData); for (const socket of SocketsLive){ socket.send(inData); } }
|
2023-06-07 11:22:49 -04:00
|
|
|
const Directory = `file://${Deno.cwd().replaceAll("\\", "/")}`;
|
|
|
|
|
2023-06-07 14:15:57 -04:00
|
|
|
Configure({
|
2023-06-07 11:22:49 -04:00
|
|
|
Proxy:Directory,
|
|
|
|
SWCOp:
|
|
|
|
{
|
|
|
|
sourceMaps: "inline",
|
|
|
|
minify: false,
|
|
|
|
jsc:
|
|
|
|
{
|
|
|
|
target:"es2022",
|
|
|
|
parser:
|
|
|
|
{
|
|
|
|
syntax: "typescript",
|
|
|
|
tsx: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2023-06-07 23:35:00 -04:00
|
|
|
Remap: (inImports)=>
|
|
|
|
{
|
|
|
|
Object.entries(inImports).forEach(([key, value])=>
|
|
|
|
{
|
|
|
|
if(value.startsWith("./"))
|
|
|
|
{
|
|
|
|
inImports[key] = value.substring(1);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return inImports;
|
|
|
|
},
|
2023-06-08 06:37:01 -04:00
|
|
|
async Serve(inReq, inURL, inExt, inMap)
|
2023-06-06 22:24:43 -04:00
|
|
|
{
|
2023-06-08 06:37:01 -04:00
|
|
|
if(inURL.pathname.startsWith("/hmr/"))
|
|
|
|
{
|
|
|
|
const path = import.meta.url+"/.."+inURL.pathname;
|
|
|
|
const code = await Transpile.Fetch(path, inURL.pathname, true);
|
|
|
|
if(code)
|
|
|
|
{
|
|
|
|
return new Response(code, {headers:{"content-type":"application/javascript"}})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-07 11:22:49 -04:00
|
|
|
if(inReq.headers.get("upgrade") == "websocket")
|
2023-06-06 22:24:43 -04:00
|
|
|
{
|
2023-06-07 11:22:49 -04:00
|
|
|
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-06-07 11:22:49 -04:00
|
|
|
});
|
2023-06-06 22:24:43 -04:00
|
|
|
|
|
|
|
let blocking = false;
|
|
|
|
const filesChanged:Map<string, string> = new Map();
|
|
|
|
for await (const event of Deno.watchFs(Deno.cwd()))
|
|
|
|
{
|
|
|
|
event.paths.forEach( path => filesChanged.set(path, event.kind) );
|
|
|
|
if(!blocking)
|
|
|
|
{
|
|
|
|
blocking = true;
|
|
|
|
setTimeout(async()=>
|
|
|
|
{
|
|
|
|
for await (const [path, action] of filesChanged)
|
|
|
|
{
|
2023-06-07 11:22:49 -04:00
|
|
|
if(Transpile.Check(Extension(path)))
|
2023-06-06 22:24:43 -04:00
|
|
|
{
|
2023-06-07 11:22:49 -04:00
|
|
|
const key = path.substring(Deno.cwd().length).replaceAll("\\", "/");
|
|
|
|
if(action != "remove")
|
|
|
|
{
|
2023-06-07 14:15:57 -04:00
|
|
|
const tsx = await Transpile.Fetch(Directory+key, key, true);
|
2023-06-07 11:22:49 -04:00
|
|
|
tsx && SocketsSend(key);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Transpile.Cache.delete(key);
|
|
|
|
}
|
2023-06-06 22:24:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
filesChanged.clear();
|
|
|
|
blocking = false;
|
|
|
|
}
|
|
|
|
, 1000);
|
|
|
|
}
|
|
|
|
}
|