Skip to content

Commit fdad588

Browse files
committed
Allow configuring the size and quality of the lqip
1 parent 8be9bf0 commit fdad588

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
A docuaurus plugin for pre-processing images to multiple formats, sizes and low quality image placeholders, replacing [ideal-image](https://docusaurus.io/docs/api/plugins/@docusaurus/plugin-ideal-image)
44

5-
> Note: this is still working in progress
6-
75
## Usage
86

97
First install

src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'node:path'
22
import type { LoadContext, Plugin, OptionValidationContext } from '@docusaurus/types'
3-
import type { LoaderOptions, Preset, SupportedOutputTypes } from './loader.js'
3+
import type { LoaderOptions, LqipFormat, Preset } from './loader.js'
44
// import { Compilation, Compiler, NormalModule, type LoaderContext } from 'webpack'
55
// import { fileURLToPath } from 'node:url'
66

@@ -13,6 +13,7 @@ export type {
1313
OutputDataForFormat,
1414
LoaderOptions,
1515
Preset,
16+
LqipFormat,
1617
} from './loader.js'
1718

1819
export type NativeIdealImageOptions = Partial<{
@@ -27,7 +28,7 @@ export type NativeIdealImageOptions = Partial<{
2728
/**
2829
* Low quality image placeholder format
2930
*/
30-
lqipFormat: SupportedOutputTypes
31+
lqipFormat: LqipFormat
3132
/**
3233
* Disable in dev mode for faster compile time
3334
*/

src/loader.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ export type Preset = {
2323
lqip?: boolean
2424
}
2525

26+
export type LqipFormat =
27+
| SupportedOutputTypes
28+
| {
29+
format: SupportedOutputTypes
30+
/** The image size to use, default to 16px */
31+
size?: number
32+
/** Encoding quality, default to 20 (1-100) */
33+
quality?: number
34+
}
35+
2636
export type LoaderOptions = {
2737
/**
2838
* File name template for output files
@@ -35,7 +45,7 @@ export type LoaderOptions = {
3545
/**
3646
* Low quality image placeholder format
3747
*/
38-
lqipFormat: SupportedOutputTypes
48+
lqipFormat: LqipFormat
3949
/**
4050
* Should disable the loader
4151
*/
@@ -221,19 +231,24 @@ function emitFile(
221231
return '__webpack_public_path__' + path
222232
}
223233

224-
async function toBase64Lqip(image: sharp.Sharp, imageType: SupportedOutputTypes) {
225-
const mimeType = MIMES[imageType]
226-
const resized = image.resize(16)
234+
async function toBase64Lqip(image: sharp.Sharp, format: LqipFormat) {
235+
const settings = typeof format === 'string' ? { format } : format
236+
const mimeType = MIMES[settings.format]
237+
const resized = image.resize(settings.size ?? 16)
227238
let output: sharp.Sharp
228239
switch (mimeType) {
229240
case 'image/jpeg':
230-
output = resized.jpeg({ quality: 20 })
241+
output = resized.jpeg({ quality: settings.quality ?? 20 })
231242
break
232243
case 'image/webp':
233-
output = resized.webp({ quality: 20, alphaQuality: 20, smartSubsample: true })
244+
output = resized.webp({
245+
quality: settings.quality ?? 20,
246+
alphaQuality: settings.quality ?? 20,
247+
smartSubsample: true,
248+
})
234249
break
235250
case 'image/avif':
236-
output = resized.avif({ quality: 20 })
251+
output = resized.avif({ quality: settings.quality ?? 20 })
237252
break
238253
}
239254
const data = await output.toBuffer()

0 commit comments

Comments
 (0)