Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] configuration validation #240

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions bin/convert-argv.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ var fs = require("fs");
fs.existsSync = fs.existsSync || path.existsSync;
var interpret = require("interpret");
var prepareOptions = require("./prepareOptions");
var webpackConfigurationSchema = require("../schemas/webpackConfigurationSchema.json");
var validateSchema = require("webpack").validateSchema;
var WebpackOptionsValidationError = require("webpack").WebpackOptionsValidationError;

module.exports = function(...args) {
var argv = args[1] || args[0];
var options = [];
Expand Down Expand Up @@ -148,10 +152,15 @@ module.exports = function(...args) {
}

function processConfiguredOptions(options) {
if (options === null || typeof options !== "object") {
console.error(
"Config did not export an object or a function returning an object."
var webpackConfigurationValidationErrors = validateSchema(
webpackConfigurationSchema,
options
);
if (webpackConfigurationValidationErrors.length) {
var error = new WebpackOptionsValidationError(
webpackConfigurationValidationErrors
);
console.error(error.message);
process.exit(-1); // eslint-disable-line
}

Expand Down
20 changes: 20 additions & 0 deletions schemas/webpackConfigurationSchema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"oneOf": [
{
"type": "object",
"description": "A webpack configuration object."
},
{
"type": "array",
"description": "An array of webpack configuration objects.",
"items": {
"description": "A webpack configuration object.",
"type": "object"
}
},
{
"instanceof": "Function",
"description": "A promise that resolves with a configuration object, or an array of configuration objects."
}
]
}
1 change: 1 addition & 0 deletions test/binCases/config-type/array/entry-a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "entry-a.js";
1 change: 1 addition & 0 deletions test/binCases/config-type/array/entry-b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "entry-b.js";
13 changes: 13 additions & 0 deletions test/binCases/config-type/array/stdin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use strict";

module.exports = function testAssertions(code, stdout, stderr) {
expect(code).toBe(0);
expect(stdout).toEqual(expect.anything());
expect(stdout[0]).toContain("Hash: ");
expect(stdout[1]).toContain("Version: ");
expect(stdout[2]).toContain("Child");
expect(stdout[6]).toContain("entry-a.bundle.js");
expect(stdout[8]).toContain("Child");
expect(stdout[12]).toContain("entry-b.bundle.js");
expect(stderr).toHaveLength(0);
};
3 changes: 3 additions & 0 deletions test/binCases/config-type/array/test.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--config ./webpack.config.js
--target async-node
--mode production
15 changes: 15 additions & 0 deletions test/binCases/config-type/array/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = [
{
entry: "./entry-a",
output: {
filename: "entry-a.bundle.js"
}
},
{
entry: "./entry-b",
output: {
filename: "entry-b.bundle.js"
}
}
];

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/binCases/config-type/function-promise/entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "entry.js";
11 changes: 11 additions & 0 deletions test/binCases/config-type/function-promise/stdin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"use strict";

module.exports = function testAssertions(code, stdout, stderr) {
expect(code).toBe(0);
expect(stdout).toEqual(expect.anything());
expect(stdout[0]).toContain("Hash: ");
expect(stdout[1]).toContain("Version: ");
expect(stdout[2]).toContain("Time: ");
expect(stdout[4]).toContain("entry.bundle.js");
expect(stderr).toHaveLength(0);
};
3 changes: 3 additions & 0 deletions test/binCases/config-type/function-promise/test.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--config ./webpack.config.js
--target async-node
--mode production
10 changes: 10 additions & 0 deletions test/binCases/config-type/function-promise/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = (/*env*/) => {
return new Promise((resolve, reject) => {
resolve({
entry: "./entry",
output: {
filename: "entry.bundle.js"
}
})
});
}
1 change: 1 addition & 0 deletions test/binCases/config-type/function/entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "entry.js";
11 changes: 11 additions & 0 deletions test/binCases/config-type/function/stdin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"use strict";

module.exports = function testAssertions(code, stdout, stderr) {
expect(code).toBe(0);
expect(stdout).toEqual(expect.anything());
expect(stdout[0]).toContain("Hash: ");
expect(stdout[1]).toContain("Version: ");
expect(stdout[2]).toContain("Time: ");
expect(stdout[4]).toContain("entry.bundle.js");
expect(stderr).toHaveLength(0);
};
3 changes: 3 additions & 0 deletions test/binCases/config-type/function/test.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--config ./webpack.config.js
--target async-node
--mode production
8 changes: 8 additions & 0 deletions test/binCases/config-type/function/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = function(env) {
return {
entry: "./entry",
output: {
filename: "entry.bundle.js"
}
};
};
1 change: 1 addition & 0 deletions test/binCases/config-type/invalid-array/entry-a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "entry-a.js";
1 change: 1 addition & 0 deletions test/binCases/config-type/invalid-array/entry-b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "entry-b.js";
7 changes: 7 additions & 0 deletions test/binCases/config-type/invalid-array/stdin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";

module.exports = function testAssertions(code, stdout, stderr) {
expect(code).not.toBe(0);
expect(stderr).toEqual(expect.anything());
expect(stderr[0]).toContain("Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.");
};
3 changes: 3 additions & 0 deletions test/binCases/config-type/invalid-array/test.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--config ./webpack.config.js
--target async-node
--mode production
16 changes: 16 additions & 0 deletions test/binCases/config-type/invalid-array/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = [
{
entry: "./entry-a",
output: {
filename: "entry-a.bundle.js"
}
},
"this string makes this array of configurations invalid.",
{
entry: "./entry-b",
output: {
filename: "entry-b.bundle.js"
}
}
];

1 change: 1 addition & 0 deletions test/binCases/config-type/invalid-type/entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "entry.js";
7 changes: 7 additions & 0 deletions test/binCases/config-type/invalid-type/stdin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";

module.exports = function testAssertions(code, stdout, stderr) {
expect(code).not.toBe(0);
expect(stderr).toEqual(expect.anything());
expect(stderr[0]).toContain("Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.");
};
3 changes: 3 additions & 0 deletions test/binCases/config-type/invalid-type/test.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--config ./webpack.config.js
--target async-node
--mode production
2 changes: 2 additions & 0 deletions test/binCases/config-type/invalid-type/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module.exports = 100;

1 change: 1 addition & 0 deletions test/binCases/config-type/object/entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "entry.js";
11 changes: 11 additions & 0 deletions test/binCases/config-type/object/stdin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"use strict";

module.exports = function testAssertions(code, stdout, stderr) {
expect(code).toBe(0);
expect(stdout).toEqual(expect.anything());
expect(stdout[0]).toContain("Hash: ");
expect(stdout[1]).toContain("Version: ");
expect(stdout[2]).toContain("Time: ");
expect(stdout[4]).toContain("entry.bundle.js");
expect(stderr).toHaveLength(0);
};
3 changes: 3 additions & 0 deletions test/binCases/config-type/object/test.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--config ./webpack.config.js
--target async-node
--mode production
8 changes: 8 additions & 0 deletions test/binCases/config-type/object/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = function(env) {
return {
entry: "./entry",
output: {
filename: "entry.bundle.js"
}
};
};
1 change: 1 addition & 0 deletions test/binCases/config-type/promise/dist/entry.bundle.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/binCases/config-type/promise/entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "entry.js";
11 changes: 11 additions & 0 deletions test/binCases/config-type/promise/stdin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"use strict";

module.exports = function testAssertions(code, stdout, stderr) {
expect(code).toBe(0);
expect(stdout).toEqual(expect.anything());
expect(stdout[0]).toContain("Hash: ");
expect(stdout[1]).toContain("Version: ");
expect(stdout[2]).toContain("Time: ");
expect(stdout[4]).toContain("entry.bundle.js");
expect(stderr).toHaveLength(0);
};
3 changes: 3 additions & 0 deletions test/binCases/config-type/promise/test.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--config ./webpack.config.js
--target async-node
--mode production
8 changes: 8 additions & 0 deletions test/binCases/config-type/promise/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = new Promise((resolve, reject) => {
resolve({
entry: "./entry",
output: {
filename: "entry.bundle.js"
}
});
});