← Back
html js tools

Minify html

You can use an online tool like this one

But if (like me) you prefer to use your command line, you can use html-minifier npm package.

Here's the gist:

minify.js:

const fs = require("fs");
const path = require("path");
var minify = require("html-minifier").minify;

const filename = process.argv[2];
if (!filename) {
console.error("Usage: `node minify <filename>`");
return;
}

const filePath = path.resolve(filename);
if (!fs.existsSync(filePath)) {
console.error(`File '${filename}' not found`);
return;
}

const content = fs.readFileSync(filePath).toString();
const result = minify(content, {
collapseWhitespace: true
});

console.log(result);