Merge pull request #3938 from carloscuatin/refactor-normal-module-replacement-plugin

refactor(es6) upgrade NormalModuleReplacementPlugin to ES6 class
This commit is contained in:
Tobias Koppers 2017-01-18 15:58:08 +01:00 committed by GitHub
commit 8ccbee608e
1 changed files with 37 additions and 32 deletions

View File

@ -2,19 +2,21 @@
MIT License http://www.opensource.org/licenses/mit-license.php MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra Author Tobias Koppers @sokra
*/ */
"use strict";
var path = require("path"); const path = require("path");
function NormalModuleReplacementPlugin(resourceRegExp, newResource) { class NormalModuleReplacementPlugin {
constructor(resourceRegExp, newResource) {
this.resourceRegExp = resourceRegExp; this.resourceRegExp = resourceRegExp;
this.newResource = newResource; this.newResource = newResource;
} }
module.exports = NormalModuleReplacementPlugin;
NormalModuleReplacementPlugin.prototype.apply = function(compiler) { apply(compiler) {
var resourceRegExp = this.resourceRegExp; let resourceRegExp = this.resourceRegExp;
var newResource = this.newResource; let newResource = this.newResource;
compiler.plugin("normal-module-factory", function(nmf) { compiler.plugin("normal-module-factory", (nmf) => {
nmf.plugin("before-resolve", function(result, callback) { nmf.plugin("before-resolve", (result, callback) => {
if(!result) return callback(); if(!result) return callback();
if(resourceRegExp.test(result.request)) { if(resourceRegExp.test(result.request)) {
if(typeof newResource === "function") { if(typeof newResource === "function") {
@ -25,7 +27,7 @@ NormalModuleReplacementPlugin.prototype.apply = function(compiler) {
} }
return callback(null, result); return callback(null, result);
}); });
nmf.plugin("after-resolve", function(result, callback) { nmf.plugin("after-resolve", (result, callback) => {
if(!result) return callback(); if(!result) return callback();
if(resourceRegExp.test(result.resource)) { if(resourceRegExp.test(result.resource)) {
if(typeof newResource === "function") { if(typeof newResource === "function") {
@ -37,4 +39,7 @@ NormalModuleReplacementPlugin.prototype.apply = function(compiler) {
return callback(null, result); return callback(null, result);
}); });
}); });
}; }
}
module.exports = NormalModuleReplacementPlugin;