From 5fc65e1f1d086f78908d044f9b0f7488eed126ce Mon Sep 17 00:00:00 2001 From: Eric Fennis Date: Mon, 16 Nov 2026 10:25:14 +0100 Subject: [PATCH] fix(lucide-static): add icon aliases to the SVG sprite (#5200) * docs(static): remove broken sprite fragment example Symbols in the sprite only render through , so sprite.svg#name in an tag shows nothing. Point readers to the per-icon SVG files for usage instead. Co-Authored-By: Claude Opus 5.5 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 `` did not work with the sprite. Each alias is now added as a symbol that references the original icon's symbol with a nested ``, instead of duplicating its elements. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01G9u8FgapYRzb6uRJnZpW7N * Adjusted documentation --------- Co-authored-by: Claude --- docs/guide/static/svg-sprite.md | 16 ++-------- packages/lucide-static/scripts/buildLib.mts | 3 +- .../lucide-static/scripts/generateSprite.mts | 30 ++++++++++++++++++- 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/docs/guide/static/svg-sprite.md b/docs/guide/static/svg-sprite.md index 09747187a..2a61f0c30 100644 --- a/docs/guide/static/svg-sprite.md +++ b/docs/guide/static/svg-sprite.md @@ -8,20 +8,10 @@ import Sandpack from '~/.vitepress/theme/components/editors/Sandpack.vue' Learn how to use SVG sprites with Lucide icons in your project, including basic usage and inline options. -:::warning Not recommended for high traffic production use -The SVG sprite includes all icons, which can significantly increase your app's bundle size and load time. For production environments, we recommend using a bundler with tree-shaking support to include only the icons you actually use. Consider using one of the framework-specific [packages](../../packages.md). -::: - -SVG sprites can be loaded as individual image, or used inline with the `` element. +The sprite is used inline: you put it in your page and reference icons with the `` element. You may also need an additional SVG loader to handle node_modules imports in your project. Check out our [codesandbox example](https://codesandbox.io/s/using-the-svg-sprite-lz1kk) for a working example. -## Basic sprite usage - -SVG sprites can be imported directory in img tags, and select the icon with
the `#{icon-name}` syntax: - -```html - -``` +To use the sprite, add it inline and reference icons with ``, as shown below. ## Inline usage @@ -104,5 +94,5 @@ import sprite from "lucide-static/sprite.svg"; document.getElementById('sprite').innerHTML = sprite; ``` - ::: + diff --git a/packages/lucide-static/scripts/buildLib.mts b/packages/lucide-static/scripts/buildLib.mts index 091b6feca..82529d5be 100644 --- a/packages/lucide-static/scripts/buildLib.mts +++ b/packages/lucide-static/scripts/buildLib.mts @@ -42,12 +42,13 @@ const aliases = (await Promise.all( return aliases.map(alias => ({ ...svg, name: typeof alias === 'string' ? alias : alias.name, + aliasOf: svgName, })); }) )).flat(); await Promise.all([ - generateSprite(svgs, PACKAGE_DIR, license), + generateSprite(svgs, aliases, PACKAGE_DIR, license), generateIconNodes(svgs, PACKAGE_DIR), copyIcons([...svgs, ...aliases], PACKAGE_DIR, license), ]); diff --git a/packages/lucide-static/scripts/generateSprite.mts b/packages/lucide-static/scripts/generateSprite.mts index 0c8b5b90f..bb492bc4e 100644 --- a/packages/lucide-static/scripts/generateSprite.mts +++ b/packages/lucide-static/scripts/generateSprite.mts @@ -3,8 +3,14 @@ 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 ) { @@ -19,6 +25,28 @@ export default async function generateSprite( children: parsedSvg.children, })); + // Aliases reference the original icon's symbol instead of duplicating its elements. + const aliasSymbols = aliases.map(({ 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', @@ -31,7 +59,7 @@ export default async function generateSprite( { name: 'defs', type: 'element', - children: symbols, + children: [...symbols, ...aliasSymbols], value: '', } as INode, ],