Allow to pass a function for custom html rendering / templating

This commit is contained in:
Jan Nicklas 2015-03-13 18:21:28 +01:00
parent 6a140b5085
commit 2815661681
2 changed files with 18 additions and 1 deletions

View File

@ -22,7 +22,8 @@ HtmlWebpackPlugin.prototype.apply = function(compiler) {
compiler.errors.push(new Error('HtmlWebpackPlugin: cannot specify both template and templateContent options'));
callback();
} else if (self.options.templateContent) {
self.emitHtml(compiler, self.options.templateContent, templateParams, outputFilename);
var templateContent = typeof self.options.templateContent === 'function' ? self.options.templateContent(templateParams, compiler) : self.options.templateContent;
self.emitHtml(compiler, templateContent, templateParams, outputFilename);
callback();
} else {
var templateFile = self.options.template;

View File

@ -83,6 +83,22 @@ describe('HtmlWebpackPlugin', function() {
['<script src="app_bundle.js"'], null, done);
});
it('allows you to specify your own HTML template function', function(done) {
testHtmlPlugin({
entry: {app: path.join(__dirname, 'fixtures/index.js')},
output: {
path: OUTPUT_DIR,
filename: 'app_bundle.js'
},
plugins: [new HtmlWebpackPlugin({
templateContent: function() {
return fs.readFileSync(path.join(__dirname, 'fixtures/test.html'), 'utf8');
}
})]
},
['<script src="app_bundle.js"'], null, done);
});
it('registers a webpack error both template and template content are specified', function(done) {
webpack({
entry: path.join(__dirname, 'fixtures/index.js'),