From cdb9b35e687bdd2051d40bb442685a446f2860bf Mon Sep 17 00:00:00 2001 From: Seth Trowbridge Date: Sat, 29 Apr 2023 11:15:54 -0400 Subject: [PATCH] default configuration for twind --- example/app.tsx | 9 +--- example/{deno.json => deno.jsonc} | 2 +- lib/iso.tsx | 8 ++++ server.tsx | 76 +++++++++++++++++++++++-------- 4 files changed, 66 insertions(+), 29 deletions(-) rename example/{deno.json => deno.jsonc} (84%) diff --git a/example/app.tsx b/example/app.tsx index 2f4abe9..3d6fac1 100644 --- a/example/app.tsx +++ b/example/app.tsx @@ -1,5 +1,3 @@ -import TWPreTail from "https://esm.sh/v115/@twind/preset-tailwind@1.1.4/es2022/preset-tailwind.mjs"; -import TWPreAuto from "https://esm.sh/v115/@twind/preset-autoprefix@1.0.7/es2022/preset-autoprefix.mjs"; import React from "react"; import * as Iso from "@eno/iso"; @@ -30,9 +28,4 @@ export default ()=>

404!

; -}; - -export const CSS = { - presets: [TWPreTail(), TWPreAuto()], - hash:false -}; +}; \ No newline at end of file diff --git a/example/deno.json b/example/deno.jsonc similarity index 84% rename from example/deno.json rename to example/deno.jsonc index ed849e6..19b81ae 100644 --- a/example/deno.json +++ b/example/deno.jsonc @@ -9,6 +9,6 @@ }, "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.jsonc 'http://localhost:4507/server.tsx?reload=1'" } } \ No newline at end of file diff --git a/lib/iso.tsx b/lib/iso.tsx index 1bb54ea..c91cde4 100644 --- a/lib/iso.tsx +++ b/lib/iso.tsx @@ -1,5 +1,13 @@ +import TWPreTail from "https://esm.sh/v115/@twind/preset-tailwind@1.1.4/es2022/preset-tailwind.mjs"; +import TWPreAuto from "https://esm.sh/v115/@twind/preset-autoprefix@1.0.7/es2022/preset-autoprefix.mjs"; import React from "react"; +export const CSS = { + presets: [TWPreTail(), TWPreAuto()], + hash:false +}; + + type Meta = {title:string, description:string, keywords:string, image:string, canonical:string } type MetaKeys = keyof Meta; export const Meta:Meta = { diff --git a/server.tsx b/server.tsx index 385f1ff..1d8ca67 100644 --- a/server.tsx +++ b/server.tsx @@ -1,6 +1,7 @@ import * as ESBuild from 'https://deno.land/x/esbuild@v0.14.45/mod.js'; 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 { parse as JSONC} from "https://deno.land/std@0.185.0/jsonc/mod.ts"; import SSR from "https://esm.sh/v113/preact-render-to-string@6.0.2"; import Prepass from "https://esm.sh/preact-ssr-prepass@1.2.0"; import * as Twind from "https://esm.sh/@twind/core@1.1.3"; @@ -60,11 +61,13 @@ const TranspileURL:Transpiler =async(inPath, inKey, inCheck)=> const LibPath = "lib"; type ImportMap = {imports?:Record, importMap?:string}; +let App, TwindInst; let ImportObject:ImportMap = {}; try { - const confDeno = await fetch(`${Path.Active}/deno.json`); - const confDenoParsed:ImportMap = await confDeno.json(); + const confDeno = await fetch(`${Path.Active}/deno.jsonc`); + const confText = await confDeno.text(); + const confDenoParsed = JSONC(confText) as ImportMap; if(confDenoParsed.importMap) { try @@ -102,16 +105,6 @@ try console.log(`"imports" configuration does not alias "react"`); } - const importApp = ImportObject.imports["@eno/app"]; - if(importApp) - { - - } - else - { - console.log(`"imports" configuration does not alias an entry-point component with "@eno/app"`); - } - const importIso = ImportObject.imports["@eno/iso"]; if(importIso) { @@ -121,6 +114,52 @@ try { } + + const importApp = ImportObject.imports["@eno/app"]; + if(importApp) + { + let appImport + try + { + appImport = await import(Path.Active+importApp); + } + catch(e) + { + console.log(`"@eno/app" entry-point (${importApp}) file not found`) + } + + if(typeof appImport.default == "function" ) + { + App = appImport.default; + } + else + { + console.log(`"@eno/app" entry-point (${importApp}) needs to export a default function to use as the app root.`) + } + + let twindConfig = Iso.CSS; + if(typeof appImport.CSS == "object") + { + twindConfig = {...twindConfig, ...appImport.CSS}; + } + try + { + // @ts-ignore + TwindInst = Twind.install(twindConfig); + } + catch(e) + { + console.log(`CSS configuration is malformed`); + } + + + } + else + { + console.log(`"imports" configuration does not alias an entry-point file as "@eno/app"`); + } + + Object.entries(ImportObject.imports).forEach(([key, value])=>{ if(value.startsWith("./") && ImportObject.imports) { @@ -135,7 +174,7 @@ try } catch(e) { - console.log(`deno.json not found`); + console.log(`error during configuration: ${e}`); } @@ -266,11 +305,11 @@ FileListen("${url.pathname}", reloadHandler);`; @@ -286,9 +325,6 @@ FileListen("${url.pathname}", reloadHandler);`; } }); -import App, {CSS} from "@eno/app"; -const TwindInst = Twind.install(CSS); - const Sockets:Set = new Set(); const SocketsBroadcast =(inData:string)=>{ for (const socket of Sockets){ socket.send(inData); } }