Created
April 28, 2017 19:26
-
-
Save mzgoddard/07c18908b42bb0709fdbcf1abbd88cca to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Entities = require('html-entities').AllHtmlEntities; | |
const htmlEntities = new Entities(); | |
class InlineCssHtmlWebpackPlugin { | |
apply(compiler) { | |
compiler.plugin('compilation', compilation => { | |
compilation.plugin('html-webpack-plugin-before-html-processing', (htmlPluginData, callback) => { | |
for (let filename of htmlPluginData.assets.css) { | |
const cssSrc = compilation.assets[filename]; | |
htmlPluginData.html = htmlPluginData.html | |
.replace('</head>', `<style>${htmlEntities.encode(cssSrc.source())}</style></head>`); | |
delete compilation.assets[filename]; | |
} | |
htmlPluginData.assets.css = []; | |
callback(null, htmlPluginData); | |
}); | |
}); | |
} | |
} | |
module.exports = InlineCssHtmlWebpackPlugin; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment