Allow template to be specified as a string

This commit is contained in:
Charles Blaxland 2014-08-14 21:18:58 +10:00
parent 0394b7845a
commit 0f15aee3e2
2 changed files with 46 additions and 21 deletions

View File

@ -16,31 +16,41 @@ HtmlWebpackPlugin.prototype.apply = function(compiler) {
templateParams.htmlWebpackPlugin.assets = self.htmlWebpackPluginAssets(compiler, webpackStatsJson);
templateParams.htmlWebpackPlugin.options = self.options;
var templateFile = self.options.template;
if (!templateFile) {
templateFile = path.join(__dirname, 'default_index.html');
}
var outputFilename = self.options.filename || 'index.html';
fs.readFile(templateFile, 'utf8', function(err, htmlTemplateContent) {
if (err) {
compiler.errors.push(new Error('HtmlWebpackPlugin: Unable to read HTML template "' + templateFile + '"'));
} else {
var html = tmpl(htmlTemplateContent, templateParams);
var outputFilename = self.options.filename || 'index.html';
compiler.assets[outputFilename] = {
source: function() {
return html;
},
size: function() {
return html.length;
}
};
}
if (self.options.templateContent) {
self.emitHtml(compiler, self.options.templateContent, templateParams, outputFilename);
callback();
});
} else {
var templateFile = self.options.template;
if (!templateFile) {
templateFile = path.join(__dirname, 'default_index.html');
}
fs.readFile(templateFile, 'utf8', function(err, htmlTemplateContent) {
if (err) {
compiler.errors.push(new Error('HtmlWebpackPlugin: Unable to read HTML template "' + templateFile + '"'));
} else {
self.emitHtml(compiler, htmlTemplateContent, templateParams, outputFilename);
}
callback();
});
}
});
};
HtmlWebpackPlugin.prototype.emitHtml = function(compiler, htmlTemplateContent, templateParams, outputFilename) {
var html = tmpl(htmlTemplateContent, templateParams);
compiler.assets[outputFilename] = {
source: function() {
return html;
},
size: function() {
return html.length;
}
};
};
HtmlWebpackPlugin.prototype.htmlWebpackPluginAssets = function(compiler, webpackStatsJson) {
var assets = {};
for (var chunk in webpackStatsJson.assetsByChunkName) {

View File

@ -55,7 +55,7 @@ describe('HtmlWebpackPlugin', function() {
}, ['<script src="util_bundle.js"', '<script src="app_bundle.js"'], null, done);
});
it('allows you to specify your own HTML template', function(done) {
it('allows you to specify your own HTML template file', function(done) {
testHtmlPlugin({
entry: {
app: path.join(__dirname, 'fixtures/index.js')
@ -69,6 +69,21 @@ describe('HtmlWebpackPlugin', function() {
['<script src="app_bundle.js"', 'Some unique text'], null, done);
});
it('allows you to specify your own HTML template string', function(done) {
testHtmlPlugin({
entry: {app: path.join(__dirname, 'fixtures/index.js')},
output: {
path: OUTPUT_DIR,
filename: 'app_bundle.js'
},
plugins: [new HtmlWebpackPlugin({
templateContent: fs.readFileSync(path.join(__dirname, 'fixtures/test.html'), 'utf8')
})]
},
['<script src="app_bundle.js"'], null, done);
});
it('works with source maps', function(done) {
testHtmlPlugin({
devtool: 'sourcemap',