Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[gatsby-plugin-sharp] add option to use mozjpeg #8621

Merged
merged 2 commits into from
Oct 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/gatsby-plugin-sharp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,18 @@ fixed(
}
```

### Using MozJPEG

You can opt-in to use [MozJPEG][16] for jpeg-encoding. MozJPEG provides even
better image compression than the default encoder used in `gatsby-plugin-sharp`.
However, when using MozJPEG the build time of your Gatsby project will increase
significantly.
To enable MozJPEG set the [environment variable](/docs/environment-variables/#environment-variables):

```shell
GATSBY_JPEG_ENCODER=MOZJPEG
```

[1]: https://alistapart.com/article/finessing-fecolormatrix
[2]: http://blog.72lions.com/blog/2015/7/7/duotone-in-js
[3]: https://ines.io/blog/dynamic-duotone-svg-jade
Expand All @@ -244,3 +256,4 @@ fixed(
[13]: https://github.com/tooolbox/node-potrace#parameters
[14]: https://github.com/oliver-moran/jimp
[15]: http://sharp.dimens.io/en/stable/api-operation/#flatten
[16]: https://github.com/mozilla/mozjpeg
1 change: 1 addition & 0 deletions packages/gatsby-plugin-sharp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"bluebird": "^3.5.0",
"fs-exists-cached": "^1.0.0",
"imagemin": "^6.0.0",
"imagemin-mozjpeg": "^7.0.0",
"imagemin-pngquant": "^6.0.0",
"imagemin-webp": "^4.1.0",
"lodash": "^4.17.10",
Expand Down
44 changes: 38 additions & 6 deletions packages/gatsby-plugin-sharp/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const Promise = require(`bluebird`)
const fs = require(`fs`)
const ProgressBar = require(`progress`)
const imagemin = require(`imagemin`)
const imageminMozjpeg = require(`imagemin-mozjpeg`)
const imageminPngquant = require(`imagemin-pngquant`)
const imageminWebp = require(`imagemin-webp`)
const queue = require(`async/queue`)
Expand Down Expand Up @@ -100,6 +101,8 @@ const healOptions = (args, defaultArgs) => {
return options
}

const useMozjpeg = process.env.GATSBY_JPEG_ENCODER === `MOZJPEG`

let totalJobs = 0
const processFile = (file, jobs, cb, reporter) => {
// console.log("totalJobs", totalJobs)
Expand Down Expand Up @@ -147,11 +150,6 @@ const processFile = (file, jobs, cb, reporter) => {
adaptiveFiltering: false,
force: args.toFormat === `png`,
})
.jpeg({
quality: args.quality,
progressive: args.jpegProgressive,
force: args.toFormat === `jpg`,
})
.webp({
quality: args.quality,
force: args.toFormat === `webp`,
Expand All @@ -161,6 +159,15 @@ const processFile = (file, jobs, cb, reporter) => {
force: args.toFormat === `tiff`,
})

// jpeg
if (!useMozjpeg) {
clonedPipeline = clonedPipeline.jpeg({
quality: args.quality,
progressive: args.jpegProgressive,
force: args.toFormat === `jpg`,
})
}

// grayscale
if (args.grayscale) {
clonedPipeline = clonedPipeline.grayscale()
Expand Down Expand Up @@ -223,6 +230,31 @@ const processFile = (file, jobs, cb, reporter) => {
.catch(onFinish)
)
.catch(onFinish)
// Compress jpeg
} else if (
useMozjpeg &&
((job.file.extension === `jpg` && args.toFormat === ``) ||
(job.file.extension === `jpeg` && args.toFormat === ``) ||
args.toFormat === `jpg`)
) {
clonedPipeline
.toBuffer()
.then(sharpBuffer =>
imagemin
.buffer(sharpBuffer, {
plugins: [
imageminMozjpeg({
quality: args.quality,
progressive: args.jpegProgressive,
}),
],
})
.then(imageminBuffer => {
fs.writeFile(job.outputPath, imageminBuffer, onFinish)
})
.catch(onFinish)
)
.catch(onFinish)
// Compress webp
} else if (
(job.file.extension === `webp` && args.toFormat === ``) ||
Expand All @@ -241,7 +273,7 @@ const processFile = (file, jobs, cb, reporter) => {
.catch(onFinish)
)
.catch(onFinish)
// any other format (jpeg, tiff) - don't compress it just handle output
// any other format (tiff) - don't compress it just handle output
} else {
clonedPipeline.toFile(job.outputPath, onFinish)
}
Expand Down