/
plugin-options.ts
266 lines (243 loc) · 7.68 KB
/
plugin-options.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import type { ISharpGatsbyImageArgs, Fit } from "gatsby-plugin-image"
import { pickBy, defaults, mergeWith, omitBy, isNil, identity } from "lodash"
import type { FailOnOptions, SharpOptions } from "sharp"
export type PluginOptionsDefaults = Pick<
ISharpGatsbyImageArgs,
| "formats"
| "placeholder"
| "quality"
| "breakpoints"
| "backgroundColor"
| "tracedSVGOptions"
| "blurredOptions"
| "jpgOptions"
| "pngOptions"
| "webpOptions"
| "avifOptions"
>
export interface ISharpPluginOptions {
base64Width?: number
forceBase64Format?: "png" | "webp" | "jpg" | string
useMozJpeg?: boolean
stripMetadata?: boolean
lazyImageGeneration?: boolean
defaultQuality: number
failOn?: SharpOptions["failOn"]
defaults?: PluginOptionsDefaults
}
interface IDuotoneArgs {
highlight: string
shadow: string
opacity?: number
}
export interface ITransformArgs {
height: number
width: number
cropFocus?: number | string
toFormat: string
pngCompressionLevel?: number
quality?: number
jpegQuality?: number
pngQuality?: number
webpQuality?: number
jpegProgressive?: boolean
grayscale?: boolean
rotate?: number
trim?: number
duotone?: IDuotoneArgs
background?: string
fit?: Fit
pathPrefix?: string
maxHeight?: number
maxWidth?: number
base64Width?: number
}
interface IGeneralArgs extends ITransformArgs {
base64: boolean
pathPrefix: string
toFormatBase64: string
pngCompressionSpeed?: number
}
// Plugin options are loaded onPreBootstrap in gatsby-node
const pluginDefaults = {
base64Width: 20,
forceBase64Format: ``, // valid formats: png,jpg,webp
useMozJpeg: process.env.GATSBY_JPEG_ENCODER === `MOZJPEG`,
stripMetadata: true,
lazyImageGeneration: true,
defaultQuality: 50,
failOn: `warning` as FailOnOptions,
}
const generalArgs: Partial<IGeneralArgs> = {
quality: 50,
jpegQuality: undefined,
pngQuality: undefined,
webpQuality: undefined,
jpegProgressive: true,
pngCompressionLevel: 9,
// default is 4 (https://github.com/kornelski/pngquant/blob/4219956d5e080be7905b5581314d913d20896934/rust/bin.rs#L61)
pngCompressionSpeed: 4,
base64: true,
grayscale: false,
duotone: undefined,
pathPrefix: ``,
toFormat: ``,
toFormatBase64: ``,
rotate: 0,
}
let pluginOptions: ISharpPluginOptions = Object.assign({}, pluginDefaults)
export const setPluginOptions = (
opts: Record<string, string>
): ISharpPluginOptions => {
pluginOptions = Object.assign({}, pluginOptions, opts)
generalArgs.quality = pluginOptions.defaultQuality
return pluginOptions
}
export const getPluginOptions = (): ISharpPluginOptions => pluginOptions
export const getPluginOptionsDefaults = (): ISharpPluginOptions =>
pluginDefaults
/**
* Creates a transform object
*/
export const createTransformObject = (
args: ITransformArgs
): Partial<ITransformArgs> => {
const options = {
height: args.height,
width: args.width,
cropFocus: args.cropFocus,
toFormat: args.toFormat,
pngCompressionLevel:
args.pngCompressionLevel || generalArgs.pngCompressionLevel,
quality: args.quality,
jpegQuality: args.jpegQuality,
pngQuality: args.pngQuality,
webpQuality: args.webpQuality,
jpegProgressive: args.jpegProgressive || generalArgs.jpegProgressive,
grayscale: args.grayscale || generalArgs.grayscale,
rotate: args.rotate,
trim: args.trim ? args.trim : undefined,
duotone: args.duotone ? args.duotone : null,
fit: args.fit,
background: args.background,
}
// get all non falsey values
return pickBy(options, identity)
}
/**
* Used for gatsbyImageData and StaticImage only
*/
export const mergeDefaults = (
args: ISharpGatsbyImageArgs
): PluginOptionsDefaults & ISharpGatsbyImageArgs =>
doMergeDefaults(args, pluginOptions.defaults)
const customizer = <T>(objValue: unknown, srcValue: T): T | undefined =>
Array.isArray(objValue) ? srcValue : undefined
export function doMergeDefaults(
args: ISharpGatsbyImageArgs,
defaults?: PluginOptionsDefaults
): PluginOptionsDefaults & ISharpGatsbyImageArgs {
if (!defaults) {
return args
}
return mergeWith({}, defaults, args, customizer)
}
export const healOptions = (
{
defaultQuality: quality,
base64Width,
}: Pick<ISharpPluginOptions, "defaultQuality" | "base64Width">,
args: ITransformArgs,
fileExtension = ``,
defaultArgs = {}
): Partial<IGeneralArgs> & {
quality: number
} & ITransformArgs => {
const options = defaults({}, args, { quality }, defaultArgs, generalArgs)
// @ts-ignore - parseInt as safeguard, expects string tho
options.quality = parseInt(options.quality, 10)
// @ts-ignore - parseInt as safeguard, expects string tho
options.pngCompressionLevel = parseInt(options.pngCompressionLevel, 10)
// @ts-ignore - parseInt as safeguard, expects string tho
options.pngCompressionSpeed = parseInt(options.pngCompressionSpeed, 10)
options.toFormat = options.toFormat.toLowerCase()
options.toFormatBase64 = options.toFormatBase64?.toLowerCase()
options.base64Width = options.base64Width || base64Width
// when toFormat is not set we set it based on fileExtension
if (options.toFormat === ``) {
if (!fileExtension) {
throw new Error(
`toFormat seems to be empty, we need a fileExtension to set it.`
)
}
options.toFormat = fileExtension.toLowerCase()
if (fileExtension === `jpeg`) {
options.toFormat = `jpg`
}
}
// only set width to 400 if neither width nor height is passed
if (options.width === undefined && options.height === undefined) {
options.width = 400
}
if (options.width !== undefined) {
// @ts-ignore - parseInt as safeguard, expects string tho
options.width = parseInt(options.width, 10)
}
if (options.height !== undefined) {
// @ts-ignore - parseInt as safeguard, expects string tho
options.height = parseInt(options.height, 10)
}
// only set maxWidth to 800 if neither maxWidth nor maxHeight is passed
if (options.maxWidth === undefined && options.maxHeight === undefined) {
options.maxWidth = 800
} else if (options.maxWidth !== undefined) {
// @ts-ignore - parseInt as safeguard, expects string tho
options.maxWidth = parseInt(options.maxWidth, 10)
} else if (options.maxHeight !== undefined) {
// @ts-ignore - parseInt as safeguard, expects string tho
options.maxHeight = parseInt(options.maxHeight, 10)
}
;[`width`, `height`, `maxWidth`, `maxHeight`].forEach(prop => {
if (typeof options[prop] !== `undefined` && options[prop] < 1) {
throw new Error(
`${prop} has to be a positive int larger than zero (> 0), now it's ${options[prop]}`
)
}
})
return options
}
/**
* Removes all default values so we have the smallest transform args
*/
export const removeDefaultValues = (
args: ITransformArgs,
pluginOptions: ISharpPluginOptions
): Partial<ITransformArgs> => {
const options = {
height: args.height,
width: args.width,
cropFocus: args.cropFocus,
toFormat: args.toFormat,
pngCompressionLevel:
args.pngCompressionLevel !== generalArgs.pngCompressionLevel
? args.pngCompressionLevel
: undefined,
quality:
args.quality !== pluginOptions.defaultQuality ? args.quality : undefined,
jpegQuality: args.jpegQuality,
pngQuality: args.pngQuality,
webpQuality: args.webpQuality,
jpegProgressive:
args.jpegProgressive !== generalArgs.jpegProgressive
? args.jpegProgressive
: undefined,
grayscale:
args.grayscale !== generalArgs.grayscale ? args.grayscale : undefined,
rotate: args.rotate !== generalArgs.rotate ? args.rotate : undefined,
trim: args.trim ? args.trim : undefined,
duotone: args.duotone || undefined,
fit: args.fit,
background: args.background,
}
return omitBy(options, isNil)
}