Skip to content

Instantly share code, notes, and snippets.

@JaeYeopHan
Last active March 27, 2018 04:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JaeYeopHan/f912bb1887a1870ab01215ed68ceb6c0 to your computer and use it in GitHub Desktop.
Save JaeYeopHan/f912bb1887a1870ab01215ed68ceb6c0 to your computer and use it in GitHub Desktop.
plugin.js
const minify = require('html-minifier').minify;
const fs = require('fs');
class HtmlMinifierPlugin {
constructor(options) {
this.options = options;
}
apply(compiler) {
compiler.plugin("emit", (compilation, callback) => {
fs.readFile(this.options.template, 'utf-8', (err, html) => {
if (err) {
callback();
throw Error();
}
const minified = minify(html, {
collapseWhitespace: true
});
compilation.assets[this.options.filename] = {
source: function() {
return minified;
},
size: function() {
return minified.length;
}
}
callback();
});
});
}
}
module.exports = HtmlMinifierPlugin;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment