Skip to content

Commit 44baa7c

Browse files
authored
Merge pull request webpack#3557 from webpack/bugfix/normalize_mixed_use_array_and_function
fix(RuleSet): normalize mixed use array and function
2 parents ccfc608 + 04bf19a commit 44baa7c

10 files changed

Lines changed: 167 additions & 11 deletions

File tree

‎lib/RuleSet.js‎

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -224,21 +224,33 @@ RuleSet.normalizeUse = function normalizeUse(use) {
224224
return [RuleSet.normalizeUseItem(use)];
225225
};
226226

227+
RuleSet.normalizeUseItemFunction = function normalizeUseItemFunction(use, data) {
228+
var result = use(data);
229+
if(typeof result === "string") {
230+
return RuleSet.normalizeUseItem(result);
231+
}
232+
return result;
233+
}
234+
235+
RuleSet.normalizeUseItemString = function normalizeUseItemString(useItemString) {
236+
var idx = useItemString.indexOf("?");
237+
if(idx >= 0) {
238+
return {
239+
loader: useItemString.substr(0, idx),
240+
options: useItemString.substr(idx + 1)
241+
};
242+
}
243+
return {
244+
loader: useItemString
245+
};
246+
}
247+
227248
RuleSet.normalizeUseItem = function normalizeUseItem(item) {
228249
if(typeof item === "function")
229250
return item;
230251

231252
if(typeof item === "string") {
232-
var idx = item.indexOf("?");
233-
if(idx >= 0) {
234-
return {
235-
loader: item.substr(0, idx),
236-
options: item.substr(idx + 1)
237-
};
238-
}
239-
return {
240-
loader: item
241-
};
253+
return RuleSet.normalizeUseItemString(item);
242254
}
243255

244256
var newItem = {};
@@ -384,7 +396,7 @@ RuleSet.prototype._run = function _run(data, rule, result) {
384396
rule.use.forEach(function(use) {
385397
result.push({
386398
type: "use",
387-
value: typeof use === "function" ? use(data) : use,
399+
value: typeof use === "function" ? RuleSet.normalizeUseItemFunction(use, data) : use,
388400
enforce: rule.enforce
389401
});
390402
});

‎schemas/webpackOptionsSchema.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,9 @@
628628
{
629629
"$ref": "#/definitions/ruleSet-loader"
630630
},
631+
{
632+
"instanceof": "Function"
633+
},
631634
{
632635
"additionalProperties": false,
633636
"properties": {

‎test/RuleSet.test.js‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,54 @@ describe("RuleSet", function() {
259259
}]);
260260
(match(loader, 'style.css')).should.eql(['style-loader', 'css-loader']);
261261
});
262+
it('should work when using an array of functions returning a loader', function() {
263+
var loader = new RuleSet([{
264+
test: /\.css$/,
265+
use: [
266+
function(data) {
267+
return {
268+
loader: 'style-loader'
269+
}
270+
},
271+
function(data) {
272+
return {
273+
loader: 'css-loader'
274+
}
275+
},
276+
]
277+
}]);
278+
(match(loader, 'style.css')).should.eql(['style-loader', 'css-loader']);
279+
});
280+
it('should work when using an array of either functions or strings returning a loader', function() {
281+
var loader = new RuleSet([{
282+
test: /\.css$/,
283+
use: [
284+
"style-loader",
285+
function(data) {
286+
return {
287+
loader: 'css-loader'
288+
}
289+
},
290+
]
291+
}]);
292+
(match(loader, 'style.css')).should.eql(['style-loader', 'css-loader']);
293+
});
294+
it('should work when using an array of functions returning either a loader obejct or loader name string', function() {
295+
var loader = new RuleSet([{
296+
test: /\.css$/,
297+
use: [
298+
function(data) {
299+
return 'style-loader'
300+
},
301+
function(data) {
302+
return {
303+
loader: 'css-loader'
304+
}
305+
},
306+
]
307+
}]);
308+
(match(loader, 'style.css')).should.eql(['style-loader', 'css-loader']);
309+
});
262310
it('should throw if using array loaders with invalid type', function() {
263311
should.throws(function() {
264312
var loader = new RuleSet([{
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = ["a"];
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = ["ab"];
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module.exports = [require("./a")];
2+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// never used
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
it("should match only one rule in a oneOf block", function() {
2+
var ab = require("./ab");
3+
ab.should.be.eql([
4+
"ab",
5+
"?first"
6+
]);
7+
});
8+
it("should match with issuer and any option value", function() {
9+
var a = require("./a");
10+
var b = require("./b");
11+
a.should.be.eql([
12+
"a",
13+
"?third",
14+
]);
15+
b.should.be.eql([[
16+
"a",
17+
"second-3",
18+
"?second-2",
19+
"?second-1",
20+
]]);
21+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = function(source) {
2+
var query = this.query;
3+
if(typeof query === "object" && typeof query.get === "function") {
4+
query = query.get();
5+
}
6+
return source + "\nmodule.exports.push(" + JSON.stringify(query) + ");";
7+
};
8+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
function createFunctionArrayFromUseArray(useArray) {
3+
return useArray.map(function(useItem) {
4+
return function(data) {
5+
return useItem;
6+
};
7+
});
8+
}
9+
10+
var useArray = createFunctionArrayFromUseArray([
11+
"./loader?second-1",
12+
{
13+
loader: "./loader",
14+
options: "second-2"
15+
},
16+
{
17+
loader: "./loader",
18+
options: {
19+
get: function() { return "second-3"; }
20+
}
21+
}
22+
]);
23+
24+
module.exports = {
25+
module: {
26+
rules: [
27+
{ oneOf: [
28+
{
29+
test: {
30+
and: [
31+
/a.\.js$/,
32+
/b\.js$/
33+
]
34+
},
35+
loader: "./loader?first"
36+
},
37+
{
38+
test: [
39+
require.resolve("./a"),
40+
require.resolve("./c"),
41+
],
42+
issuer: require.resolve("./b"),
43+
use: useArray
44+
},
45+
{
46+
test: {
47+
or: [
48+
require.resolve("./a"),
49+
require.resolve("./c"),
50+
]
51+
},
52+
loader: "./loader",
53+
options: "third"
54+
}
55+
]}
56+
]
57+
}
58+
}
59+

0 commit comments

Comments
 (0)