Native bindings for the tdewolff/minify Go minifier powered by koffi. Minification runs in-process against a Go-built shared library, so you get the Go implementation's speed without shelling out to a CLI.
- Go-powered minifier loaded through koffi; no child processes.
- Promise-based
minify(data, options)API that returns the minified string. - Supports CSS, HTML, JS, JSON, SVG, XML, and import maps with the same tuning flags as the Go minifier.
- Cross-compile the Go shared library with
GOOS/GOARCHor point to a custom build viaNODE_MINIFY_LIB_PATH.
npm install @vastblast/node-minifyThe install step builds a small Go shared library for your host platform. Ensure Go is available in PATH (or set GOOS/GOARCH to cross-compile). You can also point to a prebuilt library by setting NODE_MINIFY_LIB_PATH.
- Run
GOOS=<target> GOARCH=<target> npm run build:go(ornpm run build:go -- --goos <target> --goarch <target>) to emitbuild/<goos>-<goarch>/nodeminify.<ext>. - Keep
build/in your npm package so installs on those platforms can reuse the prebuilt. postinstallskips building when a matching prebuilt exists. SetNODE_MINIFY_FORCE_BUILD=1/NODE_MINIFY_SKIP_BUILD=1(or pass--force-build/--skip-build) to override; useNODE_MINIFY_DEBUG_BUILD=1or--debug-buildto keep symbols for native debugging.- CI workflow
.github/workflows/prebuilds.ymlbuilds prebuilts per platform and publishes whenpublish=trueonworkflow_dispatch. Publishing expects the package to be set up as an npm trusted publisher (OIDC/id-token) or for an automation token to be provided viaNODE_AUTH_TOKEN.
import { minify } from '@vastblast/node-minify';
const html = `
<!doctype html>
<html>
<head><title>Demo</title></head>
<body><h1>Hello, world!</h1></body>
</html>`;
const minified = await minify(html, {
type: 'text/html',
htmlKeepComments: false,
htmlKeepWhitespace: false,
});
console.log(minified);const js = await minify('const sum = (a, b) => a + b;', {
type: 'application/javascript',
jsPrecision: 3,
});minify returns a Promise<string> and rejects with an Error if the input or options are invalid.
data: the string to minify (required).options.type: media type (required).- Remaining
MinifyOptionsmap directly to tdewolff/minify flags.
Supported media types:
'text/css', 'text/html', 'image/svg+xml', 'application/javascript', 'application/json', 'application/rss+xml', 'application/manifest+json', 'application/xhtml-xml', 'text/xml', 'importmap'
Common options (all optional unless noted):
cssPrecision: decimal precision for CSS numbers.htmlKeepComments,htmlKeepDefaultAttrvals,htmlKeepDocumentTags,htmlKeepEndTags,htmlKeepQuotes,htmlKeepSpecialComments,htmlKeepWhitespace: control how much HTML is preserved.jsKeepVarNames,jsPrecision,jsVersion: adjust JS minification.jsonKeepNumbers,jsonPrecision: JSON minification tuning.svgKeepComments,svgPrecision: SVG-specific controls.xmlKeepWhitespace: preserve XML whitespace.url: base URL used by the underlying minifier.
The package loads a Go-built shared library via koffi. All work happens inside that library (no spawned processes), keeping throughput close to the original Go project and avoiding CLI overhead.
Powered by tdewolff/minify.
MIT