esbuild-wasm-bundler/mod.ts

65 lines
1.7 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);
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;
});
2024-06-07 16:54:12 -04:00
2024-06-08 08:09:56 -04:00
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',
};
});
},
};
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({
entryPoints: ["./app.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({
imports:{"react":"https://esm.sh/preact@10.22.0/compat"}
}) 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)
{
console.log(item.text)
}