From 3b6e38905c1c7b2afef1a604a53c6984847d7a1b Mon Sep 17 00:00:00 2001 From: Maxim Samoilov Date: Sun, 1 May 2016 12:06:31 +0600 Subject: [PATCH 1/2] Add reporter option --- middleware.js | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/middleware.js b/middleware.js index 0cb1b2f25..e993a01eb 100644 --- a/middleware.js +++ b/middleware.js @@ -42,16 +42,21 @@ module.exports = function(compiler, options) { // check if still in valid state if(!state) return; // print webpack output - var displayStats = (!options.quiet && options.stats !== false); - if(displayStats && - !(stats.hasErrors() || stats.hasWarnings()) && - options.noInfo) - displayStats = false; - if(displayStats) { - console.log(stats.toString(options.stats)); + if(options.reporter) { + options.reporter({ state: true, stats: stats, options: options }); + } else { + var displayStats = (!options.quiet && options.stats !== false); + if(displayStats && + !(stats.hasErrors() || stats.hasWarnings()) && + options.noInfo) + displayStats = false; + if(displayStats) { + console.log(stats.toString(options.stats)) + } + if(!options.noInfo && !options.quiet) { + console.info("webpack: bundle is now VALID."); + } } - if (!options.noInfo && !options.quiet) - console.info("webpack: bundle is now VALID."); // execute callback that are delayed var cbs = callbacks; @@ -70,8 +75,13 @@ module.exports = function(compiler, options) { // on compiling function invalidPlugin() { - if(state && (!options.noInfo && !options.quiet)) - console.info("webpack: bundle is now INVALID."); + if(state && (!options.noInfo && !options.quiet)) { + if(options.reporter) { + options.reporter({ state: false, options: options }) + } else { + console.info("webpack: bundle is now INVALID."); + } + } // We are now in invalid state state = false; } From 14bacb7b48266a50cc7cd4665d5cb8a4568fde5e Mon Sep 17 00:00:00 2001 From: Maxim Samoilov Date: Sun, 1 May 2016 12:30:10 +0600 Subject: [PATCH 2/2] Move defaultReporter to separate function --- middleware.js | 48 ++++++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/middleware.js b/middleware.js index e993a01eb..b2fb47b9e 100644 --- a/middleware.js +++ b/middleware.js @@ -8,6 +8,28 @@ var parseRange = require("range-parser"); var HASH_REGEXP = /[0-9a-f]{10,}/; +var defaultReporter = function (reporterOptions) { + var state = reporterOptions.state; + var stats = reporterOptions.stats; + var options = reporterOptions.options; + + if(state) { + var displayStats = (!options.quiet && options.stats !== false); + if(displayStats && + !(stats.hasErrors() || stats.hasWarnings()) && + options.noInfo) + displayStats = false; + if(displayStats) { + console.log(stats.toString(options.stats)) + } + if(!options.noInfo && !options.quiet) { + console.info("webpack: bundle is now VALID."); + } + } else { + console.info("webpack: bundle is now INVALID."); + } +} + // constructor for the middleware module.exports = function(compiler, options) { if(!options) options = {}; @@ -28,6 +50,7 @@ module.exports = function(compiler, options) { options.filename = new RegExp("^[\/]{0,1}" + str + "$"); } } + if(typeof options.reporter !== "function") options.reporter = defaultReporter; // store our files in memory var files = {}; @@ -42,21 +65,7 @@ module.exports = function(compiler, options) { // check if still in valid state if(!state) return; // print webpack output - if(options.reporter) { - options.reporter({ state: true, stats: stats, options: options }); - } else { - var displayStats = (!options.quiet && options.stats !== false); - if(displayStats && - !(stats.hasErrors() || stats.hasWarnings()) && - options.noInfo) - displayStats = false; - if(displayStats) { - console.log(stats.toString(options.stats)) - } - if(!options.noInfo && !options.quiet) { - console.info("webpack: bundle is now VALID."); - } - } + options.reporter({ state: true, stats: stats, options: options }); // execute callback that are delayed var cbs = callbacks; @@ -75,13 +84,8 @@ module.exports = function(compiler, options) { // on compiling function invalidPlugin() { - if(state && (!options.noInfo && !options.quiet)) { - if(options.reporter) { - options.reporter({ state: false, options: options }) - } else { - console.info("webpack: bundle is now INVALID."); - } - } + if(state && (!options.noInfo && !options.quiet)) + options.reporter({ state: false, options: options }) // We are now in invalid state state = false; }