Skip to content

Instantly share code, notes, and snippets.

@ngohungphuc
Created October 27, 2020 07:28
Show Gist options
  • Save ngohungphuc/ecd5d52c2b236f4396241c18a77fabe3 to your computer and use it in GitHub Desktop.
Save ngohungphuc/ecd5d52c2b236f4396241c18a77fabe3 to your computer and use it in GitHub Desktop.
var fs = require("fs");
var minify = require("html-minifier").minify;
var filePath = "dist/index.html";
fs.readFile(filePath, function (err, buf) {
if (err) {
console.log(err);
} else {
var originalValue = buf.toString();
var minifiedValue = minify(originalValue, {
caseSensitive: false,
collapseBooleanAttributes: true,
collapseInlineTagWhitespace: false,
collapseWhitespace: true,
conservativeCollapse: false,
decodeEntities: true,
html5: true,
includeAutoGeneratedTags: false,
keepClosingSlash: false,
minifyCSS: true,
minifyJS: true,
preserveLineBreaks: false,
preventAttributesEscaping: false,
processConditionalComments: true,
processScripts: ["text/html"],
removeAttributeQuotes: true,
removeComments: true,
removeEmptyAttributes: true,
removeEmptyElements: false,
removeOptionalTags: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
removeTagWhitespace: true,
sortAttributes: true,
sortClassName: true,
trimCustomFragments: true,
useShortDoctype: true,
});
fs.writeFile(filePath, minifiedValue, function (err, data) {
if (err) {
console.log(err);
} else {
var diff = originalValue.length - minifiedValue.length;
var savings = originalValue.length
? ((100 * diff) / originalValue.length).toFixed(2)
: 0;
console.log(
"Successfully minified '" +
filePath +
"'" +
". Original size: " +
originalValue.length +
". Minified size: " +
minifiedValue.length +
". Savings: " +
diff +
" (" +
savings +
"%)"
);
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment