This repository has been archived by the owner on Jan 16, 2018. It is now read-only.
/
LoadersList.js
96 lines (90 loc) · 2.68 KB
/
LoadersList.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
function LoadersList(list) {
this.list = list || [];
this.list.forEach(function(element) {
if(element === null || typeof element !== "object")
throw new Error("Each element of the loaders list must be an object or array");
});
}
module.exports = LoadersList;
function regExpAsMatcher(regExp) {
return function(str) {
return regExp.test(str);
}
}
function asMatcher(test) {
if(typeof test === "string") {
return regExpAsMatcher(new RegExp("^"+test.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&")));
} else if(typeof test === "function") {
return test;
} else if(test instanceof RegExp) {
return regExpAsMatcher(test);
} else if(Array.isArray(test)) {
var matchers = test.map(function(item) {
if(Array.isArray(item)) {
var matchers = item.map(asMatcher);
return function(str) {
return matchers.every(function(matcher) {
return matcher(str);
});
};
} else {
return asMatcher(item);
}
});
return function(str) {
for(var i = 0; i < test.length; i++) {
if(matchers[i](str))
return true;
}
return false;
};
} else {
throw new Error(test + " is not a valid test");
}
}
function getLoadersFromObject(element) {
if(element.query) {
if(!element.loader || element.loader.indexOf("!") >= 0) throw new Error("Cannot define 'query' and multiple loaders in loaders list");
if(typeof element.query === "string") return [element.loader + "?" + element.query];
return [element.loader + "?" + JSON.stringify(element.query)];
}
if(element.loader) return element.loader.split("!");
if(element.loaders) return element.loaders;
throw new Error("Element from loaders list should have one of the fields 'loader' or 'loaders'");
}
LoadersList.prototype.matchPart = function matchPart(str, test) {
if(!test) return true;
var matcher = asMatcher(test);
return matcher(str);
};
LoadersList.prototype.match = function match(str) {
return this.list.map(function(element) {
if(Array.isArray(element)) {
for(var i = 0; i < element.length; i++) {
if(this.matchObject(str, element[i]))
return getLoadersFromObject(element[i]);
}
} else {
if(this.matchObject(str, element))
return getLoadersFromObject(element);
}
}, this).filter(Boolean).reduce(function(array, r) {
r.forEach(function(r) {
array.push(r);
});
return array;
}, []) || [];
};
LoadersList.prototype.matchObject = function matchObject(str, obj) {
if(obj.test)
if(!this.matchPart(str, obj.test)) return false;
if(obj.include)
if(!this.matchPart(str, obj.include)) return false;
if(obj.exclude)
if(this.matchPart(str, obj.exclude)) return false;
return true;
};