|
1 | | -// @ts-nocheck |
| 1 | +import { imagetools, pictureFormat } from 'vite-imagetools' |
2 | 2 |
|
3 | | -import { imagetools } from 'vite-imagetools' |
4 | | -import { resize, format, setMetadata, metadataFormat, resolveConfigs } from 'imagetools-core' |
5 | | - |
6 | | -function lqip(cfg, ctx) { |
7 | | - if (cfg.lqip) { |
8 | | - const r = resize({ width: cfg.lqip }, ctx) |
9 | | - const f = format({ format: 'webp', quality: '20' }, ctx) |
10 | | - return async function (image) { |
11 | | - const img = f(r(image)) |
12 | | - const buffer = await img.toBuffer() |
13 | | - setMetadata(img, 'base64', `data:image/webp;base64,${buffer.toString('base64')}`) |
14 | | - return img |
| 3 | +function run(cfg) { |
| 4 | + return async function (metadatas) { |
| 5 | + const pic = pictureFormat()(metadatas) |
| 6 | + const lqip = (cfg && parseInt(cfg)) ?? 16 |
| 7 | + if (lqip) { |
| 8 | + const { image } = metadatas.find((i) => i.src === pic.img.src) |
| 9 | + let data |
| 10 | + if (lqip > 1) { |
| 11 | + const buf = await image.resize({ width: lqip }).toFormat('webp', { quality: 20 }).toBuffer() |
| 12 | + data = buf.toString('base64') |
| 13 | + } else { |
| 14 | + const { dominant } = await image.stats() |
| 15 | + const { r, g, b } = dominant |
| 16 | + data = `#${((1 << 24) | (r << 16) | (g << 8) | b).toString(16).slice(1)}` |
| 17 | + } |
| 18 | + pic.img.lqip = data |
15 | 19 | } |
| 20 | + return pic |
16 | 21 | } |
17 | 22 | } |
18 | 23 |
|
19 | | -function main(overrides = {}) { |
| 24 | +function main({ |
| 25 | + runDefaultDirectives = new URLSearchParams('w=480;1024;1920&format=avif;webp;jpg'), |
| 26 | + defaultDirectives = new URLSearchParams(), |
| 27 | + exclude = '{build,dist,node_modules}/**/*', |
| 28 | + extendOutputFormats = (i) => i, //noop |
| 29 | + ...rest |
| 30 | +} = {}) { |
20 | 31 | return imagetools({ |
21 | 32 | defaultDirectives: (url) => |
22 | | - url.searchParams.has('run') |
23 | | - ? new URLSearchParams('width=480;1024;1920&format=avif;webp;jpg') |
24 | | - : new URLSearchParams(''), |
25 | | - extendTransforms: (builtins) => [...builtins, lqip], |
26 | | - extendOutputFormats: (builtinOutputFormats) => ({ |
27 | | - ...builtinOutputFormats, |
28 | | - run: () => metadataFormat(['format', 'src', 'width', 'height', 'base64']) |
29 | | - }), |
30 | | - resolveConfigs: (e, f) => { |
31 | | - if (e.findIndex((i) => i[0] === 'run') > -1) { |
32 | | - const idx = e.findIndex((i) => i[0] === 'lqip') |
33 | | - const lqip = idx > -1 ? parseInt(e.splice(idx, 1)[0][1][0]) : 16 |
34 | | - const merge = new Map() |
35 | | - for (const [key, val] of e) merge.set(key, val) |
36 | | - return [...resolveConfigs([...merge], f), ...(lqip ? [{ lqip }] : [])] |
37 | | - } |
38 | | - return resolveConfigs(e, f) |
39 | | - }, |
40 | | - ...overrides |
| 33 | + url.searchParams.get('as') === 'run' ? runDefaultDirectives : defaultDirectives, |
| 34 | + extendOutputFormats: (builtins) => ({ ...extendOutputFormats(builtins), run }), |
| 35 | + exclude, |
| 36 | + ...rest |
41 | 37 | }) |
42 | 38 | } |
43 | 39 |
|
44 | | -export { main as imagetools, lqip } |
| 40 | +export { main as imagetools, run } |
0 commit comments