misc fixes

This commit is contained in:
Seth Trowbridge 2023-06-07 23:35:00 -04:00
parent 0b0396b58d
commit 6878e7e0a4
7 changed files with 53 additions and 23 deletions

View File

@ -1,4 +1,5 @@
{
"deno.enable": true,
"deno.unstable": true
"deno.unstable": true,
"deno.config": "./deno.json"
}

View File

@ -1,8 +1,11 @@
{
"compilerOptions": { "lib": ["deno.window", "dom"]},
"compilerOptions": { "lib": ["deno.window", "dom"],
"jsx": "react-jsx",
"jsxImportSource": "https://esm.sh/preact@10.15.1"
},
"imports":
{
"react":"https://esm.sh/preact@10.13.2/compat",
"react":"https://esm.sh/preact@10.15.1/compat",
"app": "./app.tsx"
}
}

View File

@ -1,4 +1,5 @@
import React from "react";
export default ()=>
{
return <div><h1>hey!</h1></div>

View File

@ -2,7 +2,7 @@
"compilerOptions": { "lib": ["deno.window", "dom"] },
"imports":
{
"react":"https://esm.sh/preact@10.13.2/compat",
"react":"https://esm.sh/preact@10.15.1/compat",
"app": "./app.tsx"
}
}

11
example/deno.lock Normal file
View File

@ -0,0 +1,11 @@
{
"version": "2",
"remote": {
"https://esm.sh/preact@10.13.2/compat/jsx-runtime": "cd2ac8b136b917d804161f394f0838e5b1643cd9a1f4ba7edc52e50caaa419d2",
"https://esm.sh/stable/preact@10.13.2/deno/compat.js": "3151a948abd84aa75dfc9e57733da7e1a45fff7a25de58c7b6025b923874b508",
"https://esm.sh/stable/preact@10.13.2/deno/compat/jsx-runtime.js": "e042cc9c6a59023f70840c4e6f9854fce0486241583e0e471c67d2b6cecf849f",
"https://esm.sh/stable/preact@10.13.2/deno/hooks.js": "c7a8e703bcbc6a05949f329b618c33d5d1ea5fee113ddcea44ff0f527af8556f",
"https://esm.sh/stable/preact@10.13.2/deno/jsx-runtime.js": "dd40c5bdfe7b277bf51009fb950c550dfb554b6d56a1b3a4cb9bc12bde84bcd1",
"https://esm.sh/stable/preact@10.13.2/deno/preact.mjs": "365fab897381f4f403f859c5d12939084560545567108cc90dae901bbe892578"
}
}

View File

@ -20,7 +20,17 @@ Configure({
}
}
},
Remap: (inImports)=>
{
Object.entries(inImports).forEach(([key, value])=>
{
if(value.startsWith("./"))
{
inImports[key] = value.substring(1);
}
});
return inImports;
},
Serve(inReq, inURL, inExt, inMap)
{
if(inReq.headers.get("upgrade") == "websocket")
@ -36,13 +46,8 @@ Configure({
}
catch(e)
{
return new Response(e);
}
}
else
{
return false;
}
}
});

View File

@ -2,22 +2,28 @@ import * as MIME from "https://deno.land/std@0.180.0/media_types/mod.ts";
import * as HTTP from "https://deno.land/std@0.177.0/http/server.ts";
import * as SWCW from "https://esm.sh/@swc/wasm-web@1.3.62";
const ImportMap = {imports:{}};
type DenoConfig = {imports:Record<string, string>};
const ImportMap:DenoConfig = {imports:{}};
const ImportMapReload =async()=>
{
let confText;
let json:DenoConfig;
const path = Configuration.Proxy+"/deno.json";
try
{
confText = await Deno.readTextFile(Deno.cwd()+"\\deno.json");
const resp = await fetch(path);
json = await resp.json();
if(!json?.imports)
{ throw new Error("imports not specified in deno.json") }
}
catch(e)
{
console.log(`No "deno.json" file found at "${Deno.cwd()}"`);
console.log(`error reading deno config "${path}" message:"${e}"`);
return;
}
confText && (ImportMap.imports = Configuration.Remap(JSON.parse(confText)?.imports || {}));
ImportMap.imports = Configuration.Remap(json.imports);
};
type CustomHTTPHandler = (inReq:Request, inURL:URL, inExt:string|false, inMap:{imports:Record<string, string>})=>false|Response|Promise<Response|false>;
type CustomHTTPHandler = (inReq:Request, inURL:URL, inExt:string|false, inMap:{imports:Record<string, string>})=>void|false|Response|Promise<Response|void|false>;
type CustomRemapper = (inImports:Record<string, string>)=>Record<string, string>;
type Configuration = {Proxy:string, Allow:string, Reset:string, SWCOp:SWCW.Options, Serve:CustomHTTPHandler, Shell:CustomHTTPHandler, Remap:CustomRemapper};
type ConfigurationArgs = {Proxy?:string, Allow?:string, Reset?:string, SWCOp?:SWCW.Options, Serve?:CustomHTTPHandler, Shell?:CustomHTTPHandler, Remap?:CustomRemapper};
@ -26,10 +32,7 @@ let Configuration:Configuration =
Proxy: `file://${Deno.cwd().replaceAll("\\", "/")}`,
Allow: "*",
Reset: "/clear-cache",
Serve(inReq, inURL, inExt, inMap)
{
return false;
},
Serve(inReq, inURL, inExt, inMap){},
Remap: (inImports)=>
{
Object.entries(inImports).forEach(([key, value])=>
@ -39,7 +42,13 @@ let Configuration:Configuration =
inImports[key] = value.substring(1);
}
});
Configuration.SWCOp.jsc.transform.react.importSource = inImports["react"];
const reactURL = inImports["react"] ?? console.log("React is not defined in imports");
const setting = Configuration.SWCOp?.jsc?.transform?.react;
if(setting)
{
setting.importSource = reactURL;
}
console.log(inImports);
return inImports;
},
Shell(inReq, inURL, inExt, inMap)
@ -119,7 +128,7 @@ export const Transpile =
}
catch(e)
{
//console.log(`Transpile.Fetch error. Key:"${inKey}" Path:"${inPath}" Error:"${e}"`);
console.log(`Transpile.Fetch error. Key:"${inKey}" Path:"${inPath}" Error:"${e}"`);
return null;
}
}