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

Add option to keep metadata in files processed by gatsby-plugin-sharp #10210

Merged
merged 5 commits into from
Dec 7, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 19 additions & 2 deletions packages/gatsby-plugin-sharp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ images. By default it uses a quality setting of [50-75].

```javascript
// In your gatsby-config.js
plugins: [`gatsby-plugin-sharp`]
plugins: [
{
resolve: `gatsby-plugin-sharp`,
options: {
useMozJpeg: process.env.GATSBY_JPEG_ENCODER === `MOZJPEG`,
FineWolf marked this conversation as resolved.
Show resolved Hide resolved
stripMetadata: true,
},
},
]
```

## Methods
Expand Down Expand Up @@ -242,12 +250,21 @@ 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):
To enable MozJPEG, you can set the `useMozJpeg` plugin option to `true` in
`gatsby-config.js` or set
the [environment variable](/docs/environment-variables/#environment-variables):

```shell
GATSBY_JPEG_ENCODER=MOZJPEG
```

### EXIF and ICC metadata

By default, `gatsby-plugin-sharp` strips all EXIF, ICC and other metadata
present in your source file. If you wish to preserve this metadata, you can
FineWolf marked this conversation as resolved.
Show resolved Hide resolved
set the `stripMetadata` plugin option to `false` in
`gatsby-config.js`.

[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 Down
5 changes: 3 additions & 2 deletions packages/gatsby-plugin-sharp/src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const { setBoundActionCreators } = require(`./index`)
const { setBoundActionCreators, setPluginOptions } = require(`./index`)

exports.onPreInit = ({ actions }) => {
exports.onPreInit = ({ actions }, pluginOptions) => {
setBoundActionCreators(actions)
setPluginOptions(pluginOptions)
}

// TODO
Expand Down
31 changes: 26 additions & 5 deletions packages/gatsby-plugin-sharp/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ exports.setBoundActionCreators = actions => {
boundActionCreators = actions
}

/// Plugin options are loaded onPreInit in gatsby-node
let pluginOptions = {
useMozJpeg: process.env.GATSBY_JPEG_ENCODER === `MOZJPEG`,
stripMetadata: true,
}
exports.setPluginOptions = options => {
// Typechecks
if (typeof options.useMozJpeg === `boolean`) {
FineWolf marked this conversation as resolved.
Show resolved Hide resolved
pluginOptions.useMozJpeg = options.useMozJpeg
}

if (typeof options.stripMetadata === `boolean`) {
pluginOptions.stripMetadata = options.stripMetadata
}
}

// Promisify the sharp prototype (methods) to promisify the alternative (for
// raw) callback-accepting toBuffer(...) method
Promise.promisifyAll(sharp.prototype, { multiArgs: true })
Expand Down Expand Up @@ -110,8 +126,6 @@ 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 All @@ -124,7 +138,14 @@ const processFile = (file, jobs, cb, reporter) => {

let pipeline
try {
pipeline = sharp(file).rotate()
pipeline = sharp(file)

// Keep Metadata
if (pluginOptions.stripMetadata === false) {
FineWolf marked this conversation as resolved.
Show resolved Hide resolved
pipeline = pipeline.withMetadata()
}

pipeline = pipeline.rotate()
} catch (err) {
reportError(`Failed to process image ${file}`, err, reporter)
jobs.forEach(job => job.outsideReject(err))
Expand Down Expand Up @@ -170,7 +191,7 @@ const processFile = (file, jobs, cb, reporter) => {
})

// jpeg
if (!useMozjpeg) {
if (!pluginOptions.useMozJpeg) {
clonedPipeline = clonedPipeline.jpeg({
quality: args.quality,
progressive: args.jpegProgressive,
Expand Down Expand Up @@ -242,7 +263,7 @@ const processFile = (file, jobs, cb, reporter) => {
.catch(onFinish)
// Compress jpeg
} else if (
useMozjpeg &&
pluginOptions.useMozJpeg &&
((job.file.extension === `jpg` && args.toFormat === ``) ||
(job.file.extension === `jpeg` && args.toFormat === ``) ||
args.toFormat === `jpg`)
Expand Down