This repository has been archived by the owner on Nov 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (44 loc) · 1.4 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
const Vibrant = require('node-vibrant');
const loaderUtils = require('loader-utils');
const Jimp = require('jimp');
module.exports = function(content) {
// somehow, Jimp.getExtension is not working to generate hashes
// https://github.com/oliver-moran/jimp/issues/193
const config = {
publicPath: false,
name: "[hash].[ext]"
};
const url = loaderUtils.interpolateName(this, config.name, {
context: config.context || this.options.context,
content: content,
regExp: config.regExp
});
this.cacheable && this.cacheable();
const loaderCallback = this.async();
Jimp.read(content).then( image => {
parseImage(image);
}).catch(function (err) {
console.error(err);
});
const parseImage = (image) => Vibrant.from(image).getPalette( (err, palette) => {
if (err) throw err;
const mime = image._originalMime;
const imageForAnts = image.clone()
.resize(3,3);
const source = image.bitmap;
const doneParsing = (err, buffer) => {
if (err) throw err;
palette.imageForAnts = buffer;
palette.src = url;
palette.source = {
width: source.width,
height: source.height,
ratio: source.width / source.height
};
this.emitFile(url, content);
loaderCallback(null, 'module.exports = ' + JSON.stringify(palette) + ';');
}
imageForAnts.getBase64( Jimp.MIME_PNG, doneParsing );
});
};
module.exports.raw = true;