webpack 2.1.0-beta.24 → 2.1.0-beta.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/RuleSet.js CHANGED
@@ -1,369 +1,369 @@
1
- /*
2
- MIT License http://www.opensource.org/licenses/mit-license.php
3
- Author Tobias Koppers @sokra
4
- */
5
- /*
6
- <rules>: <rule>
7
- <rules>: [<rule>]
8
- <rule>: {
9
- resource: {
10
- test: <condition>,
11
- include: <condition>,
12
- exclude: <condition>,
13
- },
14
- resource: <condition>, -> resource.test
15
- test: <condition>, -> resource.test
16
- include: <condition>, -> resource.include
17
- exclude: <condition>, -> resource.exclude
18
- issuer: {
19
- test: <condition>,
20
- include: <condition>,
21
- exclude: <condition>,
22
- },
23
- issuer: <condition>, -> issuer.test
24
- use: "loader", -> use[0].loader
25
- loader: <>, -> use[0].loader
26
- loaders: <>, -> use
27
- options: {}, -> use[0].options,
28
- query: {}, -> options
29
- parser: {},
30
- use: [
31
- "loader" -> use[x].loader
32
- ],
33
- use: [
34
- {
35
- loader: "loader",
36
- options: {}
37
- }
38
- ],
39
- rules: [
40
- <rule>
41
- ],
42
- oneOf: [
43
- <rule>
44
- ]
45
- }
46
-
47
- <condition>: /regExp/
48
- <condition>: function(arg) {}
49
- <condition>: "starting"
50
- <condition>: [<condition>] // or
51
- <condition>: { and: [<condition>] }
52
- <condition>: { or: [<condition>] }
53
- <condition>: { not: [<condition>] }
54
- <condition>: { test: <condition>, include: <condition>, exclude: <codition> }
55
-
56
-
57
- normalized:
58
-
59
- {
60
- resource: function(),
61
- issuer: function(),
62
- use: [
63
- {
64
- loader: string,
65
- options: string,
66
- <any>: <any>
67
- }
68
- ],
69
- rules: [<rule>],
70
- oneOf: [<rule>],
71
- <any>: <any>,
72
- }
73
-
74
- */
75
-
76
- function RuleSet(rules) {
77
- this.rules = RuleSet.normalizeRules(rules);
78
- }
79
-
80
- module.exports = RuleSet;
81
-
82
- RuleSet.normalizeRules = function(rules) {
83
- if(Array.isArray(rules)) {
84
- return rules.map(function(rule) {
85
- return RuleSet.normalizeRule(rule);
86
- });
87
- } else if(rules) {
88
- return [RuleSet.normalizeRule(rules)]
89
- } else {
90
- return [];
91
- }
92
- };
93
-
94
- RuleSet.normalizeRule = function(rule) {
95
- if(typeof rule === "string")
96
- return {
97
- use: [{
98
- loader: rule
99
- }]
100
- };
101
- if(!rule)
102
- throw new Error("Unexcepted null when object was expected as rule");
103
- if(typeof rule !== "object")
104
- throw new Error("Unexcepted " + typeof rule + " when object was expected as rule (" + rule + ")");
105
-
106
- var newRule = {};
107
- var useSource;
108
- var resourceSource;
109
-
110
- if(rule.test || rule.include || rule.exclude) {
111
- checkResourceSource("test + include + exclude");
112
- newRule.resource = RuleSet.normalizeCondition({
113
- test: rule.test,
114
- include: rule.include,
115
- exclude: rule.exclude
116
- });
117
- }
118
-
119
- if(rule.resource) {
120
- checkResourceSource("resource");
121
- newRule.resource = RuleSet.normalizeCondition(rule.resource);
122
- }
123
-
124
- if(rule.issuer) {
125
- newRule.issuer = RuleSet.normalizeCondition(rule.issuer);
126
- }
127
-
128
- if(rule.loader && rule.loaders)
129
- throw new Error("Provided loader and loaders for rule");
130
-
131
- var loader = rule.loaders || rule.loader;
132
- if(typeof loader === "string" && !rule.options && !rule.query) {
133
- checkUseSource("loader");
134
- newRule.use = RuleSet.normalizeUse(loader.split("!"));
135
- } else if(typeof loader === "string" && (rule.options || rule.query)) {
136
- checkUseSource("loader + options/query");
137
- newRule.use = RuleSet.normalizeUse({
138
- loader: loader,
139
- options: rule.options,
140
- query: rule.query
141
- });
142
- } else if(loader && (rule.options || rule.query)) {
143
- throw new Error("options/query cannot be used with loaders");
144
- } else if(loader) {
145
- checkUseSource("loaders");
146
- newRule.use = RuleSet.normalizeUse(loader);
147
- }
148
-
149
- if(rule.use) {
150
- checkUseSource("use");
151
- newRule.use = RuleSet.normalizeUse(rule.use);
152
- }
153
-
154
- if(rule.rules)
155
- newRule.rules = RuleSet.normalizeRules(rule.rules);
156
-
157
- if(rule.oneOf)
158
- newRule.oneOf = RuleSet.normalizeRules(rule.oneOf);
159
-
160
- var keys = Object.keys(rule).filter(function(key) {
161
- return ["resource", "test", "include", "exclude", "issuer", "loader", "options", "query", "loaders", "use", "rules", "oneOf"].indexOf(key) < 0;
162
- });
163
- keys.forEach(function(key) {
164
- newRule[key] = rule[key];
165
- });
166
-
167
- function checkUseSource(newSource) {
168
- if(useSource && useSource !== newSource)
169
- throw new Error("Rule can only have one result source (provided " + newSource + " and " + useSource + ")");
170
- useSource = newSource;
171
- }
172
-
173
- function checkResourceSource(newSource) {
174
- if(resourceSource && resourceSource !== newSource)
175
- throw new Error("Rule can only have one resource source (provided " + newSource + " and " + resourceSource + ")");
176
- resourceSource = newSource;
177
- }
178
-
179
- return newRule;
180
- };
181
-
182
- RuleSet.normalizeUse = function normalizeUse(use) {
183
- if(Array.isArray(use)) {
184
- return use.map(RuleSet.normalizeUse).reduce(function(arr, items) {
185
- return arr.concat(items);
186
- }, []);
187
- }
188
- return [RuleSet.normalizeUseItem(use)];
189
- };
190
-
191
- RuleSet.normalizeUseItem = function normalizeUseItem(item) {
192
- if(typeof item === "function")
193
- return item;
194
-
195
- if(typeof item === "string") {
196
- var idx = item.indexOf("?");
197
- if(idx >= 0) {
198
- return {
199
- loader: item.substr(0, idx),
200
- options: item.substr(idx + 1)
201
- };
202
- }
203
- return {
204
- loader: item
205
- };
206
- }
207
-
208
- var newItem = {};
209
-
210
- if(item.options && item.query)
211
- throw new Error("Provided options and query in use");
212
-
213
- if(!item.loader)
214
- throw new Error("No loader specified");
215
-
216
- newItem.options = item.options || item.query;
217
-
218
- var keys = Object.keys(item).filter(function(key) {
219
- return ["options", "query"].indexOf(key) < 0;
220
- });
221
-
222
- keys.forEach(function(key) {
223
- newItem[key] = item[key];
224
- });
225
-
226
- return newItem;
227
- }
228
-
229
- RuleSet.normalizeCondition = function normalizeCondition(condition) {
230
- if(!condition)
231
- throw new Error("Expected condition but got falsy value");
232
- if(typeof condition === "string") {
233
- return function(str) {
234
- return str.indexOf(condition) === 0;
235
- };
236
- }
237
- if(typeof condition === "function") {
238
- return condition;
239
- }
240
- if(condition instanceof RegExp) {
241
- return condition.test.bind(condition);
242
- }
243
- if(Array.isArray(condition)) {
244
- var items = condition.map(function(c) {
245
- return RuleSet.normalizeCondition(c);
246
- });
247
- return orMatcher(items);
248
- }
249
- if(typeof condition !== "object")
250
- throw Error("Unexcepted " + typeof condition + " when condition was expected (" + condition + ")");
251
- var matchers = [];
252
- Object.keys(condition).forEach(function(key) {
253
- var value = condition[key];
254
- switch(key) {
255
- case "or":
256
- case "include":
257
- case "test":
258
- if(value)
259
- matchers.push(RuleSet.normalizeCondition(value));
260
- break;
261
- case "and":
262
- if(value) {
263
- var items = value.map(function(c) {
264
- return RuleSet.normalizeCondition(c);
265
- });
266
- matchers.push(andMatcher(items));
267
- }
268
- break;
269
- case "not":
270
- case "exclude":
271
- if(value) {
272
- var matcher = RuleSet.normalizeCondition(value);
273
- matchers.push(notMatcher(matcher));
274
- }
275
- break;
276
- default:
277
- throw new Error("Unexcepted property " + key + " in condition");
278
- }
279
- });
280
- if(matchers.length === 0)
281
- throw new Error("Excepted condition but got " + condition);
282
- if(matchers.length === 1)
283
- return matchers[0];
284
- return andMatcher(matchers);
285
- };
286
-
287
- function notMatcher(matcher) {
288
- return function(str) {
289
- return !matcher(str);
290
- }
291
- }
292
-
293
- function orMatcher(items) {
294
- return function(str) {
295
- for(var i = 0; i < items.length; i++) {
296
- if(items[i](str))
297
- return true;
298
- }
299
- return false;
300
- }
301
- }
302
-
303
- function andMatcher(items) {
304
- return function(str) {
305
- for(var i = 0; i < items.length; i++) {
306
- if(!items[i](str))
307
- return false;
308
- }
309
- return true;
310
- }
311
- }
312
-
313
- RuleSet.prototype.exec = function(data) {
314
- var result = [];
315
- this._run(data, {
316
- rules: this.rules
317
- }, result);
318
- return result;
319
- };
320
-
321
- RuleSet.prototype._run = function _run(data, rule, result) {
322
- // test conditions
323
- if(rule.resource && !data.resource)
324
- return false;
325
- if(rule.issuer && !data.issuer)
326
- return false;
327
- if(rule.resource && !rule.resource(data.resource))
328
- return false;
329
- if(data.issuer && rule.issuer && !rule.issuer(data.issuer))
330
- return false;
331
-
332
- // apply
333
- var keys = Object.keys(rule).filter(function(key) {
334
- return ["resource", "issuer", "rules", "oneOf", "use", "enforce"].indexOf(key) < 0;
335
- });
336
- keys.forEach(function(key) {
337
- result.push({
338
- type: key,
339
- value: rule[key]
340
- });
341
- });
342
-
343
- if(rule.use) {
344
- rule.use.forEach(function(use) {
345
- result.push({
346
- type: "use",
347
- value: typeof use === "function" ? use(data) : use,
348
- enforce: rule.enforce
349
- });
350
- });
351
- }
352
-
353
- var i;
354
-
355
- if(rule.rules) {
356
- for(i = 0; i < rule.rules.length; i++) {
357
- this._run(data, rule.rules[i], result);
358
- }
359
- }
360
-
361
- if(rule.oneOf) {
362
- for(i = 0; i < rule.oneOf.length; i++) {
363
- if(this._run(data, rule.oneOf[i], result))
364
- break;
365
- }
366
- }
367
-
368
- return true;
369
- };
1
+ /*
2
+ MIT License http://www.opensource.org/licenses/mit-license.php
3
+ Author Tobias Koppers @sokra
4
+ */
5
+ /*
6
+ <rules>: <rule>
7
+ <rules>: [<rule>]
8
+ <rule>: {
9
+ resource: {
10
+ test: <condition>,
11
+ include: <condition>,
12
+ exclude: <condition>,
13
+ },
14
+ resource: <condition>, -> resource.test
15
+ test: <condition>, -> resource.test
16
+ include: <condition>, -> resource.include
17
+ exclude: <condition>, -> resource.exclude
18
+ issuer: {
19
+ test: <condition>,
20
+ include: <condition>,
21
+ exclude: <condition>,
22
+ },
23
+ issuer: <condition>, -> issuer.test
24
+ use: "loader", -> use[0].loader
25
+ loader: <>, -> use[0].loader
26
+ loaders: <>, -> use
27
+ options: {}, -> use[0].options,
28
+ query: {}, -> options
29
+ parser: {},
30
+ use: [
31
+ "loader" -> use[x].loader
32
+ ],
33
+ use: [
34
+ {
35
+ loader: "loader",
36
+ options: {}
37
+ }
38
+ ],
39
+ rules: [
40
+ <rule>
41
+ ],
42
+ oneOf: [
43
+ <rule>
44
+ ]
45
+ }
46
+
47
+ <condition>: /regExp/
48
+ <condition>: function(arg) {}
49
+ <condition>: "starting"
50
+ <condition>: [<condition>] // or
51
+ <condition>: { and: [<condition>] }
52
+ <condition>: { or: [<condition>] }
53
+ <condition>: { not: [<condition>] }
54
+ <condition>: { test: <condition>, include: <condition>, exclude: <codition> }
55
+
56
+
57
+ normalized:
58
+
59
+ {
60
+ resource: function(),
61
+ issuer: function(),
62
+ use: [
63
+ {
64
+ loader: string,
65
+ options: string,
66
+ <any>: <any>
67
+ }
68
+ ],
69
+ rules: [<rule>],
70
+ oneOf: [<rule>],
71
+ <any>: <any>,
72
+ }
73
+
74
+ */
75
+
76
+ function RuleSet(rules) {
77
+ this.rules = RuleSet.normalizeRules(rules);
78
+ }
79
+
80
+ module.exports = RuleSet;
81
+
82
+ RuleSet.normalizeRules = function(rules) {
83
+ if(Array.isArray(rules)) {
84
+ return rules.map(function(rule) {
85
+ return RuleSet.normalizeRule(rule);
86
+ });
87
+ } else if(rules) {
88
+ return [RuleSet.normalizeRule(rules)]
89
+ } else {
90
+ return [];
91
+ }
92
+ };
93
+
94
+ RuleSet.normalizeRule = function(rule) {
95
+ if(typeof rule === "string")
96
+ return {
97
+ use: [{
98
+ loader: rule
99
+ }]
100
+ };
101
+ if(!rule)
102
+ throw new Error("Unexcepted null when object was expected as rule");
103
+ if(typeof rule !== "object")
104
+ throw new Error("Unexcepted " + typeof rule + " when object was expected as rule (" + rule + ")");
105
+
106
+ var newRule = {};
107
+ var useSource;
108
+ var resourceSource;
109
+
110
+ if(rule.test || rule.include || rule.exclude) {
111
+ checkResourceSource("test + include + exclude");
112
+ newRule.resource = RuleSet.normalizeCondition({
113
+ test: rule.test,
114
+ include: rule.include,
115
+ exclude: rule.exclude
116
+ });
117
+ }
118
+
119
+ if(rule.resource) {
120
+ checkResourceSource("resource");
121
+ newRule.resource = RuleSet.normalizeCondition(rule.resource);
122
+ }
123
+
124
+ if(rule.issuer) {
125
+ newRule.issuer = RuleSet.normalizeCondition(rule.issuer);
126
+ }
127
+
128
+ if(rule.loader && rule.loaders)
129
+ throw new Error("Provided loader and loaders for rule");
130
+
131
+ var loader = rule.loaders || rule.loader;
132
+ if(typeof loader === "string" && !rule.options && !rule.query) {
133
+ checkUseSource("loader");
134
+ newRule.use = RuleSet.normalizeUse(loader.split("!"));
135
+ } else if(typeof loader === "string" && (rule.options || rule.query)) {
136
+ checkUseSource("loader + options/query");
137
+ newRule.use = RuleSet.normalizeUse({
138
+ loader: loader,
139
+ options: rule.options,
140
+ query: rule.query
141
+ });
142
+ } else if(loader && (rule.options || rule.query)) {
143
+ throw new Error("options/query cannot be used with loaders");
144
+ } else if(loader) {
145
+ checkUseSource("loaders");
146
+ newRule.use = RuleSet.normalizeUse(loader);
147
+ }
148
+
149
+ if(rule.use) {
150
+ checkUseSource("use");
151
+ newRule.use = RuleSet.normalizeUse(rule.use);
152
+ }
153
+
154
+ if(rule.rules)
155
+ newRule.rules = RuleSet.normalizeRules(rule.rules);
156
+
157
+ if(rule.oneOf)
158
+ newRule.oneOf = RuleSet.normalizeRules(rule.oneOf);
159
+
160
+ var keys = Object.keys(rule).filter(function(key) {
161
+ return ["resource", "test", "include", "exclude", "issuer", "loader", "options", "query", "loaders", "use", "rules", "oneOf"].indexOf(key) < 0;
162
+ });
163
+ keys.forEach(function(key) {
164
+ newRule[key] = rule[key];
165
+ });
166
+
167
+ function checkUseSource(newSource) {
168
+ if(useSource && useSource !== newSource)
169
+ throw new Error("Rule can only have one result source (provided " + newSource + " and " + useSource + ")");
170
+ useSource = newSource;
171
+ }
172
+
173
+ function checkResourceSource(newSource) {
174
+ if(resourceSource && resourceSource !== newSource)
175
+ throw new Error("Rule can only have one resource source (provided " + newSource + " and " + resourceSource + ")");
176
+ resourceSource = newSource;
177
+ }
178
+
179
+ return newRule;
180
+ };
181
+
182
+ RuleSet.normalizeUse = function normalizeUse(use) {
183
+ if(Array.isArray(use)) {
184
+ return use.map(RuleSet.normalizeUse).reduce(function(arr, items) {
185
+ return arr.concat(items);
186
+ }, []);
187
+ }
188
+ return [RuleSet.normalizeUseItem(use)];
189
+ };
190
+
191
+ RuleSet.normalizeUseItem = function normalizeUseItem(item) {
192
+ if(typeof item === "function")
193
+ return item;
194
+
195
+ if(typeof item === "string") {
196
+ var idx = item.indexOf("?");
197
+ if(idx >= 0) {
198
+ return {
199
+ loader: item.substr(0, idx),
200
+ options: item.substr(idx + 1)
201
+ };
202
+ }
203
+ return {
204
+ loader: item
205
+ };
206
+ }
207
+
208
+ var newItem = {};
209
+
210
+ if(item.options && item.query)
211
+ throw new Error("Provided options and query in use");
212
+
213
+ if(!item.loader)
214
+ throw new Error("No loader specified");
215
+
216
+ newItem.options = item.options || item.query;
217
+
218
+ var keys = Object.keys(item).filter(function(key) {
219
+ return ["options", "query"].indexOf(key) < 0;
220
+ });
221
+
222
+ keys.forEach(function(key) {
223
+ newItem[key] = item[key];
224
+ });
225
+
226
+ return newItem;
227
+ }
228
+
229
+ RuleSet.normalizeCondition = function normalizeCondition(condition) {
230
+ if(!condition)
231
+ throw new Error("Expected condition but got falsy value");
232
+ if(typeof condition === "string") {
233
+ return function(str) {
234
+ return str.indexOf(condition) === 0;
235
+ };
236
+ }
237
+ if(typeof condition === "function") {
238
+ return condition;
239
+ }
240
+ if(condition instanceof RegExp) {
241
+ return condition.test.bind(condition);
242
+ }
243
+ if(Array.isArray(condition)) {
244
+ var items = condition.map(function(c) {
245
+ return RuleSet.normalizeCondition(c);
246
+ });
247
+ return orMatcher(items);
248
+ }
249
+ if(typeof condition !== "object")
250
+ throw Error("Unexcepted " + typeof condition + " when condition was expected (" + condition + ")");
251
+ var matchers = [];
252
+ Object.keys(condition).forEach(function(key) {
253
+ var value = condition[key];
254
+ switch(key) {
255
+ case "or":
256
+ case "include":
257
+ case "test":
258
+ if(value)
259
+ matchers.push(RuleSet.normalizeCondition(value));
260
+ break;
261
+ case "and":
262
+ if(value) {
263
+ var items = value.map(function(c) {
264
+ return RuleSet.normalizeCondition(c);
265
+ });
266
+ matchers.push(andMatcher(items));
267
+ }
268
+ break;
269
+ case "not":
270
+ case "exclude":
271
+ if(value) {
272
+ var matcher = RuleSet.normalizeCondition(value);
273
+ matchers.push(notMatcher(matcher));
274
+ }
275
+ break;
276
+ default:
277
+ throw new Error("Unexcepted property " + key + " in condition");
278
+ }
279
+ });
280
+ if(matchers.length === 0)
281
+ throw new Error("Excepted condition but got " + condition);
282
+ if(matchers.length === 1)
283
+ return matchers[0];
284
+ return andMatcher(matchers);
285
+ };
286
+
287
+ function notMatcher(matcher) {
288
+ return function(str) {
289
+ return !matcher(str);
290
+ }
291
+ }
292
+
293
+ function orMatcher(items) {
294
+ return function(str) {
295
+ for(var i = 0; i < items.length; i++) {
296
+ if(items[i](str))
297
+ return true;
298
+ }
299
+ return false;
300
+ }
301
+ }
302
+
303
+ function andMatcher(items) {
304
+ return function(str) {
305
+ for(var i = 0; i < items.length; i++) {
306
+ if(!items[i](str))
307
+ return false;
308
+ }
309
+ return true;
310
+ }
311
+ }
312
+
313
+ RuleSet.prototype.exec = function(data) {
314
+ var result = [];
315
+ this._run(data, {
316
+ rules: this.rules
317
+ }, result);
318
+ return result;
319
+ };
320
+
321
+ RuleSet.prototype._run = function _run(data, rule, result) {
322
+ // test conditions
323
+ if(rule.resource && !data.resource)
324
+ return false;
325
+ if(rule.issuer && !data.issuer)
326
+ return false;
327
+ if(rule.resource && !rule.resource(data.resource))
328
+ return false;
329
+ if(data.issuer && rule.issuer && !rule.issuer(data.issuer))
330
+ return false;
331
+
332
+ // apply
333
+ var keys = Object.keys(rule).filter(function(key) {
334
+ return ["resource", "issuer", "rules", "oneOf", "use", "enforce"].indexOf(key) < 0;
335
+ });
336
+ keys.forEach(function(key) {
337
+ result.push({
338
+ type: key,
339
+ value: rule[key]
340
+ });
341
+ });
342
+
343
+ if(rule.use) {
344
+ rule.use.forEach(function(use) {
345
+ result.push({
346
+ type: "use",
347
+ value: typeof use === "function" ? use(data) : use,
348
+ enforce: rule.enforce
349
+ });
350
+ });
351
+ }
352
+
353
+ var i;
354
+
355
+ if(rule.rules) {
356
+ for(i = 0; i < rule.rules.length; i++) {
357
+ this._run(data, rule.rules[i], result);
358
+ }
359
+ }
360
+
361
+ if(rule.oneOf) {
362
+ for(i = 0; i < rule.oneOf.length; i++) {
363
+ if(this._run(data, rule.oneOf[i], result))
364
+ break;
365
+ }
366
+ }
367
+
368
+ return true;
369
+ };