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