esbuild-wasm-bundler/mod.ts

65 lines
1.7 KiB
TypeScript

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
export const Root = new URL(`file://${Deno.cwd().replaceAll("\\", "/")}`).toString()+"/";
console.log("running at", Root);
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.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',
};
});
},
};
type ImportMap = Parameters<typeof Mapper.importmapPlugin>[0];
await ESBuild.initialize({ worker: false });
const result = await ESBuild.build({
entryPoints: ["./app.tsx"],
bundle: true,
minify: true,
format: "esm",
jsx: "automatic",
jsxImportSource: "react",
plugins:[
resolvePlugin,
Mapper.importmapPlugin({
imports:{"react":"https://esm.sh/preact@10.22.0/compat"}
}) as ESBuild.Plugin
]
});
ESBuild.stop();
for(const item of result.outputFiles)
{
console.log(item.text)
}