esbuild-wasm-bundler/mod.ts

83 lines
2.3 KiB
TypeScript
Raw Normal View History

2024-06-08 08:09:56 -04:00
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
2024-06-07 16:54:12 -04:00
2024-06-08 08:09:56 -04:00
export const Root = new URL(`file://${Deno.cwd().replaceAll("\\", "/")}`).toString()+"/";
console.log("running at", Root);
2024-06-10 14:49:31 -04:00
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` };
}
2024-06-08 08:09:56 -04:00
const resolvePlugin:ESBuild.Plugin = {
name: "resolve-plugin",
setup(build) {
2024-06-10 14:49:31 -04:00
build.onResolve({ filter: /^\.\/.*$/ }, Resolver);
build.onResolve({ filter: /^(\.\.\/).*$/ }, Resolver);
build.onLoad({ filter: /.*/, namespace:"http" }, async(args)=> {
console.log("LOAD WEB");
console.log("in:", args);
const fetchPath = args.path.substring(prefix.length);
2024-06-07 16:54:12 -04:00
2024-06-10 14:49:31 -04:00
console.log("fetching", fetchPath);
const result = await fetch(fetchPath);
const contents = await result.text();
return { contents, loader: `tsx` };
2024-06-08 08:09:56 -04:00
});
},
};
2024-06-07 16:54:12 -04:00
2024-06-08 08:09:56 -04:00
type ImportMap = Parameters<typeof Mapper.importmapPlugin>[0];
2024-06-07 16:54:12 -04:00
2024-06-08 08:09:56 -04:00
await ESBuild.initialize({ worker: false });
const result = await ESBuild.build({
2024-06-10 14:49:31 -04:00
//entryPoints: ["./app.tsx"],
entryPoints: ["./deep/deep.tsx"],
//entryPoints: ["http://localhost:4507/deep/deep.tsx"],
2024-06-07 16:54:12 -04:00
bundle: true,
minify: true,
format: "esm",
jsx: "automatic",
jsxImportSource: "react",
plugins:[
2024-06-08 08:09:56 -04:00
resolvePlugin,
Mapper.importmapPlugin({
2024-06-10 14:49:31 -04:00
imports:{
"react":"https://esm.sh/preact@10.22.0/compat"
}
2024-06-08 08:09:56 -04:00
}) as ESBuild.Plugin
2024-06-07 16:54:12 -04:00
]
});
2024-06-08 08:09:56 -04:00
ESBuild.stop();
for(const item of result.outputFiles)
{
2024-06-10 14:49:31 -04:00
//console.log(item.text)
2024-06-08 08:09:56 -04:00
}