prep work started

This commit is contained in:
Seth Trowbridge 2024-06-12 17:05:41 -04:00
parent 5b1646cf40
commit 85631f2173
8 changed files with 69 additions and 21 deletions

22
README.md Normal file
View File

@ -0,0 +1,22 @@
# Bundle that .TSX in your Deno project for the browser.
## Supports import maps and runs on Deno Deploy
You can 1) run the bundler yourself and collect the transpiled output, or 2) just point the included static file server to some local (or HTTP!) files to get up and running quickly.
### TypeScript API
```typescript
import Bundle from "./mod.ts";
Bundle(
// ESBuild configuration:
{entry:["./app.tsx"]},
// import map:
{"imports": {
"react": "https://esm.sh/preact@10.22.0/compat",
"react/": "https://esm.sh/preact@10.22.0/compat/"
}}
);
```

View File

@ -2,19 +2,7 @@
"name": "@ttf/wasm-bundle", "name": "@ttf/wasm-bundle",
"version": "0.1.0", "version": "0.1.0",
"exports": "./mod.ts", "exports": "./mod.ts",
"imports": {
"react": "https://esm.sh/preact@10.22.0/compat",
"react/": "https://esm.sh/preact@10.22.0/compat/",
"other": "./other.tsx",
"entry": "./app.tsx",
"config": "./deno.json"
},
"tasks": { "tasks": {
"go": "deno run -A bundler-inc.tsx", "serve": "deno run -A --no-lock serve.tsx --path=./test"
"serve": "deno run -A serve.tsx"
},
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "react"
} }
} }

View File

