-
Notifications
You must be signed in to change notification settings - Fork 2
/
converttoWebP.js
36 lines (30 loc) · 996 Bytes
/
converttoWebP.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
/* eslint-disable no-console */
import fs from 'fs';
import path from 'path';
import sharp from 'sharp';
const inputFolder = 'public'; // Dossier source des images
const outputFolder = 'public'; // Dossier de sortie des images WebP
if (!fs.existsSync(outputFolder)) {
fs.mkdirSync(outputFolder, { recursive: true });
}
fs.readdir(inputFolder, (err, files) => {
if (err) {
console.error('Erreur lors de la lecture du dossier:', err);
return;
}
files
.filter(file => !file.endsWith('.svg'))
.forEach(file => {
const inputFilePath = path.join(inputFolder, file);
const outputFilePath = path.join(outputFolder, `${path.parse(file).name}.webp`);
sharp(inputFilePath)
.toFormat('webp')
.toFile(outputFilePath, err => {
if (err) {
console.error(`Erreur lors de la conversion de ${file}:`, err);
} else {
console.log(`Converti: ${file} -> ${outputFilePath}`);
}
});
});
});