Skip to content
This repository was archived by the owner on Mar 17, 2021. It is now read-only.

Commit 3356f91

Browse files
feat(index): add options validation (schema-utils)
1 parent 99b935f commit 3356f91

File tree

3 files changed

+47
-19
lines changed

3 files changed

+47
-19
lines changed

index.js

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,37 @@
33
Author Tobias Koppers @sokra
44
*/
55
var loaderUtils = require("loader-utils");
6+
var validateOptions = require("schema-utils");
7+
68
var mime = require("mime");
79

810
module.exports = function(content) {
9-
this.cacheable && this.cacheable();
11+
this.cacheable && this.cacheable();
1012

11-
var options = loaderUtils.getOptions(this) || {};
12-
// Options `dataUrlLimit` is backward compatibility with first loader versions
13-
var limit = options.limit || (this.options && this.options.url && this.options.url.dataUrlLimit);
13+
var options = loaderUtils.getOptions(this) || {};
1414

15-
if(limit) {
16-
limit = parseInt(limit, 10);
17-
}
15+
validateOptions(require("./options"), options, "URL Loader")
16+
// Options `dataUrlLimit` is backward compatibility with first loader versions
17+
var limit = options.limit || (this.options && this.options.url && this.options.url.dataUrlLimit);
1818

19-
var mimetype = options.mimetype || options.minetype || mime.lookup(this.resourcePath);
19+
if(limit) {
20+
limit = parseInt(limit, 10);
21+
}
2022

21-
// No limits or limit more than content length
22-
if(!limit || content.length < limit) {
23-
if(typeof content === "string") {
24-
content = new Buffer(content);
25-
}
26-
return "module.exports = " + JSON.stringify("data:" + (mimetype ? mimetype + ";" : "") + "base64," + content.toString("base64"));
23+
var mimetype = options.mimetype || options.minetype || mime.lookup(this.resourcePath);
24+
25+
// No limits or limit more than content length
26+
if(!limit || content.length < limit) {
27+
if(typeof content === "string") {
28+
content = new Buffer(content);
2729
}
2830

29-
var fileLoader = require("file-loader");
31+
return "module.exports = " + JSON.stringify("data:" + (mimetype ? mimetype + ";" : "") + "base64," + content.toString("base64"));
32+
}
33+
34+
var fileLoader = require("file-loader");
3035

31-
return fileLoader.call(this, content);
36+
return fileLoader.call(this, content);
3237
}
3338

3439
module.exports.raw = true;

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+
"limit": {
5+
"type": "number"
6+
},
7+
"prefix": {
8+
"type": "string"
9+
},
10+
"mimetype": {
11+
"type": "string"
12+
},
13+
"encoding": {
14+
"type": "string"
15+
}
16+
},
17+
"additionalProperties": false
18+
}

package.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@
55
"description": "url loader module for webpack",
66
"dependencies": {
77
"loader-utils": "^1.0.2",
8-
"mime": "1.3.x"
8+
"mime": "1.3.6",
9+
"schema-utils": "^0.3.0"
910
},
1011
"peerDependencies": {
1112
"file-loader": "*"
1213
},
1314
"repository": {
14-
"type": "git",
15-
"url": "git@github.com:webpack/url-loader.git"
15+
"type": "git",
16+
"url": "git+https://github.com/webpack-contrib/url-loader.git"
1617
},
18+
"bugs": {
19+
"url": "https://github.com/webpack-contrib/url-loader/issues"
20+
},
21+
"homepage": "https://github.com/webpack-contrib/url-loader",
1722
"license": "MIT"
1823
}

0 commit comments

Comments
 (0)