Compare commits
No commits in common. "d9c5b83f26a42755f0c4b6875e3bb0ea3a99e084" and "77fa38f53155e87ca92a3927e9e3660231e8a764" have entirely different histories.
d9c5b83f26
...
77fa38f531
@ -1,26 +1,18 @@
|
|||||||
import TWPreTail from "https://esm.sh/v115/@twind/preset-tailwind@1.1.4/es2022/preset-tailwind.mjs";
|
import TWPreTail from "https://esm.sh/@twind/preset-tailwind@1.1.4";
|
||||||
import TWPreAuto from "https://esm.sh/v115/@twind/preset-autoprefix@1.0.7/es2022/preset-autoprefix.mjs";
|
import TWPreAuto from "https://esm.sh/@twind/preset-autoprefix@1.0.7";
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import Component from "./deep/component.tsx";
|
import Component from "./deep/component.tsx";
|
||||||
import * as Iso from "@eno/iso";
|
import * as Iso from "@eno/iso";
|
||||||
|
|
||||||
export default ()=>
|
export default ()=>
|
||||||
{
|
{
|
||||||
|
console.log(Iso.Meta);
|
||||||
|
const [countGet, countSet] = React.useState(1);
|
||||||
return <div class="p-4 font-sans">
|
return <div class="p-4 font-sans">
|
||||||
<Iso.Metas title="Main Page!"/>
|
<Iso.Metas title="Main Page!"/>
|
||||||
<h1 class="my-2 font(bold serif) text(2xl)">Title!!</h1>
|
<h1 class="my-2 font(bold serif) text(2xl)">Title!!</h1>
|
||||||
<h2>subtitle</h2>
|
<h2>subtitle</h2>
|
||||||
<Component/>
|
<Component/>
|
||||||
<Iso.Switch>
|
|
||||||
<Iso.Case value="page">
|
|
||||||
<Iso.Switch>
|
|
||||||
<Iso.Case value="about-us">About us!</Iso.Case>
|
|
||||||
<Iso.Case default>sorry no page</Iso.Case>
|
|
||||||
</Iso.Switch>
|
|
||||||
</Iso.Case>
|
|
||||||
<Iso.Case value="lol/idk">lol/idk</Iso.Case>
|
|
||||||
<Iso.Case default><p>404!</p></Iso.Case>
|
|
||||||
</Iso.Switch>
|
|
||||||
</div>;
|
</div>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -4,14 +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+1});}}>{countGet}</button>
|
|
||||||
Component!!!
|
Component!!!
|
||||||
<a href="/page/about-us" className="p-2 text(lg blue-500) font-bold">a link</a>
|
|
||||||
</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'"
|
||||||
}
|
}
|
||||||
}
|
}
|
135
lib/iso.tsx
135
lib/iso.tsx
@ -1,5 +1,6 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
|
|
||||||
|
|
||||||
type Meta = {title:string, description:string, keywords:string, image:string, canonical:string }
|
type Meta = {title:string, description:string, keywords:string, image:string, canonical:string }
|
||||||
type MetaKeys = keyof Meta;
|
type MetaKeys = keyof Meta;
|
||||||
export const Meta:Meta = {
|
export const Meta:Meta = {
|
||||||
@ -21,6 +22,9 @@ export const Metas =(props:{concatListed?:boolean; dropUnlisted?:boolean}&MetasI
|
|||||||
const propValue = props[metaKey]||"";
|
const propValue = props[metaKey]||"";
|
||||||
propValue ? additive(metaKey, propValue) : subtractive(metaKey);
|
propValue ? additive(metaKey, propValue) : subtractive(metaKey);
|
||||||
})
|
})
|
||||||
|
|
||||||
|
console.log(`rendering metas`, Meta)
|
||||||
|
|
||||||
if(window.innerWidth)
|
if(window.innerWidth)
|
||||||
{
|
{
|
||||||
document.title = Meta.title;
|
document.title = Meta.title;
|
||||||
@ -28,134 +32,3 @@ export const Metas =(props:{concatListed?:boolean; dropUnlisted?:boolean}&MetasI
|
|||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export type Children = string | number | React.JSX.Element | React.JSX.Element[];
|
|
||||||
|
|
||||||
type RoutePath = Array<string>;
|
|
||||||
type RouteParams = Record<string, string|number|boolean>;
|
|
||||||
type RouteState = {URL:URL, Path:RoutePath, Params:RouteParams, Anchor:string};
|
|
||||||
type RouteContext = [Route:RouteState, Update:(inPath?:RoutePath, inParams?:RouteParams, inAnchor?:string)=>void];
|
|
||||||
type RouteProps = {children:Children, url?:URL };
|
|
||||||
export const Router = {
|
|
||||||
Parse(url:URL):RouteState
|
|
||||||
{
|
|
||||||
const Path = url.pathname.substring(1, url.pathname.endsWith("/") ? url.pathname.length-1 : url.pathname.length).split("/");
|
|
||||||
const Params:RouteParams = {};
|
|
||||||
new URLSearchParams(url.search).forEach((k, v)=> Params[k] = v);
|
|
||||||
const Anchor = url.hash.substring(1);
|
|
||||||
return {URL:url, Path, Params, Anchor} as RouteState;
|
|
||||||
},
|
|
||||||
Context:React.createContext([{URL:new URL("https://original.route/"), Path:[], Params:{}, Anchor:""}, ()=>{}] as RouteContext),
|
|
||||||
Provider(props:RouteProps)
|
|
||||||
{
|
|
||||||
const [routeGet, routeSet] = React.useState(Router.Parse(props.url || new URL(document.location.href)));
|
|
||||||
const [dirtyGet, dirtySet] = React.useState(true);
|
|
||||||
|
|
||||||
const routeUpdate:RouteContext[1] =(inPath, inParams, inAnchor)=>
|
|
||||||
{
|
|
||||||
const clone = new URL(routeGet.URL);
|
|
||||||
inPath && (clone.pathname = inPath.join("/"));
|
|
||||||
inParams && (clone.search = new URLSearchParams(inParams as Record<string, string>).toString());
|
|
||||||
routeSet({
|
|
||||||
URL:clone,
|
|
||||||
Path: inPath || routeGet.Path,
|
|
||||||
Params: inParams || routeGet.Params,
|
|
||||||
Anchor: inAnchor || routeGet.Anchor
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
// when the state changes, update the page url
|
|
||||||
React.useEffect(()=> dirtyGet ? dirtySet(false) : history.pushState({...routeGet, URL:undefined}, "", routeGet.URL), [routeGet.URL.href]);
|
|
||||||
|
|
||||||
React.useEffect(()=>{
|
|
||||||
history.replaceState({...routeGet, URL:undefined}, "", routeGet.URL);
|
|
||||||
window.addEventListener("popstate", ({state})=>
|
|
||||||
{
|
|
||||||
dirtySet(true);
|
|
||||||
routeUpdate(state.Path, state.Params, state.Anchor);
|
|
||||||
});
|
|
||||||
document.addEventListener("click", e=>
|
|
||||||
{
|
|
||||||
const t = e.target as HTMLAnchorElement;
|
|
||||||
if(t.href)
|
|
||||||
{
|
|
||||||
const u = new URL(t.href);
|
|
||||||
if(u.origin == document.location.origin)
|
|
||||||
{
|
|
||||||
e.preventDefault();
|
|
||||||
const parts = Router.Parse(u);
|
|
||||||
routeUpdate(parts.Path, parts.Params, parts.Anchor);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
return <Router.Context.Provider value={[routeGet, routeUpdate]}>{props.children}</Router.Context.Provider>;
|
|
||||||
},
|
|
||||||
Consumer()
|
|
||||||
{
|
|
||||||
return React.useContext(Router.Context);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
type SwitchContext = {depth:number, keys:Record<string, string>};
|
|
||||||
export const SwitchContext = React.createContext({depth:0, keys:{}} as SwitchContext);
|
|
||||||
export const Switch =({children}:{children:Children})=>
|
|
||||||
{
|
|
||||||
let fallback = null;
|
|
||||||
if(Array.isArray(children))
|
|
||||||
{
|
|
||||||
const contextSelection = React.useContext(SwitchContext);
|
|
||||||
const [contextRoute] = Router.Consumer();
|
|
||||||
const routeSegment = contextRoute.Path.slice(contextSelection.depth);
|
|
||||||
const checkChild =(inChild:{props:{value?:string}})=>
|
|
||||||
{
|
|
||||||
if(inChild?.props?.value)
|
|
||||||
{
|
|
||||||
const parts = inChild.props.value.split("/");
|
|
||||||
if(parts.length > routeSegment.length)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
const output:SwitchContext = {depth:contextSelection.depth+parts.length, keys:{}};
|
|
||||||
for(let i=0; i<parts.length; i++)
|
|
||||||
{
|
|
||||||
const partRoute = routeSegment[i];
|
|
||||||
const partCase = parts[i];
|
|
||||||
if(partCase[0] == ":")
|
|
||||||
{
|
|
||||||
output.keys[partCase.substring(1)] = partRoute;
|
|
||||||
}
|
|
||||||
else if(partCase != "*" && partCase != partRoute)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
for(let i=0; i<children.length; i++)
|
|
||||||
{
|
|
||||||
const childCase = children[i];
|
|
||||||
const childCaseChildren = childCase.props?.__args?.[2] || childCase.props.children;
|
|
||||||
const newContextValue = checkChild(childCase);
|
|
||||||
if(newContextValue)
|
|
||||||
{
|
|
||||||
return <SwitchContext.Provider value={newContextValue}>{childCaseChildren}</SwitchContext.Provider>
|
|
||||||
}
|
|
||||||
if(childCase?.props?.default && !fallback)
|
|
||||||
{
|
|
||||||
console.log(routeSegment);
|
|
||||||
fallback = childCaseChildren;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return fallback;
|
|
||||||
};
|
|
||||||
export const Case =({children, value}:{children:Children, value?:string, default?:true})=>null;
|
|
||||||
export const useRouteVars =()=> React.useContext(SwitchContext).keys;
|
|
64
server.tsx
64
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=>
|
||||||
{
|
{
|
||||||
@ -35,11 +28,24 @@ const Transpileable =(inFilePath:string):boolean=>
|
|||||||
};
|
};
|
||||||
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", 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)=>
|
||||||
|
{
|
||||||
|
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,9 +147,10 @@ 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);
|
||||||
|
|
||||||
@ -181,16 +189,17 @@ Deno.serve({ port: Deno.args[0]||3000 }, async(_req:Request) =>
|
|||||||
|
|
||||||
const isLib = url.pathname.startsWith(`/${LibPath}/`);
|
const isLib = url.pathname.startsWith(`/${LibPath}/`);
|
||||||
|
|
||||||
if(Transpileable(url.pathname))
|
if(Transpileable(pathLast))
|
||||||
{
|
{
|
||||||
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)
|
||||||
|
{
|
||||||
|
const _fetch = await fetch(import.meta.resolve(`.${url.pathname}`));
|
||||||
body = await _fetch.text();
|
body = await _fetch.text();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
const results = Twind.extract(SSR(<Iso.Router.Provider url={url}><App/></Iso.Router.Provider>), TwindInst);
|
body = await Deno.readFile(fsPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
const results = Twind.extract(SSR(<App/>), TwindInst);
|
||||||
|
|
||||||
type = `text/html`;
|
type = `text/html`;
|
||||||
body = `<!doctype html>
|
body = `<!doctype html>
|
||||||
@ -232,16 +248,16 @@ FileListen("${url.pathname}", reloadHandler);`;
|
|||||||
<meta charset="utf-8"/>
|
<meta charset="utf-8"/>
|
||||||
<style data-twind>${results.css}</style>
|
<style data-twind>${results.css}</style>
|
||||||
<script type="importmap">${JSON.stringify(ImportObject)}</script>
|
<script type="importmap">${JSON.stringify(ImportObject)}</script>
|
||||||
|
<script async src="https://ga.jspm.io/npm:es-module-shims@1.5.1/dist/es-module-shims.js" crossorigin="anonymous"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div id="app">${results.html}</div>
|
<div id="app">${results.html}</div>
|
||||||
<script type="module">
|
<script type="module">
|
||||||
import {render, createElement as H} from "react";
|
import {render, createElement as H} from "react";
|
||||||
import * as Twind from "https://esm.sh/v115/@twind/core@1.1.3/es2022/core.mjs";
|
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>`;
|
||||||
@ -273,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