-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
53 lines (40 loc) · 1.51 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
49
50
51
52
53
var loaderUtils = require('loader-utils');
var sizeOf = require('image-size');
var fs = require('fs');
var imageToString = function(image) {
return 'module.exports = {' + '\n'
+ ' src: __webpack_public_path__ + ' + JSON.stringify(image.src) + ',\n'
+ ' width: ' + JSON.stringify(image.width) + ',\n'
+ ' height: ' + JSON.stringify(image.height) + ',\n'
+ ' bytes: ' + JSON.stringify(image.bytes) + ',\n'
+ ' type: ' + JSON.stringify(image.type) + ',\n'
+ '};' + '\n'
// For requires from CSS when used with webpack css-loader,
// outputting an Object doesn't make sense,
// So overriding the toString method to output just the URL
+ 'module.exports.toString = function() {' + '\n'
+ ' return __webpack_public_path__ + ' + JSON.stringify(image.src) + ';\n'
+ '};';
};
module.exports = function(content) {
this.cacheable && this.cacheable(true);
this.addDependency(this.resourcePath);
var options = loaderUtils.getOptions(this) || {};
var filename = "[name].[ext]";
if ('string' === typeof options.name) {
filename = options.name;
}
var url = loaderUtils.interpolateName(this, filename, {
context: options.context || this.rootContext || this.context,
content: content,
regExp: options.regExp
});
var image = sizeOf(this.resourcePath);
image.src = url;
image.bytes = fs.statSync(this.resourcePath).size;
if (options.emitFile || this.emitFile) {
this.emitFile(url, content);
}
return imageToString(image);
};
module.exports.raw = true;