mirror of
https://github.com/lucide-icons/lucide.git
synced 2026-11-12 13:38:32 +00:00
* cleanup scripts * Move helpers to package * Fixes scripts * Fix scripts * Formatting * Fix helpers import paths * Remove lucide-figma * Rename helpers package * Fix build * formatting * Adjust main build-icons file * Add export casing * Adds `exportModuleNameCasing` fro lab project * format files * Bump package version @lucide/build-icons * Revert changes in icons * Revert changes in PR yml * Fix lint issues * Fix site build * fix lint errors * Attempt fix linting * Fix lint errors
66 lines
1.5 KiB
JavaScript
66 lines
1.5 KiB
JavaScript
import { optimize } from 'svgo';
|
|
import * as prettier from 'prettier';
|
|
import { parseSync, stringify } from 'svgson';
|
|
import DEFAULT_ATTRS from '@lucide/build-icons/render/default-attrs.json' assert { type: 'json' };
|
|
|
|
/**
|
|
* Optimize SVG with `svgo`.
|
|
* @param {string} svg - An SVG string.
|
|
* @returns {Promise<string>} An optimized svg
|
|
*/
|
|
async function optimizeSvg(svg, path) {
|
|
const result = optimize(svg, {
|
|
path,
|
|
plugins: [
|
|
{
|
|
name: 'preset-default',
|
|
params: {
|
|
overrides: {
|
|
convertShapeToPath: false,
|
|
mergePaths: false,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: 'removeAttrs',
|
|
params: {
|
|
attrs: '(fill|stroke.*)',
|
|
},
|
|
},
|
|
],
|
|
});
|
|
|
|
return result.data;
|
|
}
|
|
|
|
/**
|
|
* Set default attibutes on SVG.
|
|
* @param {string} svg - An SVG string.
|
|
* @returns {string} An SVG string, included with the default attributes.
|
|
*/
|
|
function setAttrs(svg) {
|
|
const contents = parseSync(svg);
|
|
|
|
contents.attributes = DEFAULT_ATTRS;
|
|
|
|
return stringify(contents);
|
|
}
|
|
|
|
/**
|
|
* Process SVG string.
|
|
* @param {string} svg An SVG string.
|
|
* @returns {Promise<string>} An optimized svg
|
|
*/
|
|
function processSvg(svg, path) {
|
|
return (
|
|
optimizeSvg(svg, path)
|
|
.then(setAttrs)
|
|
.then((optimizedSvg) => prettier.format(optimizedSvg, { parser: 'babel' }))
|
|
// remove semicolon inserted by prettier
|
|
// because prettier thinks it's formatting JSX not HTML
|
|
.then((svg) => svg.replace(/;/g, ''))
|
|
);
|
|
}
|
|
|
|
export default processSvg;
|