use /lib/ as a general location
This commit is contained in:
parent
08ecdfa6f0
commit
0186e40e6f
@ -127,6 +127,6 @@ const ProxyState =(arg)=>
|
||||
|
||||
};
|
||||
|
||||
export * from "react-alias";
|
||||
export * from "react-original";
|
||||
export { ProxyCreate as createElement, ProxyState as useState };
|
||||
export default {...ReactParts.default, createElement:ProxyCreate, useState:ProxyState};
|
82
server.tsx
82
server.tsx
@ -25,13 +25,42 @@ export const Transpileable =(inFilePath:string):boolean=>
|
||||
|
||||
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(body, { loader: "tsx", sourcemap: "inline", minify:true });
|
||||
const transpile = await ESBuild.transform(inCode, { loader: "tsx", sourcemap: "inline", minify:true });
|
||||
Transpiled.set(inKey, 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};
|
||||
@ -152,9 +181,6 @@ window.HMR = (path, handler)=>
|
||||
</html>
|
||||
`;
|
||||
|
||||
const path = import.meta.resolve(`./hmr/react.sx`);
|
||||
console.log("meta path", path);
|
||||
|
||||
Deno.serve({ port: 3000 }, async(_req:Request) =>
|
||||
{
|
||||
const url:URL = new URL(_req.url);
|
||||
@ -193,21 +219,17 @@ Deno.serve({ port: 3000 }, async(_req:Request) =>
|
||||
let type = `text/html`;
|
||||
let body:BodyInit = Index;
|
||||
|
||||
if(url.pathname.startsWith("/hmr/"))
|
||||
{
|
||||
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
|
||||
{
|
||||
const isLib = url.pathname.startsWith("/lib/")
|
||||
|
||||
// serve .tsx .jsx .ts .js
|
||||
if(Transpileable(url.pathname))
|
||||
{
|
||||
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}`;
|
||||
console.log(path);
|
||||
@ -216,27 +238,33 @@ Deno.serve({ port: 3000 }, async(_req:Request) =>
|
||||
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)=>
|
||||
{
|
||||
`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);`;
|
||||
};
|
||||
window.HMR("${url.pathname}", reloadHandler);`;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
body = Transpiled.get(url.pathname) || await Transpile(fsPath, url.pathname);
|
||||
body = await TranspileFS(fsPath, url.pathname, true);
|
||||
}
|
||||
}
|
||||
// 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";
|
||||
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}});
|
||||
@ -264,7 +292,7 @@ const ProcessFiles =debounce(async()=>
|
||||
{
|
||||
if(action !== "remove")
|
||||
{
|
||||
await Transpile(file, pathname);
|
||||
await TranspileFS(file, pathname, false);
|
||||
console.log(` ...cached "${pathname}"`);
|
||||
SocketsBroadcast(pathname);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user