2024-09-04 09:54:42 +08:00

37 lines
960 B
JavaScript

import { basename, extname } from 'node:path';
import { readFileSync, statSync } from 'node:fs';
import { optimize } from 'svgo';
const presetDefault = [
{
name: 'preset-default',
params: {
overrides: {
removeViewBox: false,
},
},
},
'cleanupListOfValues',
{
name: 'removeAttrs',
params: {
attrs: '(class)',
},
},
];
export default function optimizeSvg(files) {
const optimizedSvgData = [];
for (const filePath of files) {
if (statSync(filePath).isFile() && extname(filePath) === '.svg') {
const data = readFileSync(filePath, 'utf-8');
const svgData = optimize(data, { path: filePath, plugins: presetDefault });
optimizedSvgData.push({
fileName: basename(filePath),
...svgData,
});
}
}
return Promise.all(optimizedSvgData);
}