api #3

Merged
SethTrowbridge merged 7 commits from api into master 2023-07-29 20:30:03 -04:00
8 changed files with 70 additions and 61 deletions
Showing only changes of commit 254cd45a2c - Show all commits

1
api.tsx Normal file
View File

@ -0,0 +1 @@
export default ()=>{};

1
app.tsx Normal file
View File

@ -0,0 +1 @@
export default ()=>{};

View File

@ -7,8 +7,6 @@
{
"react":"https://esm.sh/preact@10.15.1/compat",
"react-original":"https://esm.sh/preact@10.15.1/compat",
"able/": "http://localhost:1234/",
"able:app": "./example/app.tsx",
">able/": "https://gitea.hptrow.me/SethTrowbridge/able-baker/raw/branch/master/"
},
"tasks":

View File

@ -1,4 +1,4 @@
import { Router, Switch, Case } from "able/iso-elements.tsx";
import { Router, Switch, Case } from ">able/iso-elements.tsx";
import React from "react";
@ -39,7 +39,7 @@ export default ()=>
return <CTXString.Provider value="intradestink">
<Router.Provider>
<div class="my-4 font-sans">
<h1 class="font-black text-xl text-red-500">Title</h1>
<h1 class="font-black text-xl text-red-500">Title?????</h1>
<h2 class="font-black text-blue-500 p-4">subtitle</h2>
<p>
<button onClick={e=>Dispatch(1)}>{Store.name}|{Store.age}?</button>

View File

@ -3,12 +3,12 @@
"imports":
{
"react": "https://esm.sh/preact@10.15.1/compat",
"able/": "http://localhost:1234/",
"entry": "./app.tsx"
">able/app.tsx": "./app.tsx",
">able/": "http://localhost:4507/"
},
"tasks":
{
"local": "deno run -A --no-lock --reload=http://localhost:1234 http://localhost:1234/run-local.tsx",
"serve": "deno run -A --no-lock --reload=http://localhost:1234 http://localhost:1234/run-serve.tsx"
"local": "deno run -A --no-lock --reload=http://localhost:4507/ http://localhost:4507/run-local.tsx",
"serve": "deno run -A --no-lock --reload=http://localhost:4507/ http://localhost:4507/run-serve.tsx"
}
}

View File

@ -1,15 +0,0 @@
import * as Util from "@able/";
import * as React from "react";
import {createElement} from "react/client";
import('react').then((module) => {
console.log(module);
});
function unimport(n:number)
{
return n;
}
unimport(123)

View File

