From 78230a5672544e3083e7f5a1e11b47aec5c67861 Mon Sep 17 00:00:00 2001 From: Seth Trowbridge Date: Sun, 30 Jul 2023 13:04:26 -0400 Subject: [PATCH] allow for code or config setup --- api => api.tsx | 0 app => app.tsx | 0 {example => example-config}/app.tsx | 0 example-config/deno.json | 19 +++ example-script/app.tsx | 56 ++++++++ {example => example-script}/deno.json | 3 +- {example => example-script}/run__.tsx | 0 run-deploy.tsx | 6 +- run-serve.tsx | 185 +++++++++++++------------- run.tsx | 23 +++- 10 files changed, 194 insertions(+), 98 deletions(-) rename api => api.tsx (100%) rename app => app.tsx (100%) rename {example => example-config}/app.tsx (100%) create mode 100644 example-config/deno.json create mode 100644 example-script/app.tsx rename {example => example-script}/deno.json (78%) rename {example => example-script}/run__.tsx (100%) diff --git a/api b/api.tsx similarity index 100% rename from api rename to api.tsx diff --git a/app b/app.tsx similarity index 100% rename from app rename to app.tsx diff --git a/example/app.tsx b/example-config/app.tsx similarity index 100% rename from example/app.tsx rename to example-config/app.tsx diff --git a/example-config/deno.json b/example-config/deno.json new file mode 100644 index 0000000..ae6483b --- /dev/null +++ b/example-config/deno.json @@ -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"] + } +} \ No newline at end of file diff --git a/example-script/app.tsx b/example-script/app.tsx new file mode 100644 index 0000000..88f14aa --- /dev/null +++ b/example-script/app.tsx @@ -0,0 +1,56 @@ +import { Router, Switch, Case } from ">able/iso-elements.tsx"; + +import React from "react"; + +const CTXString = React.createContext("lol"); + +type StateBinding = [get:T, set:React.StateUpdater]; +const CTXState = React.createContext(null) as React.Context|null>; +const Outer =(props:{children:React.JSX.Element})=> +{ + const binding = React.useState(11); + return + {props.children} + +}; +const Inner =()=> +{ + const [stateGet, stateSet] = React.useContext(CTXState) || ["default", ()=>{}]; + return +}; + + +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 + +
+

Title

+

subtitle

+

+ +

+
+ + + + + + +
+
; +} diff --git a/example/deno.json b/example-script/deno.json similarity index 78% rename from example/deno.json rename to example-script/deno.json index a27803a..c8e26a5 100644 --- a/example/deno.json +++ b/example-script/deno.json @@ -9,6 +9,7 @@ { "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", - "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" } } \ No newline at end of file diff --git a/example/run__.tsx b/example-script/run__.tsx similarity index 100% rename from example/run__.tsx rename to example-script/run__.tsx diff --git a/run-deploy.tsx b/run-deploy.tsx index 4d20a1c..d2ab39a 100644 --- a/run-deploy.tsx +++ b/run-deploy.tsx @@ -30,6 +30,7 @@ const collect =async(inKey:string, inArg:Record, inEnv:Record=> @@ -45,8 +46,7 @@ const prompt =async(question: string):Promise=> try { - console.log("Runing deploy!"); - const serveScript = import.meta.resolve("./run-serve.tsx"); + console.log("Runing deploy!", Deno.mainModule); let arg = parse(Deno.args); let env = await Env.load(); @@ -66,7 +66,7 @@ try `--project=${useProject}`, `--import-map=./deno.json`, `--token=${useToken}`, - serveScript + Deno.mainModule ], stdin: "piped", stdout: "piped" diff --git a/run-serve.tsx b/run-serve.tsx index a31b014..2c45b08 100644 --- a/run-serve.tsx +++ b/run-serve.tsx @@ -1,6 +1,7 @@ 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 CustomServe from ">able/api.tsx"; 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; let Configuration:Configuration = { - Start: "", + Start: ">able/app.tsx", Allow: "*", Reset: "/clear-cache", async Extra(inReq, inURL, inExt, inMap, inConfig){}, - async Serve(inReq, inURL, inExt, inMap, inConfig){}, + Serve: CustomServe, Remap: (inImports, inConfig)=> { return inImports; @@ -258,104 +259,106 @@ export const Configure =(config:ConfigurationArgs)=> ImportMapReload(); } -await ImportMapReload(); -try +export default async()=> { - await SWCW.default(); -} -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) + await ImportMapReload(); + try { - return new Response(`{"error":"unmatched route", "path":"${url.pathname}"}`, {status:404, headers}); + await SWCW.default(); } - - // proxy imports - if(url.pathname.startsWith(encodeURI("/>"))) + catch(e) { - let bestMatch=""; - for(let key in ImportMapProxies) + 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) { - 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]; - 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; + return new Response(`{"cleared":${Transpile.Clear()}}`, {headers}); } - } - - // cache-reset route - if(url.pathname === Configuration.Reset) - { - return new Response(`{"cleared":${Transpile.Clear()}}`, {headers}); - } - - // all other static files - if(ext) - { - try + + // all other static files + if(ext) { - const type = MIME.typeByExtension(ext); - const file = await fetch(proxy); - return new Response(file.body, {headers:{...headers, "content-type":type||""}}); + try + { + 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":"${e}", "path":"${url.pathname}"}`, {status:404, headers}); - } - } - - return new Response(`{"error":"unmatched route", "path":"${url.pathname}"}`, {status:404, headers}); - -}); - + + return new Response(`{"error":"unmatched route", "path":"${url.pathname}"}`, {status:404, headers}); + + }); +} diff --git a/run.tsx b/run.tsx index 047650f..1fa0c64 100644 --- a/run.tsx +++ b/run.tsx @@ -1,5 +1,14 @@ 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=> { @@ -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(); } \ No newline at end of file