Skip to content

Commit be9ade2

Browse files
committed
refactor(plugins): tidy convertToWebp formatting and use plugin logger
1 parent 404770c commit be9ade2

1 file changed

Lines changed: 60 additions & 60 deletions

File tree

plugins/convertToWebp.ts

Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,72 @@
1-
import { globSync } from "node:fs";
2-
import sharp from "sharp";
3-
import path from "node:path";
4-
import { access, mkdir, rm } from "node:fs/promises";
5-
import { PluginOption } from "vite";
1+
import { globSync } from "node:fs"
2+
import sharp from "sharp"
3+
import path from "node:path"
4+
import { access, mkdir, rm } from "node:fs/promises"
5+
import { PluginOption } from "vite"
66

77
const plugin: (webpOptions: sharp.WebpOptions) => PluginOption = (webpOptions) => {
8-
const generated: string[] = [];
9-
const cacheRoot = path.resolve(process.cwd(), ".vite-webp-cache");
8+
const generated: string[] = []
9+
const cacheRoot = path.resolve(process.cwd(), ".vite-webp-cache")
1010

11-
const pathExists = async (file: string): Promise<boolean> => {
12-
try {
13-
await access(file);
14-
return true;
15-
} catch {
16-
return false;
11+
const pathExists = async (file: string): Promise<boolean> => {
12+
try {
13+
await access(file)
14+
return true
15+
} catch {
16+
return false
17+
}
1718
}
18-
};
1919

20-
const toWebpCachePath = (file: string): string => {
21-
const relative = path.relative(process.cwd(), file);
22-
return path.join(cacheRoot, relative).replace(/\.(jpg|jpeg|png)$/i, ".webp");
23-
};
20+
const toWebpCachePath = (file: string): string => {
21+
const relative = path.relative(process.cwd(), file)
22+
return path.join(cacheRoot, relative).replace(/\.(jpg|jpeg|png)$/i, ".webp")
23+
}
2424

25-
return {
26-
name: "convert-to-webp",
27-
apply: "build",
28-
enforce: "pre",
25+
return {
26+
name: "convert-to-webp",
27+
apply: "build",
28+
enforce: "pre",
2929

30-
buildStart: async () => {
31-
const files = globSync("src/**/*.{jpg,jpeg,png}");
32-
for (const file of files) {
33-
const out = toWebpCachePath(path.resolve(file));
34-
await mkdir(path.dirname(out), { recursive: true });
35-
const pathExistsInCache = await pathExists(out);
36-
if (!pathExistsInCache) {
37-
await sharp(file).webp(webpOptions).toFile(out);
38-
generated.push(out);
39-
console.log(`✓ ${path.basename(file)} → webp`);
40-
}
41-
}
42-
},
30+
async buildStart() {
31+
const files = globSync("src/**/*.{jpg,jpeg,png}")
32+
for (const file of files) {
33+
const out = toWebpCachePath(path.resolve(file))
34+
await mkdir(path.dirname(out), { recursive: true })
35+
const pathExistsInCache = await pathExists(out)
36+
if (!pathExistsInCache) {
37+
await sharp(file).webp(webpOptions).toFile(out)
38+
generated.push(out)
39+
this.info(`✓ ${path.basename(file)} → webp`)
40+
}
41+
}
42+
},
4343

44-
resolveId: async function (source, importer) {
45-
if (/\.(jpg|jpeg|png)$/i.test(source) && importer) {
46-
const resolved = await this.resolve(source, importer, { skipSelf: true });
47-
if (!resolved?.id) return;
44+
resolveId: async function (source, importer) {
45+
if (/\.(jpg|jpeg|png)$/i.test(source) && importer) {
46+
const resolved = await this.resolve(source, importer, { skipSelf: true })
47+
if (!resolved?.id) return
4848

49-
const cleanId = resolved.id.split("?")[0];
50-
const webp = toWebpCachePath(path.resolve(cleanId));
51-
const isWebpPathValid = await pathExists(webp);
52-
if (isWebpPathValid) return webp;
53-
}
54-
},
49+
const cleanId = resolved.id.split("?")[0]
50+
const webp = toWebpCachePath(path.resolve(cleanId))
51+
const isWebpPathValid = await pathExists(webp)
52+
if (isWebpPathValid) return webp
53+
}
54+
},
5555

56-
closeBundle: async () => {
57-
for (const file of generated) {
58-
if (await pathExists(file)) {
59-
await rm(file, { force: true });
60-
console.log(`🗑 ${path.basename(file)} supprimé`);
61-
}
62-
}
56+
async closeBundle() {
57+
for (const file of generated) {
58+
if (await pathExists(file)) {
59+
await rm(file, { force: true })
60+
this.info(`🗑 ${path.basename(file)} supprimé`)
61+
}
62+
}
6363

64-
const doesPathExist = await pathExists(cacheRoot);
65-
if (doesPathExist) {
66-
await rm(cacheRoot, { recursive: true, force: true });
67-
}
68-
},
69-
};
70-
};
64+
const doesPathExist = await pathExists(cacheRoot)
65+
if (doesPathExist) {
66+
await rm(cacheRoot, { recursive: true, force: true })
67+
}
68+
},
69+
}
70+
}
7171

72-
export default plugin;
72+
export default plugin

0 commit comments

Comments
 (0)