@ -22,44 +22,55 @@ Configure({
Remap: (inImports, inConfig)=>
{
inImports["react-original"] = inImports["react"];
inImports["react"] = import.meta.resolve(`./hmr-react.tsx`);
inImports["react"] = `/>able/hmr-react.tsx`;
return inImports;
},
async Serve(inReq, inURL, inExt, inMap, inConfig)
{
if(Transpile.Check(inExt) && !inURL.searchParams.get("reload"))
if(!inURL.pathname.startsWith(encodeURI("/>")))
{
// we dont need to add ?reload= because this fetch is by way the file system not the hosted url
const [local, foreign] = await Collect.FileExports(Root+inURL.pathname);
const code =`
import {FileListen} from "${import.meta.resolve(`./hmr-listen.tsx`)}";
import * as Import from "${inURL.pathname}?reload=0";
${ local.map(m=>`let proxy_${m} = Import.${m}; export { proxy_${m} as ${m} };`).join("\n") }
FileListen("${inURL.pathname}", (updatedModule)=>
{
${ local.map(m=>`proxy_${m} = updatedModule.${m};`).join("\n") }
});
${ foreign.join(";\n") }
`
return new Response(code, {headers:{"content-type":"application/javascript"}});
}
if(inReq.headers.get("upgrade") == "websocket")
{
try
if(Transpile.Check(inExt) && !inURL.searchParams.get("reload"))
{
const { response, socket } = Deno.upgradeWebSocket(inReq);
socket.onopen = () => SocketsLive.add(socket);
socket.onclose = () => SocketsLive.delete(socket);
socket.onmessage = (e) => {};
socket.onerror = (e) => console.log("Socket errored:", e);
return response;
// we dont need to add ?reload= because this fetch is by way the file system not the hosted url
console.log("collecting exports for", Root+inURL.pathname);
const [local, foreign] = await Collect.FileExports(Root+inURL.pathname);
const code =`
import {FileListen} from ">able/hmr-listen.tsx";
import * as Import from "${inURL.pathname}?reload=0";
${ local.map(m=>`let proxy_${m} = Import.${m}; export { proxy_${m} as ${m} };`).join("\n") }
FileListen("${inURL.pathname}", (updatedModule)=>
{
${ local.map(m=>`proxy_${m} = updatedModule.${m};`).join("\n") }
});
${ foreign.join(";\n") }
`
return new Response(code, {headers:{"content-type":"application/javascript"}});
}
catch(e){ /**/ }
if(inReq.headers.get("upgrade") == "websocket")
{
try
{
const { response, socket } = Deno.upgradeWebSocket(inReq);
socket.onopen = () => SocketsLive.add(socket);
socket.onclose = () => SocketsLive.delete(socket);
socket.onmessage = (e) => {};
socket.onerror = (e) => console.log("Socket errored:", e);
return response;
}
catch(e){ /**/ }
}
}
else
{
console.log("not collecting", inURL.pathname)
}
}
});
@ -82,7 +93,7 @@ const Watcher =async()=>
const key = path.substring(Deno.cwd().length).replaceAll("\\", "/");
if(action != "remove")
{
const tsx = await Transpile.Fetch(`file://${Deno.cwd().replaceAll("\\", "/")}`+key, key, true);
const tsx = await Transpile.Fetch(Root+key, key, true);
tsx && SocketsSend(key);
}
else

View File

@ -56,10 +56,18 @@ const ImportMapReload =async()=>
{
json.imports[key] = value.substring(1);
}
if(key.startsWith(">")||key.startsWith("able/"))
if(key.startsWith(">"))
{
ImportMapProxies["/"+encodeURI(key)] = value;
json.imports[key] = "/"+key;
if(value.startsWith("./"))
{
ImportMapProxies[encodeURI(key)] = value.substring(1);
json.imports[key] = value.substring(1);
}
else
{
ImportMapProxies["/"+encodeURI(key)] = value;
json.imports[key] = "/"+key;
}
}
});
@ -92,8 +100,8 @@ let Configuration:Configuration =
<div id="app"></div>
<script type="importmap">${JSON.stringify(inMap)}</script>
<script type="module">
import Mount from "${import.meta.resolve("./run-browser.tsx")}";
Mount("#app", "able:app");
import Mount from ">able/run-browser.tsx";
Mount("#app", ">able/app.tsx");
</script>
</body>
</html>`, {status:200, headers:{"content-type":"text/html"}});
@ -296,9 +304,13 @@ const server = Deno.serve({port:parseInt(Deno.env.get("port")||"8000")}, async(r
}
if(bestMatch.length)
{
proxy = ImportMapProxies[bestMatch] + url.pathname.substring(bestMatch.length);
}
const match = ImportMapProxies[bestMatch];
const path = url.pathname.substring(bestMatch.length);
proxy = path ? match + path : Root + match;
}
}
console.log("proxy will be", proxy);
// allow for custom handler
const custom = await Configuration.Serve(req, url, ext, ImportMap, Configuration);
@ -310,6 +322,7 @@ const server = Deno.serve({port:parseInt(Deno.env.get("port")||"8000")}, async(r
// transpileable files
if(Transpile.Check(ext))
{
console.log("transpiling:", proxy);
const code = await Transpile.Fetch(proxy, url.pathname);
if(code)
{