./ 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()+"/";
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 = {
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<typeof Mapper.importmapPlugin>[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<typeof Mapper.importmapPlugin>[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<typeof Mapper.importmapPlugin>[0];
for(const item of result.outputFiles)
{
console.log(item.text)
//console.log(item.text)
}