start-file #5

Merged
SethTrowbridge merged 5 commits from start-file into master 2023-07-30 14:29:34 -04:00
10 changed files with 194 additions and 98 deletions
Showing only changes of commit 78230a5672 - Show all commits

View File

View File

19
example-config/deno.json Normal file
View File

@ -0,0 +1,19 @@
{
"imports":
{
"react": "https://esm.sh/preact@10.15.1/compat",
">able/": "http://localhost:4507/",
">able/app.tsx": "./app.tsx"
},
"tasks":
{
"serve": "deno run -A --no-lock --config=deno.json --reload=http://localhost:4507 http://localhost:4507/run.tsx",
"local": "deno run -A --no-lock --config=deno.json --reload=http://localhost:4507 http://localhost:4507/run.tsx --dev",
"debug": "deno run -A --no-lock --config=deno.json --reload=http://localhost:4507 --inspect-wait http://localhost:4507/run.tsx",
"cloud": "deno run -A --no-lock --config=deno.json --reload=http://localhost:4507 http://localhost:4507/run.tsx --dep"
},
"compilerOptions":
{
"lib": ["deno.window", "dom"]
}
}

56
example-script/app.tsx Normal file
View File

@ -0,0 +1,56 @@
import { Router, Switch, Case } from ">able/iso-elements.tsx";
import React from "react";
const CTXString = React.createContext("lol");
type StateBinding<T> = [get:T, set:React.StateUpdater<T>];
const CTXState = React.createContext(null) as React.Context<StateBinding<number>|null>;
const Outer =(props:{children:React.JSX.Element})=>
{
const binding = React.useState(11);
return <CTXState.Provider value={binding}>
{props.children}
</CTXState.Provider>
};
const Inner =()=>
{
const [stateGet, stateSet] = React.useContext(CTXState) || ["default", ()=>{}];
return <button onClick={e=>stateSet((old)=>old+1)}>count: {stateGet} :)</button>
};
type Store = {name:string, age:number}
const reducer =(inState:Store, inAction:number)=>
{
return {...inState, age:inState.age+inAction};
}
const builder =(inState:Store):Store=>
{
inState.age = 100;
return inState;
}
export default ()=>
{
const [Store, Dispatch] = React.useReducer(reducer, {name:"seth", age:24} as Store, builder)
return <CTXString.Provider value="intradestink">
<Router.Provider>
<div class="my-4 font-sans">
<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>
</p>
</div>
<Outer>
<Inner/>
</Outer>
<Outer>
<Inner/>
</Outer>
</Router.Provider>
</CTXString.Provider>;
}

View File

@ -9,6 +9,7 @@
{ {
"local": "deno run -A --no-lock --reload=http://localhost:4507 run__.tsx --dev", "local": "deno run -A --no-lock --reload=http://localhost:4507 run__.tsx --dev",
"serve": "deno run -A --no-lock --reload=http://localhost:4507 run__.tsx", "serve": "deno run -A --no-lock --reload=http://localhost:4507 run__.tsx",
"debug": "deno run -A --no-lock --reload=http://localhost:4507 --inspect-wait run__.tsx" "debug": "deno run -A --no-lock --reload=http://localhost:4507 --inspect-wait run__.tsx",
"cloud": "deno run -A --no-lock --reload=http://localhost:4507 run__.tsx --dep"
} }
} }

View File

