hmr #1
@ -127,6 +127,6 @@ const ProxyState =(arg)=>
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export * from "react-alias";
|
export * from "react-original";
|
||||||
export { ProxyCreate as createElement, ProxyState as useState };
|
export { ProxyCreate as createElement, ProxyState as useState };
|
||||||
export default {...ReactParts.default, createElement:ProxyCreate, useState:ProxyState};
|
export default {...ReactParts.default, createElement:ProxyCreate, useState:ProxyState};
|
70
server.tsx
70
server.tsx
@ -25,13 +25,42 @@ export const Transpileable =(inFilePath:string):boolean=>
|
|||||||
|
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
export const Transpile =async(inPath:string, inKey:string):Promise<string>=>
|
export const Transpile =async(inCode:string, inKey:string):Promise<string>=>
|
||||||
{
|
{
|
||||||
const body = await Deno.readTextFile(inPath);
|
const transpile = await ESBuild.transform(inCode, { loader: "tsx", sourcemap: "inline", minify:true });
|
||||||
const transpile = await ESBuild.transform(body, { loader: "tsx", sourcemap: "inline", minify:true });
|
|
||||||
Transpiled.set(inKey, transpile.code);
|
Transpiled.set(inKey, transpile.code);
|
||||||
return transpile.code;
|
return transpile.code;
|
||||||
};
|
};
|
||||||
|
type Transpiler = (inPath:string, inKey:string, inCheck?:boolean)=>Promise<string>;
|
||||||
|
export const TranspileFS:Transpiler =async(inPath, inKey, inCheck)=>
|
||||||
|
{
|
||||||
|
if(inCheck)
|
||||||
|
{
|
||||||
|
const cached = Transpiled.get(inKey);
|
||||||
|
if(cached)
|
||||||
|
{
|
||||||
|
return cached;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const body = await Deno.readTextFile(inPath);
|
||||||
|
return Transpile(body, inKey);
|
||||||
|
};
|
||||||
|
export const TranspileURL:Transpiler =async(inPath, inKey, inCheck)=>
|
||||||
|
{
|
||||||
|
if(inCheck)
|
||||||
|
{
|
||||||
|
const cached = Transpiled.get(inKey);
|
||||||
|
if(cached)
|
||||||
|
{
|
||||||
|
return cached;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const path = import.meta.resolve(`.${inPath}`);
|
||||||
|
let body = await fetch(path);
|
||||||
|
let text = await body.text();
|
||||||
|
return Transpile(text, inKey);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
type ImportMap = {imports?:Record<string, string>, importMap?:string};
|
type ImportMap = {imports?:Record<string, string>, importMap?:string};
|
||||||
@ -152,9 +181,6 @@ window.HMR = (path, handler)=>
|
|||||||
</html>
|
</html>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const path = import.meta.resolve(`./hmr/react.sx`);
|
|
||||||
console.log("meta path", path);
|
|
||||||
|
|
||||||
Deno.serve({ port: 3000 }, async(_req:Request) =>
|
Deno.serve({ port: 3000 }, async(_req:Request) =>
|
||||||
{
|
{
|
||||||
const url:URL = new URL(_req.url);
|
const url:URL = new URL(_req.url);
|
||||||
@ -193,21 +219,17 @@ Deno.serve({ port: 3000 }, async(_req:Request) =>
|
|||||||
let type = `text/html`;
|
let type = `text/html`;
|
||||||
let body:BodyInit = Index;
|
let body:BodyInit = Index;
|
||||||
|
|
||||||
if(url.pathname.startsWith("/hmr/"))
|
const isLib = url.pathname.startsWith("/lib/")
|
||||||
{
|
|
||||||
const path = import.meta.resolve(`.${url.pathname}`);
|
|
||||||
console.log("import path", path);
|
|
||||||
const resp = await fetch(path);
|
|
||||||
body = await resp.text();
|
|
||||||
//body = Transpiled.get(url.pathname) || await Transpile("."+url.pathname, url.pathname);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// serve .tsx .jsx .ts .js
|
// serve .tsx .jsx .ts .js
|
||||||
if(Transpileable(url.pathname))
|
if(Transpileable(url.pathname))
|
||||||
{
|
{
|
||||||
type = `application/javascript`;
|
type = `application/javascript`;
|
||||||
if(!url.searchParams.get("reload"))
|
if(isLib)
|
||||||
|
{
|
||||||
|
body = await TranspileURL(url.pathname, url.pathname, true);
|
||||||
|
}
|
||||||
|
else if(!url.searchParams.get("reload"))
|
||||||
{
|
{
|
||||||
const path = `file://${Deno.cwd().replaceAll("\\", "/")+url.pathname}`;
|
const path = `file://${Deno.cwd().replaceAll("\\", "/")+url.pathname}`;
|
||||||
console.log(path);
|
console.log(path);
|
||||||
@ -227,16 +249,22 @@ Deno.serve({ port: 3000 }, async(_req:Request) =>
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
body = Transpiled.get(url.pathname) || await Transpile(fsPath, url.pathname);
|
body = await TranspileFS(fsPath, url.pathname, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// serve static media
|
// serve static media
|
||||||
else if( url.pathname.endsWith("/") === false)
|
else if( url.pathname.endsWith("/") === false)
|
||||||
{
|
{
|
||||||
body = await Deno.readFile(fsPath);
|
|
||||||
type = MIME.typeByExtension(url.pathname.substring(url.pathname.lastIndexOf("."))) || "text/html";
|
type = MIME.typeByExtension(url.pathname.substring(url.pathname.lastIndexOf("."))) || "text/html";
|
||||||
|
if(isLib)
|
||||||
|
{
|
||||||
|
const _fetch = await fetch(import.meta.resolve(`.${url.pathname}`));
|
||||||
|
body = await _fetch.text();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
body = await Deno.readFile(fsPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Response(body, {headers:{"content-type":type as string}});
|
return new Response(body, {headers:{"content-type":type as string}});
|
||||||
@ -264,7 +292,7 @@ const ProcessFiles =debounce(async()=>
|
|||||||
{
|
{
|
||||||
if(action !== "remove")
|
if(action !== "remove")
|
||||||
{
|
{
|
||||||
await Transpile(file, pathname);
|
await TranspileFS(file, pathname, false);
|
||||||
console.log(` ...cached "${pathname}"`);
|
console.log(` ...cached "${pathname}"`);
|
||||||
SocketsBroadcast(pathname);
|
SocketsBroadcast(pathname);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user