Skip to content

Commit 5727dad

Browse files
committed
feat: throw an error if configuration is invalid
1 parent a29e393 commit 5727dad

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"url": "http://gajus.com"
66
},
77
"dependencies": {
8+
"ajv": "^4.11.4",
89
"babel-plugin-syntax-jsx": "^6.18.0",
910
"babel-types": "^6.19.0",
1011
"generic-names": "^1.0.2",

src/index.js

+13
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@ import {
66
} from 'path';
77
import babelPluginJsxSyntax from 'babel-plugin-syntax-jsx';
88
import BabelTypes from 'babel-types';
9+
import Ajv from 'ajv';
10+
import optionsSchema from './schemas/optionsSchema.json';
911
import createObjectExpression from './createObjectExpression';
1012
import requireCssModule from './requireCssModule';
1113
import resolveStringLiteral from './resolveStringLiteral';
1214
import replaceJsxExpressionContainer from './replaceJsxExpressionContainer';
1315

16+
const ajv = new Ajv();
17+
18+
const validate = ajv.compile(optionsSchema);
19+
1420
export default ({
1521
types: t
1622
}: {
@@ -170,6 +176,13 @@ export default ({
170176
}
171177
},
172178
Program (path: Object, stats: Object): void {
179+
if (!validate(stats.opts)) {
180+
// eslint-disable-next-line no-console
181+
console.error(validate.errors);
182+
183+
throw new Error('Invalid configuration');
184+
}
185+
173186
const filename = stats.file.opts.filename;
174187

175188
filenameMap[filename] = {

src/schemas/optionsSchema.json

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"additionalProperties": false,
3+
"properties": {
4+
"context": {
5+
"type": "string"
6+
},
7+
"filetypes": {
8+
"patternGroups": {
9+
".*": {
10+
"type": "string"
11+
}
12+
},
13+
"type": "object"
14+
},
15+
"generateScopedName": {
16+
"type": "string"
17+
},
18+
"webpackHotModuleReloading": {
19+
"type": "boolean"
20+
}
21+
},
22+
"type": "object"
23+
}

0 commit comments

Comments
 (0)