Add options.plugins schema validation, ensure apply method is present. Add tests

This commit is contained in:
EugeneHlushko 2017-11-24 18:54:48 +02:00
parent 5537d0ec41
commit 70c50ea8f1
3 changed files with 120 additions and 10 deletions

View File

@ -30,14 +30,6 @@ function webpack(options, callback) {
compiler.options = options; compiler.options = options;
new NodeEnvironmentPlugin().apply(compiler); new NodeEnvironmentPlugin().apply(compiler);
if(options.plugins && Array.isArray(options.plugins)) { if(options.plugins && Array.isArray(options.plugins)) {
// sanity check the options plugins
options.plugins.forEach((plugin, index) => {
const typeofPlugin = typeof plugin;
if((typeofPlugin !== "object" && typeofPlugin !== "function") || Array.isArray(plugin)) {
let failureValue = Array.isArray(plugin) ? "array" : typeofPlugin;
throw new Error("Invalid plugin provided at index " + index + ": " + failureValue + " given");
}
});
compiler.apply.apply(compiler, options.plugins); compiler.apply.apply(compiler, options.plugins);
} }
compiler.applyPlugins("environment"); compiler.applyPlugins("environment");

View File

@ -1,6 +1,34 @@
{ {
"additionalProperties": false, "additionalProperties": false,
"definitions": { "definitions": {
"common.pluginFunction": {
"description": "Compiler or Resolver Plugin instance of Function",
"instanceof": "Function",
"properties": {
"apply": {
"description": "The run point of the plugin, required method.",
"instanceof": "Function"
}
},
"additionalProperties": true,
"required": [
"apply"
]
},
"common.pluginObject": {
"description": "Compiler or Resolver Plugin instance of Function",
"type": "object",
"properties": {
"apply": {
"description": "The run point of the plugin, required method.",
"instanceof": "Function"
}
},
"additionalProperties": true,
"required": [
"apply"
]
},
"common.arrayOfStringOrStringArrayValues": { "common.arrayOfStringOrStringArrayValues": {
"items": { "items": {
"description": "string or array of strings", "description": "string or array of strings",
@ -600,7 +628,18 @@
}, },
"plugins": { "plugins": {
"description": "Plugins for the resolver", "description": "Plugins for the resolver",
"type": "array" "type": "array",
"items": {
"description": "Plugin of type object or instanceof Function",
"anyOf": [
{
"$ref": "#/definitions/common.pluginObject"
},
{
"$ref": "#/definitions/common.pluginFunction"
}
]
}
}, },
"resolver": { "resolver": {
"description": "Custom resolver" "description": "Custom resolver"
@ -1178,7 +1217,18 @@
}, },
"plugins": { "plugins": {
"description": "Add additional plugins to the compiler.", "description": "Add additional plugins to the compiler.",
"type": "array" "type": "array",
"items": {
"description": "Plugin of type object or instanceof Function",
"anyOf": [
{
"$ref": "#/definitions/common.pluginObject"
},
{
"$ref": "#/definitions/common.pluginFunction"
}
]
}
}, },
"profile": { "profile": {
"description": "Capture timing information for each module.", "description": "Capture timing information for each module.",

View File

@ -255,6 +255,74 @@ describe("Validation", () => {
" - configuration.stats should be one of these:" " - configuration.stats should be one of these:"
); );
} }
}, {
name: "Invalid plugin provided: bool",
config: {
entry: "foo.js",
plugins: [
false
]
},
test(err) {
err.message.should.startWith("Invalid configuration object.");
err.message.split("\n").slice(1)[0].should.be.eql(
" - configuration.plugins[0] should be one of these:"
);
}
}, {
name: "Invalid plugin provided: array",
config: {
entry: "foo.js",
plugins: [
[]
]
},
test(err) {
err.message.should.startWith("Invalid configuration object.");
err.message.split("\n").slice(1)[0].should.be.eql(
" - configuration.plugins[0] should be one of these:"
);
}
}, {
name: "Invalid plugin provided: string",
config: {
entry: "foo.js",
plugins: ["abc123"]
},
test(err) {
err.message.should.startWith("Invalid configuration object.");
err.message.split("\n").slice(1)[0].should.be.eql(
" - configuration.plugins[0] should be one of these:"
);
}
}, {
name: "Invalid plugin provided: int",
config: {
entry: "foo.js",
plugins: [
12
]
},
test(err) {
err.message.should.startWith("Invalid configuration object.");
err.message.split("\n").slice(1)[0].should.be.eql(
" - configuration.plugins[0] should be one of these:"
);
}
}, {
name: "Invalid plugin provided: object without apply function",
config: {
entry: "foo.js",
plugins: [
new function() {}
]
},
test(err) {
err.message.should.startWith("Invalid configuration object.");
err.message.split("\n").slice(1)[0].should.be.eql(
" - configuration.plugins[0] should be one of these:"
);
}
}]; }];
testCases.forEach((testCase) => { testCases.forEach((testCase) => {