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

feat(index): add options validation (schema-utils) #78

Merged
merged 1 commit into from
Jun 13, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
37 changes: 21 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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"
}