From fbeb8ca2fc573e3823eb0cf72c5b7ca82933fef3 Mon Sep 17 00:00:00 2001 From: Carlos Cuatin Date: Wed, 4 Jan 2017 00:13:20 -0500 Subject: [PATCH] refactor(es6): refactor LoaderOptionsPlugin to ES6 class (#3657) * refactor(es6): refactor LoaderOptionsPlugin to ES6 class --- lib/LoaderOptionsPlugin.js | 56 ++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/lib/LoaderOptionsPlugin.js b/lib/LoaderOptionsPlugin.js index 8fcccedbe..21600984c 100644 --- a/lib/LoaderOptionsPlugin.js +++ b/lib/LoaderOptionsPlugin.js @@ -2,33 +2,35 @@ MIT License http://www.opensource.org/licenses/mit-license.php Author Tobias Koppers @sokra */ -var ModuleFilenameHelpers = require("./ModuleFilenameHelpers"); +"use strict"; -function LoaderOptionsPlugin(options) { - if(typeof options !== "object") options = {}; - if(!options.test) options.test = { - test: function() { - return true; - } - }; - this.options = options; -} -module.exports = LoaderOptionsPlugin; +const ModuleFilenameHelpers = require("./ModuleFilenameHelpers"); -LoaderOptionsPlugin.prototype.apply = function(compiler) { - var options = this.options; - compiler.plugin("compilation", function(compilation) { - compilation.plugin("normal-module-loader", function(context, module) { - var resource = module.resource; - if(!resource) return; - var i = resource.indexOf("?"); - if(ModuleFilenameHelpers.matchObject(options, i < 0 ? resource : resource.substr(0, i))) { - Object.keys(options).filter(function(key) { - return ["include", "exclude", "test"].indexOf(key) < 0 - }).forEach(function(key) { - context[key] = options[key]; - }); - } +class LoaderOptionsPlugin { + constructor(options) { + if(typeof options !== "object") options = {}; + if(!options.test) options.test = { + test: () => true + }; + this.options = options; + } + + apply(compiler) { + let options = this.options; + compiler.plugin("compilation", (compilation) => { + compilation.plugin("normal-module-loader", (context, module) => { + let resource = module.resource; + if(!resource) return; + let i = resource.indexOf("?"); + if(ModuleFilenameHelpers.matchObject(options, i < 0 ? resource : resource.substr(0, i))) { + let filterSet = new Set(["include", "exclude", "test"]); + Object.keys(options) + .filter((key) => !filterSet.has(key)) + .forEach((key) => context[key] = options[key]); + } + }); }); - }); -}; + } +} + +module.exports = LoaderOptionsPlugin;