@ -30,6 +30,7 @@ const collect =async(inKey:string, inArg:Record<string, string>, inEnv:Record<st
console.log("Exiting..."); console.log("Exiting...");
Deno.exit(); Deno.exit();
} }
return scanUser;
}; };
const prompt =async(question: string):Promise<string>=> const prompt =async(question: string):Promise<string>=>
@ -45,8 +46,7 @@ const prompt =async(question: string):Promise<string>=>
try try
{ {
console.log("Runing deploy!"); console.log("Runing deploy!", Deno.mainModule);
const serveScript = import.meta.resolve("./run-serve.tsx");
let arg = parse(Deno.args); let arg = parse(Deno.args);
let env = await Env.load(); let env = await Env.load();
@ -66,7 +66,7 @@ try
`--project=${useProject}`, `--project=${useProject}`,
`--import-map=./deno.json`, `--import-map=./deno.json`,
`--token=${useToken}`, `--token=${useToken}`,
serveScript Deno.mainModule
], ],
stdin: "piped", stdin: "piped",
stdout: "piped" stdout: "piped"

View File

@ -1,6 +1,7 @@
import * as MIME from "https://deno.land/std@0.180.0/media_types/mod.ts"; import * as MIME from "https://deno.land/std@0.180.0/media_types/mod.ts";
import * as SWCW from "https://esm.sh/@swc/wasm-web@1.3.62"; import * as SWCW from "https://esm.sh/@swc/wasm-web@1.3.62";
import CustomServe from ">able/api.tsx";
export const Root = new URL(`file://${Deno.cwd().replaceAll("\\", "/")}`).toString(); export const Root = new URL(`file://${Deno.cwd().replaceAll("\\", "/")}`).toString();
@ -72,11 +73,11 @@ export type Configuration = {Start:string, Allow:string, Reset:string, SWCOp
export type ConfigurationArgs = Partial<Configuration>; export type ConfigurationArgs = Partial<Configuration>;
let Configuration:Configuration = let Configuration:Configuration =
{ {
Start: "", Start: ">able/app.tsx",
Allow: "*", Allow: "*",
Reset: "/clear-cache", Reset: "/clear-cache",
async Extra(inReq, inURL, inExt, inMap, inConfig){}, async Extra(inReq, inURL, inExt, inMap, inConfig){},
async Serve(inReq, inURL, inExt, inMap, inConfig){}, Serve: CustomServe,
Remap: (inImports, inConfig)=> Remap: (inImports, inConfig)=>
{ {
return inImports; return inImports;
@ -258,104 +259,106 @@ export const Configure =(config:ConfigurationArgs)=>
ImportMapReload(); ImportMapReload();
} }
await ImportMapReload(); export default async()=>
try
{ {
await SWCW.default(); await ImportMapReload();
} try
catch(e)
{
console.log("swc init error:", e);
}
const server = Deno.serve({port:parseInt(Deno.env.get("port")||"8000")}, async(req: Request)=>
{
const url:URL = new URL(req.url);
const ext = Extension(url.pathname);
const headers = {"content-type":"application/json", "Access-Control-Allow-Origin": Configuration.Allow, "charset":"UTF-8"};
let proxy = Root + url.pathname;
if(url.pathname.includes("__/") || url.pathname.lastIndexOf("__.") > -1)
{ {
return new Response(`{"error":"unmatched route", "path":"${url.pathname}"}`, {status:404, headers}); await SWCW.default();
} }
catch(e)
// proxy imports
if(url.pathname.startsWith(encodeURI("/>")))
{ {
let bestMatch=""; console.log("swc init error:", e);
for(let key in ImportMapProxies) }
const server = Deno.serve({port:parseInt(Deno.env.get("port")||"8000")}, async(req: Request)=>
{
const url:URL = new URL(req.url);
const ext = Extension(url.pathname);
const headers = {"content-type":"application/json", "Access-Control-Allow-Origin": Configuration.Allow, "charset":"UTF-8"};
let proxy = Root + url.pathname;
if(url.pathname.includes("__/") || url.pathname.lastIndexOf("__.") > -1)
{ {
if(url.pathname.startsWith(key) && key.length > bestMatch.length) return new Response(`{"error":"unmatched route", "path":"${url.pathname}"}`, {status:404, headers});
}
// proxy imports
if(url.pathname.startsWith(encodeURI("/>")))
{
let bestMatch="";
for(let key in ImportMapProxies)
{ {
bestMatch = key; if(url.pathname.startsWith(key) && key.length > bestMatch.length)
{
bestMatch = key;
}
}
if(bestMatch.length)
{
const match = ImportMapProxies[bestMatch];
const path = url.pathname.substring(bestMatch.length);
proxy = path ? match + path : Root + match;
}
}
// allow for custom handlers
const custom = await Configuration.Extra(req, url, ext, ImportMap, Configuration);
if(custom)
{
return custom;
}
const api = await Configuration.Serve(req, url, ext, ImportMap, Configuration);
if(api)
{
return api;
}
// transpileable files
if(Transpile.Check(ext))
{
console.log("transpiling:", proxy);
const code = await Transpile.Fetch(proxy, url.pathname);
if(code)
{
return new Response(code, {headers:{...headers, "content-type":"application/javascript"}} );
}
}
// custom page html
if(!ext)
{
const shell = await Configuration.Shell(req, url, ext, ImportMap, Configuration);
if(shell)
{
return shell;
} }
} }
if(bestMatch.length)
// cache-reset route
if(url.pathname === Configuration.Reset)
{ {
const match = ImportMapProxies[bestMatch]; return new Response(`{"cleared":${Transpile.Clear()}}`, {headers});
const path = url.pathname.substring(bestMatch.length);
proxy = path ? match + path : Root + match;
}
}
// allow for custom handlers
const custom = await Configuration.Extra(req, url, ext, ImportMap, Configuration);
if(custom)
{
return custom;
}
const api = await Configuration.Serve(req, url, ext, ImportMap, Configuration);
if(api)
{
return api;
}
// transpileable files
if(Transpile.Check(ext))
{
console.log("transpiling:", proxy);
const code = await Transpile.Fetch(proxy, url.pathname);
if(code)
{
return new Response(code, {headers:{...headers, "content-type":"application/javascript"}} );
}
}
// custom page html
if(!ext)
{
const shell = await Configuration.Shell(req, url, ext, ImportMap, Configuration);
if(shell)
{
return shell;
} }
}
// all other static files
// cache-reset route if(ext)
if(url.pathname === Configuration.Reset)
{
return new Response(`{"cleared":${Transpile.Clear()}}`, {headers});
}
// all other static files
if(ext)
{
try
{ {
const type = MIME.typeByExtension(ext); try
const file = await fetch(proxy); {
return new Response(file.body, {headers:{...headers, "content-type":type||""}}); const type = MIME.typeByExtension(ext);
const file = await fetch(proxy);
return new Response(file.body, {headers:{...headers, "content-type":type||""}});
}
catch(e)
{
return new Response(`{"error":"${e}", "path":"${url.pathname}"}`, {status:404, headers});
}
} }
catch(e)
{ return new Response(`{"error":"unmatched route", "path":"${url.pathname}"}`, {status:404, headers});
return new Response(`{"error":"${e}", "path":"${url.pathname}"}`, {status:404, headers});
} });
} }
return new Response(`{"error":"unmatched route", "path":"${url.pathname}"}`, {status:404, headers});
});

23
run.tsx
View File

@ -1,5 +1,14 @@
import * as Serve from "./run-serve.tsx"; import * as Serve from "./run-serve.tsx";
export default Serve.Configure;
export default function(config:Serve.ConfigurationArgs)
{
if(Deno.env.get("deploy"))
{
return;
}
Serve.Configure(config)
Serve.default();
}
Deno.args.forEach(arg=> Deno.args.forEach(arg=>
{ {
@ -10,7 +19,15 @@ Deno.args.forEach(arg=>
} }
}); });
if(Deno.env.get("dev")) if(Deno.env.get("dep"))
{ {
import("./run-local.tsx"); import("./run-deploy.tsx");
}
else
{
if(Deno.env.get("dev"))
{
await import("./run-local.tsx");
}
Serve.default();
} }