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-10 16:23:31 -04:00
|
|
|
import Introspect, {Root} from "./introspect.ts";
|
2024-06-07 16:54:12 -04:00
|
|
|
|
2024-06-10 14:49:31 -04:00
|
|
|
const prefix = "/_dot_importer_/";
|
2024-06-08 08:09:56 -04:00
|
|
|
const resolvePlugin:ESBuild.Plugin = {
|
|
|
|
name: "resolve-plugin",
|
|
|
|
setup(build) {
|
2024-06-10 16:23:31 -04:00
|
|
|
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;
|
|
|
|
});
|
2024-06-10 14:49:31 -04:00
|
|
|
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` };
|
2024-06-08 08:09:56 -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;
|
|
|
|
export default async function(buildOptions={} as BuildOptions, importMap:ImportMap|false = false)
|
|
|
|
{
|
|
|
|
if(importMap === false)
|
|
|
|
{
|
|
|
|
importMap = await Introspect() as ImportMap;
|
|
|
|
}
|
2024-06-07 16:54:12 -04:00
|
|
|
|
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-08 08:09:56 -04:00
|
|
|
resolvePlugin,
|
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 };
|