@ -1,11 +1,22 @@
{ {
"version": "3", "version": "3",
"packages": {
"specifiers": {
"jsr:@std/cli": "jsr:@std/cli@0.224.6"
},
"jsr": {
"@std/cli@0.224.6": {
"integrity": "98d7e17a70c1c24980baa2b9a511b29e0a460e7a3b8e3d4e220979d77d5f348e"
}
}
},
"remote": { "remote": {
"https://deno.land/x/esbuild@v0.19.2/wasm.js": "5ffeb3d973e57351eb4d2d03ffafc8ce5672e946d0f0a786c4aed2ca29cec004", "https://deno.land/x/esbuild@v0.19.2/wasm.js": "5ffeb3d973e57351eb4d2d03ffafc8ce5672e946d0f0a786c4aed2ca29cec004",
"https://deno.land/x/jsonct@v0.1.0/mod.ts": "dba7e7f3529be6369f5c718e3a18b69f15ffa176006d2a7565073ce6c5bd9f3f", "https://deno.land/x/jsonct@v0.1.0/mod.ts": "dba7e7f3529be6369f5c718e3a18b69f15ffa176006d2a7565073ce6c5bd9f3f",
"https://deno.land/x/jsonct@v0.1.0/src/_util/asserts.ts": "178dfc49a464aee693a7e285567b3d0b555dc805ff490505a8aae34f9cfb1462", "https://deno.land/x/jsonct@v0.1.0/src/_util/asserts.ts": "178dfc49a464aee693a7e285567b3d0b555dc805ff490505a8aae34f9cfb1462",
"https://deno.land/x/jsonct@v0.1.0/src/parse.ts": "a3a016822446b0584b40bae9098df480db5590a9915c9e3c623ba2801cf1b8df", "https://deno.land/x/jsonct@v0.1.0/src/parse.ts": "a3a016822446b0584b40bae9098df480db5590a9915c9e3c623ba2801cf1b8df",
"https://esm.sh/esbuild-plugin-importmaps@1.0.0": "ab9e79660ff4d57d2ed7ef5e8516fbe0e79305267f22e1e7270d0a17ae0c2029", "https://esm.sh/esbuild-plugin-importmaps@1.0.0": "ab9e79660ff4d57d2ed7ef5e8516fbe0e79305267f22e1e7270d0a17ae0c2029",
"https://esm.sh/mime@4.0.3": "9e2ae6a32fa99974824ef71da6e10a921702bc82dd321802ad14b84ae68b515d",
"https://esm.sh/preact@10.22.0/compat/jsx-runtime": "a2f6ddc2ce374813df1c13826a9ad010e90b5a70a989f1069a367ef60dd52eb0", "https://esm.sh/preact@10.22.0/compat/jsx-runtime": "a2f6ddc2ce374813df1c13826a9ad010e90b5a70a989f1069a367ef60dd52eb0",
"https://esm.sh/stable/preact@10.22.0/denonext/compat.js": "7c0b206984707cfef58efae492ea8d5212b8f620dd8c83294a5c832fb422c766", "https://esm.sh/stable/preact@10.22.0/denonext/compat.js": "7c0b206984707cfef58efae492ea8d5212b8f620dd8c83294a5c832fb422c766",
"https://esm.sh/stable/preact@10.22.0/denonext/compat/jsx-runtime.js": "fecfa3df69d580507801575175087de9a2a9fc23bb4900004a1f4cbd5b362634", "https://esm.sh/stable/preact@10.22.0/denonext/compat/jsx-runtime.js": "fecfa3df69d580507801575175087de9a2a9fc23bb4900004a1f4cbd5b362634",
@ -13,6 +24,9 @@
"https://esm.sh/stable/preact@10.22.0/denonext/jsx-runtime.js": "de60943799b1cbe6066c4f83f4ca71ef37011d7f5be7bef58ed980e8ff3f996a", "https://esm.sh/stable/preact@10.22.0/denonext/jsx-runtime.js": "de60943799b1cbe6066c4f83f4ca71ef37011d7f5be7bef58ed980e8ff3f996a",
"https://esm.sh/stable/preact@10.22.0/denonext/preact.mjs": "20c9563e051dd66e053d3afb450f61b48f2fa0d0ce4f69f8f0a2f23c1ef090da", "https://esm.sh/stable/preact@10.22.0/denonext/preact.mjs": "20c9563e051dd66e053d3afb450f61b48f2fa0d0ce4f69f8f0a2f23c1ef090da",
"https://esm.sh/v135/@jspm/import-map@1.0.8/denonext/import-map.mjs": "fc291e729df6bef849df47df8893b64749785ca65fd5fe1d0e7969db5d3b63ea", "https://esm.sh/v135/@jspm/import-map@1.0.8/denonext/import-map.mjs": "fc291e729df6bef849df47df8893b64749785ca65fd5fe1d0e7969db5d3b63ea",
"https://esm.sh/v135/esbuild-plugin-importmaps@1.0.0/denonext/esbuild-plugin-importmaps.mjs": "08b603d074dd2861345f7d224c255c46d7f7213a283026552c492f465fe595ce" "https://esm.sh/v135/esbuild-plugin-importmaps@1.0.0/denonext/esbuild-plugin-importmaps.mjs": "08b603d074dd2861345f7d224c255c46d7f7213a283026552c492f465fe595ce",
"https://esm.sh/v135/mime@4.0.3/denonext/mime.mjs": "b0fd9fc3e38041cc74ea371133ae2d30e1d610f6c89f038c609a63cce1160d05",
"https://esm.sh/v135/mime@4.0.3/denonext/types/other.js": "45cb4b3bf4f070e2d92a2c60e67570fb73b6aac6672a86da50448a85ee15be1d",
"https://esm.sh/v135/mime@4.0.3/denonext/types/standard.js": "46a2e7cf4eec9dfed5e9928d6597aa01898030654c18e8be71facd4d904e4d1c"
} }
} }

View File

