GeezerVison is showing github.com — perf(web): load project icons as one chunk instead of ~1,700 (#20147) · pingdotgg/t3code@b47746b
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/ProjectFavicon.test.tsx -4Lines changed: 0 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -53,10 +53,6 @@ vi.mock("react", async (importOriginal) => { | 53 | 53 | }); | 54 | 54 |
| 55 | 55 | 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 | | -})); | 60 | 56 | vi.mock("@effect/atom-react", () => ({ | 61 | 57 | useAtomValue: () => testState.faviconUrl, | 62 | 58 | })); | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/ProjectFavicon.tsx +12-7Lines changed: 12 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -4,18 +4,24 @@ import { | 4 | 4 | isProjectFaviconFallbackUrl, | 5 | 5 | } from "@t3tools/shared/projectFavicon"; | 6 | 6 | import { FolderCodeIcon } from "lucide-react"; | 7 | | -import type { IconName } from "lucide-react/dynamic"; | 8 | 7 | import type { ComponentType } from "react"; | 9 | | -import { lazy, Suspense, useState } from "react"; | | 8 | +import { createElement, lazy, Suspense, useState } from "react"; | 10 | 9 | import { useAtomValue } from "@effect/atom-react"; | 11 | 10 | import { projectFaviconUrlAtom } from "../state/assets"; | 12 | 11 | import { deriveProjectIdentity } from "../projectIdentity"; | 13 | 12 | import { projectIconColorClassName } from "../projectIconColors"; | 14 | 13 | import { ProjectMonogram } from "./ProjectMonogram"; | 15 | 14 | import { cn } from "~/lib/utils"; | 16 | 15 |
| 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 | + })), | 19 | 25 | ); | 20 | 26 |
| 21 | 27 | function DynamicProjectIconFallback() { | @@ -71,10 +77,9 @@ export function ProjectFavicon(input: { | 71 | 77 | return ( | 72 | 78 | <span aria-hidden="true" className={iconClassName}> | 73 | 79 | <Suspense fallback={<DynamicProjectIconFallback />}> | 74 | | - <DynamicIcon | 75 | | - name={project.projectIcon.name as IconName} | | 80 | + <LucideProjectIcon | | 81 | + name={project.projectIcon.name} | 76 | 82 | className={cn("size-full", colorClassName)} | 77 | | - fallback={DynamicProjectIconFallback} | 78 | 83 | /> | 79 | 84 | </Suspense> | 80 | 85 | </span> | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/components/settings/ProjectIconPickerDialog.tsx +12-7Lines changed: 12 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 6 | 6 | type ProjectIconColor, | 7 | 7 | type ProjectIconOverride, | 8 | 8 | } 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"; | 11 | 10 | import { | 12 | 11 | filterProjectIconNames, | 13 | 12 | firstEmoji, | @@ -16,6 +15,7 @@ import { | 16 | 15 | projectIconColorClassName, | 17 | 16 | } from "../../projectIconOptions"; | 18 | 17 | import { cn } from "~/lib/utils"; | | 18 | +import { lucideIconByName } from "../../lib/lucideIconByName"; | 19 | 19 | import { Button } from "../ui/button"; | 20 | 20 | import { | 21 | 21 | Dialog, | @@ -30,9 +30,14 @@ import { Input } from "../ui/input"; | 30 | 30 | import { ScrollArea } from "../ui/scroll-area"; | 31 | 31 | import { Toggle, ToggleGroup } from "../ui/toggle-group"; | 32 | 32 |
| 33 | | -const DEFAULT_ICON: IconName = "folder-code"; | | 33 | +const DEFAULT_ICON = "folder-code"; | 34 | 34 | const isMonogramText = Schema.is(ProjectMonogramText); | 35 | 35 |
| | 36 | +function PickerIcon({ name }: { name: string }) { | | 37 | + const icon = lucideIconByName(name); | | 38 | + return icon ? createElement(icon, { className: "size-5" }) : null; | | 39 | +} | | 40 | + | 36 | 41 | function iconLabel(name: string): string { | 37 | 42 | return name | 38 | 43 | .split("-") | @@ -55,8 +60,8 @@ export function ProjectIconPickerDialog({ | 55 | 60 | }) { | 56 | 61 | const automatic = deriveProjectIdentity(projectName); | 57 | 62 | 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, | 60 | 65 | ); | 61 | 66 | const [color, setColor] = useState<ProjectIconColor>( | 62 | 67 | current && current.kind !== "emoji" ? current.color : automatic.color, | @@ -72,7 +77,7 @@ export function ProjectIconPickerDialog({ | 72 | 77 | useEffect(() => { | 73 | 78 | if (open && !previousOpenRef.current) { | 74 | 79 | setMode(current?.kind ?? "lucide"); | 75 | | - setIconName(current?.kind === "lucide" ? (current.name as IconName) : DEFAULT_ICON); | | 80 | + setIconName(current?.kind === "lucide" ? current.name : DEFAULT_ICON); | 76 | 81 | setColor(current && current.kind !== "emoji" ? current.color : automatic.color); | 77 | 82 | setLetters(current?.kind === "monogram" ? current.text : automatic.monogram); | 78 | 83 | setEmoji(current?.kind === "emoji" ? current.emoji : "💻"); | @@ -167,7 +172,7 @@ export function ProjectIconPickerDialog({ | 167 | 172 | )} | 168 | 173 | onClick={() => setIconName(name)} | 169 | 174 | > | 170 | | - <DynamicIcon name={name} className="size-5" /> | | 175 | + <PickerIcon name={name} /> | 171 | 176 | </button> | 172 | 177 | ))} | 173 | 178 | </div> | |
Collapse file Copy file name to clipboard +32Lines changed: 32 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | +} |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/projectIconOptions.test.ts +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { AlarmClockCheck, ArrowDown01, Layers3 } from "lucide-react"; | 1 | 2 | import { describe, expect, it } from "vite-plus/test"; | | 3 | +import { lucideIconByName } from "./lib/lucideIconByName"; | 2 | 4 | import { | 3 | 5 | filterProjectIconNames, | 4 | 6 | firstEmoji, | @@ -11,6 +13,15 @@ describe("projectIconOptions", () => { | 11 | 13 | expect(filterProjectIconNames("alarm \tclock")).toContain("alarm-clock"); | 12 | 14 | }); | 13 | 15 |
| | 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 | + | 14 | 25 | it("extracts one complete emoji grapheme", () => { | 15 | 26 | expect(firstEmoji(" 👩🏽💻 hello")).toBe("👩🏽💻"); | 16 | 27 | expect(firstEmoji("🇺🇸 project")).toBe("🇺🇸"); | |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/projectIconOptions.ts +5-5Lines changed: 5 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -import { iconNames, type IconName } from "lucide-react/dynamic"; | | 1 | +import { lucideIconNames } from "./lib/lucideIconByName"; | 2 | 2 | export { PROJECT_ICON_COLORS, projectIconColorClassName } from "./projectIconColors"; | 3 | 3 |
| 4 | | -const POPULAR_PROJECT_ICONS = [ | | 4 | +const POPULAR_PROJECT_ICONS: ReadonlyArray<string> = [ | 5 | 5 | "folder-code", | 6 | 6 | "code-2", | 7 | 7 | "terminal", | @@ -26,7 +26,7 @@ const POPULAR_PROJECT_ICONS = [ | 26 | 26 | "workflow", | 27 | 27 | "wrench", | 28 | 28 | "layers-3", | 29 | | -] as const satisfies ReadonlyArray<IconName>; | | 29 | +]; | 30 | 30 |
| 31 | 31 | export const PROJECT_EMOJIS: ReadonlyArray<{ readonly emoji: string; readonly label: string }> = [ | 32 | 32 | { emoji: "💻", label: "Computer" }, | @@ -61,10 +61,10 @@ export const PROJECT_EMOJIS: ReadonlyArray<{ readonly emoji: string; readonly la | 61 | 61 | { emoji: "🌱", label: "Seedling" }, | 62 | 62 | ]; | 63 | 63 |
| 64 | | -export function filterProjectIconNames(query: string): ReadonlyArray<IconName> { | | 64 | +export function filterProjectIconNames(query: string): ReadonlyArray<string> { | 65 | 65 | const normalized = query.trim().toLowerCase().replaceAll(/\s+/g, "-"); | 66 | 66 | 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); | 68 | 68 | } | 69 | 69 |
| 70 | 70 | export function firstEmoji(value: string): string | null { | |
|