diff --git a/deno.json b/deno.json index 820ca77..a245428 100644 --- a/deno.json +++ b/deno.json @@ -1,9 +1,9 @@ { - "name": "@ttf/wasm-bundle", - "version": "0.1.0", - "exports": "./mod.ts", - "tasks": { - "serve": "deno run -A --no-lock serve.tsx --path=./test --html=index.html", - "jsapi": "deno run -A --no-lock jsapi.tsx" - } -} \ No newline at end of file + "name": "@ttf/wasm-bundle", + "version": "0.1.0", + "exports": "./mod.ts", + "tasks": { + "serve": "deno run -A --no-lock serve.tsx --path=./test --html=index.html", + "jsapi": "deno run -A --no-lock jsapi.tsx" + } +} diff --git a/introspect.ts b/introspect.ts index f57f56a..52b1013 100644 --- a/introspect.ts +++ b/introspect.ts @@ -1,77 +1,68 @@ -import { parse as JSONC } from "https://deno.land/x/jsonct@v0.1.0/mod.ts"; +import { parse as JSONC } from "jsr:@std/jsonc"; type JsonLike = { [key: string]: string | string[] | JsonLike; }; -/** 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(directory=Root) -{ - let path:string, json:JsonLike|undefined; - console.log("searchig in directory", directory) - const loadJSON =async(inPath:string)=>{ - try{ - const path = new URL(inPath, directory); - console.log("looking at", path.href); - const resp = await fetch(path); - if(inPath.endsWith("./.jsonc")) - { - const text = await resp.text(); +/** 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(directory = Root) { + let path = ""; + let json: JsonLike | undefined; + + console.log("Searching in directory", directory); + + const loadJSON = async (inPath: string): Promise => { + try { + const pathUrl = new URL(inPath, directory); + console.log("Looking at", pathUrl.href); + + let text: string; + if (inPath.endsWith(".jsonc")) { + text = await Deno.readTextFile(pathUrl); json = JSONC(text) as JsonLike; + } else { + text = await Deno.readTextFile(pathUrl); + json = JSON.parse(text) as JsonLike; } - else - { - json = await resp.json(); - } - return json; - } - catch(_e) - { - return {}; - } - } - try// look for `deno.json` - { + return json; + } catch (e) { + console.error(`Error loading JSON from ${inPath}:`, e.message); + return undefined; + } + }; + + try { json = await loadJSON("./deno.json"); - } - catch(_e) - { - - try // look for `deno.jsonc` - { + } catch (_e) { + try { json = await loadJSON("./deno.jsonc"); - } - catch(_e) - { - try // look in the vscode plugin settings - { - json = await loadJSON("./.vscode/settings.json") - path = json ? json["deno.config"] as string : ""; + } catch (_e) { + try { + json = await loadJSON("./.vscode/settings.json"); + path = json?.["deno.config"] as string || ""; json = undefined; - if(path) - { - json = await loadJSON(path) + if (path) { + json = await loadJSON(path); } - } - catch(_e) - { - // cant find a config using the vscode plugin + } catch (_e) { + console.warn("Cannot find a config using the VSCode plugin"); } } } - if(!json) - { + if (!json) { json = {}; } - - path = json.importMap as string; - if(!json.imports && path) - { + path = json.importMap as string || ""; + if (!json.imports && path) { json.imports = await loadJSON(path) as JsonLike; - } - - return json as {imports:JsonLike, compilerOptions:JsonLike}; + } + // Ensure the return type always includes imports and compilerOptions + return { + imports: json.imports || {}, + compilerOptions: json.compilerOptions || {} + } as { imports: JsonLike, compilerOptions: JsonLike }; } \ No newline at end of file diff --git a/jsapi.tsx b/jsapi.tsx index c534a27..c7c3aa7 100644 --- a/jsapi.tsx +++ b/jsapi.tsx @@ -1,7 +1,7 @@ import Bundle from "./mod.ts"; import Config from "./test/deno.json" with {type:"json"}; -const path = import.meta.resolve("./test/"); +const path = await import.meta.resolve("./test/"); const {outputFiles} = await Bundle(path, {entryPoints:["./app.tsx"]}, Config); if(outputFiles) { diff --git a/mod.ts b/mod.ts index a957ea1..1445dad 100644 --- a/mod.ts +++ b/mod.ts @@ -2,10 +2,12 @@ import * as ESBuild from "https://deno.land/x/esbuild@v0.19.2/wasm.js"; import * as Mapper from "https://esm.sh/esbuild-plugin-importmaps@1.0.0"; // https://github.com/andstellar/esbuild-plugin-importmaps import Introspect from "./introspect.ts"; -const prefix = "/_dot_importer_/"; + const resolvePlugin =(fullPathDir:string):ESBuild.Plugin=>({ name: "resolve-plugin", setup(build) { + const namespace = "http"; + const prefix = "/_dot_importer_/"; build.onResolve({ filter: /^(\.\/|\.\.\/).*/ }, (args)=> { let resolveRoot = args.importer||fullPathDir; @@ -13,15 +15,13 @@ const resolvePlugin =(fullPathDir:string):ESBuild.Plugin=>({ { resolveRoot = resolveRoot.substring(prefix.length); } - const output:ESBuild.OnResolveResult = { + return { path:prefix + new URL(args.path, resolveRoot).href, - namespace:"http", - } - return output; + namespace, + } as ESBuild.OnResolveResult; }); - build.onLoad({ filter: /.*/, namespace:"http" }, async(args)=> { + build.onLoad({ filter: /.*/, namespace }, async(args)=> { const fetchPath = args.path.substring(prefix.length); - console.log("fetch path", fetchPath); const result = await fetch(fetchPath); const contents = await result.text(); return { contents, loader: `tsx` }; @@ -34,8 +34,8 @@ export type ImportMap = Parameters[0]; export type BuildOptions = ESBuild.BuildOptions; /** * - * @param directory Full file:// or http(s):// path to the directory containing assets you want to build (needed to resolve relative imports) - * @param buildOptions ESBuild "build" options (will be merged with "reasonable defaults") for docs: https://esbuild.github.io/api/#general-options + * @param directory Full "file:///..." or "http(s)://..." url to the directory containing assets you want to build (needed to resolve relative imports) + * @param buildOptions ESBuild "build" options (will be merged with "reasonable defaults") official docs: https://esbuild.github.io/api/#general-options * @param importMap An object to act as the import map ({imports:Record}). If this is left blank, a configuration will be scanned for in the "directory" * @returns build result */ diff --git a/serve.tsx b/serve.tsx index 283ec83..53020b8 100644 --- a/serve.tsx +++ b/serve.tsx @@ -1,4 +1,4 @@ -import mime from "https://esm.sh/mime@4.0.3"; +import * as mime from "jsr:@std/media-types"; import { parseArgs } from "jsr:@std/cli/parse-args"; import bundler, {type ESBuild, type ImportMap} from "./mod.ts"; import Introspect from "./introspect.ts"; @@ -24,7 +24,7 @@ async function serve(settings:ServeArgs):Promise let type:string|null = null if(extension) { - type = (ExtensionsJS.includes(extension)) ? "application/javascript" : mime.getType(extension); + type = (ExtensionsJS.includes(extension)) ? "application/javascript" : mime.contentType(extension); } return {path, extension, type}; @@ -98,6 +98,7 @@ async function serve(settings:ServeArgs):Promise ///// go Deno.serve({port:parseInt(settings.port as string)||8000}, async(req)=> { + console.log("new request", req.url) const checkHash = req.headers.get("if-none-match") if(checkHash === ETag){ return resp304; diff --git a/test/deno.json b/test/deno.json index 56ee7b3..5368ab6 100644 --- a/test/deno.json +++ b/test/deno.json @@ -1,7 +1,7 @@ { "imports": { - "react": "npm:preact@10.22.0/compat", - "react/": "npm:preact@10.22.0/compat/" + "react": "https://esm.sh/preact@10.22.0/compat", + "react/": "https://esm.sh/preact@10.22.0/compat/" }, "compilerOptions": { "jsx": "react-jsx", diff --git a/test/deno.lock b/test/deno.lock new file mode 100644 index 0000000..a6cada0 --- /dev/null +++ b/test/deno.lock @@ -0,0 +1,11 @@ +{ + "version": "3", + "remote": { + "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", + "https://esm.sh/stable/preact@10.22.0/denonext/hooks.js": "09230113132c216bbc3847aaad11289771e088be1b0eb9e49cbc724faaeac205", + "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" + } +}