Minify the ESM bundle whitespace in postbuild

This commit is contained in:
Kamran Ahmed
2026-07-06 21:03:11 +01:00
parent f28d08bfb7
commit 8ccf77faa6
+10 -1
View File
@@ -1,4 +1,5 @@
import { copyFileSync } from "node:fs";
import { copyFileSync, readFileSync, writeFileSync } from "node:fs";
import { transform } from "esbuild";
// vite-plugin-dts emits a single bundled declaration file. Copy it to the
// ESM/CJS-specific extensions so each `exports` condition resolves to types
@@ -6,3 +7,11 @@ import { copyFileSync } from "node:fs";
const source = "dist/driver.js.d.ts";
copyFileSync(source, "dist/driver.js.d.mts");
copyFileSync(source, "dist/driver.js.d.cts");
// Vite 8 whitespace-minifies the CJS and IIFE outputs but ships the ESM bundle
// with its whitespace intact, so run a final esbuild pass on just the .mjs to
// strip it (~9% smaller gzipped). Identifiers are already mangled and no
// sourcemaps are shipped, so there is no downside for consumers.
const esmBundle = "dist/driver.js.mjs";
const minified = await transform(readFileSync(esmBundle, "utf8"), { minify: true, legalComments: "none" });
writeFileSync(esmBundle, minified.code);