rehype-preset-minify

A preset of 23 small minification plugins plus serializer settings that shrink HTML for production.

Category
Formatting & size
Maintained by
rehype team
License
MIT
Module format
ESM, default export
Install
npm install rehype-preset-minify

What 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

minify.mjs
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`)
Output node minify.mjs
<!doctypehtml><html lang=en><meta charset=utf8><title>Minify me</title><p class=lead>Hello, world!
209 -> 98 characters

Notice 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:

Terminal
npx rehype input.html --use rehype-preset-minify --output output.html

Customizing

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

Facts on this page were checked against the package readme on November 11, 2026. Options not covered here are documented upstream.

Use ↑ ↓ to move, Enter to open.