forked from tcoopman/image-webpack-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (88 loc) · 3.25 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
var imagemin = require('imagemin');
var imageminGifsicle = require('imagemin-gifsicle');
var imageminMozjpeg = require('imagemin-mozjpeg');
var imageminOptipng = require('imagemin-optipng');
var imageminSvgo = require('imagemin-svgo');
var imageminPngquant = require('imagemin-pngquant');
var imageminWebp = require('imagemin-webp');
var loaderUtils = require('loader-utils');
var assign = require('object-assign');
/**
* Basically the getLoaderConfig() function from loader-utils v0.2.
*/
function getLegacyLoaderConfig(loaderContext, defaultConfigKey) {
var options = loaderUtils.getOptions(loaderContext);
var configKey = options ? options.config : defaultConfigKey;
if (configKey) {
return assign({}, options, loaderContext.options[configKey]);
}
return options;
}
module.exports = function(content) {
this.cacheable && this.cacheable();
var config = this.version === 2 ?
loaderUtils.getOptions(this)
: getLegacyLoaderConfig(this, "imageWebpackLoader");
if (config === null) {
// handle the cases in which loaderUtils.getOptions() returns null
// see https://github.com/webpack/loader-utils#getoptions
config = {}
}
var options = {
bypassOnDebug: config.bypassOnDebug || false,
// default optimizers
gifsicle: config.gifsicle || {},
mozjpeg: config.mozjpeg || {},
pngquant: config.pngquant || {},
optipng: config.optipng || {},
svgo: config.svgo || {},
// optional optimizers
webp: config.webp || null
};
// Remove in interlaced, progressive and optimizationLevel checks in new major version
if (config.hasOwnProperty('interlaced')) {
options.gifsicle.interlaced = config.interlaced;
this.emitWarning("DEPRECATED. Configure gifsicle's interlaced option in its own options. (gifsicle.interlaced)");
}
if (config.hasOwnProperty('progressive')) {
options.mozjpeg.progressive = config.progressive;
this.emitWarning("DEPRECATED. Configure mozjpeg's progressive option in its own options. (mozjpeg.progressive)");
}
if (config.hasOwnProperty('optimizationLevel')) {
options.optipng.optimizationLevel = config.optimizationLevel;
this.emitWarning("DEPRECATED. Configure optipng's optimizationLevel option in its own options. (optipng.optimizationLevel)");
}
var callback = this.async(),
called = false;
if (this.debug === true && options.bypassOnDebug === true) {
// Bypass processing while on watch mode
return callback(null, content);
} else {
var plugins = [];
// default optimizers
if(options.gifsicle.enabled !== false)
plugins.push(imageminGifsicle(options.gifsicle));
if(options.mozjpeg.enabled !== false)
plugins.push(imageminMozjpeg(options.mozjpeg));
if(options.svgo.enabled !== false)
plugins.push(imageminSvgo(options.svgo));
if(options.pngquant.enabled !== false)
plugins.push(imageminPngquant(options.pngquant));
if(options.optipng.enabled !== false)
plugins.push(imageminOptipng(options.optipng));
// optional optimizers
if(options.webp)
plugins.push(imageminWebp(options.webp));
imagemin
.buffer(content, {
plugins
})
.then(data => {
callback(null, data);
})
.catch(err => {
callback(err);
});
}
};
module.exports.raw = true;