mirror of https://github.com/webpack/webpack.git
Merge pull request #4291 from Tushkiz/refactor-ruleset-to-es6
migrate 'lib/RuleSet.js' to es6
This commit is contained in:
commit
8b0b7aa64c
173
lib/RuleSet.js
173
lib/RuleSet.js
|
|
@ -69,18 +69,18 @@ normalized:
|
|||
}
|
||||
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
function RuleSet(rules) {
|
||||
module.exports = class RuleSet {
|
||||
constructor(rules) {
|
||||
this.references = Object.create(null);
|
||||
this.rules = RuleSet.normalizeRules(rules, this.references, "ref-");
|
||||
}
|
||||
|
||||
module.exports = RuleSet;
|
||||
|
||||
RuleSet.normalizeRules = function(rules, refs, ident) {
|
||||
static normalizeRules(rules, refs, ident) {
|
||||
if(Array.isArray(rules)) {
|
||||
return rules.map(function(rule, idx) {
|
||||
return rules.map((rule, idx) => {
|
||||
return RuleSet.normalizeRule(rule, refs, `${ident}-${idx}`);
|
||||
});
|
||||
} else if(rules) {
|
||||
|
|
@ -88,9 +88,9 @@ RuleSet.normalizeRules = function(rules, refs, ident) {
|
|||
} else {
|
||||
return [];
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
RuleSet.normalizeRule = function(rule, refs, ident) {
|
||||
static normalizeRule(rule, refs, ident) {
|
||||
if(typeof rule === "string")
|
||||
return {
|
||||
use: [{
|
||||
|
|
@ -102,10 +102,10 @@ RuleSet.normalizeRule = function(rule, refs, ident) {
|
|||
if(typeof rule !== "object")
|
||||
throw new Error("Unexcepted " + typeof rule + " when object was expected as rule (" + rule + ")");
|
||||
|
||||
var newRule = {};
|
||||
var useSource;
|
||||
var resourceSource;
|
||||
var condition;
|
||||
let newRule = {};
|
||||
let useSource;
|
||||
let resourceSource;
|
||||
let condition;
|
||||
|
||||
if(rule.test || rule.include || rule.exclude) {
|
||||
checkResourceSource("test + include + exclude");
|
||||
|
|
@ -149,7 +149,7 @@ RuleSet.normalizeRule = function(rule, refs, ident) {
|
|||
if(rule.loader && rule.loaders)
|
||||
throw new Error(RuleSet.buildErrorMessage(rule, new Error("Provided loader and loaders for rule (use only one of them)")));
|
||||
|
||||
var loader = rule.loaders || rule.loader;
|
||||
const loader = rule.loaders || rule.loader;
|
||||
if(typeof loader === "string" && !rule.options && !rule.query) {
|
||||
checkUseSource("loader");
|
||||
newRule.use = RuleSet.normalizeUse(loader.split("!"), ident);
|
||||
|
|
@ -180,10 +180,10 @@ RuleSet.normalizeRule = function(rule, refs, ident) {
|
|||
if(rule.oneOf)
|
||||
newRule.oneOf = RuleSet.normalizeRules(rule.oneOf, refs, `${ident}-oneOf`);
|
||||
|
||||
var keys = Object.keys(rule).filter(function(key) {
|
||||
const keys = Object.keys(rule).filter((key) => {
|
||||
return ["resource", "resourceQuery", "test", "include", "exclude", "issuer", "loader", "options", "query", "loaders", "use", "rules", "oneOf"].indexOf(key) < 0;
|
||||
});
|
||||
keys.forEach(function(key) {
|
||||
keys.forEach((key) => {
|
||||
newRule[key] = rule[key];
|
||||
});
|
||||
|
||||
|
|
@ -200,7 +200,7 @@ RuleSet.normalizeRule = function(rule, refs, ident) {
|
|||
}
|
||||
|
||||
if(Array.isArray(newRule.use)) {
|
||||
newRule.use.forEach(function(item) {
|
||||
newRule.use.forEach((item) => {
|
||||
if(item.ident) {
|
||||
refs[item.ident] = item.options;
|
||||
}
|
||||
|
|
@ -208,35 +208,34 @@ RuleSet.normalizeRule = function(rule, refs, ident) {
|
|||
}
|
||||
|
||||
return newRule;
|
||||
};
|
||||
}
|
||||
|
||||
RuleSet.buildErrorMessage = function buildErrorMessage(condition, error) {
|
||||
var conditionAsText = JSON.stringify(condition, function(key, value) {
|
||||
static buildErrorMessage(condition, error) {
|
||||
const conditionAsText = JSON.stringify(condition, (key, value) => {
|
||||
return value === undefined ? "undefined" : value;
|
||||
}, 2);
|
||||
var message = error.message + " in " + conditionAsText;
|
||||
return message;
|
||||
};
|
||||
return error.message + " in " + conditionAsText;
|
||||
}
|
||||
|
||||
RuleSet.normalizeUse = function normalizeUse(use, ident) {
|
||||
static normalizeUse(use, ident) {
|
||||
if(Array.isArray(use)) {
|
||||
return use.map((item, idx) => RuleSet.normalizeUse(item, `${ident}-${idx}`)).reduce(function(arr, items) {
|
||||
return arr.concat(items);
|
||||
}, []);
|
||||
return use
|
||||
.map((item, idx) => RuleSet.normalizeUse(item, `${ident}-${idx}`))
|
||||
.reduce((arr, items) => arr.concat(items), []);
|
||||
}
|
||||
return [RuleSet.normalizeUseItem(use, ident)];
|
||||
};
|
||||
}
|
||||
|
||||
RuleSet.normalizeUseItemFunction = function normalizeUseItemFunction(use, data) {
|
||||
var result = use(data);
|
||||
static normalizeUseItemFunction(use, data) {
|
||||
const result = use(data);
|
||||
if(typeof result === "string") {
|
||||
return RuleSet.normalizeUseItem(result);
|
||||
}
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
RuleSet.normalizeUseItemString = function normalizeUseItemString(useItemString) {
|
||||
var idx = useItemString.indexOf("?");
|
||||
static normalizeUseItemString(useItemString) {
|
||||
const idx = useItemString.indexOf("?");
|
||||
if(idx >= 0) {
|
||||
return {
|
||||
loader: useItemString.substr(0, idx),
|
||||
|
|
@ -246,9 +245,9 @@ RuleSet.normalizeUseItemString = function normalizeUseItemString(useItemString)
|
|||
return {
|
||||
loader: useItemString
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
RuleSet.normalizeUseItem = function normalizeUseItem(item, ident) {
|
||||
static normalizeUseItem(item, ident) {
|
||||
if(typeof item === "function")
|
||||
return item;
|
||||
|
||||
|
|
@ -256,7 +255,7 @@ RuleSet.normalizeUseItem = function normalizeUseItem(item, ident) {
|
|||
return RuleSet.normalizeUseItemString(item);
|
||||
}
|
||||
|
||||
var newItem = {};
|
||||
let newItem = {};
|
||||
|
||||
if(item.options && item.query)
|
||||
throw new Error("Provided options and query in use");
|
||||
|
|
@ -273,7 +272,7 @@ RuleSet.normalizeUseItem = function normalizeUseItem(item, ident) {
|
|||
newItem.ident = ident;
|
||||
}
|
||||
|
||||
var keys = Object.keys(item).filter(function(key) {
|
||||
const keys = Object.keys(item).filter(function(key) {
|
||||
return ["options", "query"].indexOf(key) < 0;
|
||||
});
|
||||
|
||||
|
|
@ -282,15 +281,13 @@ RuleSet.normalizeUseItem = function normalizeUseItem(item, ident) {
|
|||
});
|
||||
|
||||
return newItem;
|
||||
};
|
||||
}
|
||||
|
||||
RuleSet.normalizeCondition = function normalizeCondition(condition) {
|
||||
static normalizeCondition(condition) {
|
||||
if(!condition)
|
||||
throw new Error("Expected condition but got falsy value");
|
||||
if(typeof condition === "string") {
|
||||
return function(str) {
|
||||
return str.indexOf(condition) === 0;
|
||||
};
|
||||
return str => str.indexOf(condition) === 0;
|
||||
}
|
||||
if(typeof condition === "function") {
|
||||
return condition;
|
||||
|
|
@ -299,16 +296,15 @@ RuleSet.normalizeCondition = function normalizeCondition(condition) {
|
|||
return condition.test.bind(condition);
|
||||
}
|
||||
if(Array.isArray(condition)) {
|
||||
var items = condition.map(function(c) {
|
||||
return RuleSet.normalizeCondition(c);
|
||||
});
|
||||
const items = condition.map(c => RuleSet.normalizeCondition(c));
|
||||
return orMatcher(items);
|
||||
}
|
||||
if(typeof condition !== "object")
|
||||
throw Error("Unexcepted " + typeof condition + " when condition was expected (" + condition + ")");
|
||||
var matchers = [];
|
||||
Object.keys(condition).forEach(function(key) {
|
||||
var value = condition[key];
|
||||
|
||||
let matchers = [];
|
||||
Object.keys(condition).forEach(key => {
|
||||
const value = condition[key];
|
||||
switch(key) {
|
||||
case "or":
|
||||
case "include":
|
||||
|
|
@ -318,16 +314,14 @@ RuleSet.normalizeCondition = function normalizeCondition(condition) {
|
|||
break;
|
||||
case "and":
|
||||
if(value) {
|
||||
var items = value.map(function(c) {
|
||||
return RuleSet.normalizeCondition(c);
|
||||
});
|
||||
const items = value.map(c => RuleSet.normalizeCondition(c));
|
||||
matchers.push(andMatcher(items));
|
||||
}
|
||||
break;
|
||||
case "not":
|
||||
case "exclude":
|
||||
if(value) {
|
||||
var matcher = RuleSet.normalizeCondition(value);
|
||||
const matcher = RuleSet.normalizeCondition(value);
|
||||
matchers.push(notMatcher(matcher));
|
||||
}
|
||||
break;
|
||||
|
|
@ -340,43 +334,17 @@ RuleSet.normalizeCondition = function normalizeCondition(condition) {
|
|||
if(matchers.length === 1)
|
||||
return matchers[0];
|
||||
return andMatcher(matchers);
|
||||
};
|
||||
|
||||
function notMatcher(matcher) {
|
||||
return function(str) {
|
||||
return !matcher(str);
|
||||
};
|
||||
}
|
||||
|
||||
function orMatcher(items) {
|
||||
return function(str) {
|
||||
for(var i = 0; i < items.length; i++) {
|
||||
if(items[i](str))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
}
|
||||
|
||||
function andMatcher(items) {
|
||||
return function(str) {
|
||||
for(var i = 0; i < items.length; i++) {
|
||||
if(!items[i](str))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
RuleSet.prototype.exec = function(data) {
|
||||
var result = [];
|
||||
exec(data) {
|
||||
const result = [];
|
||||
this._run(data, {
|
||||
rules: this.rules
|
||||
}, result);
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
RuleSet.prototype._run = function _run(data, rule, result) {
|
||||
_run(data, rule, result) {
|
||||
// test conditions
|
||||
if(rule.resource && !data.resource)
|
||||
return false;
|
||||
|
|
@ -392,10 +360,10 @@ RuleSet.prototype._run = function _run(data, rule, result) {
|
|||
return false;
|
||||
|
||||
// apply
|
||||
var keys = Object.keys(rule).filter(function(key) {
|
||||
const keys = Object.keys(rule).filter((key) => {
|
||||
return ["resource", "resourceQuery", "issuer", "rules", "oneOf", "use", "enforce"].indexOf(key) < 0;
|
||||
});
|
||||
keys.forEach(function(key) {
|
||||
keys.forEach((key) => {
|
||||
result.push({
|
||||
type: key,
|
||||
value: rule[key]
|
||||
|
|
@ -403,7 +371,7 @@ RuleSet.prototype._run = function _run(data, rule, result) {
|
|||
});
|
||||
|
||||
if(rule.use) {
|
||||
rule.use.forEach(function(use) {
|
||||
rule.use.forEach((use) => {
|
||||
result.push({
|
||||
type: "use",
|
||||
value: typeof use === "function" ? RuleSet.normalizeUseItemFunction(use, data) : use,
|
||||
|
|
@ -412,26 +380,51 @@ RuleSet.prototype._run = function _run(data, rule, result) {
|
|||
});
|
||||
}
|
||||
|
||||
var i;
|
||||
|
||||
if(rule.rules) {
|
||||
for(i = 0; i < rule.rules.length; i++) {
|
||||
for(let i = 0; i < rule.rules.length; i++) {
|
||||
this._run(data, rule.rules[i], result);
|
||||
}
|
||||
}
|
||||
|
||||
if(rule.oneOf) {
|
||||
for(i = 0; i < rule.oneOf.length; i++) {
|
||||
for(let i = 0; i < rule.oneOf.length; i++) {
|
||||
if(this._run(data, rule.oneOf[i], result))
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
||||
RuleSet.prototype.findOptionsByIdent = function findOptionsByIdent(ident) {
|
||||
var options = this.references[ident];
|
||||
findOptionsByIdent(ident) {
|
||||
const options = this.references[ident];
|
||||
if(!options) throw new Error("Can't find options with ident '" + ident + "'");
|
||||
return options;
|
||||
}
|
||||
};
|
||||
|
||||
function notMatcher(matcher) {
|
||||
return function(str) {
|
||||
return !matcher(str);
|
||||
};
|
||||
}
|
||||
|
||||
function orMatcher(items) {
|
||||
return function(str) {
|
||||
for(let i = 0; i < items.length; i++) {
|
||||
if(items[i](str))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
}
|
||||
|
||||
function andMatcher(items) {
|
||||
return function(str) {
|
||||
for(let i = 0; i < items.length; i++) {
|
||||
if(!items[i](str))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue