Compare commits

..

No commits in common. "2d688e71e65e106e7caa9050c08891f1d0ed9c22" and "4039a8b926c3c12416f8c5112b2a10f4e83760c5" have entirely different histories.

View File

@ -3,7 +3,10 @@ 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();
const Transpileable =(inFilePath:string):boolean=>
/**
* checks for (.tsx | .jsx | .ts | .js) extensions
*/
export const Transpileable =(inFilePath:string):boolean=>
{
let dotIndex = inFilePath.length-4;
if(inFilePath[dotIndex] !== ".")
@ -22,14 +25,14 @@ const Transpileable =(inFilePath:string):boolean=>
return false;
};
const Transpile =async(inCode:string, inKey:string):Promise<string>=>
export 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>;
const TranspileFS:Transpiler =async(inPath, inKey, inCheck)=>
export const TranspileFS:Transpiler =async(inPath, inKey, inCheck)=>
{
if(inCheck)
{
@ -42,7 +45,7 @@ const TranspileFS:Transpiler =async(inPath, inKey, inCheck)=>
const body = await Deno.readTextFile(inPath);
return Transpile(body, inKey);
};
const TranspileURL:Transpiler =async(inPath, inKey, inCheck)=>
export const TranspileURL:Transpiler =async(inPath, inKey, inCheck)=>
{
if(inCheck)
{
@ -59,7 +62,7 @@ const TranspileURL:Transpiler =async(inPath, inKey, inCheck)=>
};
const LibPath = "lib";
type ImportMap = {imports?:Record<string, string>, importMap?:string};
let ImportString = ``;
let ImportObject:ImportMap = {};
@ -99,7 +102,8 @@ try
if(importReact)
{
ImportObject.imports["react-original"] = importReact;
ImportObject.imports["react"] = `./${LibPath}/react.tsx`;
ImportObject.imports["react"] = "/lib/react.tsx";
ImportObject.imports["hmr"] = "/lib/hmr.tsx";
ImportString = JSON.stringify(ImportObject);
}
else
@ -117,16 +121,21 @@ 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 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>
</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";
@ -188,7 +197,7 @@ Deno.serve({ port: 3000 }, async(_req:Request) =>
let type = `text/html`;
let body:BodyInit = Index;
const isLib = url.pathname.startsWith(`/${LibPath}/`)
const isLib = url.pathname.startsWith("/lib/")
// serve .tsx .jsx .ts .js
if(Transpileable(url.pathname))
@ -208,7 +217,7 @@ Deno.serve({ port: 3000 }, async(_req:Request) =>
for( const key in imp ) { members.push(key); }
body =
`
import {FileListen} from "/${LibPath}/hmr.tsx";
import {FileListen} from "hmr";
import * as Import from "${url.pathname}?reload=0";
${ members.map(m=>`let proxy_${m} = Import.${m};
export { proxy_${m} as ${m} };
@ -256,36 +265,25 @@ const SocketsBroadcast =(inData:string)=>{ for (const socket of Sockets){ socket
const FilesChanged:Map<string, string> = new Map();
const ProcessFiles =debounce(async()=>
{
console.log("Processing Files...", FilesChanged);
for await (const [path, action] of FilesChanged)
console.log("Files changed...")
for await (const [file, action] of FilesChanged)
{
const key = path.substring(Deno.cwd().length).replaceAll("\\", "/");
console.log(key, action);
if(action != "remove")
const pathname = file.substring(Deno.cwd().length).replaceAll("\\", "/");
console.log(pathname, action);
if(Transpileable(pathname))
{
await TranspileFS(path, key, false);
console.log(` ...cached "${key}"`);
SocketsBroadcast(key);
if(action !== "remove")
{
await TranspileFS(file, pathname, false);
console.log(` ...cached "${pathname}"`);
SocketsBroadcast(pathname);
}
else
{
Transpiled.delete(key)
}
}
FilesChanged.clear();
}, 1000);
for await (const event of Deno.watchFs(Deno.cwd()))
{
event.paths.forEach( path =>
{
if(Transpileable(path))
{
FilesChanged.set(path, event.kind);
}
});
if(FilesChanged.size)
{
event.paths.forEach( path => FilesChanged.set(path, event.kind) );
ProcessFiles();
}
}