This repository has been archived by the owner on Mar 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 132
/
index.js
104 lines (94 loc) · 3.44 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
98
99
100
101
102
103
104
var imagemin = require('imagemin');
var loaderUtils = require('loader-utils');
var assign = require('object-assign');
var schemaValidation = require('schema-utils')
var loaderSchema = require('./schema.json')
/**
* 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,
disable: config.disable || false,
// default optimizers
gifsicle: config.gifsicle || {},
mozjpeg: config.mozjpeg || {},
pngquant: config.pngquant || {},
optipng: config.optipng || {},
svgo: config.svgo || {},
// optional optimizers
webp: config.webp || { enabled: false }
};
// 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)");
}
schemaValidation(loaderSchema, options, {
name: 'imageWebpackLoader'
});
var callback = this.async(),
called = false;
if ((this.debug === true && options.bypassOnDebug === true) || options.disable === true) {
// Bypass processing while on watch mode
return callback(null, content);
} else {
var plugins = [];
// default optimizers
if(options.gifsicle.enabled !== false)
plugins.push(require('imagemin-gifsicle')(options.gifsicle));
if(options.mozjpeg.enabled !== false)
plugins.push(require('imagemin-mozjpeg')(options.mozjpeg));
if(options.svgo.enabled !== false)
plugins.push(require('imagemin-svgo')(options.svgo));
if(options.pngquant.enabled !== false)
plugins.push(require('imagemin-pngquant')(options.pngquant));
if(options.optipng.enabled !== false)
plugins.push(require('imagemin-optipng')(options.optipng));
// optional optimizers
if(options.webp.enabled !== false) {
async function loadWebp() {
await import('imagemin-webp').then(({ default: imageminWebp }) => {
plugins.push(imageminWebp(options.webp));
});
}
loadWebp()
}
imagemin
.buffer(content, {
plugins
})
.then(data => {
callback(null, data);
})
.catch(err => {
callback(err);
});
}
};
module.exports.raw = true;