|
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" |
6 | 6 |
|
7 | 7 | 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") |
10 | 10 |
|
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 | + } |
17 | 18 | } |
18 | | - }; |
19 | 19 |
|
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 | + } |
24 | 24 |
|
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", |
29 | 29 |
|
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 | + }, |
43 | 43 |
|
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 |
48 | 48 |
|
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 | + }, |
55 | 55 |
|
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 | + } |
63 | 63 |
|
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 | +} |
71 | 71 |
|
72 | | -export default plugin; |
| 72 | +export default plugin |
0 commit comments