mirror of
https://github.com/lucide-icons/lucide.git
synced 2026-12-03 16:59:42 +00:00
* docs(static): remove broken <img> sprite fragment example Symbols in the sprite only render through <use>, so sprite.svg#name in an <img> tag shows nothing. Point readers to the per-icon SVG files for <img> usage instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01G9u8FfpNbRB7XQ9kggfSA1 * feat(lucide-static): add icon aliases to the SVG sprite Aliases were only written as separate SVG files, so `<use href="#home" />` did not work with the sprite. Each alias is now added as a symbol that references the original icon's symbol with a nested `<use>`, instead of duplicating its elements. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01G9u8FgapYRzb6uRJnZpW7N * Adjusted documentation --------- Co-authored-by: Claude <noreply@anthropic.com>
76 lines
1.8 KiB
TypeScript
76 lines
1.8 KiB
TypeScript
import { type INode, stringify } from 'svgson';
|
|
import { format } from 'prettier';
|
|
import { appendFile } from '@lucide/helpers';
|
|
import { type SVGFile } from './readSvgs.mts';
|
|
|
|
export type AliasSVGFile = SVGFile & {
|
|
/** Name of the icon this alias points to. */
|
|
aliasOf: string;
|
|
};
|
|
|
|
export default async function generateSprite(
|
|
svgs: SVGFile[],
|
|
aliases: AliasSVGFile[],
|
|
packageDir: string,
|
|
license: string
|
|
) {
|
|
const symbols = svgs.map<INode>(({ name, parsedSvg }) => ({
|
|
name: 'symbol',
|
|
type: 'element',
|
|
value: '',
|
|
attributes: {
|
|
id: name,
|
|
viewBox: parsedSvg.attributes.viewBox,
|
|
},
|
|
children: parsedSvg.children,
|
|
}));
|
|
|
|
// Aliases reference the original icon's symbol instead of duplicating its elements.
|
|
const aliasSymbols = aliases.map<INode>(({ name, aliasOf, parsedSvg }) => ({
|
|
name: 'symbol',
|
|
type: 'element',
|
|
value: '',
|
|
attributes: {
|
|
id: name,
|
|
viewBox: parsedSvg.attributes.viewBox,
|
|
},
|
|
children: [
|
|
{
|
|
name: 'use',
|
|
type: 'element',
|
|
value: '',
|
|
attributes: {
|
|
href: `#${aliasOf}`,
|
|
},
|
|
children: [],
|
|
},
|
|
],
|
|
}));
|
|
|
|
const spriteSvgObject: INode = {
|
|
name: 'svg',
|
|
type: 'element',
|
|
attributes: {
|
|
xmlns: 'http://www.w3.org/2000/svg',
|
|
version: '1.1',
|
|
},
|
|
value: '',
|
|
children: [
|
|
{
|
|
name: 'defs',
|
|
type: 'element',
|
|
children: [...symbols, ...aliasSymbols],
|
|
value: '',
|
|
} as INode,
|
|
],
|
|
};
|
|
|
|
const spriteSvg = stringify(spriteSvgObject);
|
|
const prettifiedSprite = (await format(spriteSvg, { parser: 'babel' })).replace(/;/g, '');
|
|
|
|
const xmlMeta = `<?xml version="1.0" encoding="utf-8"?>\n<!-- ${license} -->\n`;
|
|
|
|
await appendFile(xmlMeta, `sprite.svg`, packageDir);
|
|
await appendFile(prettifiedSprite, `sprite.svg`, packageDir);
|
|
}
|