mirror of https://github.com/webpack/webpack.git
Add options.plugins schema validation, ensure apply method is present. Add tests
This commit is contained in:
parent
5537d0ec41
commit
70c50ea8f1
|
@ -30,14 +30,6 @@ function webpack(options, callback) {
|
|||
compiler.options = options;
|
||||
new NodeEnvironmentPlugin().apply(compiler);
|
||||
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.applyPlugins("environment");
|
||||
|
|
|
@ -1,6 +1,34 @@
|
|||
{
|
||||
"additionalProperties": false,
|
||||
"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": {
|
||||
"items": {
|
||||
"description": "string or array of strings",
|
||||
|
@ -600,7 +628,18 @@
|
|||
},
|
||||
"plugins": {
|
||||
"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": {
|
||||
"description": "Custom resolver"
|
||||
|
@ -1178,7 +1217,18 @@
|
|||
},
|
||||
"plugins": {
|
||||
"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": {
|
||||
"description": "Capture timing information for each module.",
|
||||
|
|
|
@ -255,6 +255,74 @@ describe("Validation", () => {
|
|||
" - 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) => {
|
||||
|
|
Loading…
Reference in New Issue