mirror of https://github.com/webpack/webpack.git
Merge pull request #5938 from webpack/bugfix/use-function
allow to pass functions to rules.use and return arrays
This commit is contained in:
commit
f58b66a724
|
|
@ -228,6 +228,9 @@ module.exports = class RuleSet {
|
|||
}
|
||||
|
||||
static normalizeUse(use, ident) {
|
||||
if(typeof use === "function") {
|
||||
return data => RuleSet.normalizeUse(use(data), ident);
|
||||
}
|
||||
if(Array.isArray(use)) {
|
||||
return use
|
||||
.map((item, idx) => RuleSet.normalizeUse(item, `${ident}-${idx}`))
|
||||
|
|
@ -236,14 +239,6 @@ module.exports = class RuleSet {
|
|||
return [RuleSet.normalizeUseItem(use, ident)];
|
||||
}
|
||||
|
||||
static normalizeUseItemFunction(use, data) {
|
||||
const result = use(data);
|
||||
if(typeof result === "string") {
|
||||
return RuleSet.normalizeUseItem(result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static normalizeUseItemString(useItemString) {
|
||||
const idx = useItemString.indexOf("?");
|
||||
if(idx >= 0) {
|
||||
|
|
@ -258,9 +253,6 @@ module.exports = class RuleSet {
|
|||
}
|
||||
|
||||
static normalizeUseItem(item, ident) {
|
||||
if(typeof item === "function")
|
||||
return item;
|
||||
|
||||
if(typeof item === "string") {
|
||||
return RuleSet.normalizeUseItemString(item);
|
||||
}
|
||||
|
|
@ -385,13 +377,20 @@ module.exports = class RuleSet {
|
|||
});
|
||||
|
||||
if(rule.use) {
|
||||
rule.use.forEach((use) => {
|
||||
result.push({
|
||||
type: "use",
|
||||
value: typeof use === "function" ? RuleSet.normalizeUseItemFunction(use, data) : use,
|
||||
enforce: rule.enforce
|
||||
});
|
||||
});
|
||||
const process = use => {
|
||||
if(typeof use === "function") {
|
||||
process(use(data));
|
||||
} else if(Array.isArray(use)) {
|
||||
use.forEach(process);
|
||||
} else {
|
||||
result.push({
|
||||
type: "use",
|
||||
value: use,
|
||||
enforce: rule.enforce
|
||||
});
|
||||
}
|
||||
};
|
||||
process(rule.use);
|
||||
}
|
||||
|
||||
if(rule.rules) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
module.exports = ["a"];
|
||||
|
|
@ -0,0 +1 @@
|
|||
module.exports = ["ab"];
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
module.exports = [require("./a")];
|
||||
|
||||
|
|
@ -0,0 +1 @@
|
|||
// never used
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
it("should match only one rule in a oneOf block", function() {
|
||||
var ab = require("./ab");
|
||||
ab.should.be.eql([
|
||||
"ab",
|
||||
"?first"
|
||||
]);
|
||||
});
|
||||
it("should match with issuer and any option value", function() {
|
||||
var a = require("./a");
|
||||
var b = require("./b");
|
||||
a.should.be.eql([
|
||||
"a",
|
||||
"?third",
|
||||
]);
|
||||
b.should.be.eql([[
|
||||
"a",
|
||||
"second-3",
|
||||
"?second-2",
|
||||
"?second-1",
|
||||
]]);
|
||||
});
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
module.exports = function(source) {
|
||||
var query = this.query;
|
||||
if(typeof query === "object" && typeof query.get === "function") {
|
||||
query = query.get();
|
||||
}
|
||||
return source + "\nmodule.exports.push(" + JSON.stringify(query) + ");";
|
||||
};
|
||||
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
module.exports = {
|
||||
module: {
|
||||
rules: [
|
||||
{ oneOf: [
|
||||
{
|
||||
test: {
|
||||
and: [
|
||||
/a.\.js$/,
|
||||
/b\.js$/
|
||||
]
|
||||
},
|
||||
loader: "./loader?first"
|
||||
},
|
||||
{
|
||||
test: [
|
||||
require.resolve("./a"),
|
||||
require.resolve("./c"),
|
||||
],
|
||||
issuer: require.resolve("./b"),
|
||||
use: data => ([
|
||||
"./loader?second-1",
|
||||
{
|
||||
loader: "./loader",
|
||||
options: "second-2"
|
||||
},
|
||||
{
|
||||
loader: "./loader",
|
||||
options: {
|
||||
get: function() {
|
||||
return "second-3";
|
||||
}
|
||||
}
|
||||
}
|
||||
])
|
||||
},
|
||||
{
|
||||
test: {
|
||||
or: [
|
||||
require.resolve("./a"),
|
||||
require.resolve("./c"),
|
||||
]
|
||||
},
|
||||
loader: "./loader",
|
||||
options: "third"
|
||||
}
|
||||
] }
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue