⚡ Skip if file already exists
This commit is contained in:
parent
8a1964be84
commit
b4fb95ffe6
|
@ -18,22 +18,33 @@ import { trace } from "./util/trace"
|
|||
import { options } from "./util/sourcemap"
|
||||
import { Mutex } from "async-mutex"
|
||||
import sharp from "sharp"
|
||||
import { promises as fs } from 'fs';
|
||||
|
||||
async function compressImages(inputDir, outputDir) {
|
||||
const files = await glob(`**/*.{jpg,png}`, inputDir);
|
||||
|
||||
console.log(`Found ${files.length} files:`);
|
||||
|
||||
// Use sharp for image compression
|
||||
await Promise.all(
|
||||
files.map(async (file) => {
|
||||
const inputFilePath = path.join(inputDir, file);
|
||||
const relativeOutputPath = path.relative(inputDir, inputFilePath);
|
||||
const outputWebpPath = path.join(outputDir, relativeOutputPath.replace(/\.(jpg|png)$/, '.webp'));
|
||||
|
||||
await sharp(inputFilePath)
|
||||
.webp({ quality: 75 })
|
||||
.toFile(outputWebpPath);
|
||||
try {
|
||||
// Check if the output WebP file already exists
|
||||
await fs.access(outputWebpPath);
|
||||
|
||||
// File exists, so skip compression
|
||||
console.log(`Skipped: ${inputFilePath}`);
|
||||
} catch (error) {
|
||||
// File doesn't exist, proceed with compression
|
||||
await sharp(inputFilePath)
|
||||
.webp({ quality: 75 })
|
||||
.toFile(outputWebpPath);
|
||||
|
||||
console.log(`Compressed: ${inputFilePath}`);
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
|
|
Loading…
Reference in New Issue