minify-html-stream

A streaming HTML minifier for Node.js

Usage no npm install needed!

<script type="module">
  import minifyHtmlStream from 'https://cdn.skypack.dev/minify-html-stream';
</script>

README

Build Status Coverage Status

Minify HTML Stream

Strips whitespace from an HTML stream to reduce the size of the final payload.

Install

Install with npm

npm install --save minify-html-stream

Simple Example

"use strict";

const fetch = require("node-fetch");
const Minifier = require("minify-html-stream").Minifier;

fetch("http://example.com/").then(function (response) {
    response.body.pipe(new Minifier()).pipe(process.stdout);
}).catch(function (err) {
    console.error("Yikes!", err);
});

How You Might Use It

"use strict";

// Declare variables
const http = require("http");
const talisman = require("talismanjs");
const Minifier = require("minify-html-stream").Minifier;

// Start a server
const server = http.createServer(function (request, response) {
    talisman.create("homepage.html").then(function (view) {
        view.toStream().pipe(new Minifier()).pipe(response);
    }).catch(function (error) {
        response.writeHead(500);
        response.write(error.message);
        response.end();
    });
});

// Start listening
server.listen(8000);

Configuration

{
    stripCarriageReturns: true, // remove carriage return characters (\r)
    trimLines: true, // remove whitespace from the ends of lines
    trimElements: true, // remove whitespace from around elements where it is safe
    normalizeWhiteSpace: true, // normalize multiple whitespace characters to single characters
    stripComments: true // remove HTML comments (except conditional comments)
}

Notes

Minify HTML Stream is really naïve and conservative about how it goes about minification, because it sort of has to be. Pull requests are most welcome.

Published under the MIT License.