Compare commits
No commits in common. "06266d7ca6c7a79728505428ec0f148bb26e5246" and "0a5d96ce7ab594c469fb98c002ff5cea577fa0ee" have entirely different histories.
06266d7ca6
...
0a5d96ce7a
@ -4,13 +4,8 @@ import * as Iso from "@eno/iso";
|
|||||||
export default ()=>
|
export default ()=>
|
||||||
{
|
{
|
||||||
const [countGet, countSet] = React.useState(1);
|
const [countGet, countSet] = React.useState(1);
|
||||||
|
|
||||||
const [routeGet, routeSet] = Iso.Router.Consumer();
|
|
||||||
|
|
||||||
return <div class="p-4 text-red-500">
|
return <div class="p-4 text-red-500">
|
||||||
<Iso.Metas title="Component!"/>
|
<Iso.Metas title="Component!"/>
|
||||||
Route is: {routeGet.Path.toString()}
|
|
||||||
<button className="p-4 bg-red-500 text-white" onClick={e=>{countSet(countGet+1); routeSet(["lol", "idk"], {count:countGet});}}>{countGet}</button>
|
|
||||||
Component!!!
|
Component!!!
|
||||||
</div>;
|
</div>;
|
||||||
};
|
};
|
@ -8,7 +8,6 @@
|
|||||||
"@eno/iso": "http://localhost:4507/lib/iso.tsx"
|
"@eno/iso": "http://localhost:4507/lib/iso.tsx"
|
||||||
},
|
},
|
||||||
"tasks": {
|
"tasks": {
|
||||||
"host": "deno run -A --unstable https://deno.land/std@0.181.0/http/file_server.ts",
|
|
||||||
"dev": "deno run -A --unstable --reload=http://localhost:4507/ --no-lock --config=deno.json 'http://localhost:4507/server.tsx?reload=1'"
|
"dev": "deno run -A --unstable --reload=http://localhost:4507/ --no-lock --config=deno.json 'http://localhost:4507/server.tsx?reload=1'"
|
||||||
}
|
}
|
||||||
}
|
}
|
46
lib/iso.tsx
46
lib/iso.tsx
@ -31,48 +31,4 @@ export const Metas =(props:{concatListed?:boolean; dropUnlisted?:boolean}&MetasI
|
|||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
type RoutePath = Array<string>;
|
|
||||||
type RouteParams = Record<string, string>;
|
|
||||||
type RouteState = {URL:URL, Path:string[]};
|
|
||||||
type RouteContext = [Route:RouteState, Update:(inPath?:RoutePath, inParams?:RouteParams)=>void];
|
|
||||||
type RouteProps = {children:typeof React.Children, url?:URL };
|
|
||||||
export const Router = {
|
|
||||||
Path(url:URL)
|
|
||||||
{
|
|
||||||
return url.pathname.substring(1, url.pathname.endsWith("/") ? url.pathname.length-1 : url.pathname.length).split("/");
|
|
||||||
},
|
|
||||||
Context:React.createContext([{URL:new URL("https://original.route/"), Path:[]}, ()=>{}] as RouteContext),
|
|
||||||
Provider(props:RouteProps)
|
|
||||||
{
|
|
||||||
const url = props.url || new URL(document.location.href);
|
|
||||||
const path = Router.Path(url);
|
|
||||||
|
|
||||||
const [routeGet, routeSet] = React.useState({URL:url, Path:path} as RouteState);
|
|
||||||
//const routeUpdate:RouteContext[1] =(inURL:URL)=>routeSet({URL:inURL, Path:Router.Path(inURL)});
|
|
||||||
const routeUpdate:RouteContext[1] =(inPath?:RoutePath, inParams?:RouteParams)=>
|
|
||||||
{
|
|
||||||
const clone = new URL(routeGet.URL.href);
|
|
||||||
inPath && (clone.pathname = `/${inPath.join("/")}`);
|
|
||||||
inParams && (clone.search = new URLSearchParams(inParams).toString());
|
|
||||||
routeSet({
|
|
||||||
URL:clone,
|
|
||||||
Path: inPath ? inPath : routeGet.Path
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
React.useEffect(()=>history.pushState({Path:routeGet.Path, Params:routeGet.URL.search}, "", routeGet.URL), [routeGet.URL.href]);
|
|
||||||
React.useEffect(()=>{
|
|
||||||
window.addEventListener("popstate", ()=>routeUpdate());
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
return <Router.Context.Provider value={[routeGet, routeUpdate]}>
|
|
||||||
{props.children}
|
|
||||||
</Router.Context.Provider>;
|
|
||||||
},
|
|
||||||
Consumer()
|
|
||||||
{
|
|
||||||
return React.useContext(Router.Context);
|
|
||||||
}
|
|
||||||
};
|
|
61
server.tsx
61
server.tsx
@ -3,16 +3,9 @@ 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";
|
||||||
import SSR from "https://esm.sh/v113/preact-render-to-string@6.0.2";
|
import SSR from "https://esm.sh/v113/preact-render-to-string@6.0.2";
|
||||||
import * as Twind from "https://esm.sh/@twind/core@1.1.3";
|
import * as Twind from "https://esm.sh/@twind/core@1.1.3";
|
||||||
import React from "react";
|
import React, {JSX, createElement as h} from "react";
|
||||||
import * as Iso from "@eno/iso";
|
import * as Iso from "@eno/iso";
|
||||||
|
|
||||||
let hosted = import.meta.resolve("./");
|
|
||||||
const Path = {
|
|
||||||
Hosted:hosted.substring(0, hosted.length-1),
|
|
||||||
Active:`file://${Deno.cwd().replaceAll("\\", "/")}`
|
|
||||||
};
|
|
||||||
console.log(Path);
|
|
||||||
|
|
||||||
const Transpiled = new Map();
|
const Transpiled = new Map();
|
||||||
const Transpileable =(inFilePath:string):boolean=>
|
const Transpileable =(inFilePath:string):boolean=>
|
||||||
{
|
{
|
||||||
@ -40,6 +33,19 @@ const Transpile =async(inCode:string, inKey:string):Promise<string>=>
|
|||||||
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)=>
|
||||||
|
{
|
||||||
|
if(inCheck)
|
||||||
|
{
|
||||||
|
const cached = Transpiled.get(inKey);
|
||||||
|
if(cached)
|
||||||
|
{
|
||||||
|
return cached;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const body = await Deno.readTextFile(inPath);
|
||||||
|
return Transpile(body, inKey);
|
||||||
|
};
|
||||||
const TranspileURL:Transpiler =async(inPath, inKey, inCheck)=>
|
const TranspileURL:Transpiler =async(inPath, inKey, inCheck)=>
|
||||||
{
|
{
|
||||||
if(inCheck)
|
if(inCheck)
|
||||||
@ -50,7 +56,8 @@ const TranspileURL:Transpiler =async(inPath, inKey, inCheck)=>
|
|||||||
return cached;
|
return cached;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let body = await fetch(inPath);
|
const path = import.meta.resolve(`.${inPath}`);
|
||||||
|
let body = await fetch(path);
|
||||||
let text = await body.text();
|
let text = await body.text();
|
||||||
return Transpile(text, inKey);
|
return Transpile(text, inKey);
|
||||||
};
|
};
|
||||||
@ -61,8 +68,8 @@ type ImportMap = {imports?:Record<string, string>, importMap?:string};
|
|||||||
let ImportObject:ImportMap = {};
|
let ImportObject:ImportMap = {};
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
const confDeno = await fetch(`${Path.Active}/deno.json`);
|
const confDeno = await Deno.readTextFile(Deno.cwd()+"/deno.json");
|
||||||
const confDenoParsed:ImportMap = await confDeno.json();
|
const confDenoParsed:ImportMap = JSON.parse(confDeno);
|
||||||
if(confDenoParsed.importMap)
|
if(confDenoParsed.importMap)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@ -140,13 +147,14 @@ catch(e)
|
|||||||
Deno.serve({ port: Deno.args[0]||3000 }, async(_req:Request) =>
|
Deno.serve({ port: Deno.args[0]||3000 }, async(_req:Request) =>
|
||||||
{
|
{
|
||||||
const url:URL = new URL(_req.url);
|
const url:URL = new URL(_req.url);
|
||||||
|
const fsPath = Deno.cwd()+url.pathname;
|
||||||
const pathParts = url.pathname.substring(1, url.pathname.endsWith("/") ? url.pathname.length-1 : url.pathname.length).split("/");
|
const pathParts = url.pathname.substring(1, url.pathname.endsWith("/") ? url.pathname.length-1 : url.pathname.length).split("/");
|
||||||
const pathLast = pathParts.at(-1);
|
const pathLast = pathParts[pathParts.length-1];
|
||||||
const pathExt:string|undefined = pathLast?.split(".")[1];
|
const pathExt:string|undefined = pathLast.split(".")[1];
|
||||||
|
|
||||||
console.log(pathParts, pathLast, pathExt);
|
console.log(pathParts, pathLast, pathExt);
|
||||||
|
|
||||||
console.log(`Request for "${url.pathname}"...`);
|
console.log(`Request for "${url.pathname}"...`);
|
||||||
|
|
||||||
if(_req.headers.get("upgrade") == "websocket")
|
if(_req.headers.get("upgrade") == "websocket")
|
||||||
{
|
{
|
||||||
@ -186,11 +194,12 @@ Deno.serve({ port: Deno.args[0]||3000 }, async(_req:Request) =>
|
|||||||
type = `application/javascript`;
|
type = `application/javascript`;
|
||||||
if(isLib)
|
if(isLib)
|
||||||
{
|
{
|
||||||
body = await TranspileURL(Path.Hosted+url.pathname, url.pathname, true);
|
body = await TranspileURL(url.pathname, url.pathname, true);
|
||||||
}
|
}
|
||||||
else if(!url.searchParams.get("reload"))
|
else if(!url.searchParams.get("reload"))
|
||||||
{
|
{
|
||||||
const imp = await import(Path.Active+url.pathname);
|
const path = `file://${Deno.cwd().replaceAll("\\", "/")+url.pathname}`;
|
||||||
|
const imp = await import(path);
|
||||||
const members = [];
|
const members = [];
|
||||||
for( const key in imp ) { members.push(key); }
|
for( const key in imp ) { members.push(key); }
|
||||||
body =
|
body =
|
||||||
@ -209,19 +218,26 @@ FileListen("${url.pathname}", reloadHandler);`;
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
body = await TranspileURL(Path.Active+url.pathname, url.pathname, true);
|
body = await TranspileFS(fsPath, url.pathname, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// serve static media
|
// serve static media
|
||||||
else if( pathExt )
|
else if( pathExt )
|
||||||
{
|
{
|
||||||
type = MIME.typeByExtension(pathExt) || "text/html";
|
type = MIME.typeByExtension(pathExt) || "text/html";
|
||||||
const _fetch = await fetch((isLib ? Path.Hosted : Path.Active)+url.pathname);
|
if(isLib)
|
||||||
body = await _fetch.text();
|
{
|
||||||
|
const _fetch = await fetch(import.meta.resolve(`.${url.pathname}`));
|
||||||
|
body = await _fetch.text();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
body = await Deno.readFile(fsPath);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
const results = Twind.extract(SSR(<Iso.Router.Provider url={url}><App/></Iso.Router.Provider>), TwindInst);
|
const results = Twind.extract(SSR(<App/>), TwindInst);
|
||||||
|
|
||||||
type = `text/html`;
|
type = `text/html`;
|
||||||
body = `<!doctype html>
|
body = `<!doctype html>
|
||||||
@ -240,9 +256,8 @@ FileListen("${url.pathname}", reloadHandler);`;
|
|||||||
import {render, createElement as H} from "react";
|
import {render, createElement as H} from "react";
|
||||||
import * as Twind from "https://esm.sh/@twind/core@1.1.3";
|
import * as Twind from "https://esm.sh/@twind/core@1.1.3";
|
||||||
import {default as App, CSS} from "@eno/app";
|
import {default as App, CSS} from "@eno/app";
|
||||||
import {Router} from "@eno/iso";
|
|
||||||
Twind.install(CSS);
|
Twind.install(CSS);
|
||||||
render( H(Router.Provider, null, H(App)), document.querySelector("#app"));
|
render(H(()=>H(App)), document.querySelector("#app"));
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>`;
|
</html>`;
|
||||||
@ -274,7 +289,7 @@ const ProcessFiles =debounce(async()=>
|
|||||||
|
|
||||||
if(action != "remove")
|
if(action != "remove")
|
||||||
{
|
{
|
||||||
await TranspileURL(Path.Active+key, key, false);
|
await TranspileFS(path, key, false);
|
||||||
console.log(` ...cached "${key}"`);
|
console.log(` ...cached "${key}"`);
|
||||||
SocketsBroadcast(key);
|
SocketsBroadcast(key);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user