Skip to content

Commit 1073928

Browse files
author
A Kudikovs
committed
fix(lib): avoid unnecessary React fragments in JSX output of the icon definition map
(/scripts/assemble.ts): Previously, when generating icon weight maps, all SVG JSX content for icon weights was wrapped in a React fragment, even when a single top-level element was present. This update uses JSDOM to parse the SVG content and only adds a fragment when multiple top-level children exist. The result is cleaner and more efficient output that aligns with React's expectations.
1 parent 57424d5 commit 1073928

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

scripts/assemble.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import fs from "node:fs";
33
import path from "node:path";
44
import chalk from "chalk";
55
import { exec } from "node:child_process";
6+
import { JSDOM } from "jsdom";
67

78
import {
89
ALIASES,
@@ -48,6 +49,8 @@ import {
4849
function generateComponents(icons: AssetMap) {
4950
let passes = 0;
5051
let fails = 0;
52+
const dom = new JSDOM(`<!DOCTYPE html><body></body>`);
53+
const domParser = new dom.window.DOMParser();
5154

5255
if (fs.existsSync(CSR_PATH)) {
5356
fs.rmSync(CSR_PATH, { recursive: true });
@@ -71,7 +74,16 @@ import { IconWeight } from "../lib";
7174
7275
export default new Map<IconWeight, ReactElement>([
7376
${Object.entries(icon)
74-
.map(([weight, { jsx }]) => `["${weight}", <>${jsx.trim()}</>]`)
77+
.map(([weight, { jsx }]) => {
78+
const doc = domParser.parseFromString(
79+
`<svg xmlns="http://www.w3.org/2000/svg">${jsx}</svg>`,
80+
"image/svg+xml"
81+
);
82+
const svgElm = doc.querySelector("svg");
83+
const normalizedJSX =
84+
svgElm?.children?.length === 1 ? jsx.trim() : `<>${jsx.trim()}</>`;
85+
return `["${weight}", ${normalizedJSX}]`;
86+
})
7587
.join(",")}
7688
]);
7789
`;

0 commit comments

Comments
 (0)