forked from zouhir/lqip-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
77 lines (67 loc) · 2.24 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// http://sharp.dimens.io/en/stable/performance/#results
// Faster npm image processing
var sharp = require('sharp');
var loaderUtils = require('loader-utils');
var lqipName = require('./package.json').name;
var packageversion = require('./package.json').version;
// supported images \ mimetypes
// best results have been seen on JPEG banners
var SUPPORTED_MIMES = {
'jpeg': 'image/jpeg',
'jpg': 'image/jpeg',
'png': 'image/png'
};
// extension: file extension
// data: image file Buffer after resize
var toBase64 = function (extension, data) {
return 'data:' + SUPPORTED_MIMES[extension] + ';base64,' + data.toString('base64');
};
module.exports = function() { }
module.exports.pitch = function(content) {
this.cacheable && this.cacheable();
var callback = this.async();
// image file path
var path = this.resourcePath;
// image file extension
var extension = path.split('.').pop().toLowerCase();
// the source HQ image file
var source = null;
// the low quality placeholder
var presource = null;
// user options
var baseConfig = loaderUtils.getOptions(this) || { };
// default options
var config = {
path: '',
name: '[name].[ext]'
};
// take the user's specified options as a preference
Object.keys(config).forEach(function(key) {
config[key] = baseConfig[key] || config[key];
});
// loader context
var context = config.context || this.options.context;
// use loaderUtils to construct a proper name
// config.name eg. ([name].[ext]: car.jpg) or ([hash].[ext]: [96redfghjk.....].[jpg])
source = loaderUtils.interpolateName(this, config.path + '/' + config.name, {
context: context,
content: content
}) || "/";
// output object
var output = {};
if (typeof SUPPORTED_MIMES[extension] === 'undefined') {
throw new Error('Unsupported image format passed to ' + packageName + ' v. ' + packageversion);
}
sharp(path)
.resize(14) // resize to 16px width and auto height
.toBuffer() // converts to buffer for Base64 conversion
.then(data => {
presource = toBase64(extension, data);
output.src = source;
output.preSrc = presource;
callback(null, 'module.exports = ' + JSON.stringify(output) + ';');
})
.catch(err => {
callback(err, null);
});
};