mirror of https://github.com/webpack/webpack.git
added resourceQuery condition
added function use to schema
This commit is contained in:
parent
e3865e03c7
commit
efa3fc058b
|
|
@ -127,12 +127,16 @@ function NormalModuleFactory(context, resolvers, options) {
|
|||
var userRequest = loaders.map(loaderToIdent).concat([resource]).join("!");
|
||||
|
||||
var resourcePath = resource;
|
||||
var resourceQuery = "";
|
||||
var queryIndex = resourcePath.indexOf("?");
|
||||
if(queryIndex >= 0)
|
||||
if(queryIndex >= 0) {
|
||||
resourceQuery = resourcePath.substr(queryIndex);
|
||||
resourcePath = resourcePath.substr(0, queryIndex);
|
||||
}
|
||||
|
||||
var result = _this.ruleSet.exec({
|
||||
resource: resourcePath,
|
||||
resourceQuery: resourceQuery,
|
||||
issuer: contextInfo.issuer
|
||||
});
|
||||
var settings = {};
|
||||
|
|
|
|||
|
|
@ -15,12 +15,8 @@
|
|||
test: <condition>, -> resource.test
|
||||
include: <condition>, -> resource.include
|
||||
exclude: <condition>, -> resource.exclude
|
||||
issuer: {
|
||||
test: <condition>,
|
||||
include: <condition>,
|
||||
exclude: <condition>,
|
||||
},
|
||||
issuer: <condition>, -> issuer.test
|
||||
resourceQuery: <condition>,
|
||||
issuer: <condition>,
|
||||
use: "loader", -> use[0].loader
|
||||
loader: <>, -> use[0].loader
|
||||
loaders: <>, -> use
|
||||
|
|
@ -58,6 +54,7 @@ normalized:
|
|||
|
||||
{
|
||||
resource: function(),
|
||||
resourceQuery: function(),
|
||||
issuer: function(),
|
||||
use: [
|
||||
{
|
||||
|
|
@ -131,6 +128,14 @@ RuleSet.normalizeRule = function(rule) {
|
|||
}
|
||||
}
|
||||
|
||||
if(rule.resourceQuery) {
|
||||
try {
|
||||
newRule.resourceQuery = RuleSet.normalizeCondition(rule.resourceQuery);
|
||||
} catch(error) {
|
||||
throw new Error(RuleSet.buildErrorMessage(rule.resourceQuery, error));
|
||||
}
|
||||
}
|
||||
|
||||
if(rule.issuer) {
|
||||
try {
|
||||
newRule.issuer = RuleSet.normalizeCondition(rule.issuer);
|
||||
|
|
@ -172,7 +177,7 @@ RuleSet.normalizeRule = function(rule) {
|
|||
newRule.oneOf = RuleSet.normalizeRules(rule.oneOf);
|
||||
|
||||
var keys = Object.keys(rule).filter(function(key) {
|
||||
return ["resource", "test", "include", "exclude", "issuer", "loader", "options", "query", "loaders", "use", "rules", "oneOf"].indexOf(key) < 0;
|
||||
return ["resource", "resourceQuery", "test", "include", "exclude", "issuer", "loader", "options", "query", "loaders", "use", "rules", "oneOf"].indexOf(key) < 0;
|
||||
});
|
||||
keys.forEach(function(key) {
|
||||
newRule[key] = rule[key];
|
||||
|
|
@ -344,16 +349,20 @@ RuleSet.prototype._run = function _run(data, rule, result) {
|
|||
// test conditions
|
||||
if(rule.resource && !data.resource)
|
||||
return false;
|
||||
if(rule.resourceQuery && !data.resourceQuery)
|
||||
return false;
|
||||
if(rule.issuer && !data.issuer)
|
||||
return false;
|
||||
if(rule.resource && !rule.resource(data.resource))
|
||||
return false;
|
||||
if(data.issuer && rule.issuer && !rule.issuer(data.issuer))
|
||||
return false;
|
||||
if(data.resourceQuery && rule.resourceQuery && !rule.resourceQuery(data.resourceQuery))
|
||||
return false;
|
||||
|
||||
// apply
|
||||
var keys = Object.keys(rule).filter(function(key) {
|
||||
return ["resource", "issuer", "rules", "oneOf", "use", "enforce"].indexOf(key) < 0;
|
||||
return ["resource", "resourceQuery", "issuer", "rules", "oneOf", "use", "enforce"].indexOf(key) < 0;
|
||||
});
|
||||
keys.forEach(function(key) {
|
||||
result.push({
|
||||
|
|
|
|||
|
|
@ -567,6 +567,9 @@
|
|||
"resource": {
|
||||
"$ref": "#/definitions/ruleSet-condition"
|
||||
},
|
||||
"resourceQuery": {
|
||||
"$ref": "#/definitions/ruleSet-condition"
|
||||
},
|
||||
"rules": {
|
||||
"$ref": "#/definitions/ruleSet-rules"
|
||||
},
|
||||
|
|
@ -590,6 +593,9 @@
|
|||
{
|
||||
"$ref": "#/definitions/ruleSet-use-item"
|
||||
},
|
||||
{
|
||||
"instanceof": "Function"
|
||||
},
|
||||
{
|
||||
"items": {
|
||||
"$ref": "#/definitions/ruleSet-use-item"
|
||||
|
|
|
|||
|
|
@ -93,7 +93,7 @@ describe("Validation", function() {
|
|||
},
|
||||
message: [
|
||||
" - configuration.module.rules[0].oneOf[0] has an unknown property 'paser'. These properties are valid:",
|
||||
" object { enforce?, exclude?, include?, issuer?, loader?, loaders?, oneOf?, options?, parser?, query?, resource?, rules?, test?, use? }"
|
||||
" object { enforce?, exclude?, include?, issuer?, loader?, loaders?, oneOf?, options?, parser?, query?, resource?, resourceQuery?, rules?, test?, use? }"
|
||||
]
|
||||
}, {
|
||||
name: "additional key on root",
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
module.exports = ["a"];
|
||||
|
|
@ -0,0 +1 @@
|
|||
module.exports = ["b"];
|
||||
|
|
@ -0,0 +1 @@
|
|||
module.exports = require("./a" + __resourceQuery);
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
it("should match a custom loader", function() {
|
||||
var a = require("./a");
|
||||
a.should.be.eql([
|
||||
"a",
|
||||
{
|
||||
issuer: "index.js",
|
||||
resource: "a.js",
|
||||
resourceQuery: ""
|
||||
}
|
||||
]);
|
||||
var b = require("./b?hello");
|
||||
b.should.be.eql([
|
||||
"b",
|
||||
{
|
||||
issuer: "index.js",
|
||||
resource: "b.js",
|
||||
resourceQuery: "?hello"
|
||||
}
|
||||
]);
|
||||
var ca = require("./call-a?hello");
|
||||
ca.should.be.eql([
|
||||
"a",
|
||||
{
|
||||
issuer: "call-a.js",
|
||||
resource: "a.js",
|
||||
resourceQuery: "?hello"
|
||||
},
|
||||
{
|
||||
issuer: "index.js",
|
||||
resource: "call-a.js",
|
||||
resourceQuery: "?hello"
|
||||
}
|
||||
]);
|
||||
});
|
||||
|
|
@ -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,19 @@
|
|||
module.exports = {
|
||||
module: {
|
||||
rules: [
|
||||
{
|
||||
test: /[ab]\.js$/,
|
||||
use: function(data) {
|
||||
return {
|
||||
loader: "./loader",
|
||||
options: {
|
||||
resource: data.resource.replace(/^.*[\\\/]/g, ""),
|
||||
resourceQuery: data.resourceQuery,
|
||||
issuer: data.issuer.replace(/^.*[\\\/]/g, ""),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
module.exports = ["a"];
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
it("should match rule with resource query", function() {
|
||||
var a1 = require("./a");
|
||||
a1.should.be.eql([
|
||||
"a"
|
||||
]);
|
||||
var a2 = require("./a?loader");
|
||||
a2.should.be.eql([
|
||||
"a",
|
||||
"?query"
|
||||
]);
|
||||
});
|
||||
|
|
@ -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,10 @@
|
|||
module.exports = {
|
||||
module: {
|
||||
rules: [
|
||||
{
|
||||
resourceQuery: /^\?loader/,
|
||||
use: "./loader?query"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue