Compare commits

..

2 Commits

Author SHA1 Message Date
2d688e71e6 only monitor transpileable files 2023-04-01 10:27:24 -04:00
85bfaf5386 variablaize lib path 2023-04-01 08:44:39 -04:00

View File

@ -3,10 +3,7 @@ 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";
const Transpiled = new Map();
/**
* checks for (.tsx | .jsx | .ts | .js) extensions
*/
export const Transpileable =(inFilePath:string):boolean=>
const Transpileable =(inFilePath:string):boolean=>
{
let dotIndex = inFilePath.length-4;
if(inFilePath[dotIndex] !== ".")
@ -25,14 +22,14 @@ export const Transpileable =(inFilePath:string):boolean=>
return false;
};
export const Transpile =async(inCode:string, inKey:string):Promise<string>=>
const Transpile =async(inCode:string, inKey:string):Promise<string>=>
{
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)=>
const TranspileFS:Transpiler =async(inPath, inKey, inCheck)=>
{
if(inCheck)
{
@ -45,7 +42,7 @@ export const TranspileFS:Transpiler =async(inPath, inKey, inCheck)=>
const body = await Deno.readTextFile(inPath);
return Transpile(body, inKey);
};
export const TranspileURL:Transpiler =async(inPath, inKey, inCheck)=>
const TranspileURL:Transpiler =async(inPath, inKey, inCheck)=>
{
if(inCheck)
{
@ -62,7 +59,7 @@ export const TranspileURL:Transpiler =async(inPath, inKey, inCheck)=>
};
const LibPath = "lib";
type ImportMap = {imports?:Record<string, string>, importMap?:string};
let ImportString = ``;
let ImportObject:ImportMap = {};
@ -102,8 +99,7 @@ try
if(importReact)
{
ImportObject.imports["react-original"] = importReact;
ImportObject.imports["react"] = "/lib/react.tsx";
ImportObject.imports["hmr"] = "/lib/hmr.tsx";
ImportObject.imports["react"] = `./${LibPath}/react.tsx`;
ImportString = JSON.stringify(ImportObject);
}
else
@ -121,21 +117,16 @@ catch(e)
{
console.log(`deno.json not found`);
}
const Index = `
<!doctype html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>
</script>
<script type="importmap">${ImportString}</script>
<script async src="https://ga.jspm.io/npm:es-module-shims@1.5.1/dist/es-module-shims.js" crossorigin="anonymous"></script>
</head>
<body>
<div id="app">Loading</div>
<script type="importmap">${ImportString}</script>
<script async src="https://ga.jspm.io/npm:es-module-shims@1.5.1/dist/es-module-shims.js" crossorigin="anonymous"></script>
<script type="module">
import * as TW from "https://esm.sh/@twind/core@1.0.1";
import TWPreTail from "https://esm.sh/@twind/preset-tailwind@1.0.1";
@ -197,7 +188,7 @@ Deno.serve({ port: 3000 }, async(_req:Request) =>
let type = `text/html`;
let body:BodyInit = Index;
const isLib = url.pathname.startsWith("/lib/")
const isLib = url.pathname.startsWith(`/${LibPath}/`)
// serve .tsx .jsx .ts .js
if(Transpileable(url.pathname))
@ -217,7 +208,7 @@ Deno.serve({ port: 3000 }, async(_req:Request) =>
for( const key in imp ) { members.push(key); }
body =
`
import {FileListen} from "hmr";
import {FileListen} from "/${LibPath}/hmr.tsx";
import * as Import from "${url.pathname}?reload=0";
${ members.map(m=>`let proxy_${m} = Import.${m};
export { proxy_${m} as ${m} };
@ -265,25 +256,36 @@ const SocketsBroadcast =(inData:string)=>{ for (const socket of Sockets){ socket
const FilesChanged:Map<string, string> = new Map();
const ProcessFiles =debounce(async()=>
{
console.log("Files changed...")
for await (const [file, action] of FilesChanged)
console.log("Processing Files...", FilesChanged);
for await (const [path, action] of FilesChanged)
{
const pathname = file.substring(Deno.cwd().length).replaceAll("\\", "/");
console.log(pathname, action);
if(Transpileable(pathname))
const key = path.substring(Deno.cwd().length).replaceAll("\\", "/");
console.log(key, action);
if(action != "remove")
{
if(action !== "remove")
{
await TranspileFS(file, pathname, false);
console.log(` ...cached "${pathname}"`);
SocketsBroadcast(pathname);
await TranspileFS(path, key, false);
console.log(` ...cached "${key}"`);
SocketsBroadcast(key);
}
else
{
Transpiled.delete(key)
}
}
FilesChanged.clear();
}, 1000);
for await (const event of Deno.watchFs(Deno.cwd()))
{
event.paths.forEach( path => FilesChanged.set(path, event.kind) );
event.paths.forEach( path =>
{
if(Transpileable(path))
{
FilesChanged.set(path, event.kind);
}
});
if(FilesChanged.size)
{
ProcessFiles();
}
}