-
Notifications
You must be signed in to change notification settings - Fork 1
/
process-lqip.js
43 lines (37 loc) · 1.17 KB
/
process-lqip.js
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
import lqip from 'lqip-modern';
import fs from 'fs/promises';
import path from 'path';
const lqipDir = "lqip/";
const dir = new URL("./assets/img/", import.meta.url).pathname;
async function processLQIP() {
let files = await fs.readdir(dir, {
withFileTypes: true
});
// Filter all image files
const imgFiles = files
.map(item => {
const ext = path.extname(item.name);
return {
name: item.name,
ext
};
})
.filter(({ ext }) => {
return [".png", ".jpg", ".jpeg", ".webp"].includes(ext);
});
// Remove current lqip directory
await fs.rm(path.join(dir, lqipDir), { force: true, recursive: true });
await fs.mkdir(path.join(dir, lqipDir));
for (const fileItem of imgFiles) {
const { name, ext } = fileItem;
const result = await lqip(path.join(dir, name), {
resize: 1200,
outputFormat: "webp"
});
await fs.writeFile(
path.join(dir, lqipDir, (name + "_temp").replace(ext + "_temp", "") + ".webp"),
result.content
);
}
}
processLQIP().catch(console.error);