-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (37 loc) · 1.45 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
var loaderUtils = require("loader-utils");
var validateOptions = require("schema-utils");
var FS = require("fs");
var mime = require("mime");
module.exports = function (content) {
this.cacheable && this.cacheable();
var options = loaderUtils.getOptions(this) || {};
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);
if (limit) {
limit = parseInt(limit, 10);
}
var replace = options.replace || [];
replace.forEach(function (params) {
if (params.test && params.path && FS.existsSync(params.path)) {
content = FS.readFileSync(params.path);
this.resourcePath = params.path;
}
})
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);
}
return "module.exports = " + JSON.stringify("data:" + (mimetype ? mimetype + ";" : "") + "base64," + content.toString("base64"));
}
var fallback = options.fallback || "file-loader";
var fallbackLoader = require(fallback);
return fallbackLoader.call(this, content);
}
module.exports.raw = true;