Skip to content

Commit 10bc679

Browse files
Fettenpieh
authored andcommitted
feat: add option to use mozjpeg (#8621)
1 parent 679a4bb commit 10bc679

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

packages/gatsby-plugin-sharp/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,18 @@ fixed(
229229
}
230230
```
231231

232+
### Using MozJPEG
233+
234+
You can opt-in to use [MozJPEG][16] for jpeg-encoding. MozJPEG provides even
235+
better image compression than the default encoder used in `gatsby-plugin-sharp`.
236+
However, when using MozJPEG the build time of your Gatsby project will increase
237+
significantly.
238+
To enable MozJPEG set the [environment variable](/docs/environment-variables/#environment-variables):
239+
240+
```shell
241+
GATSBY_JPEG_ENCODER=MOZJPEG
242+
```
243+
232244
[1]: https://alistapart.com/article/finessing-fecolormatrix
233245
[2]: http://blog.72lions.com/blog/2015/7/7/duotone-in-js
234246
[3]: https://ines.io/blog/dynamic-duotone-svg-jade
@@ -244,3 +256,4 @@ fixed(
244256
[13]: https://github.com/tooolbox/node-potrace#parameters
245257
[14]: https://github.com/oliver-moran/jimp
246258
[15]: http://sharp.dimens.io/en/stable/api-operation/#flatten
259+
[16]: https://github.com/mozilla/mozjpeg

packages/gatsby-plugin-sharp/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"bluebird": "^3.5.0",
1313
"fs-exists-cached": "^1.0.0",
1414
"imagemin": "^6.0.0",
15+
"imagemin-mozjpeg": "^7.0.0",
1516
"imagemin-pngquant": "^6.0.0",
1617
"imagemin-webp": "^4.1.0",
1718
"lodash": "^4.17.10",

packages/gatsby-plugin-sharp/src/index.js

+38-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const Promise = require(`bluebird`)
66
const fs = require(`fs`)
77
const ProgressBar = require(`progress`)
88
const imagemin = require(`imagemin`)
9+
const imageminMozjpeg = require(`imagemin-mozjpeg`)
910
const imageminPngquant = require(`imagemin-pngquant`)
1011
const imageminWebp = require(`imagemin-webp`)
1112
const queue = require(`async/queue`)
@@ -100,6 +101,8 @@ const healOptions = (args, defaultArgs) => {
100101
return options
101102
}
102103

104+
const useMozjpeg = process.env.GATSBY_JPEG_ENCODER === `MOZJPEG`
105+
103106
let totalJobs = 0
104107
const processFile = (file, jobs, cb, reporter) => {
105108
// console.log("totalJobs", totalJobs)
@@ -147,11 +150,6 @@ const processFile = (file, jobs, cb, reporter) => {
147150
adaptiveFiltering: false,
148151
force: args.toFormat === `png`,
149152
})
150-
.jpeg({
151-
quality: args.quality,
152-
progressive: args.jpegProgressive,
153-
force: args.toFormat === `jpg`,
154-
})
155153
.webp({
156154
quality: args.quality,
157155
force: args.toFormat === `webp`,
@@ -161,6 +159,15 @@ const processFile = (file, jobs, cb, reporter) => {
161159
force: args.toFormat === `tiff`,
162160
})
163161

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+
164171
// grayscale
165172
if (args.grayscale) {
166173
clonedPipeline = clonedPipeline.grayscale()
@@ -223,6 +230,31 @@ const processFile = (file, jobs, cb, reporter) => {
223230
.catch(onFinish)
224231
)
225232
.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)
226258
// Compress webp
227259
} else if (
228260
(job.file.extension === `webp` && args.toFormat === ``) ||
@@ -241,7 +273,7 @@ const processFile = (file, jobs, cb, reporter) => {
241273
.catch(onFinish)
242274
)
243275
.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
245277
} else {
246278
clonedPipeline.toFile(job.outputPath, onFinish)
247279
}

0 commit comments

Comments
 (0)