feat(packages): extract build logic to @lucide/shared, still wip

This commit is contained in:
Karsa
2026-06-05 16:49:09 +02:00
parent 423afc6d03
commit f5437f0a4c
70 changed files with 1125 additions and 596 deletions
+21 -21
View File
@@ -1,28 +1,27 @@
import base64SVG from '@lucide/build-icons/utils/base64SVG';
import defineExportTemplate from '@lucide/build-icons/utils/defineExportTemplate';
export default defineExportTemplate(async ({
componentName,
iconName,
children,
getSvg,
deprecated,
deprecationReason,
}) => {
const svgContents = await getSvg();
const svgBase64 = base64SVG(svgContents);
export default defineExportTemplate(
async ({ componentName, iconName, iconData, getSvg, deprecated, deprecationReason }) => {
const svgContents = await getSvg();
const svgBase64 = base64SVG(svgContents);
// Astro doesn't need keyed children in loops
const keylessChildren = children.map((c) => {
const [element, { key, ...otherAttrs }] = c;
return [element, otherAttrs];
});
// Astro doesn't need keyed children in loops
const keylessIconData = {
...iconData,
node: iconData.node.map((child) => {
const [element, { key, ...otherAttrs }] = child;
return [element, otherAttrs];
}),
};
// TODO: build-icons' `pretty` is set to false as the prettier
// formatter uses babel which I'm not sure it supports typescript
return `
// TODO: build-icons' `pretty` is set to false as the prettier
// formatter uses babel which I'm not sure it supports typescript
return `
import createLucideIcon from '../createLucideIcon';
import type { AstroComponent } from '../types'
import type { AstroComponent, LucideIconData } from '../types'
const iconData: LucideIconData = ${JSON.stringify(keylessIconData)};
/**
* @component @name ${componentName}
@@ -35,8 +34,9 @@ import type { AstroComponent } from '../types'
* @returns {any} Astro Component
* ${deprecated ? `@deprecated ${deprecationReason}` : ''}
*/
const ${componentName} = createLucideIcon('${iconName}', ${JSON.stringify(keylessChildren)}) as AstroComponent;
const ${componentName} = createLucideIcon(iconData) as AstroComponent;
export default ${componentName};
`;
});
},
);
+31 -13
View File
@@ -1,32 +1,50 @@
---
import defaultAttributes from './defaultAttributes';
import buildLucideIconNode from './utils/buildLucideIconNode';
import type { IconProps as Props } from './types';
import { hasA11yProp } from './utils/hasA11yProp';
const {
color = 'currentColor',
size = 24,
'stroke-width': strokeWidth = 2,
absoluteStrokeWidth = false,
name,
aliases,
iconNode = [],
class: className,
...rest
} = Astro.props;
const calculatedStrokeWidth =
absoluteStrokeWidth ? (Number(strokeWidth) * 24) / Number(size) : strokeWidth;
const [, rawIconAttributes, builtIconNode = []] = buildLucideIconNode(
{
name,
aliases,
size: 24,
node: iconNode,
},
{
color,
size,
strokeWidth: calculatedStrokeWidth,
className,
hasA11yProp: hasA11yProp(rest),
},
);
const { class: iconClassName, ...builtIconAttributes } = rawIconAttributes;
const iconAttributes = {
...builtIconAttributes,
...rest,
};
---
<svg
{...{
...defaultAttributes,
width: size,
height: size,
stroke: color,
'stroke-width': absoluteStrokeWidth ? (Number(strokeWidth) * 24) / Number(size) : strokeWidth,
...(!hasA11yProp(rest) && { 'aria-hidden': 'true' }),
...rest,
}}
class:list={['lucide', className]}
{...iconAttributes}
class={iconClassName}
>
{iconNode.map(([Tag, attrs]) => <Tag {...attrs} />)}
{builtIconNode.map(([Tag, attrs]) => <Tag {...attrs} />)}
<slot />
</svg>
+6 -9
View File
@@ -1,11 +1,9 @@
import type { AstroComponentFactory } from 'astro/runtime/server/render/astro/factory.js';
import type { IconNode } from './types';
import type { LucideIconData } from './types';
import { render, renderSlot, createComponent, renderComponent } from 'astro/compiler-runtime';
import Icon from './Icon.astro';
import { mergeClasses } from './utils/mergeClasses';
import { toKebabCase } from './utils/toKebabCase';
export default (iconName: string, iconNode: IconNode): AstroComponentFactory => {
export default (iconData: LucideIconData): AstroComponentFactory => {
const Component = createComponent(
($$result, $$props: Record<string, any>, $$slots) => {
const { class: className, ...restProps } = $$props;
@@ -14,11 +12,10 @@ export default (iconName: string, iconNode: IconNode): AstroComponentFactory =>
'Icon',
Icon,
{
class: mergeClasses(
Boolean(iconName) && `lucide-${toKebabCase(iconName)}`,
Boolean(className) && className,
),
iconNode,
class: className,
name: iconData.name,
aliases: iconData.aliases,
iconNode: iconData.node,
...restProps,
},
{ default: () => render`${renderSlot($$result, $$slots['default'])}` },
+8
View File
@@ -9,6 +9,8 @@ export interface IconProps extends SVGAttributes {
'stroke-width'?: number | string;
absoluteStrokeWidth?: boolean;
class?: string;
name?: string;
aliases?: string[];
iconNode?: IconNode;
title?: string;
}
@@ -17,6 +19,12 @@ export type SVGAttributes = HTMLAttributes<'svg'>;
export type IconNode = IconNodeChild[];
export interface LucideIconData {
name: string;
node: IconNode;
aliases?: string[];
}
type IconNodeChild = [elementName: SVGElements, attrs: SVGAttributes];
// All possible svg elements according to the Astro definitions
@@ -47,7 +47,7 @@ exports[`Using lucide icon components > should adjust the size, stroke color and
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
class="lucide lucide-grid-3x3"
class="lucide lucide-grid-3x3 lucide-grid lucide-grid-3-x-3"
>
<rect width="18"
height="18"
@@ -96,7 +96,7 @@ exports[`Using lucide icon components > should not scale the strokeWidth when ab
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
class="lucide lucide-grid-3x3"
class="lucide lucide-grid-3x3 lucide-grid lucide-grid-3-x-3"
>
<rect width="18"
height="18"
@@ -165,7 +165,7 @@ exports[`Using lucide icon components > should render a component 1`] = `
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
class="lucide lucide-grid-3x3"
class="lucide lucide-grid-3x3 lucide-grid lucide-grid-3-x-3"
>
<rect width="18"
height="18"
@@ -196,7 +196,7 @@ exports[`Using lucide icon components > should render the icon with default attr
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
class="lucide lucide-grid-3x3"
class="lucide lucide-grid-3x3 lucide-grid lucide-grid-3-x-3"
>
<rect width="18"
height="18"
@@ -5,7 +5,10 @@ import { render } from './utils';
describe('Using createLucideIcon', () => {
it('should create a component from an iconNode', async () => {
const AirVent = createLucideIcon('AirVent', airVent);
const AirVent = createLucideIcon({
name: 'air-vent',
node: airVent,
});
const { container } = await render(AirVent);
expect(container.innerHTML).toBeDefined();