From a0ec91940c32f9310674fcf51381084bdc13ea8a Mon Sep 17 00:00:00 2001 From: Seth Trowbridge Date: Mon, 10 Jun 2024 22:51:23 -0400 Subject: [PATCH] server updates --- app.tsx | 8 +++++++ deno.json | 4 ++++ index.html | 3 ++- mod.ts | 7 +++--- other.tsx | 4 ++-- serve.tsx | 65 ++++++++++++++++++++++++++++++++++-------------------- 6 files changed, 61 insertions(+), 30 deletions(-) diff --git a/app.tsx b/app.tsx index bc9feff..204350f 100644 --- a/app.tsx +++ b/app.tsx @@ -1,2 +1,10 @@ +import * as React from "react" +const dyn = await import("./app-dynamic.tsx"); +console.log(dyn); + + import Other from "./other.tsx"; + console.log(Other); + +React.render(

sup!

, document.querySelector("#app")||document.body); \ No newline at end of file diff --git a/deno.json b/deno.json index 5907466..3ba0237 100644 --- a/deno.json +++ b/deno.json @@ -9,6 +9,10 @@ "entry": "./app.tsx", "config": "./deno.json" }, + "tasks": { + "go": "deno run -A bundler-inc.tsx", + "serve": "deno run -A serve.tsx" + }, "compilerOptions": { "jsx": "react-jsx", "jsxImportSource": "react" diff --git a/index.html b/index.html index 55b0ee2..acdad26 100644 --- a/index.html +++ b/index.html @@ -4,5 +4,6 @@
- + + diff --git a/mod.ts b/mod.ts index 74a9bb2..62c7ac5 100644 --- a/mod.ts +++ b/mod.ts @@ -29,6 +29,8 @@ const resolvePlugin:ESBuild.Plugin = { }, }; +await ESBuild.initialize({ worker: false }); + export type ImportMap = Parameters[0]; export type BuildOptions = ESBuild.BuildOptions; export default async function(buildOptions={} as BuildOptions, importMap:ImportMap|false = false) @@ -55,10 +57,9 @@ export default async function(buildOptions={} as BuildOptions, importMap:ImportM ...buildOptions.plugins||[] ] }; - await ESBuild.initialize({ worker: false }); + const result = await ESBuild.build(configuration); - ESBuild.stop(); + //ESBuild.stop(); return result; } - export { ESBuild }; \ No newline at end of file diff --git a/other.tsx b/other.tsx index cc8968b..0104b81 100644 --- a/other.tsx +++ b/other.tsx @@ -1,3 +1,3 @@ -//import React from "react"; -//console.log(React.createElement("div", {}, "hey")); +import React from "react"; +console.log(React.createElement("div", {}, "hey")); export default "TEST STRING"; \ No newline at end of file diff --git a/serve.tsx b/serve.tsx index 9ad845c..ba093ed 100644 --- a/serve.tsx +++ b/serve.tsx @@ -1,38 +1,55 @@ import bundler from "./mod.ts"; +import {Root} from "./introspect.ts" const transpiled:Map = new Map(); +const ServeJS =(inText:string)=> new Response(inText, {headers:{"content-type":"application/javascript"}}); + +const htmlFile = Deno.readTextFileSync(Deno.cwd()+"/index.html"); + Deno.serve(async(req)=>{ const url = new URL(req.url); const index = url.pathname.lastIndexOf("."); if(index > -1) { - const ext = url.pathname.substring(index+1, index+3); - - console.log(ext); - console.log(url.pathname); - - if(ext === "ts") + const ext = url.pathname.substring(index+1); + if(ext === "ts" || ext == "tsx" || ext == "js" || ext == "jsx") { - const results = await bundler({ - entryPoints:["."+url.pathname], - outfile:"bundle.js", - entryNames: `[dir][name]`, - }); - results.outputFiles?.forEach(output=>{ - transpiled.set(output.path, output.text); - }) - return new Response(` -path: ${results.outputFiles[0].path} -hash: ${results.outputFiles[0].hash} -`, {headers:{"content-type":"application/javascript"}}); - } - - if(ext == "js") - { - return new Response(transpiled.get(url.pathname)||"---", {headers:{"content-type":"application/javascript"}}); + if(ext == "js") + { + const lookup = transpiled.get(url.pathname); + if(lookup) + { + return ServeJS(lookup); + } + } + else + { + const lookup = transpiled.get(url.pathname.substring(0, index)+".js"); + if(lookup) + { + return ServeJS(lookup); + } + } + try + { + const results = await bundler({ + entryPoints:["."+url.pathname], + outdir:"/", + entryNames: `[dir][name]`, + splitting: true + }); + results.outputFiles?.forEach(output=>{ + transpiled.set(output.path, output.text); + }) + return ServeJS(results.outputFiles[0].text); + } + catch(e) + { + return new Response("404", {status:404}); + } } } - return new Response(url.pathname); + return new Response(htmlFile, {headers:{"content-type": "text/html"}}); }); \ No newline at end of file