From fe33072cd55040c32055c3e8dd18d507334d87a3 Mon Sep 17 00:00:00 2001 From: Seth Trowbridge Date: Mon, 10 Jun 2024 14:49:31 -0400 Subject: [PATCH] ./ and ../ for file and http --- deep/deep.tsx | 2 ++ mod.ts | 82 +++++++++++++++++++++++++++++++-------------------- 2 files changed, 52 insertions(+), 32 deletions(-) create mode 100644 deep/deep.tsx diff --git a/deep/deep.tsx b/deep/deep.tsx new file mode 100644 index 0000000..c12fca5 --- /dev/null +++ b/deep/deep.tsx @@ -0,0 +1,2 @@ +import "../app.tsx"; +console.log("we deep!"); \ No newline at end of file diff --git a/mod.ts b/mod.ts index fdd0784..92e1b7a 100644 --- a/mod.ts +++ b/mod.ts @@ -3,39 +3,53 @@ import * as Mapper from "https://esm.sh/esbuild-plugin-importmaps@1.0.0"; // htt export const Root = new URL(`file://${Deno.cwd().replaceAll("\\", "/")}`).toString()+"/"; console.log("running at", Root); + +type Resolver = Parameters[1]; + +const prefix = "/_dot_importer_/"; +const Resolver:Resolver =(args)=> +{ + console.log("RESOLVE"); + console.log("in:", args); + + let resolveRoot = args.importer||Root; + if(resolveRoot.startsWith(prefix)) + { + resolveRoot = resolveRoot.substring(prefix.length) + } + + const output:ESBuild.OnResolveResult = { + path:prefix + new URL(args.path, resolveRoot).href, + namespace:"http", + } + + console.log("out:", output); + return output; +} + +const FetchTSX =async(inPath:string):Promise=> +{ + console.log("fetching", inPath); + const result = await fetch(inPath); + const contents = await result.text(); + return { contents, loader: `tsx` }; +} + const resolvePlugin:ESBuild.Plugin = { name: "resolve-plugin", setup(build) { - build.onResolve({ filter: /^\.\/.*$/ }, args => { - console.log("RESOLVE") - console.log("in:", args) - const path = new URL(args.path, Root).pathname - const output = { - path, - namespace: 'file', - }; - console.log("out:", output) - return output; - }); + build.onResolve({ filter: /^\.\/.*$/ }, Resolver); + build.onResolve({ filter: /^(\.\.\/).*$/ }, Resolver); - build.onLoad({ filter: /.*/, namespace:"file" }, async (args) => { - console.log("LOAD") - console.log(args) - let contents; - try - { - const result = await fetch("file://"+args.path); - contents = await result.text(); - } - catch(e) - { - console.log("deno fs access error:", e) - } - - return { - contents, - loader: args.path.endsWith('.tsx') ? 'tsx' : 'default', - }; + build.onLoad({ filter: /.*/, namespace:"http" }, async(args)=> { + console.log("LOAD WEB"); + console.log("in:", args); + const fetchPath = args.path.substring(prefix.length); + + console.log("fetching", fetchPath); + const result = await fetch(fetchPath); + const contents = await result.text(); + return { contents, loader: `tsx` }; }); }, }; @@ -44,7 +58,9 @@ type ImportMap = Parameters[0]; await ESBuild.initialize({ worker: false }); const result = await ESBuild.build({ - entryPoints: ["./app.tsx"], + //entryPoints: ["./app.tsx"], + entryPoints: ["./deep/deep.tsx"], + //entryPoints: ["http://localhost:4507/deep/deep.tsx"], bundle: true, minify: true, format: "esm", @@ -53,7 +69,9 @@ type ImportMap = Parameters[0]; plugins:[ resolvePlugin, Mapper.importmapPlugin({ - imports:{"react":"https://esm.sh/preact@10.22.0/compat"} + imports:{ + "react":"https://esm.sh/preact@10.22.0/compat" + } }) as ESBuild.Plugin ] }); @@ -61,5 +79,5 @@ type ImportMap = Parameters[0]; for(const item of result.outputFiles) { - console.log(item.text) + //console.log(item.text) } \ No newline at end of file