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 import Introspect, {Root} from "./introspect.ts"; const prefix = "/_dot_importer_/"; const resolvePlugin:ESBuild.Plugin = { name: "resolve-plugin", setup(build) { build.onResolve({ filter: /^(\.\/|\.\.\/).*/ }, (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", } return output; }); build.onLoad({ filter: /.*/, namespace:"http" }, async(args)=> { const fetchPath = args.path.substring(prefix.length); const result = await fetch(fetchPath); const contents = await result.text(); return { contents, loader: `tsx` }; }); }, }; await ESBuild.initialize({ worker: false }); export type ImportMap = Parameters[0]; export type BuildOptions = ESBuild.BuildOptions; /** * * @param buildOptions ESBuild "build" options (will be merged with "reasonable defaults") for docs: https://esbuild.github.io/api/#general-options * @param importMap * @returns build result */ export default async function(buildOptions={} as BuildOptions, importMap:ImportMap|string|false = false):Promise> { if(importMap === false) { importMap = await Introspect() as ImportMap; } else if(typeof importMap === "string") { importMap = await Introspect(importMap) as ImportMap; } const configuration:ESBuild.BuildOptions = { entryPoints: ["entry"], bundle: true, minify: true, format: "esm", jsx: "automatic", jsxImportSource: "react", ...buildOptions, plugins: [ resolvePlugin, Mapper.importmapPlugin(importMap) as ESBuild.Plugin, ...buildOptions.plugins||[] ] }; const result = await ESBuild.build(configuration); return result; } export { ESBuild };