forked from bt404/base64-inline-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (34 loc) · 1.03 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
'use strict';
let loaderUtils = require('loader-utils');
let fileLoader = require('file-loader');
let mimeType = require('mime-types');
module.exports = function (content) {
if (this.cacheable) {
this.cacheable();
}
let query = loaderUtils.getOptions(this);
let extension = loaderUtils.interpolateName(this, '[ext]', {
context: query.context || this.options.context,
content: content,
regExp: query.regExp
});
extension = extension.toLowerCase();
let type = mimeType.lookup(extension),
data = content.toString('base64');
if (!type) {
throw new Error(`${extension} type is not supported`);
}
let { limit = 0 } = query;
try {
({dataUrlLimit: limit} = this.options.url);
}
catch (error) {
// throw new Error('something bad happened:' + error);
}
if (limit <= 0 || content.length < limit) {
let url = JSON.stringify(`data:${type};charset=utf-8;base64,${data}`);
return `module.exports = ${url}`;
}
return fileLoader.call(this, content);
}
module.exports.raw = true;