Formatting and minifying are opposite operations on the same thing: the whitespace and optional syntax in HTML. Formatting makes HTML easy for people to read and review. Minifying makes it small for browsers to download. rehype does both by changing the tree, which keeps the result valid.
Install
npm install rehype rehype-format rehype-preset-minifyFormatting with rehype-format
import {rehype} from 'rehype'
import rehypeFormat from 'rehype-format'
const input = `<!doctype html><html lang=en><head><title>Demo</title></head>
<body><main><h1>Hello</h1><p>Formatted by <b>rehype</b>.</p></main></body></html>`
const file = await rehype().use(rehypeFormat).process(input)
console.log(String(file))<!doctype html>
<html lang="en">
<head>
<title>Demo</title>
</head>
<body>
<main>
<h1>Hello</h1>
<p>Formatted by <b>rehype</b>.</p>
</main>
</body>
</html>Every element is on its own line and indented by two spaces. Set indent: 4 or indent: '\t' to change that, and indentInitial: false to keep <head> and <body> flush with <html>.
What formatting does not touch
- Content of
<pre>,<textarea>,<script>and<style>. - Whitespace that would change rendering, for example between inline elements in a sentence.
Because the HTML is parsed first, unclosed tags are closed and attributes are quoted consistently. That can make diffs larger the first time you format a hand-written file.
Minifying with rehype-preset-minify
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 charactersThe preset removed the comment, the optional <head>, <body> and closing tags, the duplicate class, extra spaces and quotes, and normalized utf-8 to utf8. The example counts characters; for a real page, measure bytes after compression too, because gzip and Brotli already shrink repetitive whitespace well.
Choosing individual plugins
If the full preset is too aggressive for your setup, use its parts directly. For example, only collapsing whitespace and removing comments:
import {rehype} from 'rehype'
import rehypeMinifyWhitespace from 'rehype-minify-whitespace'
import rehypeRemoveComments from 'rehype-remove-comments'
const file = await rehype()
.use(rehypeRemoveComments)
.use(rehypeMinifyWhitespace)
.process('<p> Hi <!-- note --> there </p>')From the command line
With rehype-cli, both are one-liners, which makes them easy to add to package.json scripts:
# Format every HTML file in src/ in place
npx rehype src --use rehype-format -o
# Minify the build output in place
npx rehype dist --use rehype-preset-minify -oA typical setup formats source files (committed to version control) and minifies only build output (deployed to users).
Pitfalls
- Do not minify your source. You lose readability and get noisy diffs. Minify as a build step.
- Templates are not HTML. Files with template syntax (PHP tags, Handlebars, JSX) are not valid HTML; parsing them may move or mangle the template parts. Process the rendered output instead.
- Pick one. Formatting and minifying in the same pipeline undo each other.
Try it in the browser
The HTML Formatter and HTML Minifier run these same plugins on your own markup and show exact byte savings.