From 85631f217392d07d9db73834acc914b4a054fcbe Mon Sep 17 00:00:00 2001 From: Seth Trowbridge Date: Wed, 12 Jun 2024 17:05:41 -0400 Subject: [PATCH] prep work started --- README.md | 22 ++++++++++++++++++++++ deno.json | 14 +------------- deno.lock | 16 +++++++++++++++- introspect.ts | 12 +++++++++--- jsapi.tsx | 3 +++ mod.ts | 12 +++++++++++- serve.tsx | 5 ++--- test/deno.json | 6 ++++++ 8 files changed, 69 insertions(+), 21 deletions(-) create mode 100644 README.md create mode 100644 jsapi.tsx create mode 100644 test/deno.json diff --git a/README.md b/README.md new file mode 100644 index 0000000..f8588ee --- /dev/null +++ b/README.md @@ -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/" + }} +); +``` + diff --git a/deno.json b/deno.json index 432c3c4..2f4acd2 100644 --- a/deno.json +++ b/deno.json @@ -2,19 +2,7 @@ "name": "@ttf/wasm-bundle", "version": "0.1.0", "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": { - "go": "deno run -A bundler-inc.tsx", - "serve": "deno run -A serve.tsx" - }, - "compilerOptions": { - "jsx": "react-jsx", - "jsxImportSource": "react" + "serve": "deno run -A --no-lock serve.tsx --path=./test" } } \ No newline at end of file diff --git a/deno.lock b/deno.lock index 79ae698..2ea5425 100644 --- a/deno.lock +++ b/deno.lock @@ -1,11 +1,22 @@ { "version": "3", + "packages": { + "specifiers": { + "jsr:@std/cli": "jsr:@std/cli@0.224.6" + }, + "jsr": { + "@std/cli@0.224.6": { + "integrity": "98d7e17a70c1c24980baa2b9a511b29e0a460e7a3b8e3d4e220979d77d5f348e" + } + } + }, "remote": { "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/src/_util/asserts.ts": "178dfc49a464aee693a7e285567b3d0b555dc805ff490505a8aae34f9cfb1462", "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/mime@4.0.3": "9e2ae6a32fa99974824ef71da6e10a921702bc82dd321802ad14b84ae68b515d", "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/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/preact.mjs": "20c9563e051dd66e053d3afb450f61b48f2fa0d0ce4f69f8f0a2f23c1ef090da", "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" } } diff --git a/introspect.ts b/introspect.ts index 856f222..7b66836 100644 --- a/introspect.ts +++ b/introspect.ts @@ -1,16 +1,22 @@ 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; }; -/** 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 default async function HuntConfig() +export default async function HuntConfig(directory=Root) { let path:string, json:JsonLike|undefined; const loadJSON =async(inPath:string)=>{ try{ - const resp = await fetch(Root + inPath); + const resp = await fetch(directory + inPath); if(inPath.endsWith(".jsonc")) { const text = await resp.text(); diff --git a/jsapi.tsx b/jsapi.tsx new file mode 100644 index 0000000..8512104 --- /dev/null +++ b/jsapi.tsx @@ -0,0 +1,3 @@ +import Bundle from "./mod.ts"; + +Bundle({entryPoints:["./test/app.tsx"]}, {}) diff --git a/mod.ts b/mod.ts index bd13d7c..b8e51d9 100644 --- a/mod.ts +++ b/mod.ts @@ -31,12 +31,22 @@ 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):Promise> +/** + * + * @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> { if(importMap === false) { importMap = await Introspect() as ImportMap; } + else if(typeof importMap === "string") + { + importMap = await Introspect(importMap) as ImportMap; + } const configuration:ESBuild.BuildOptions = { entryPoints: ["entry"], diff --git a/serve.tsx b/serve.tsx index b516f21..8c1a098 100644 --- a/serve.tsx +++ b/serve.tsx @@ -2,8 +2,6 @@ import bundler from "./mod.ts"; import mime from "https://esm.sh/mime@4.0.3"; import { parseArgs } from "jsr:@std/cli/parse-args"; - - if(Deno.mainModule == import.meta.url) { serve(parseArgs(Deno.args) as ServeArgs); @@ -42,6 +40,7 @@ function serve(settings:ServeArgs):void } catch(_e) { + console.log("404", inPath); return resp404; } }; @@ -92,7 +91,7 @@ function serve(settings:ServeArgs):void outdir:"/", entryNames: `[dir][name]`, splitting: true - }); + }, basePath); if(results.outputFiles) { results.outputFiles.forEach(output=>transpiled.set(output.path, output.text)) diff --git a/test/deno.json b/test/deno.json new file mode 100644 index 0000000..a306003 --- /dev/null +++ b/test/deno.json @@ -0,0 +1,6 @@ +{ + "imports": { + "react": "https://esm.sh/preact@10.22.0/compat", + "react/": "https://esm.sh/preact@10.22.0/compat/" + } +} \ No newline at end of file