Skip to content

Commit 8af9826

Browse files
sbardiansidharthachatterjee
authored andcommitted
feat(gatsby-plugin-sharp): add defaultQuality option
* Add defaultQuality option to gatsby-plugin-sharp * Update docs for defaultQuality option * Remove default quality value from gatsby-transformer-sharp * Remove check for gatsby-tranformer-sharp default quality value * Update setting a default quality in README Co-Authored-By: sbardian <sbardian@gmail.com>
1 parent a977a22 commit 8af9826

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

packages/gatsby-plugin-sharp/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ plugins: [
3232
options: {
3333
useMozJpeg: false,
3434
stripMetadata: true,
35+
defaultQuality: 75,
3536
},
3637
},
3738
]
@@ -249,6 +250,10 @@ fixed(
249250
}
250251
```
251252

253+
### Setting a default quality
254+
255+
You can pass a default image quality to `sharp` by setting the `defaultQuality` option.
256+
252257
### Using MozJPEG
253258

254259
You can opt-in to use [MozJPEG][16] for jpeg-encoding. MozJPEG provides even

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ exports.setBoundActionCreators = actions => {
4545
const pluginDefaults = {
4646
useMozJpeg: process.env.GATSBY_JPEG_ENCODER === `MOZJPEG`,
4747
stripMetadata: true,
48+
defaultQuality: 50,
4849
}
4950
let pluginOptions = Object.assign({}, pluginDefaults)
5051
exports.setPluginOptions = opts => {
@@ -95,8 +96,8 @@ const generalArgs = {
9596
sizeByPixelDensity: false,
9697
}
9798

98-
const healOptions = (args, defaultArgs) => {
99-
let options = _.defaults({}, args, defaultArgs, generalArgs)
99+
const healOptions = ({ defaultQuality: quality }, args, defaultArgs) => {
100+
let options = _.defaults({}, args, { quality }, defaultArgs, generalArgs)
100101
options.quality = parseInt(options.quality, 10)
101102
options.pngCompressionLevel = parseInt(options.pngCompressionLevel, 10)
102103
options.pngCompressionSpeed = parseInt(options.pngCompressionSpeed, 10)
@@ -125,7 +126,6 @@ const healOptions = (args, defaultArgs) => {
125126

126127
let totalJobs = 0
127128
const processFile = (file, jobs, cb, reporter) => {
128-
// console.log("totalJobs", totalJobs)
129129
bar.total = totalJobs
130130

131131
let imagesFinished = 0
@@ -372,7 +372,7 @@ const queueJob = (job, reporter) => {
372372
}
373373

374374
function queueImageResizing({ file, args = {}, reporter }) {
375-
const options = healOptions(args, {})
375+
const options = healOptions(pluginOptions, args, {})
376376
// Filter out false args, and args not for this extension and put width at
377377
// end (for the file path)
378378
const pairedArgs = _.toPairs(args)
@@ -476,7 +476,7 @@ function queueImageResizing({ file, args = {}, reporter }) {
476476
}
477477

478478
async function generateBase64({ file, args, reporter }) {
479-
const options = healOptions(args, { width: 20 })
479+
const options = healOptions(pluginOptions, args, { width: 20 })
480480
let pipeline
481481
try {
482482
pipeline = sharp(file.absolutePath).rotate()
@@ -558,7 +558,7 @@ async function base64(arg) {
558558
}
559559

560560
async function fluid({ file, args = {}, reporter, cache }) {
561-
const options = healOptions(args, {})
561+
const options = healOptions(pluginOptions, args, {})
562562
// Account for images with a high pixel density. We assume that these types of
563563
// images are intended to be displayed at their native resolution.
564564
let metadata
@@ -733,7 +733,7 @@ async function fluid({ file, args = {}, reporter, cache }) {
733733
}
734734

735735
async function fixed({ file, args = {}, reporter, cache }) {
736-
const options = healOptions(args, {})
736+
const options = healOptions(pluginOptions, args, {})
737737

738738
// if no width is passed, we need to resize the image based on the passed height
739739
const fixedDimension = options.width === undefined ? `height` : `width`
@@ -843,7 +843,7 @@ async function notMemoizedtraceSVG({ file, args, fileArgs, reporter }) {
843843
turnPolicy: potrace.Potrace.TURNPOLICY_MAJORITY,
844844
}
845845
const optionsSVG = _.defaults(args, defaultArgs)
846-
const options = healOptions(fileArgs, {})
846+
const options = healOptions(pluginOptions, fileArgs, {})
847847
let pipeline
848848
try {
849849
pipeline = sharp(file.absolutePath).rotate()

packages/gatsby-transformer-sharp/src/extend-node-type.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ const fixedNodeType = ({
137137
},
138138
quality: {
139139
type: GraphQLInt,
140-
defaultValue: 50,
141140
},
142141
toFormat: {
143142
type: ImageFormatType,
@@ -263,7 +262,6 @@ const fluidNodeType = ({
263262
},
264263
quality: {
265264
type: GraphQLInt,
266-
defaultValue: 50,
267265
},
268266
toFormat: {
269267
type: ImageFormatType,
@@ -413,7 +411,6 @@ module.exports = ({
413411
},
414412
quality: {
415413
type: GraphQLInt,
416-
defaultValue: 50,
417414
},
418415
jpegProgressive: {
419416
type: GraphQLBoolean,

0 commit comments

Comments
 (0)