Skip to content

Commit b47746b

Browse files
perf(web): load project icons as one chunk instead of ~1,700 (pingdotgg#20147)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 9b150f7 commit b47746b

6 files changed

Lines changed: 72 additions & 23 deletions

File tree

‎apps/web/src/components/ProjectFavicon.test.tsx‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ vi.mock("react", async (importOriginal) => {
5353
});
5454

5555
vi.mock("react/compiler-runtime", () => ({ c: hooks.useMemoCache }));
56-
vi.mock("lucide-react/dynamic", () => ({
57-
DynamicIcon: "dynamic-icon",
58-
iconNames: ["alarm-clock", "folder-code"],
59-
}));
6056
vi.mock("@effect/atom-react", () => ({
6157
useAtomValue: () => testState.faviconUrl,
6258
}));

‎apps/web/src/components/ProjectFavicon.tsx‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,24 @@ import {
44
isProjectFaviconFallbackUrl,
55
} from "@t3tools/shared/projectFavicon";
66
import { FolderCodeIcon } from "lucide-react";
7-
import type { IconName } from "lucide-react/dynamic";
87
import type { ComponentType } from "react";
9-
import { lazy, Suspense, useState } from "react";
8+
import { createElement, lazy, Suspense, useState } from "react";
109
import { useAtomValue } from "@effect/atom-react";
1110
import { projectFaviconUrlAtom } from "../state/assets";
1211
import { deriveProjectIdentity } from "../projectIdentity";
1312
import { projectIconColorClassName } from "../projectIconColors";
1413
import { ProjectMonogram } from "./ProjectMonogram";
1514
import { cn } from "~/lib/utils";
1615

17-
const DynamicIcon = lazy(() =>
18-
import("lucide-react/dynamic").then((module) => ({ default: module.DynamicIcon })),
16+
// All lucide icons load as one chunk, only once a project uses a lucide override.
17+
const LucideProjectIcon = lazy(() =>
18+
import("../lib/lucideIconByName").then(({ lucideIconByName }) => ({
19+
default: function LucideProjectIcon(props: { name: string; className: string }) {
20+
return createElement(lucideIconByName(props.name) ?? FolderCodeIcon, {
21+
className: props.className,
22+
});
23+
},
24+
})),
1925
);
2026

2127
function DynamicProjectIconFallback() {
@@ -71,10 +77,9 @@ export function ProjectFavicon(input: {
7177
return (
7278
<span aria-hidden="true" className={iconClassName}>
7379
<Suspense fallback={<DynamicProjectIconFallback />}>
74-
<DynamicIcon
75-
name={project.projectIcon.name as IconName}
80+
<LucideProjectIcon
81+
name={project.projectIcon.name}
7682
className={cn("size-full", colorClassName)}
77-
fallback={DynamicProjectIconFallback}
7883
/>
7984
</Suspense>
8085
</span>

‎apps/web/src/components/settings/ProjectIconPickerDialog.tsx‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import {
66
type ProjectIconColor,
77
type ProjectIconOverride,
88
} from "@t3tools/contracts";
9-
import { DynamicIcon, type IconName } from "lucide-react/dynamic";
10-
import { useEffect, useMemo, useRef, useState } from "react";
9+
import { createElement, useEffect, useMemo, useRef, useState } from "react";
1110
import {
1211
filterProjectIconNames,
1312
firstEmoji,
@@ -16,6 +15,7 @@ import {
1615
projectIconColorClassName,
1716
} from "../../projectIconOptions";
1817
import { cn } from "~/lib/utils";
18+
import { lucideIconByName } from "../../lib/lucideIconByName";
1919
import { Button } from "../ui/button";
2020
import {
2121
Dialog,
@@ -30,9 +30,14 @@ import { Input } from "../ui/input";
3030
import { ScrollArea } from "../ui/scroll-area";
3131
import { Toggle, ToggleGroup } from "../ui/toggle-group";
3232

33-
const DEFAULT_ICON: IconName = "folder-code";
33+
const DEFAULT_ICON = "folder-code";
3434
const isMonogramText = Schema.is(ProjectMonogramText);
3535

36+
function PickerIcon({ name }: { name: string }) {
37+
const icon = lucideIconByName(name);
38+
return icon ? createElement(icon, { className: "size-5" }) : null;
39+
}
40+
3641
function iconLabel(name: string): string {
3742
return name
3843
.split("-")
@@ -55,8 +60,8 @@ export function ProjectIconPickerDialog({
5560
}) {
5661
const automatic = deriveProjectIdentity(projectName);
5762
const [mode, setMode] = useState<ProjectIconOverride["kind"]>(current?.kind ?? "lucide");
58-
const [iconName, setIconName] = useState<IconName>(
59-
current?.kind === "lucide" ? (current.name as IconName) : DEFAULT_ICON,
63+
const [iconName, setIconName] = useState<string>(
64+
current?.kind === "lucide" ? current.name : DEFAULT_ICON,
6065
);
6166
const [color, setColor] = useState<ProjectIconColor>(
6267
current && current.kind !== "emoji" ? current.color : automatic.color,
@@ -72,7 +77,7 @@ export function ProjectIconPickerDialog({
7277
useEffect(() => {
7378
if (open && !previousOpenRef.current) {
7479
setMode(current?.kind ?? "lucide");
75-
setIconName(current?.kind === "lucide" ? (current.name as IconName) : DEFAULT_ICON);
80+
setIconName(current?.kind === "lucide" ? current.name : DEFAULT_ICON);
7681
setColor(current && current.kind !== "emoji" ? current.color : automatic.color);
7782
setLetters(current?.kind === "monogram" ? current.text : automatic.monogram);
7883
setEmoji(current?.kind === "emoji" ? current.emoji : "💻");
@@ -167,7 +172,7 @@ export function ProjectIconPickerDialog({
167172
)}
168173
onClick={() => setIconName(name)}
169174
>
170-
<DynamicIcon name={name} className="size-5" />
175+
<PickerIcon name={name} />
171176
</button>
172177
))}
173178
</div>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* oxlint-disable eslint/no-restricted-imports -- Resolves saved project icons by name, so it needs the whole set. */
2+
// Project icon overrides save lucide icons by kebab-case name. Import this module
3+
// lazily: it holds every lucide icon as one chunk, where `lucide-react/dynamic`
4+
// emits a separate chunk per icon (~1,900), which dominated build output.
5+
import * as Lucide from "lucide-react";
6+
import type { LucideIcon } from "lucide-react";
7+
8+
const iconComponents = new Set<unknown>(Object.values(Lucide.icons));
9+
const lucideExports: Record<string, unknown> = Lucide;
10+
11+
// Lucide derives export names this way, so it also resolves its deprecated
12+
// aliases ("alarm-check" -> AlarmCheck) that older overrides may have saved.
13+
function toPascalName(name: string): string {
14+
return name
15+
.split("-")
16+
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
17+
.join("");
18+
}
19+
20+
// Inverse of toPascalName for canonical exports: "ArrowDown01" -> "arrow-down-01".
21+
// Lucide spells a handful of digit names differently ("arrow-down-0-1"); both
22+
// forms resolve to the same icon through toPascalName.
23+
function toKebabName(pascalName: string): string {
24+
return (pascalName.match(/[A-Z][a-z]*|[0-9]+[a-z]*/g) ?? []).join("-").toLowerCase();
25+
}
26+
27+
export const lucideIconNames: ReadonlyArray<string> = Object.keys(Lucide.icons).map(toKebabName);
28+
29+
export function lucideIconByName(name: string): LucideIcon | undefined {
30+
const icon = lucideExports[toPascalName(name)];
31+
return iconComponents.has(icon) ? (icon as LucideIcon) : undefined;
32+
}

‎apps/web/src/projectIconOptions.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { AlarmClockCheck, ArrowDown01, Layers3 } from "lucide-react";
12
import { describe, expect, it } from "vite-plus/test";
3+
import { lucideIconByName } from "./lib/lucideIconByName";
24
import {
35
filterProjectIconNames,
46
firstEmoji,
@@ -11,6 +13,15 @@ describe("projectIconOptions", () => {
1113
expect(filterProjectIconNames("alarm \tclock")).toContain("alarm-clock");
1214
});
1315

16+
it("resolves every offered icon name, including saved legacy spellings", () => {
17+
for (const name of filterProjectIconNames("")) expect(lucideIconByName(name)).toBeDefined();
18+
expect(lucideIconByName("layers-3")).toBe(Layers3);
19+
expect(lucideIconByName("arrow-down-01")).toBe(ArrowDown01);
20+
expect(lucideIconByName("arrow-down-0-1")).toBe(ArrowDown01);
21+
expect(lucideIconByName("alarm-check")).toBe(AlarmClockCheck);
22+
expect(lucideIconByName("create-lucide-icon")).toBeUndefined();
23+
});
24+
1425
it("extracts one complete emoji grapheme", () => {
1526
expect(firstEmoji(" 👩🏽‍💻 hello")).toBe("👩🏽‍💻");
1627
expect(firstEmoji("🇺🇸 project")).toBe("🇺🇸");

‎apps/web/src/projectIconOptions.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { iconNames, type IconName } from "lucide-react/dynamic";
1+
import { lucideIconNames } from "./lib/lucideIconByName";
22
export { PROJECT_ICON_COLORS, projectIconColorClassName } from "./projectIconColors";
33

4-
const POPULAR_PROJECT_ICONS = [
4+
const POPULAR_PROJECT_ICONS: ReadonlyArray<string> = [
55
"folder-code",
66
"code-2",
77
"terminal",
@@ -26,7 +26,7 @@ const POPULAR_PROJECT_ICONS = [
2626
"workflow",
2727
"wrench",
2828
"layers-3",
29-
] as const satisfies ReadonlyArray<IconName>;
29+
];
3030

3131
export const PROJECT_EMOJIS: ReadonlyArray<{ readonly emoji: string; readonly label: string }> = [
3232
{ emoji: "💻", label: "Computer" },
@@ -61,10 +61,10 @@ export const PROJECT_EMOJIS: ReadonlyArray<{ readonly emoji: string; readonly la
6161
{ emoji: "🌱", label: "Seedling" },
6262
];
6363

64-
export function filterProjectIconNames(query: string): ReadonlyArray<IconName> {
64+
export function filterProjectIconNames(query: string): ReadonlyArray<string> {
6565
const normalized = query.trim().toLowerCase().replaceAll(/\s+/g, "-");
6666
if (!normalized) return POPULAR_PROJECT_ICONS;
67-
return iconNames.filter((name) => name.includes(normalized)).slice(0, 60);
67+
return lucideIconNames.filter((name) => name.includes(normalized)).slice(0, 60);
6868
}
6969

7070
export function firstEmoji(value: string): string | null {

0 commit comments

Comments
 (0)