From 7b7620d8f955b8df115f8af599d5ac227800d22c Mon Sep 17 00:00:00 2001 From: Tom Chen Date: Mon, 7 Sep 2015 23:18:28 +0800 Subject: [PATCH] feat(options): add failOnUnused to enable generating error * Original commit: 33d7a62ffd5bdd762e2a600bfb14e554e476dc6a * Original author: @oliviertassinari * Closes #3 --- lib/index.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index 9c5be64..db2c27a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -8,6 +8,7 @@ var glob = require("glob"); function UnusedFilesWebpackPlugin (_options_) { var options = this.options = _options_ || {}; options.pattern = options.pattern || "**/*.*"; + options.failOnUnused = options.failOnUnused === true; var globOptions = this.globOptions = objectAssign({ ignore: "node_modules/**/*" @@ -22,7 +23,7 @@ UnusedFilesWebpackPlugin.prototype.apply = function(compiler) { UnusedFilesWebpackPlugin.prototype._applyAfterEmit = function(compiler, compilation, done) { var globOptions = this._getGlobOptions(compiler); - glob(this.options.pattern, globOptions, onGlobResult); + glob(this.options.pattern, globOptions, onGlobResult.bind(this)); var fileDepsMap = this._getFileDepsMap(compilation); @@ -37,8 +38,13 @@ UnusedFilesWebpackPlugin.prototype._applyAfterEmit = function(compiler, compilat }); if (unused.length) { var errmsg = "UnusedFilesWebpackPlugin found some unused files:\n" + unused.join("\n"); + var error = new Error(errmsg); - compilation.warnings.push(new Error(errmsg)); + if (this.options.failOnUnused) { + compilation.errors.push(error); + } else { + compilation.warnings.push(error); + } } } done();