From 14b1a38dfa9ee6ca4132aa0754de05327b472aa5 Mon Sep 17 00:00:00 2001 From: Charles Blaxland Date: Thu, 14 Aug 2014 20:36:15 +1000 Subject: [PATCH] Read template file asynchronously --- index.js | 27 +++++++++++++---------- spec/HtmlWebpackPluginSpec.js | 41 ++++++++++++++++++++++++----------- 2 files changed, 44 insertions(+), 24 deletions(-) diff --git a/index.js b/index.js index 0e192bc..b48abd4 100644 --- a/index.js +++ b/index.js @@ -21,18 +21,23 @@ HtmlWebpackPlugin.prototype.apply = function(compiler) { templateFile = path.join(__dirname, 'default_index.html'); } - var htmlTemplateContent = fs.readFileSync(templateFile, 'utf8'); - var html = tmpl(htmlTemplateContent, templateParams); - var outputFilename = self.options.filename || 'index.html'; - compiler.assets[outputFilename] = { - source: function() { - return html; - }, - size: function() { - return html.length; + 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; + } + }; } - }; - callback(); + callback(); + }); }); }; diff --git a/spec/HtmlWebpackPluginSpec.js b/spec/HtmlWebpackPluginSpec.js index 766d87b..b1f15f1 100644 --- a/spec/HtmlWebpackPluginSpec.js +++ b/spec/HtmlWebpackPluginSpec.js @@ -6,7 +6,7 @@ var HtmlWebpackPlugin = require('../index.js'); var OUTPUT_DIR = path.join(__dirname, '../dist'); -function testHtmlPlugin(webpackConfig, expectedResults, done, outputFile) { +function testHtmlPlugin(webpackConfig, expectedResults, outputFile, done) { outputFile = outputFile || 'index.html'; webpack(webpackConfig, function(err, stats) { expect(err).toBeFalsy(); @@ -37,7 +37,7 @@ describe('HtmlWebpackPlugin', function() { filename: 'index_bundle.js' }, plugins: [new HtmlWebpackPlugin()] - }, ['