You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.6 KiB
JavaScript

import * as esbuild from "esbuild";
import { default as glob } from "tiny-glob";
import { promises as fs } from "node:fs";
import { extname, resolve, relative, dirname } from "node:path";
import { fileURLToPath } from "node:url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const watch = process.argv.at(-1) === "--watch";
// plugin that rewrites the import paths so in typescript we can use
//
// import smth from src/index.ts
//
// instead of providing relative paths
const handle_absolute_paths = (project_dir) => ({
name: "sealgen-rewrite-asset-imports",
setup(build) {
build.onLoad({ filter: /\.tsx?$/ }, async (args) => {
let contents = await fs.readFile(args.path, "utf8");
const regex_import = new RegExp(
`^import ([^ ]+, )?(\\w+|{[^}]+}) from "([^"]+)";`,
"gm"
);
contents = contents.replaceAll(regex_import, (line) => {
const replaced = line.replace(
`"src/`,
`"` +
relative(args.path, resolve(project_dir, "./lib/src")) +
"/"
);
return replaced;
});
return {
contents,
loader: extname(args.path) === ".tsx" ? "tsx" : "ts",
};
});
},
});
(async () => {
let entryPoints = Object.fromEntries(
(await glob("./src/**/*.ts")).map((e) => [e.replace(/\.ts$/, ""), e])
);
const buildOptions = {
entryPoints,
sourcemap: true,
bundle: false,
outdir: "./lib",
logLevel: "info",
platform: "node",
target: "es2022",
format: "esm",
plugins: [handle_absolute_paths(__dirname)],
};
if (watch) {
const context = await esbuild.context(buildOptions);
await context.watch();
} else {
await esbuild.build(buildOptions);
}
})();