73 lines
2.7 KiB
TypeScript
73 lines
2.7 KiB
TypeScript
import * as ESBuild from "https://deno.land/x/esbuild@v0.25.0/wasm.js";
|
|
import * as Mapper from "https://esm.sh/esbuild-plugin-importmaps@1.0.0"; // https://github.com/andstellar/esbuild-plugin-importmaps
|
|
import Introspect from "./introspect.ts";
|
|
|
|
const resolvePlugin =(fullPathDir:string):ESBuild.Plugin=>({
|
|
name: "resolve-plugin",
|
|
setup(build) {
|
|
|
|
|
|
build.onResolve( {/* `/`, `./`, and `../` */ filter:/^(\/|\.\/|\.\.\/).*/}, args=>{
|
|
const resolveRoot = args.importer||fullPathDir;
|
|
const url = new URL(args.path, resolveRoot).href;
|
|
|
|
const out = { path:url, namespace:"FULLPATH" };
|
|
console.log(`SLASHPATH=>FULLPATH RESOLVE`, {args, out}, "\n");
|
|
return out;
|
|
} );
|
|
|
|
build.onResolve( {/* `file://`, `http://`, and `https://` */ filter:/^(file:\/\/|http:\/\/|https:\/\/).*/}, args=>{
|
|
const out = { path:args.path, namespace:"FULLPATH" };
|
|
console.log(`FULLPATH RESOLVE`, {args, out}, "\n");
|
|
return out;
|
|
} );
|
|
|
|
build.onLoad(
|
|
{/* `file://`, `http://`, and `https://` */ filter:/^(file:\/\/|http:\/\/|https:\/\/).*/},
|
|
async(args)=>
|
|
{
|
|
const contents = await fetch(args.path).then(r=>r.text());
|
|
const out = { contents, loader: `tsx` };
|
|
console.log(`FULLPATH LOAD`, {args, out:{...out, contents:contents.substring(0, 100)}}, "\n");
|
|
return out;
|
|
} );
|
|
},
|
|
});
|
|
|
|
await ESBuild.initialize({ worker: false });
|
|
export type ImportMap = Parameters<typeof Mapper.importmapPlugin>[0];
|
|
export type BuildOptions = ESBuild.BuildOptions;
|
|
|
|
/**
|
|
*
|
|
* @param {string} directory Full file:// or http(s):// path to the directory containing assets you want to build (needed to resolve relative imports)
|
|
* @param {ESBuild.BuildOptions} buildOptions ESBuild "build" options (will be merged with "reasonable defaults") for docs: https://esbuild.github.io/api/#general-options
|
|
* @param {ImportMap|null} 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"
|
|
* @returns {Promise<ESBuild.BuildResult<ESBuild.BuildOptions>>} build result
|
|
*/
|
|
export default async function(directory, buildOptions={}, importMap)
|
|
{
|
|
if(!importMap)
|
|
{
|
|
importMap = await Introspect(directory) as ImportMap
|
|
}
|
|
console.log("using import map", importMap);
|
|
const configuration:ESBuild.BuildOptions = {
|
|
entryPoints: ["entry"],
|
|
bundle: true,
|
|
minify: true,
|
|
format: "esm",
|
|
jsx: "automatic",
|
|
jsxImportSource: "react",
|
|
...buildOptions,
|
|
plugins: [
|
|
resolvePlugin(directory),
|
|
Mapper.importmapPlugin(importMap) as ESBuild.Plugin,
|
|
...buildOptions.plugins||[]
|
|
]
|
|
};
|
|
|
|
const result = await ESBuild.build(configuration);
|
|
return result;
|
|
}
|
|
export { ESBuild }; |