43 lines
982 B
JavaScript
43 lines
982 B
JavaScript
import fs from 'node:fs/promises';
|
|
import path from 'node:path';
|
|
|
|
const distDir = 'dist';
|
|
|
|
async function walk(dir) {
|
|
const entries = await fs.readdir(dir, { withFileTypes: true });
|
|
const files = await Promise.all(
|
|
entries.map(async (entry) => {
|
|
const fullPath = path.join(dir, entry.name);
|
|
if (entry.isDirectory()) {
|
|
return walk(fullPath);
|
|
}
|
|
return fullPath;
|
|
}),
|
|
);
|
|
|
|
return files.flat();
|
|
}
|
|
|
|
try {
|
|
const files = await walk(distDir);
|
|
const pngFiles = files.filter((filePath) => filePath.toLowerCase().endsWith('.png'));
|
|
|
|
let removed = 0;
|
|
|
|
for (const pngFile of pngFiles) {
|
|
const webpFile = pngFile.replace(/\.png$/i, '.webp');
|
|
|
|
try {
|
|
await fs.access(webpFile);
|
|
await fs.unlink(pngFile);
|
|
removed += 1;
|
|
} catch {
|
|
// Keep PNGs that have no optimized WebP counterpart.
|
|
}
|
|
}
|
|
|
|
console.log(`Pruned ${removed} PNG files from dist.`);
|
|
} catch {
|
|
console.log('No dist directory to prune.');
|
|
}
|