From c26597403c77d25ec4b153f8beabc4cd8911e2bb Mon Sep 17 00:00:00 2001 From: Charles Blaxland Date: Tue, 12 Aug 2014 22:16:41 +1000 Subject: [PATCH] Allow user defined HTML templates --- index.js | 11 ++++++++--- spec/HtmlWebpackPluginSpec.js | 23 +++++++++++++++++++---- spec/fixtures/test.html | 11 +++++++++++ 3 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 spec/fixtures/test.html diff --git a/index.js b/index.js index 5d73257..3a3418c 100644 --- a/index.js +++ b/index.js @@ -2,7 +2,8 @@ var fs = require('fs'); var path = require('path'); var tmpl = require('blueimp-tmpl').tmpl; -function HtmlWebpackPlugin() { +function HtmlWebpackPlugin(options) { + this.options = options || {}; } HtmlWebpackPlugin.prototype.apply = function(compiler) { @@ -13,8 +14,12 @@ HtmlWebpackPlugin.prototype.apply = function(compiler) { templateParams.webpack = webpackStatsJson; templateParams.htmlWebpackPlugin = self.htmlWebpackPluginJson(webpackStatsJson); - var htmlTemplate = fs.readFileSync(path.join(__dirname, 'default_index.html'), 'utf8'); - fs.writeFileSync(path.join(compiler.options.output.path, 'index.html'), tmpl(htmlTemplate, templateParams)); + var templateFile = self.options.template; + if (!templateFile) { + templateFile = path.join(__dirname, 'default_index.html'); + } + var htmlTemplateContent = fs.readFileSync(templateFile, 'utf8'); + fs.writeFileSync(path.join(compiler.options.output.path, 'index.html'), tmpl(htmlTemplateContent, templateParams)); }); }; diff --git a/spec/HtmlWebpackPluginSpec.js b/spec/HtmlWebpackPluginSpec.js index 522cb3b..c26ca50 100644 --- a/spec/HtmlWebpackPluginSpec.js +++ b/spec/HtmlWebpackPluginSpec.js @@ -6,9 +6,10 @@ var HtmlWebpackPlugin = require('../index.js'); var OUTPUT_DIR = path.join(__dirname, '..', 'dist'); -function testHtmlPlugin(webpackConfig, expectedResults, done) { +function testHtmlPlugin(webpackConfig, htmlPluginOptions, expectedResults, done) { var outputHtmlFile = path.join(OUTPUT_DIR, 'index.html'); - webpackConfig.plugins = [new HtmlWebpackPlugin()]; + var htmlPlugin = htmlPluginOptions ? new HtmlWebpackPlugin(htmlPluginOptions) : new HtmlWebpackPlugin(); + webpackConfig.plugins = [htmlPlugin]; webpack(webpackConfig, function(err, stats) { expect(err).toBeFalsy(); expect(stats.hasErrors()).toBe(false); @@ -32,7 +33,7 @@ describe('HtmlWebpackPlugin', function() { path: OUTPUT_DIR, filename: 'index_bundle.js' } - }, [' + +