@ -1,16 +1,22 @@
import { parse as JSONC } from "https://deno.land/x/jsonct@v0.1.0/mod.ts"; import { parse as JSONC } from "https://deno.land/x/jsonct@v0.1.0/mod.ts";
import { parseArgs } from "jsr:@std/cli/parse-args";
if(Deno.mainModule == import.meta.url)
{
serve(parseArgs(Deno.args) as ServeArgs);
}
type JsonLike = { [key: string]: string | string[] | JsonLike; }; type JsonLike = { [key: string]: string | string[] | JsonLike; };
/** A `file://` url version of Deno.cwd() */ /** A `file://` url version of Deno.cwd() (contains trailing slash) */
export const Root = new URL(`file://${Deno.cwd().replaceAll("\\", "/")}`).toString() + "/"; export const Root = new URL(`file://${Deno.cwd().replaceAll("\\", "/")}`).toString() + "/";
export default async function HuntConfig() export default async function HuntConfig(directory=Root)
{ {
let path:string, json:JsonLike|undefined; let path:string, json:JsonLike|undefined;
const loadJSON =async(inPath:string)=>{ const loadJSON =async(inPath:string)=>{
try{ try{
const resp = await fetch(Root + inPath); const resp = await fetch(directory + inPath);
if(inPath.endsWith(".jsonc")) if(inPath.endsWith(".jsonc"))
{ {
const text = await resp.text(); const text = await resp.text();

3
jsapi.tsx Normal file
View File

@ -0,0 +1,3 @@
import Bundle from "./mod.ts";
Bundle({entryPoints:["./test/app.tsx"]}, {})

12
mod.ts
View File

@ -31,12 +31,22 @@ const resolvePlugin:ESBuild.Plugin = {
await ESBuild.initialize({ worker: false }); 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):Promise<ESBuild.BuildResult<ESBuild.BuildOptions>> /**
*
* @param buildOptions ESBuild "build" options (will be merged with "reasonable defaults") for docs: https://esbuild.github.io/api/#general-options
* @param importMap
* @returns build result
*/
export default async function(buildOptions={} as BuildOptions, importMap:ImportMap|string|false = false):Promise<ESBuild.BuildResult<ESBuild.BuildOptions>>
{ {
if(importMap === false) if(importMap === false)
{ {
importMap = await Introspect() as ImportMap; importMap = await Introspect() as ImportMap;
} }
else if(typeof importMap === "string")
{
importMap = await Introspect(importMap) as ImportMap;
}
const configuration:ESBuild.BuildOptions = { const configuration:ESBuild.BuildOptions = {
entryPoints: ["entry"], entryPoints: ["entry"],

View File

@ -2,8 +2,6 @@ import bundler from "./mod.ts";
import mime from "https://esm.sh/mime@4.0.3"; import mime from "https://esm.sh/mime@4.0.3";
import { parseArgs } from "jsr:@std/cli/parse-args"; import { parseArgs } from "jsr:@std/cli/parse-args";
if(Deno.mainModule == import.meta.url) if(Deno.mainModule == import.meta.url)
{ {
serve(parseArgs(Deno.args) as ServeArgs); serve(parseArgs(Deno.args) as ServeArgs);
@ -42,6 +40,7 @@ function serve(settings:ServeArgs):void
} }
catch(_e) catch(_e)
{ {
console.log("404", inPath);
return resp404; return resp404;
} }
}; };
@ -92,7 +91,7 @@ function serve(settings:ServeArgs):void
outdir:"/", outdir:"/",
entryNames: `[dir][name]`, entryNames: `[dir][name]`,
splitting: true splitting: true
}); }, basePath);
if(results.outputFiles) if(results.outputFiles)
{ {
results.outputFiles.forEach(output=>transpiled.set(output.path, output.text)) results.outputFiles.forEach(output=>transpiled.set(output.path, output.text))

6
test/deno.json Normal file
View File

@ -0,0 +1,6 @@
{
"imports": {
"react": "https://esm.sh/preact@10.22.0/compat",
"react/": "https://esm.sh/preact@10.22.0/compat/"
}
}