From 9e2b7a575975772445ed75e8e2d756507da38987 Mon Sep 17 00:00:00 2001 From: Seth Trowbridge Date: Mon, 20 Mar 2023 17:41:57 -0400 Subject: [PATCH] HMR started --- server.tsx | 123 ++++++++++++++++++++++++++++++++++++----------------- test.tsx | 1 + 2 files changed, 84 insertions(+), 40 deletions(-) create mode 100644 test.tsx diff --git a/server.tsx b/server.tsx index e86a661..85405c7 100644 --- a/server.tsx +++ b/server.tsx @@ -2,8 +2,6 @@ import * as ESBuild from 'https://deno.land/x/esbuild@v0.14.45/mod.js'; import * as MIME from "https://deno.land/std@0.180.0/media_types/mod.ts"; import { debounce } from "https://deno.land/std@0.151.0/async/debounce.ts"; -console.log(`Serving files from "${Deno.cwd()}"`); - const Transpiled = new Map(); /** * checks for (.tsx | .jsx | .ts | .js) extensions @@ -40,6 +38,7 @@ let ImportString = ``; let ImportObject = {}; try { + const confDeno = await Deno.readTextFile(Deno.cwd()+"/deno.json"); const confDenoParsed:{importMap?:string, imports?:string} = JSON.parse(confDeno); if(confDenoParsed.importMap) @@ -77,6 +76,35 @@ const Index = ` +
Loading
@@ -109,43 +137,6 @@ Deno.serve({ port: 3000 }, async(_req:Request) => console.log(`Request for "${url.pathname}"...`); - try - { - // serve index by default - let type = `text/html`; - let body:BodyInit = Index; - - // serve .tsx .jsx .ts .js - if(Transpileable(url.pathname)) - { - body = Transpiled.get(url.pathname); - type = `application/javascript`; - if(!body) - { - body = await Transpile(fsPath, url.pathname); - } - } - // serve static media - else if( url.pathname.endsWith("/") === false) - { - body = await Deno.readFile(fsPath); - type = MIME.typeByExtension(url.pathname.substring(url.pathname.lastIndexOf("."))) || "text/html"; - } - - return new Response(body, {headers:{"content-type":type as string}}); - } - catch(error) - { - console.log(` ...404`); - return new Response(error, {status:404}); - } -}); - - -const Sockets:Set = new Set(); -const SocketsBroadcast =(inData:string)=>{ for (const socket of Sockets){ socket.send(inData); } } -Deno.serve({ port: 3001 }, (_req:Request) => -{ if(_req.headers.get("upgrade") == "websocket") { try @@ -170,8 +161,60 @@ Deno.serve({ port: 3001 }, (_req:Request) => // } } - return new Response("Overwatch: Not a websocket request."); + + try + { + // serve index by default + let type = `text/html`; + let body:BodyInit = Index; + + // serve .tsx .jsx .ts .js + if(Transpileable(url.pathname)) + { + type = `application/javascript`; + if(!url.searchParams.get("reload")) + { + const path = `file://${Deno.cwd().replaceAll("\\", "/")+url.pathname}`; + console.log(path); + + const imp = await import(path); + const members = []; + for( const key in imp ) { members.push(key); } + body = +`import * as Import from "${url.pathname}?reload=0"; +${ members.map(m=>`let proxy_${m} = Import.${m}; export { proxy_${m} as ${m} };`).join(" ") } +const reloadHandler = (updatedModule)=> +{ + ${ members.map(m=>`proxy_${m} = updatedModule.${m};`).join("\n") } +}; +window.HMR("${url.pathname}", reloadHandler);`; + + } + else + { + body = Transpiled.get(url.pathname) || await Transpile(fsPath, url.pathname); + } + } + // serve static media + else if( url.pathname.endsWith("/") === false) + { + body = await Deno.readFile(fsPath); + type = MIME.typeByExtension(url.pathname.substring(url.pathname.lastIndexOf("."))) || "text/html"; + } + + return new Response(body, {headers:{"content-type":type as string}}); + } + catch(error) + { + console.log(` ...404`); + return new Response(error, {status:404}); + } }); + + +const Sockets:Set = new Set(); +const SocketsBroadcast =(inData:string)=>{ for (const socket of Sockets){ socket.send(inData); } } + const FilesChanged:Map = new Map(); const ProcessFiles =debounce(async()=> { diff --git a/test.tsx b/test.tsx new file mode 100644 index 0000000..e43494b --- /dev/null +++ b/test.tsx @@ -0,0 +1 @@ +export const test = "true"; \ No newline at end of file