-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwebp.js
40 lines (30 loc) · 1.07 KB
/
webp.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
const fs = require("fs");
const webp = require('webp-converter');
const path = require("path");
webp.grant_permission();
function getFiles(dir) {
return fs.readdirSync(dir).flatMap((item) => {
const path = `${dir}/${item}`;
if (fs.statSync(path).isDirectory()) {
return getFiles(path);
}
return path;
});
}
const base = path.resolve("public");
const noConvert = ["webp", "m4v", "mp4", "mkv"];
const images = getFiles(path.resolve("public/img")).filter((img) => noConvert.indexOf(img.split(".")[1]) === -1);
const html = getFiles(path.resolve("src/templates")).filter((html) => html.endsWith(".html"));
images.forEach(async (img) => {
await webp.cwebp(img, img.split(".")[0] + ".webp");
fs.unlink(img, console.log);
});
html.forEach(async (p) =>{
let content = fs.readFileSync(p, 'utf8');
images.forEach((img) =>
{
img = img.replace(base+"\\", "/").replaceAll("\\\\", "/");
content = content.replaceAll(img, img.split(".")[0] + ".webp");
});
fs.writeFile(p, content, 'utf8', console.log);
})