⚡ 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 { options } from "./util/sourcemap"
|
||||||
import { Mutex } from "async-mutex"
|
import { Mutex } from "async-mutex"
|
||||||
import sharp from "sharp"
|
import sharp from "sharp"
|
||||||
|
import { promises as fs } from 'fs';
|
||||||
|
|
||||||
async function compressImages(inputDir, outputDir) {
|
async function compressImages(inputDir, outputDir) {
|
||||||
const files = await glob(`**/*.{jpg,png}`, inputDir);
|
const files = await glob(`**/*.{jpg,png}`, inputDir);
|
||||||
|
|
||||||
console.log(`Found ${files.length} files:`);
|
console.log(`Found ${files.length} files:`);
|
||||||
|
|
||||||
// Use sharp for image compression
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
files.map(async (file) => {
|
files.map(async (file) => {
|
||||||
const inputFilePath = path.join(inputDir, file);
|
const inputFilePath = path.join(inputDir, file);
|
||||||
const relativeOutputPath = path.relative(inputDir, inputFilePath);
|
const relativeOutputPath = path.relative(inputDir, inputFilePath);
|
||||||
const outputWebpPath = path.join(outputDir, relativeOutputPath.replace(/\.(jpg|png)$/, '.webp'));
|
const outputWebpPath = path.join(outputDir, relativeOutputPath.replace(/\.(jpg|png)$/, '.webp'));
|
||||||
|
|
||||||
|
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)
|
await sharp(inputFilePath)
|
||||||
.webp({ quality: 75 })
|
.webp({ quality: 75 })
|
||||||
.toFile(outputWebpPath);
|
.toFile(outputWebpPath);
|
||||||
|
|
||||||
|
console.log(`Compressed: ${inputFilePath}`);
|
||||||
|
}
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue