./ and ../ for file and http

This commit is contained in:
Seth Trowbridge 2024-06-10 14:49:31 -04:00
parent 023b9a6474
commit fe33072cd5
2 changed files with 52 additions and 32 deletions

2
deep/deep.tsx Normal file
View File

@ -0,0 +1,2 @@
import "../app.tsx";
console.log("we deep!");

82
mod.ts
View File

@ -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()+"/"; export const Root = new URL(`file://${Deno.cwd().replaceAll("\\", "/")}`).toString()+"/";
console.log("running at", Root); console.log("running at", Root);
type Resolver = Parameters<ESBuild.PluginBuild["onResolve"]>[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<ESBuild.OnLoadResult>=>
{
console.log("fetching", inPath);
const result = await fetch(inPath);
const contents = await result.text();
return { contents, loader: `tsx` };
}
const resolvePlugin:ESBuild.Plugin = { const resolvePlugin:ESBuild.Plugin = {
name: "resolve-plugin", name: "resolve-plugin",
setup(build) { setup(build) {
build.onResolve({ filter: /^\.\/.*$/ }, args => { build.onResolve({ filter: /^\.\/.*$/ }, Resolver);
console.log("RESOLVE") build.onResolve({ filter: /^(\.\.\/).*$/ }, Resolver);
console.log("in:", args)
const path = new URL(args.path, Root).pathname
const output = {
path,
namespace: 'file',
};
console.log("out:", output)
return output;
});
build.onLoad({ filter: /.*/, namespace:"file" }, async (args) => { build.onLoad({ filter: /.*/, namespace:"http" }, async(args)=> {
console.log("LOAD") console.log("LOAD WEB");
console.log(args) console.log("in:", args);
let contents; const fetchPath = args.path.substring(prefix.length);
try
{ console.log("fetching", fetchPath);
const result = await fetch("file://"+args.path); const result = await fetch(fetchPath);
contents = await result.text(); const contents = await result.text();
} return { contents, loader: `tsx` };
catch(e)
{
console.log("deno fs access error:", e)
}
return {
contents,
loader: args.path.endsWith('.tsx') ? 'tsx' : 'default',
};
}); });
}, },
}; };
@ -44,7 +58,9 @@ type ImportMap = Parameters<typeof Mapper.importmapPlugin>[0];
await ESBuild.initialize({ worker: false }); await ESBuild.initialize({ worker: false });
const result = await ESBuild.build({ const result = await ESBuild.build({
entryPoints: ["./app.tsx"], //entryPoints: ["./app.tsx"],
entryPoints: ["./deep/deep.tsx"],
//entryPoints: ["http://localhost:4507/deep/deep.tsx"],
bundle: true, bundle: true,
minify: true, minify: true,
format: "esm", format: "esm",
@ -53,7 +69,9 @@ type ImportMap = Parameters<typeof Mapper.importmapPlugin>[0];
plugins:[ plugins:[
resolvePlugin, resolvePlugin,
Mapper.importmapPlugin({ 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 }) as ESBuild.Plugin
] ]
}); });
@ -61,5 +79,5 @@ type ImportMap = Parameters<typeof Mapper.importmapPlugin>[0];
for(const item of result.outputFiles) for(const item of result.outputFiles)
{ {
console.log(item.text) //console.log(item.text)
} }