Skip to content

Commit 9075cfc

Browse files
feat: validate options
1 parent ede061d commit 9075cfc

File tree

4 files changed

+463
-108
lines changed

4 files changed

+463
-108
lines changed

lib/loader.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
Author Tobias Koppers @sokra
44
*/
55

6+
const schema = require("./options.json");
7+
const validate = require("@webpack-contrib/schema-utils");
68
const loaderUtils = require("loader-utils");
79
const postcss = require("postcss");
810
const plugin = require("./plugin");
@@ -11,13 +13,14 @@ const SyntaxError = require("./SyntaxError");
1113
module.exports = function(content, map, meta) {
1214
const options = loaderUtils.getOptions(this) || {};
1315

14-
// Todo validate options
16+
validate({ name: "CSS Loader", schema, target: options });
1517

1618
const cb = this.async();
17-
const sourceMap = options.sourceMap || false;
18-
const url = typeof options.url === "undefined" ? true : options.url;
19-
const importOpt = typeof options.import === "undefined" ? true : options.import;
20-
const importLoaders = options.importLoaders || 0;
19+
const { url, import: importOpt, sourceMap, importLoaders } = Object.assign(
20+
{},
21+
{ url: true, import: true, sourceMap: false, importLoaders: 0 },
22+
loaderUtils.getOptions(this) || {}
23+
);
2124

2225
if (sourceMap && map) {
2326
if (typeof map === "string") {
@@ -115,11 +118,8 @@ module.exports = function(content, map, meta) {
115118

116119
if (result.messages && result.messages.length > 0) {
117120
result.messages
118-
.filter(
119-
message =>
120-
message.type === "modify-code" ? message : false
121-
)
122-
.forEach((message) => {
121+
.filter(message => (message.type === "modify-code" ? message : false))
122+
.forEach(message => {
123123
try {
124124
newContentObj = message.modifyCode(this, newContentObj);
125125
} catch (err) {

lib/options.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"type": "object",
3+
"properties": {
4+
"url": {
5+
"type": "boolean"
6+
},
7+
"import": {
8+
"type": "boolean"
9+
},
10+
"sourceMap": {
11+
"type": "boolean"
12+
},
13+
"importLoaders": {
14+
"type": "number"
15+
}
16+
},
17+
"additionalProperties": false
18+
}

0 commit comments

Comments
 (0)