39 lines
1.1 KiB
JavaScript
39 lines
1.1 KiB
JavaScript
// tools/svg-to-png.js
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const sharp = require('sharp');
|
|
|
|
const srcDir = path.join(__dirname, '..', 'static', 'svg'); // 源 svg 目录(按需修改)
|
|
const outDir = path.join(__dirname, '..', 'static', 'png'); // 输出目录(按需修改)
|
|
const sizes = [128]; // 你想导出的尺寸(可只留一个)
|
|
|
|
if (!fs.existsSync(outDir)) fs.mkdirSync(outDir, { recursive: true });
|
|
|
|
function walk(dir){
|
|
return fs.readdirSync(dir, { withFileTypes: true }).flatMap(d => {
|
|
const full = path.join(dir, d.name);
|
|
if (d.isDirectory()) return walk(full);
|
|
return full;
|
|
});
|
|
}
|
|
|
|
const files = walk(srcDir).filter(f => f.endsWith('.svg'));
|
|
if (!files.length) {
|
|
console.log('No SVG files found in', srcDir);
|
|
process.exit(0);
|
|
}
|
|
|
|
(async () => {
|
|
for (const f of files) {
|
|
const name = path.basename(f, '.svg');
|
|
for (const size of sizes) {
|
|
const out = path.join(outDir, `${name}-${size}.png`);
|
|
await sharp(f)
|
|
.resize(size)
|
|
.png({ quality: 90 })
|
|
.toFile(out);
|
|
console.log('wrote', out);
|
|
}
|
|
}
|
|
console.log('Done');
|
|
})(); |