mirror of
https://github.com/WeBankFinTech/fes.js.git
synced 2025-04-06 03:59:53 +08:00
37 lines
960 B
JavaScript
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);
|
|
}
|