@@ -6,6 +6,7 @@ const Promise = require(`bluebird`)
6
6
const fs = require ( `fs` )
7
7
const ProgressBar = require ( `progress` )
8
8
const imagemin = require ( `imagemin` )
9
+ const imageminMozjpeg = require ( `imagemin-mozjpeg` )
9
10
const imageminPngquant = require ( `imagemin-pngquant` )
10
11
const imageminWebp = require ( `imagemin-webp` )
11
12
const queue = require ( `async/queue` )
@@ -100,6 +101,8 @@ const healOptions = (args, defaultArgs) => {
100
101
return options
101
102
}
102
103
104
+ const useMozjpeg = process . env . GATSBY_JPEG_ENCODER === `MOZJPEG`
105
+
103
106
let totalJobs = 0
104
107
const processFile = ( file , jobs , cb , reporter ) => {
105
108
// console.log("totalJobs", totalJobs)
@@ -147,11 +150,6 @@ const processFile = (file, jobs, cb, reporter) => {
147
150
adaptiveFiltering : false ,
148
151
force : args . toFormat === `png` ,
149
152
} )
150
- . jpeg ( {
151
- quality : args . quality ,
152
- progressive : args . jpegProgressive ,
153
- force : args . toFormat === `jpg` ,
154
- } )
155
153
. webp ( {
156
154
quality : args . quality ,
157
155
force : args . toFormat === `webp` ,
@@ -161,6 +159,15 @@ const processFile = (file, jobs, cb, reporter) => {
161
159
force : args . toFormat === `tiff` ,
162
160
} )
163
161
162
+ // jpeg
163
+ if ( ! useMozjpeg ) {
164
+ clonedPipeline = clonedPipeline . jpeg ( {
165
+ quality : args . quality ,
166
+ progressive : args . jpegProgressive ,
167
+ force : args . toFormat === `jpg` ,
168
+ } )
169
+ }
170
+
164
171
// grayscale
165
172
if ( args . grayscale ) {
166
173
clonedPipeline = clonedPipeline . grayscale ( )
@@ -223,6 +230,31 @@ const processFile = (file, jobs, cb, reporter) => {
223
230
. catch ( onFinish )
224
231
)
225
232
. catch ( onFinish )
233
+ // Compress jpeg
234
+ } else if (
235
+ useMozjpeg &&
236
+ ( ( job . file . extension === `jpg` && args . toFormat === `` ) ||
237
+ ( job . file . extension === `jpeg` && args . toFormat === `` ) ||
238
+ args . toFormat === `jpg` )
239
+ ) {
240
+ clonedPipeline
241
+ . toBuffer ( )
242
+ . then ( sharpBuffer =>
243
+ imagemin
244
+ . buffer ( sharpBuffer , {
245
+ plugins : [
246
+ imageminMozjpeg ( {
247
+ quality : args . quality ,
248
+ progressive : args . jpegProgressive ,
249
+ } ) ,
250
+ ] ,
251
+ } )
252
+ . then ( imageminBuffer => {
253
+ fs . writeFile ( job . outputPath , imageminBuffer , onFinish )
254
+ } )
255
+ . catch ( onFinish )
256
+ )
257
+ . catch ( onFinish )
226
258
// Compress webp
227
259
} else if (
228
260
( job . file . extension === `webp` && args . toFormat === `` ) ||
@@ -241,7 +273,7 @@ const processFile = (file, jobs, cb, reporter) => {
241
273
. catch ( onFinish )
242
274
)
243
275
. catch ( onFinish )
244
- // any other format (jpeg, tiff) - don't compress it just handle output
276
+ // any other format (tiff) - don't compress it just handle output
245
277
} else {
246
278
clonedPipeline . toFile ( job . outputPath , onFinish )
247
279
}
0 commit comments