Hook webpack 'emit' to allow html to be emitted as an asset

This commit is contained in:
Charles Blaxland 2014-08-13 20:34:45 +10:00
parent 4ea9894058
commit f44af556d8
3 changed files with 12 additions and 6 deletions

View File

@ -8,7 +8,7 @@ function HtmlWebpackPlugin(options) {
HtmlWebpackPlugin.prototype.apply = function(compiler) {
var self = this;
compiler.plugin('after-emit', function(compiler, callback) {
compiler.plugin('emit', function(compiler, callback) {
var webpackStatsJson = compiler.getStats().toJson();
var templateParams = {};
templateParams.webpack = webpackStatsJson;
@ -24,7 +24,15 @@ HtmlWebpackPlugin.prototype.apply = function(compiler) {
var htmlTemplateContent = fs.readFileSync(templateFile, 'utf8');
var html = tmpl(htmlTemplateContent, templateParams);
var outputPath = path.join(compiler.options.output.path, 'index.html');
this.outputFileSystem.writeFile(outputPath, html, callback);
compiler.assets['index.html'] = {
source: function() {
return html;
},
size: function() {
return html.length;
}
};
callback();
});
};

View File

@ -1 +1 @@
document.body.textContent = helloText();
document.body.innerHTML = document.body.innerHTML + "<p>index.js</p>";

View File

@ -1,3 +1 @@
function helloText() {
return "Hello world";
}
document.body.innerHTML = document.body.innerHTML + "<p>util.js</p>";