forked from dazuaz/responsive-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
136 lines (115 loc) · 4.71 KB
/
index.js
File metadata and controls
136 lines (115 loc) · 4.71 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const path = require('path');
const loaderUtils = require('loader-utils');
const jimp = require('jimp');
const queue = require('d3-queue').queue;
const MIMES = {
'jpg': 'image/jpeg',
'jpeg': 'image/jpeg',
'png': 'image/png'
};
module.exports = function loader(content) {
this.cacheable && this.cacheable();
const loaderCallback = this.async();
const query = loaderUtils.parseQuery(this.query);
const options = this.options.responsiveLoader || {};
const sizes = query.sizes || query.size || options.sizes || [Number.MAX_SAFE_INTEGER];
const name = query.name || options.name || '[hash]-[width].';
const outputContext = query.context || options.context || '';
const outputPlaceholder = query.placeholder || query.placeholder !== false && options.placeholder || false;
const placeholderSize = query.placeholderSize || options.placeholderSize || 40;
// JPEG compression
const quality = parseInt(query.quality, 10) || options.quality || 95;
// Useful when converting from PNG to JPG
const background = parseInt(query.background, 16) || options.background || 0xFFFFFFFF;
// Specify ext to convert to another format
const ext = query.ext || path.extname(this.resourcePath).replace(/\./, '');
const mime = MIMES[ext];
const loaderContext = this;
if (!sizes) {
return loaderCallback(null, content);
}
if (!mime) {
return loaderCallback(new Error('No mime type for file with extension ' + ext + 'supported'));
}
if (options.pass) {
// emit original content only
const f = loaderUtils.interpolateName(loaderContext, '[hash].[ext]', {context: outputContext, content: content});
loaderContext.emitFile(f, content);
const p = '__webpack_public_path__ + ' + JSON.stringify(f);
return loaderCallback(null, 'module.exports = {srcSet:' + p + ',images:[{path:' + p + ',width:1}],src: ' + p + ',toString:function(){return ' + p + '}};');
}
return jimp.read(loaderContext.resourcePath, (err, img) => {
if (err) {
return loaderCallback(err);
}
function resizeImage(width, queueCallback) {
img
.clone()
.resize(width, jimp.AUTO)
.quality(quality)
.background(background)
.getBuffer(mime, function resizeCallback(queueErr, buf) {
if (err) {
return queueCallback(queueErr);
}
const fileName = loaderUtils.interpolateName(loaderContext, name + ext, {
context: outputContext,
content: buf
}).replace(/\[width\]/ig, width);
loaderContext.emitFile(fileName, buf);
return queueCallback(null, {
src: '__webpack_public_path__ + ' + JSON.stringify(fileName + ' ' + width + 'w'),
path: '__webpack_public_path__ + ' + JSON.stringify(fileName),
width: width,
height: this.bitmap.height
});
});
}
const q = queue();
const widthsToGenerate = new Set();
(Array.isArray(sizes) ? sizes : [sizes]).forEach((size) => {
const width = Math.min(img.bitmap.width, parseInt(size, 10));
// Only resize images if they aren't an exact copy of one already being resized...
if (!widthsToGenerate.has(width)) {
widthsToGenerate.add(width);
q.defer(resizeImage, width);
}
});
if (outputPlaceholder) {
q.defer(function generatePlaceholder(queueCallback) {
img
.clone()
.resize(placeholderSize, jimp.AUTO)
.quality(quality)
.background(background)
.getBuffer(mime, function resizeCallback(queueErr, buf) {
if (err) {
return queueCallback(queueErr);
}
const placeholder = buf.toString('base64');
return queueCallback(null, JSON.stringify('data:' + (mime ? mime + ';' : '') + 'base64,' + placeholder));
});
});
}
return q.awaitAll((queueErr, files) => {
'use strict'; // eslint-disable-line
let placeholder;
if (outputPlaceholder) {
placeholder = files.pop();
}
const srcset = files.map(f => f.src).join('+","+');
const images = files.map(f => '{path:' + f.path + ',width:' + f.width + ',height:' + f.height + '}').join(',');
const firstImage = files[0];
loaderCallback(null, 'module.exports = {' +
'srcSet:' + srcset + ',' +
'images:[' + images + '],' +
'src:' + firstImage.path + ',' +
'toString:function(){return ' + firstImage.path + '},' +
'placeholder: ' + placeholder + ',' +
'width:' + firstImage.width + ',' +
'height:' + firstImage.height +
'};');
});
});
};
module.exports.raw = true; // get buffer stream instead of utf8 string