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

Commit

Permalink
feat(index): add options validation (schema-utils) (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-ciniawsky authored and joshwiens committed Jun 13, 2017
1 parent 5574ed3 commit ced5990
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 19 deletions.
37 changes: 21 additions & 16 deletions index.js
Expand Up @@ -3,32 +3,37 @@
Author Tobias Koppers @sokra
*/
var loaderUtils = require("loader-utils");
var validateOptions = require("schema-utils");

var mime = require("mime");

module.exports = function(content) {
this.cacheable && this.cacheable();
this.cacheable && this.cacheable();

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

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

var mimetype = options.mimetype || options.minetype || mime.lookup(this.resourcePath);
if(limit) {
limit = parseInt(limit, 10);
}

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

// No limits or limit more than content length
if(!limit || content.length < limit) {
if(typeof content === "string") {
content = new Buffer(content);
}

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

var fileLoader = require("file-loader");

return fileLoader.call(this, content);
return fileLoader.call(this, content);
}

module.exports.raw = true;
18 changes: 18 additions & 0 deletions options.json
@@ -0,0 +1,18 @@
{
"type": "object",
"properties": {
"limit": {
"type": "number"
},
"prefix": {
"type": "string"
},
"mimetype": {
"type": "string"
},
"encoding": {
"type": "string"
}
},
"additionalProperties": false
}
12 changes: 9 additions & 3 deletions package.json
Expand Up @@ -9,7 +9,8 @@
},
"dependencies": {
"loader-utils": "^1.0.2",
"mime": "1.3.x"
"mime": "^1.3.6",
"schema-utils": "^0.3.0"
},
"devDependencies": {
"standard-version": "^4.0.0"
Expand All @@ -19,6 +20,11 @@
},
"repository": {
"type": "git",
"url": "git@github.com:webpack/url-loader.git"
}
"url": "git+https://github.com/webpack-contrib/url-loader.git"
},
"bugs": {
"url": "https://github.com/webpack-contrib/url-loader/issues"
},
"homepage": "https://github.com/webpack-contrib/url-loader",
"license": "MIT"
}

0 comments on commit ced5990

Please sign in to comment.