- Category
- Formatting & size
- Maintained by
- rehype team
- License
- MIT
- Module format
- ESM, default export
npm install rehype-preset-minifyWhat it does
rehype-preset-minify comes from the rehype-minify monorepo. A preset is a bundle of plugins and settings; this one combines 23 single-purpose plugins (remove comments, collapse whitespace, sort attributes, minify inline CSS and JavaScript, drop default type attributes, shorten lang values and more) with serializer settings that omit optional tags and unnecessary quotes.
Example
import {rehype} from 'rehype'
import rehypePresetMinify from 'rehype-preset-minify'
const input = `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Minify me</title>
</head>
<body>
<!-- a comment -->
<p class="lead lead"> Hello, world! </p>
</body>
</html>`
const file = await rehype().use(rehypePresetMinify).process(input)
const output = String(file)
console.log(output)
console.log(`${input.length} -> ${output.length} characters`)<!doctypehtml><html lang=en><meta charset=utf8><title>Minify me</title><p class=lead>Hello, world!
209 -> 98 charactersNotice what changed: the comment is gone, <head>, <body> and closing tags that HTML allows you to omit are dropped, duplicate class names are merged, whitespace is collapsed, and utf-8 becomes utf8.
Using the CLI
With rehype-cli installed:
npx rehype input.html --use rehype-preset-minify --output output.htmlCustomizing
The plugins can be used individually instead of the preset, for example rehype-minify-whitespace and rehype-remove-comments only. The readme also notes that you can reconfigure a plugin from a preset by adding it again afterwards with different options.
Caveats
- Minified HTML is valid but hard to read; keep your source formatted and minify during the build.
- Inline JavaScript minification relies on uglify-js and CSS minification on clean-css, both installed as dependencies.
- Test the result: omitting optional tags is valid HTML, but tools that parse HTML with simplistic regular expressions may be confused by it.
Measure the savings on your own markup with the HTML Minifier.
Official references
- rehype-preset-minify readme (API and all options) (opens in a new tab)
- rehype-preset-minify on npm (current version) (opens in a new tab)
Facts on this page were checked against the package readme on November 11, 2026. Options not covered here are documented upstream.