UNPKG

radish

Version:

Radish is a React-based static site generator that outputs plain HTML and CSS.

36 lines (35 loc) 1.43 kB
import * as fs from "node:fs"; import * as path from "node:path"; import esbuild from "esbuild"; export const jsPlugin = (options) => ({ name: "js", setup(build) { build.onLoad({ filter: /\.bundle\.[jt]sx?$/ }, async (args) => { const r = await esbuild.build({ entryPoints: [args.path], entryNames: "[name]-[hash]", outdir: options.dest, bundle: true, write: false, minify: true, metafile: true, format: "esm" }); const [file, ...rest] = r.outputFiles; if (!file) throw Error("No output file returned."); if (rest.length > 1) throw Error("Too many output files returned."); const [, hash] = file.path.match(/.+\.bundle-(\w+)\.js/) ?? [], basename = path.basename(file.path, `.bundle-${hash}.js`), filename = `${basename}-${hash}.js`; await fs.promises.mkdir(options.dest, { recursive: true }); await fs.promises.writeFile(path.join(options.dest, filename), file.text); return { contents: path.join(options.prefix, filename), loader: "text", errors: r.errors, warnings: r.warnings, watchFiles: Object.keys(r.metafile?.inputs ?? {}) }; }); } });