-
Notifications
You must be signed in to change notification settings - Fork 5
/
presets.ts
103 lines (89 loc) · 3.66 KB
/
presets.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
import type { ResizeOptions } from 'sharp'
import type { ImageAttrs, ImageFormat, ImageFormats, ImageGenerator, ImagePreset, ImageSource } from './types'
import { cleanObject, mimeTypeFor } from './utils'
type FormatOptions = ImageFormats & { original?: {} }
interface WidthPresetOptions extends ImageAttrs {
density?: number
widths: (number | 'original')[]
formats: FormatOptions
resizeOptions?: ResizeOptions
withImage?: ImageGenerator
media?: string
}
export { mimeTypeFor }
export function formatPreset (options: Omit<WidthPresetOptions, 'widths'>) {
return widthPreset({ widths: ['original'], ...options })
}
export function hdPreset (options: WidthPresetOptions) {
const highDensity = widthPreset({ density: 2, media: '(-webkit-min-device-pixel-ratio: 1.5)', ...options })
const desktopWidth = Math.max(...options.widths as any) || 'original'
const desktop = widthPreset({ ...options, widths: [desktopWidth] })
return { attrs: desktop.attrs, images: highDensity.images.concat(desktop.images) }
}
export function widthPreset ({ density, widths, formats, resizeOptions, withImage, ...options }: WidthPresetOptions): ImagePreset {
const [attrs, sourceAttrs] = extractSourceAttrs(options)
return {
attrs,
images: Object.entries(formats)
.map(([format, formatOptions]) => ({
...sourceAttrs,
type: mimeTypeFor(format as ImageFormat),
srcset: widths.map(width => cleanObject({
condition: width === 'original' ? undefined : `${width}w`,
args: { preset: 'width', format, width, density, formatOptions, resizeOptions },
generate: async (image, args) => {
if (format !== 'original')
image = image.toFormat(format as ImageFormat, formatOptions)
if (width !== 'original') {
const hdWidth = density ? width * density : width
image = image.resize({ width: hdWidth, withoutEnlargement: true, ...resizeOptions })
}
return await withImage?.(image, args) || image
},
})),
})),
}
}
interface DensityPresetOptions extends ImageAttrs {
density: number[]
baseHeight?: number
baseWidth?: number
formats: FormatOptions
resizeOptions?: ResizeOptions
withImage?: ImageGenerator
media?: string
}
function multiply (quantity: number, n?: number | undefined) {
return n ? quantity * n : undefined
}
export function densityPreset ({ baseWidth, baseHeight, density, formats, resizeOptions, withImage, ...options }: DensityPresetOptions): ImagePreset {
const [attrs, sourceAttrs] = extractSourceAttrs(options)
return {
attrs,
images: Object.entries(formats)
.map(([format, formatOptions]) => ({
...sourceAttrs,
type: mimeTypeFor(format as ImageFormat),
srcset: density.map(density => cleanObject({
condition: `${density}x`,
args: { preset: 'density', format, density, baseWidth, baseHeight, formatOptions, resizeOptions },
generate: async (image, args) => {
if (format !== 'original')
image = image.toFormat(format as ImageFormat, formatOptions)
if (baseWidth || baseHeight) {
image = image.resize({
width: multiply(density, baseWidth),
height: multiply(density, baseHeight),
withoutEnlargement: true,
...resizeOptions,
})
}
return await withImage?.(image, args) || image
},
})),
})),
}
}
export function extractSourceAttrs ({ media, sizes, ...attrs }: any): [ImageAttrs, Partial<ImageSource>] {
return [cleanObject({ loading: 'lazy', ...attrs }), cleanObject({ media, sizes })]
}