server updates

This commit is contained in:
Seth Trowbridge 2024-06-10 22:51:23 -04:00
parent 0cc2d914e1
commit a0ec91940c
6 changed files with 61 additions and 30 deletions

View File

@ -1,2 +1,10 @@
import * as React from "react"
const dyn = await import("./app-dynamic.tsx");
console.log(dyn);
import Other from "./other.tsx"; import Other from "./other.tsx";
console.log(Other); console.log(Other);
React.render(<h1>sup!</h1>, document.querySelector("#app")||document.body);

View File

@ -9,6 +9,10 @@
"entry": "./app.tsx", "entry": "./app.tsx",
"config": "./deno.json" "config": "./deno.json"
}, },
"tasks": {
"go": "deno run -A bundler-inc.tsx",
"serve": "deno run -A serve.tsx"
},
"compilerOptions": { "compilerOptions": {
"jsx": "react-jsx", "jsx": "react-jsx",
"jsxImportSource": "react" "jsxImportSource": "react"

View File

@ -4,5 +4,6 @@
</head> </head>
<body> <body>
<div id="app"></div> <div id="app"></div>
</body><script src="dump.js" type="module"></script> </body>
<script src="/app.tsx" type="module"></script>
</html> </html>

7
mod.ts
View File

@ -29,6 +29,8 @@ const resolvePlugin:ESBuild.Plugin = {
}, },
}; };
await ESBuild.initialize({ worker: false });
export type ImportMap = Parameters<typeof Mapper.importmapPlugin>[0]; export type ImportMap = Parameters<typeof Mapper.importmapPlugin>[0];
export type BuildOptions = ESBuild.BuildOptions; export type BuildOptions = ESBuild.BuildOptions;
export default async function(buildOptions={} as BuildOptions, importMap:ImportMap|false = false) 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||[] ...buildOptions.plugins||[]
] ]
}; };
await ESBuild.initialize({ worker: false });
const result = await ESBuild.build(configuration); const result = await ESBuild.build(configuration);
ESBuild.stop(); //ESBuild.stop();
return result; return result;
} }
export { ESBuild }; export { ESBuild };

View File

@ -1,3 +1,3 @@
//import React from "react"; import React from "react";
//console.log(React.createElement("div", {}, "hey")); console.log(React.createElement("div", {}, "hey"));
export default "TEST STRING"; export default "TEST STRING";

View File

@ -1,38 +1,55 @@
import bundler from "./mod.ts"; import bundler from "./mod.ts";
import {Root} from "./introspect.ts"
const transpiled:Map<string, string> = new Map(); const transpiled:Map<string, string> = 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)=>{ Deno.serve(async(req)=>{
const url = new URL(req.url); const url = new URL(req.url);
const index = url.pathname.lastIndexOf("."); const index = url.pathname.lastIndexOf(".");
if(index > -1) if(index > -1)
{ {
const ext = url.pathname.substring(index+1, index+3); const ext = url.pathname.substring(index+1);
if(ext === "ts" || ext == "tsx" || ext == "js" || ext == "jsx")
console.log(ext);
console.log(url.pathname);
if(ext === "ts")
{ {
const results = await bundler({ if(ext == "js")
entryPoints:["."+url.pathname], {
outfile:"bundle.js", const lookup = transpiled.get(url.pathname);
entryNames: `[dir][name]`, if(lookup)
}); {
results.outputFiles?.forEach(output=>{ return ServeJS(lookup);
transpiled.set(output.path, output.text); }
}) }
return new Response(` else
path: ${results.outputFiles[0].path} {
hash: ${results.outputFiles[0].hash} const lookup = transpiled.get(url.pathname.substring(0, index)+".js");
`, {headers:{"content-type":"application/javascript"}}); if(lookup)
} {
return ServeJS(lookup);
if(ext == "js") }
{ }
return new Response(transpiled.get(url.pathname)||"---", {headers:{"content-type":"application/javascript"}}); 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"}});
}); });