Last active
March 27, 2018 04:54
-
-
Save JaeYeopHan/f912bb1887a1870ab01215ed68ceb6c0 to your computer and use it in GitHub Desktop.
plugin.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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