esbuild-wasm-bundler/mod.ts

67 lines
2.4 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-14 17:07:50 -04:00
import Introspect from "./introspect.ts";
2024-06-07 16:54:12 -04:00
2024-08-13 10:25:16 -04:00
2024-06-14 17:07:50 -04:00
const resolvePlugin =(fullPathDir:string):ESBuild.Plugin=>({
2024-06-08 08:09:56 -04:00
name: "resolve-plugin",
setup(build) {
2024-08-13 10:25:16 -04:00
const namespace = "http";
const prefix = "/_dot_importer_/";
2024-06-10 16:23:31 -04:00
build.onResolve({ filter: /^(\.\/|\.\.\/).*/ }, (args)=>
{
2024-06-14 17:07:50 -04:00
let resolveRoot = args.importer||fullPathDir;
2024-06-10 16:23:31 -04:00
if(resolveRoot.startsWith(prefix))
{
resolveRoot = resolveRoot.substring(prefix.length);
}
2024-08-13 10:25:16 -04:00
return {
2024-06-10 16:23:31 -04:00
path:prefix + new URL(args.path, resolveRoot).href,
2024-08-13 10:25:16 -04:00
namespace,
} as ESBuild.OnResolveResult;
2024-06-10 16:23:31 -04:00
});
2024-08-13 10:25:16 -04:00
build.onLoad({ filter: /.*/, namespace }, async(args)=> {
2024-06-10 14:49:31 -04:00
const fetchPath = args.path.substring(prefix.length);
const result = await fetch(fetchPath);
const contents = await result.text();
return { contents, loader: `tsx` };
2024-06-08 08:09:56 -04:00
});
},
2024-06-14 17:07:50 -04:00
});
2024-06-07 16:54:12 -04:00
2024-06-10 22:51:23 -04:00
await ESBuild.initialize({ worker: false });
2024-06-10 16:23:31 -04:00
export type ImportMap = Parameters<typeof Mapper.importmapPlugin>[0];
export type BuildOptions = ESBuild.BuildOptions;
2024-06-12 17:05:41 -04:00
/**
*
2024-08-13 10:25:16 -04:00
* @param directory Full "file:///..." or "http(s)://..." url to the directory containing assets you want to build (needed to resolve relative imports)
* @param buildOptions ESBuild "build" options (will be merged with "reasonable defaults") official docs: https://esbuild.github.io/api/#general-options
2024-06-14 17:07:50 -04:00
* @param importMap An object to act as the import map ({imports:Record<string, string>}). If this is left blank, a configuration will be scanned for in the "directory"
2024-06-12 17:05:41 -04:00
* @returns build result
*/
2024-06-14 17:07:50 -04:00
export default async function(directory:string, buildOptions={} as BuildOptions, importMap?:ImportMap):Promise<ESBuild.BuildResult<ESBuild.BuildOptions>>
2024-06-10 16:23:31 -04:00
{
2024-06-14 17:07:50 -04:00
if(!importMap)
2024-06-10 16:23:31 -04:00
{
2024-06-14 17:07:50 -04:00
importMap = await Introspect(directory) as ImportMap
2024-06-10 16:23:31 -04:00
}
2024-06-14 17:07:50 -04:00
console.log("using import map", importMap);
2024-06-10 16:23:31 -04:00
const configuration:ESBuild.BuildOptions = {
entryPoints: ["entry"],
2024-06-07 16:54:12 -04:00
bundle: true,
minify: true,
format: "esm",
jsx: "automatic",
jsxImportSource: "react",
2024-06-10 16:23:31 -04:00
...buildOptions,
plugins: [
2024-06-14 17:07:50 -04:00
resolvePlugin(directory),
2024-06-10 16:23:31 -04:00
Mapper.importmapPlugin(importMap) as ESBuild.Plugin,
...buildOptions.plugins||[]
2024-06-07 16:54:12 -04:00
]
2024-06-10 16:23:31 -04:00
};
2024-06-10 22:51:23 -04:00
2024-06-10 16:23:31 -04:00
const result = await ESBuild.build(configuration);
return result;
}
export { ESBuild };