From 8ccf77faa6ecf30090de2bf40c6dcb5ad1095cb5 Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Mon, 6 Jul 2026 21:03:11 +0100 Subject: [PATCH] Minify the ESM bundle whitespace in postbuild --- scripts/postbuild.mjs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/scripts/postbuild.mjs b/scripts/postbuild.mjs index 68a967a..eb400aa 100644 --- a/scripts/postbuild.mjs +++ b/scripts/postbuild.mjs @@ -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);