react-icons-sprite 0.8.0 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { r as createCollector } from "./core-C637uIv9.mjs";
1
+ import { r as createCollector } from "./core-B0kAb7AT.mjs";
2
2
  //#region src/collector.ts
3
3
  const collector = createCollector();
4
4
  //#endregion
@@ -0,0 +1,404 @@
1
+ import { createElement } from "react";
2
+ import { renderToStaticMarkup } from "react-dom/server";
3
+ import MagicString from "magic-string";
4
+ import { Visitor, parseSync } from "oxc-parser";
5
+ //#region src/core.ts
6
+ const ICON_SOURCE = "react-icons-sprite";
7
+ const ICON_COMPONENT_NAME = "ReactIconsSpriteIcon";
8
+ const DEFAULT_ICON_SOURCES = [
9
+ /^react-icons\/[\w-]+$/,
10
+ /^lucide-react$/,
11
+ /^@radix-ui\/react-icons$/,
12
+ /^@heroicons\/react(?:\/.*)?$/,
13
+ /^@tabler\/icons-react$/,
14
+ /^phosphor-react$/,
15
+ /^@phosphor-icons\/react$/,
16
+ /^react-feather$/,
17
+ /^react-bootstrap-icons$/,
18
+ /^grommet-icons$/,
19
+ /^@remixicon\/react$/,
20
+ /^devicons-react$/,
21
+ /^@fortawesome\/react-fontawesome$/,
22
+ /^@fortawesome\/[\w-]+-svg-icons$/,
23
+ /^@mui\/icons-material(?:\/.*)?$/,
24
+ /^@carbon\/icons-react$/
25
+ ];
26
+ const sourceMatchesSupported = (source, sources = DEFAULT_ICON_SOURCES) => sources.some((re) => re.test(source));
27
+ const normalizeAlias = (pack) => {
28
+ return pack.replace(/^@/, "").replace(/[^a-zA-Z0-9]+/g, "-").replace(/^-+|-+$/g, "");
29
+ };
30
+ const computeIconId = (pack, exportName) => {
31
+ return `ri-${normalizeAlias(pack)}-${exportName}`;
32
+ };
33
+ const getRange = (node) => {
34
+ if (Array.isArray(node.range) && node.range.length === 2) return [node.range[0], node.range[1]];
35
+ if (typeof node.start === "number" && typeof node.end === "number") return [node.start, node.end];
36
+ return null;
37
+ };
38
+ const getParseLangCandidates = (filename) => {
39
+ const lower = (filename.split("?", 1)[0] ?? filename).toLowerCase();
40
+ if (lower.endsWith(".tsx")) return [
41
+ "tsx",
42
+ "ts",
43
+ "jsx",
44
+ "js"
45
+ ];
46
+ if (lower.endsWith(".ts")) return [
47
+ "ts",
48
+ "tsx",
49
+ "js",
50
+ "jsx"
51
+ ];
52
+ if (lower.endsWith(".jsx")) return [
53
+ "jsx",
54
+ "js",
55
+ "tsx",
56
+ "ts"
57
+ ];
58
+ return [
59
+ "js",
60
+ "jsx",
61
+ "ts",
62
+ "tsx"
63
+ ];
64
+ };
65
+ const parseAst = (code, filename) => {
66
+ const languages = getParseLangCandidates(filename);
67
+ let firstResult;
68
+ for (const lang of languages) {
69
+ const result = parseSync(filename, code, {
70
+ lang,
71
+ sourceType: "module",
72
+ range: true
73
+ });
74
+ firstResult ??= result;
75
+ if (result.errors.length === 0) return result;
76
+ }
77
+ return firstResult ?? parseSync(filename, code, {
78
+ lang: "tsx",
79
+ sourceType: "module",
80
+ range: true
81
+ });
82
+ };
83
+ const collectIconImports = (program, sources = DEFAULT_ICON_SOURCES) => {
84
+ const map = /* @__PURE__ */ new Map();
85
+ const body = program.body ?? [];
86
+ for (const node of body) {
87
+ if (node.type !== "ImportDeclaration") continue;
88
+ const pack = node.source?.value;
89
+ if (typeof pack !== "string" || !sourceMatchesSupported(pack, sources) || node.importKind === "type") continue;
90
+ const specifiers = node.specifiers ?? [];
91
+ for (const spec of specifiers) if (spec.type === "ImportSpecifier") {
92
+ if (spec.importKind === "type") continue;
93
+ const imported = spec.imported;
94
+ const local = spec.local;
95
+ if (imported?.type === "Identifier" && local?.type === "Identifier" && imported.name && local.name) map.set(local.name, {
96
+ pack,
97
+ exportName: imported.name,
98
+ decl: node,
99
+ spec
100
+ });
101
+ } else if (spec.type === "ImportDefaultSpecifier") {
102
+ const local = spec.local;
103
+ if (local?.type === "Identifier" && local.name) map.set(local.name, {
104
+ pack,
105
+ exportName: "default",
106
+ decl: node,
107
+ spec
108
+ });
109
+ }
110
+ }
111
+ return map;
112
+ };
113
+ const findExistingIconImport = (program) => {
114
+ let iconLocalName = ICON_COMPONENT_NAME;
115
+ let hasIconImport = false;
116
+ const body = program.body ?? [];
117
+ for (const node of body) {
118
+ if (node.type !== "ImportDeclaration") continue;
119
+ if (node.source?.value !== "react-icons-sprite") continue;
120
+ const specifiers = node.specifiers ?? [];
121
+ for (const spec of specifiers) {
122
+ if (spec.type !== "ImportSpecifier") continue;
123
+ const imported = spec.imported;
124
+ if (imported?.type === "Identifier" && imported.name === "ReactIconsSpriteIcon") {
125
+ hasIconImport = true;
126
+ const local = spec.local;
127
+ iconLocalName = local?.type === "Identifier" && local.name ? local.name : ICON_COMPONENT_NAME;
128
+ break;
129
+ }
130
+ }
131
+ if (hasIconImport) break;
132
+ }
133
+ return {
134
+ hasIconImport,
135
+ iconLocalName
136
+ };
137
+ };
138
+ const removeImportSpecifier = (ms, code, spec) => {
139
+ const range = getRange(spec);
140
+ if (!range) return;
141
+ const [start, end] = range;
142
+ let from = start;
143
+ let to = end;
144
+ let i = start - 1;
145
+ while (i >= 0 && /\s/.test(code[i])) i -= 1;
146
+ if (i >= 0 && code[i] === ",") from = i;
147
+ else {
148
+ let j = end;
149
+ while (j < code.length && /\s/.test(code[j])) j += 1;
150
+ if (j < code.length && code[j] === ",") to = j + 1;
151
+ }
152
+ ms.remove(from, to);
153
+ };
154
+ const removeEntireImport = (ms, code, decl) => {
155
+ const range = getRange(decl);
156
+ if (!range) return;
157
+ let [from, to] = range;
158
+ while (to < code.length && /[ \t]/.test(code[to])) to += 1;
159
+ if (code[to] === "\r" && code[to + 1] === "\n") to += 2;
160
+ else if (code[to] === "\n") to += 1;
161
+ ms.remove(from, to);
162
+ };
163
+ const fixIconSelfClosingSpacing = (outputCode, iconLocalName) => {
164
+ const re = new RegExp(`<${iconLocalName}([^>]*?)/>`, "g");
165
+ return outputCode.replace(re, (_match, attrs) => {
166
+ return `<${iconLocalName}${attrs.replace(/\s+$/g, "")} />`;
167
+ });
168
+ };
169
+ const transformModule = (code, id, register, sources = DEFAULT_ICON_SOURCES, options = {}) => {
170
+ const { sourceMap = false } = options;
171
+ const parsed = parseAst(code, id);
172
+ if (parsed.errors.length > 0) throw new Error(parsed.errors[0]?.message ?? `Failed to parse: ${id}`);
173
+ const { program } = parsed;
174
+ const localNameToImport = collectIconImports(program, sources);
175
+ if (localNameToImport.size === 0) return {
176
+ code,
177
+ map: null,
178
+ anyReplacements: false
179
+ };
180
+ const { hasIconImport, iconLocalName } = findExistingIconImport(program);
181
+ const ms = new MagicString(code);
182
+ const usedLocalNames = /* @__PURE__ */ new Set();
183
+ let anyReplacements = false;
184
+ new Visitor({
185
+ JSXOpeningElement(node) {
186
+ const name = node.name;
187
+ if (name?.type !== "JSXIdentifier") return;
188
+ const local = name.name;
189
+ if (!local || local === iconLocalName) return;
190
+ const meta = localNameToImport.get(local);
191
+ if (!meta) return;
192
+ let iconPack = meta.pack;
193
+ let iconExport = meta.exportName;
194
+ let usedLocal = local;
195
+ const attrs = node.attributes ?? [];
196
+ let hasIconId = false;
197
+ let iconAttr;
198
+ for (const a of attrs) {
199
+ if (a.type !== "JSXAttribute") continue;
200
+ const attrName = a.name;
201
+ if (attrName?.type === "JSXIdentifier" && attrName.name === "iconId") hasIconId = true;
202
+ if (attrName?.type === "JSXIdentifier" && attrName.name === "icon") iconAttr = a;
203
+ }
204
+ if (meta.pack === "@fortawesome/react-fontawesome" && meta.exportName === "FontAwesomeIcon" && iconAttr) {
205
+ const value = iconAttr.value;
206
+ if (value?.type === "JSXExpressionContainer") {
207
+ const expr = value.expression;
208
+ if (expr?.type === "Identifier") {
209
+ const iconLocal = expr.name;
210
+ if (iconLocal) {
211
+ const iconMeta = localNameToImport.get(iconLocal);
212
+ if (iconMeta) {
213
+ iconPack = iconMeta.pack;
214
+ iconExport = iconMeta.exportName;
215
+ usedLocal = iconLocal;
216
+ const iconAttrRange = getRange(iconAttr);
217
+ if (iconAttrRange) {
218
+ let [from, to] = iconAttrRange;
219
+ while (to < code.length && /\s/.test(code[to])) to += 1;
220
+ ms.remove(from, to);
221
+ }
222
+ }
223
+ }
224
+ }
225
+ }
226
+ }
227
+ const nameRange = getRange(name);
228
+ if (nameRange) ms.overwrite(nameRange[0], nameRange[1], iconLocalName);
229
+ if (!hasIconId) {
230
+ const idValue = computeIconId(iconPack, iconExport);
231
+ const insertPos = nameRange?.[1];
232
+ if (typeof insertPos === "number") ms.appendLeft(insertPos, ` iconId="${idValue}"`);
233
+ }
234
+ usedLocalNames.add(local);
235
+ if (usedLocal !== local) usedLocalNames.add(usedLocal);
236
+ anyReplacements = true;
237
+ register(iconPack, iconExport);
238
+ },
239
+ JSXClosingElement(node) {
240
+ const name = node.name;
241
+ if (name?.type !== "JSXIdentifier") return;
242
+ const local = name.name;
243
+ if (!local || local === iconLocalName) return;
244
+ if (!localNameToImport.get(local)) return;
245
+ const nameRange = getRange(name);
246
+ if (nameRange) ms.overwrite(nameRange[0], nameRange[1], iconLocalName);
247
+ }
248
+ }).visit(program);
249
+ if (!anyReplacements) return {
250
+ code,
251
+ map: null,
252
+ anyReplacements: false
253
+ };
254
+ const declToAllSpecs = /* @__PURE__ */ new Map();
255
+ const declToUsedSpecs = /* @__PURE__ */ new Map();
256
+ for (const { decl, spec } of localNameToImport.values()) {
257
+ const declNode = decl;
258
+ const specNode = spec;
259
+ const all = declToAllSpecs.get(declNode) ?? [];
260
+ if (all.length === 0) {
261
+ const specifiers = declNode.specifiers ?? [];
262
+ for (const oneSpec of specifiers) if (oneSpec.type === "ImportSpecifier" || oneSpec.type === "ImportDefaultSpecifier") all.push(oneSpec);
263
+ declToAllSpecs.set(declNode, all);
264
+ }
265
+ const localName = specNode.local;
266
+ if (!localName?.name || !usedLocalNames.has(localName.name)) continue;
267
+ const used = declToUsedSpecs.get(declNode) ?? [];
268
+ used.push(specNode);
269
+ declToUsedSpecs.set(declNode, used);
270
+ }
271
+ for (const [declNode, usedSpecs] of declToUsedSpecs.entries()) {
272
+ const allSpecs = declToAllSpecs.get(declNode) ?? [];
273
+ if (allSpecs.length > 0 && usedSpecs.length >= allSpecs.length) {
274
+ removeEntireImport(ms, code, declNode);
275
+ continue;
276
+ }
277
+ const byStartDesc = [...usedSpecs].sort((a, b) => {
278
+ const aRange = getRange(a);
279
+ const bRange = getRange(b);
280
+ if (!aRange || !bRange) return 0;
281
+ return bRange[0] - aRange[0];
282
+ });
283
+ for (const usedSpec of byStartDesc) removeImportSpecifier(ms, code, usedSpec);
284
+ }
285
+ if (!hasIconImport) ms.prepend(`import { ${iconLocalName} } from "${ICON_SOURCE}";\n`);
286
+ const transformedCode = ms.toString();
287
+ return {
288
+ code: transformedCode.includes(`<${iconLocalName}`) ? fixIconSelfClosingSpacing(transformedCode, iconLocalName) : transformedCode,
289
+ map: sourceMap ? (() => {
290
+ const rawMap = ms.generateMap({
291
+ source: id,
292
+ includeContent: true,
293
+ hires: true
294
+ });
295
+ return {
296
+ ...rawMap,
297
+ sourcesContent: rawMap.sourcesContent?.map((sourceContent) => sourceContent ?? "")
298
+ };
299
+ })() : null,
300
+ anyReplacements
301
+ };
302
+ };
303
+ const PRESENTATION_ATTRS = new Set([
304
+ "fill",
305
+ "stroke",
306
+ "stroke-width",
307
+ "stroke-linecap",
308
+ "stroke-linejoin",
309
+ "stroke-miterlimit",
310
+ "stroke-dasharray",
311
+ "stroke-dashoffset",
312
+ "stroke-opacity",
313
+ "fill-rule",
314
+ "fill-opacity",
315
+ "color",
316
+ "opacity",
317
+ "shape-rendering",
318
+ "vector-effect"
319
+ ]);
320
+ const ATTR_RE = /([a-zA-Z_:.-]+)\s*=\s*"([^"]*)"/g;
321
+ const toKebab = (s) => s.replace(/([a-z0-9])([A-Z])/g, "$1-$2").replace(/([A-Z])([A-Z][a-z])/g, "$1-$2").toLowerCase();
322
+ const resolveSpecificImportPath = (pack, exportName) => {
323
+ if (/^@mui\/icons-material(?:\/.*)?$/.test(pack)) {
324
+ if (pack.split("/").length > 2) return pack;
325
+ return `${pack}/${exportName}`;
326
+ }
327
+ if (/^@radix-ui\/react-icons$/.test(pack)) return `${pack}/${exportName}`;
328
+ if (/^@heroicons\/react\/(?:\d{2})\/(?:outline|solid)$/.test(pack)) return `${pack}/${exportName}`;
329
+ if (/^@fortawesome\/[\w-]+-svg-icons$/.test(pack)) return `${pack}/${exportName}`;
330
+ if (/^lucide-react$/.test(pack)) return `${pack}/icons/${toKebab(exportName)}`;
331
+ if (/^@phosphor-icons\/react$/.test(pack)) return `${pack}/dist/ssr/${exportName}.es.js`;
332
+ if (/^phosphor-react$/.test(pack)) return `${pack}/dist/icons/${exportName}.esm.js`;
333
+ if (/^@tabler\/icons-react$/.test(pack)) return `${pack}/dist/esm/icons/${exportName}.mjs`;
334
+ if (/^react-feather$/.test(pack)) return `${pack}/dist/icons/${toKebab(exportName)}`;
335
+ if (/^react-bootstrap-icons$/.test(pack)) return `${pack}/dist/icons/${toKebab(exportName)}`;
336
+ if (/^@carbon\/icons-react$/.test(pack)) return `${pack}/lib/${exportName}.js`;
337
+ return null;
338
+ };
339
+ const renderOneIcon = async (pack, exportName) => {
340
+ let mod;
341
+ const specificPath = resolveSpecificImportPath(pack, exportName);
342
+ if (specificPath) try {
343
+ mod = await import(
344
+ /* @vite-ignore */
345
+ specificPath
346
+ );
347
+ if (mod && "default" in mod && Object.keys(mod).length === 1) mod[exportName] = mod.default;
348
+ } catch {
349
+ mod = await import(
350
+ /* @vite-ignore */
351
+ pack
352
+ );
353
+ }
354
+ else mod = await import(
355
+ /* @vite-ignore */
356
+ pack
357
+ );
358
+ const modRecord = mod;
359
+ const Comp = modRecord[exportName] ?? modRecord.default;
360
+ if (!Comp) throw new Error(`Icon export not found: ${pack} -> ${exportName}`);
361
+ const id = computeIconId(pack, exportName);
362
+ if (pack.includes("fortawesome")) {
363
+ const [width, height, , , pathData] = Comp.icon;
364
+ return {
365
+ id,
366
+ symbol: `<symbol id="${id}" viewBox="${`0 0 ${width} ${height}`}">${(Array.isArray(pathData) ? pathData : [pathData]).map((d) => `<path d="${d}" />`).join("")}</symbol>`
367
+ };
368
+ }
369
+ const html = renderToStaticMarkup(createElement(Comp, {}));
370
+ const viewBox = html.match(/viewBox="([^"]+)"/i)?.[1] ?? "0 0 24 24";
371
+ const svgAttrsRaw = html.match(/^<svg\b([^>]*)>/i)?.[1] ?? "";
372
+ const attrs = [];
373
+ for (const [, k, v] of svgAttrsRaw.matchAll(ATTR_RE)) {
374
+ const key = k.toLowerCase();
375
+ if (PRESENTATION_ATTRS.has(key)) attrs.push(`${key}="${v}"`);
376
+ }
377
+ const inner = html.replace(/^<svg[^>]*>/i, "").replace(/<\/svg>\s*$/i, "").replace(/<svg[^>]*>/gi, "").replace(/<\/svg>/gi, "");
378
+ return {
379
+ id,
380
+ symbol: `<symbol id="${id}" viewBox="${viewBox}"${attrs.length ? ` ${attrs.join(" ")}` : ""}>${inner}</symbol>`
381
+ };
382
+ };
383
+ const buildSprite = async (icons) => {
384
+ return `<svg xmlns="http://www.w3.org/2000/svg"><defs>${(await Promise.all(Array.from(icons).map(({ pack, exportName }) => renderOneIcon(pack, exportName)))).map((r) => r.symbol).join("")}</defs></svg>`;
385
+ };
386
+ const createCollector = () => {
387
+ const set = /* @__PURE__ */ new Map();
388
+ return {
389
+ add(pack, exportName) {
390
+ set.set(`${pack}:${exportName}`, {
391
+ pack,
392
+ exportName
393
+ });
394
+ },
395
+ toList() {
396
+ return Array.from(set.values());
397
+ },
398
+ clear() {
399
+ set.clear();
400
+ }
401
+ };
402
+ };
403
+ //#endregion
404
+ export { transformModule as i, buildSprite as n, createCollector as r, DEFAULT_ICON_SOURCES as t };
@@ -1,5 +1,5 @@
1
1
  import { REACT_ICONS_SPRITE_URL_PLACEHOLDER } from "../index.mjs";
2
- import { i as transformModule, n as buildSprite, r as createCollector, t as DEFAULT_ICON_SOURCES } from "../core-C637uIv9.mjs";
2
+ import { i as transformModule, n as buildSprite, r as createCollector, t as DEFAULT_ICON_SOURCES } from "../core-B0kAb7AT.mjs";
3
3
  import { createHash } from "node:crypto";
4
4
  //#region src/vite/plugin.ts
5
5
  const reactIconsSprite = (options = {}) => {
@@ -18,7 +18,7 @@ const reactIconsSprite = (options = {}) => {
18
18
  try {
19
19
  const { code: next, map, anyReplacements } = transformModule(code, id, (pack, exportName) => {
20
20
  collector.add(pack, exportName);
21
- }, DEFAULT_ICON_SOURCES);
21
+ }, DEFAULT_ICON_SOURCES, { sourceMap: true });
22
22
  if (!anyReplacements) return null;
23
23
  return {
24
24
  code: next,
@@ -1,5 +1,5 @@
1
- import { i as transformModule } from "../core-C637uIv9.mjs";
2
- import { t as collector } from "../collector-D8c_jwWT.mjs";
1
+ import { i as transformModule } from "../core-B0kAb7AT.mjs";
2
+ import { t as collector } from "../collector-BvJbH8Da.mjs";
3
3
  //#region src/webpack/loader.ts
4
4
  const reactIconsSpriteLoader = async function(source) {
5
5
  if (this.mode === "development") return source;
@@ -1,6 +1,6 @@
1
1
  import { REACT_ICONS_SPRITE_URL_PLACEHOLDER } from "../index.mjs";
2
- import { n as buildSprite } from "../core-C637uIv9.mjs";
3
- import { t as collector } from "../collector-D8c_jwWT.mjs";
2
+ import { n as buildSprite } from "../core-B0kAb7AT.mjs";
3
+ import { t as collector } from "../collector-BvJbH8Da.mjs";
4
4
  import { createHash } from "node:crypto";
5
5
  //#region src/webpack/plugin.ts
6
6
  var ReactIconsSpriteWebpackPlugin = class {
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "$schema": "https://www.schemastore.org/package.json",
3
3
  "name": "react-icons-sprite",
4
- "version": "0.8.0",
4
+ "version": "0.9.1",
5
5
  "type": "module",
6
- "description": "A lightweight Vite and Webpack plugin for react-icons that builds a single SVG sprite and rewrites icons to <use>, reducing bundle size and runtime overhead.",
6
+ "description": "A lightweight Vite, Rsbuild and Webpack plugin for react-icons that builds a single SVG sprite and rewrites icons to <use>, reducing bundle size and runtime overhead.",
7
7
  "author": "Jure Rotar <hello@jurerotar.com>",
8
8
  "homepage": "https://github.com/jurerotar/react-icons-sprite#readme",
9
9
  "license": "MIT",
@@ -65,15 +65,11 @@
65
65
  "react-dom": ">= 16"
66
66
  },
67
67
  "dependencies": {
68
- "@babel/generator": "7.29.1",
69
- "@babel/parser": "7.29.0",
70
- "@babel/traverse": "7.29.0",
71
- "@babel/types": "7.29.0"
68
+ "magic-string": "0.30.21",
69
+ "oxc-parser": "0.119.0"
72
70
  },
73
71
  "devDependencies": {
74
72
  "@carbon/icons-react": "11.76.0",
75
- "@types/babel__generator": "7.27.0",
76
- "@types/babel__traverse": "7.28.0",
77
73
  "@types/node": "25.5.0",
78
74
  "@types/react-dom": "19.2.3",
79
75
  "@typescript/native-preview": "7.0.0-dev.20260311.1",
@@ -81,8 +77,7 @@
81
77
  "react-dom": "19.2.4",
82
78
  "tsdown": "0.21.2",
83
79
  "typescript": "5.9.3",
84
- "vite": "npm:rolldown-vite@7.3.1",
85
- "rolldown-vite": "7.3.1",
80
+ "vite": "8.0.0",
86
81
  "vitest": "4.1.0",
87
82
  "webpack": "5.105.4"
88
83
  },
@@ -1,285 +0,0 @@
1
- import { createElement } from "react";
2
- import { renderToStaticMarkup } from "react-dom/server";
3
- import * as t from "@babel/types";
4
- import { parse } from "@babel/parser";
5
- import _traverse from "@babel/traverse";
6
- import _generate from "@babel/generator";
7
- //#region src/core.ts
8
- const traverse = _traverse.default ?? _traverse;
9
- const generate = _generate.default ?? _generate;
10
- const ICON_SOURCE = "react-icons-sprite";
11
- const ICON_COMPONENT_NAME = "ReactIconsSpriteIcon";
12
- const DEFAULT_ICON_SOURCES = [
13
- /^react-icons\/[\w-]+$/,
14
- /^lucide-react$/,
15
- /^@radix-ui\/react-icons$/,
16
- /^@heroicons\/react(?:\/.*)?$/,
17
- /^@tabler\/icons-react$/,
18
- /^phosphor-react$/,
19
- /^@phosphor-icons\/react$/,
20
- /^react-feather$/,
21
- /^react-bootstrap-icons$/,
22
- /^grommet-icons$/,
23
- /^@remixicon\/react$/,
24
- /^devicons-react$/,
25
- /^@fortawesome\/react-fontawesome$/,
26
- /^@fortawesome\/[\w-]+-svg-icons$/,
27
- /^@mui\/icons-material(?:\/.*)?$/,
28
- /^@carbon\/icons-react$/
29
- ];
30
- const sourceMatchesSupported = (source, sources = DEFAULT_ICON_SOURCES) => sources.some((re) => re.test(source));
31
- const normalizeAlias = (pack) => {
32
- return pack.replace(/^@/, "").replace(/[^a-zA-Z0-9]+/g, "-").replace(/^-+|-+$/g, "");
33
- };
34
- const computeIconId = (pack, exportName) => {
35
- return `ri-${normalizeAlias(pack)}-${exportName}`;
36
- };
37
- const parseAst = (code, filename = "module.tsx") => {
38
- return parse(code, {
39
- sourceType: "module",
40
- plugins: ["jsx", "typescript"],
41
- sourceFilename: filename
42
- });
43
- };
44
- const collectIconImports = (ast, sources = DEFAULT_ICON_SOURCES) => {
45
- const map = /* @__PURE__ */ new Map();
46
- for (const node of ast.program.body) if (t.isImportDeclaration(node) && sourceMatchesSupported(node.source.value, sources) && node.importKind !== "type") {
47
- const pack = node.source.value;
48
- for (const spec of node.specifiers) if (t.isImportSpecifier(spec) && t.isIdentifier(spec.imported) && t.isIdentifier(spec.local) && spec.importKind !== "type") {
49
- const exportName = spec.imported.name;
50
- const localName = spec.local.name;
51
- map.set(localName, {
52
- pack,
53
- exportName,
54
- decl: node,
55
- spec
56
- });
57
- } else if (t.isImportDefaultSpecifier(spec) && t.isIdentifier(spec.local)) {
58
- const exportName = "default";
59
- const localName = spec.local.name;
60
- map.set(localName, {
61
- pack,
62
- exportName,
63
- decl: node,
64
- spec
65
- });
66
- }
67
- }
68
- return map;
69
- };
70
- const findExistingIconImport = (ast) => {
71
- let iconLocalName = ICON_COMPONENT_NAME;
72
- let hasIconImport = false;
73
- for (const n of ast.program.body) if (t.isImportDeclaration(n) && n.source.value === "react-icons-sprite") {
74
- for (const s of n.specifiers) if (t.isImportSpecifier(s) && t.isIdentifier(s.imported, { name: "ReactIconsSpriteIcon" })) {
75
- hasIconImport = true;
76
- iconLocalName = t.isIdentifier(s.local) ? s.local.name : ICON_COMPONENT_NAME;
77
- break;
78
- }
79
- if (hasIconImport) break;
80
- }
81
- return {
82
- hasIconImport,
83
- iconLocalName
84
- };
85
- };
86
- const replaceJsxWithSprite = (ast, localNameToImport, iconLocalName, register) => {
87
- const usedLocalNames = /* @__PURE__ */ new Set();
88
- let anyReplacements = false;
89
- const isAlreadyIcon = (name) => t.isJSXIdentifier(name) && name.name === iconLocalName;
90
- traverse(ast, {
91
- JSXOpeningElement(path) {
92
- const name = path.node.name;
93
- if (!t.isJSXIdentifier(name)) return;
94
- const local = name.name;
95
- const meta = localNameToImport.get(local);
96
- if (!meta) return;
97
- if (isAlreadyIcon(name)) return;
98
- let iconPack = meta.pack;
99
- let iconExport = meta.exportName;
100
- let usedLocal = local;
101
- if (meta.pack === "@fortawesome/react-fontawesome" && meta.exportName === "FontAwesomeIcon") {
102
- const iconAttr = path.node.attributes.find((a) => t.isJSXAttribute(a) && t.isJSXIdentifier(a.name, { name: "icon" }));
103
- if (iconAttr && t.isJSXExpressionContainer(iconAttr.value) && t.isIdentifier(iconAttr.value.expression)) {
104
- const iconLocalName = iconAttr.value.expression.name;
105
- const iconMeta = localNameToImport.get(iconLocalName);
106
- if (iconMeta) {
107
- iconPack = iconMeta.pack;
108
- iconExport = iconMeta.exportName;
109
- usedLocal = iconLocalName;
110
- path.node.attributes = path.node.attributes.filter((a) => a !== iconAttr);
111
- }
112
- }
113
- }
114
- path.node.name = t.jSXIdentifier(iconLocalName);
115
- if (!path.node.attributes.some((a) => t.isJSXAttribute(a) && t.isJSXIdentifier(a.name, { name: "iconId" }))) {
116
- const idValue = computeIconId(iconPack, iconExport);
117
- path.node.attributes.unshift(t.jSXAttribute(t.jSXIdentifier("iconId"), t.stringLiteral(idValue)));
118
- }
119
- usedLocalNames.add(local);
120
- if (usedLocal !== local) usedLocalNames.add(usedLocal);
121
- anyReplacements = true;
122
- register(iconPack, iconExport);
123
- },
124
- JSXClosingElement(path) {
125
- const name = path.node.name;
126
- if (!t.isJSXIdentifier(name)) return;
127
- const local = name.name;
128
- if (!localNameToImport.has(local)) return;
129
- if (!isAlreadyIcon(name)) path.node.name = t.jSXIdentifier(iconLocalName);
130
- }
131
- });
132
- return {
133
- usedLocalNames,
134
- anyReplacements
135
- };
136
- };
137
- const insertIconImport = (ast, iconLocalName = ICON_COMPONENT_NAME) => {
138
- const firstImportIndex = ast.program.body.findIndex((n) => t.isImportDeclaration(n));
139
- const iconImportDecl = t.importDeclaration([t.importSpecifier(t.identifier(iconLocalName), t.identifier(ICON_COMPONENT_NAME))], t.stringLiteral(ICON_SOURCE));
140
- if (firstImportIndex >= 0) ast.program.body.splice(firstImportIndex + 1, 0, iconImportDecl);
141
- else ast.program.body.unshift(iconImportDecl);
142
- };
143
- const pruneUsedSpecifiers = (ast, localNameToImport, usedLocalNames) => {
144
- for (const { decl } of new Set([...localNameToImport.values()])) decl.specifiers = decl.specifiers.filter((s) => {
145
- if ((t.isImportSpecifier(s) || t.isImportDefaultSpecifier(s)) && t.isIdentifier(s.local)) return !usedLocalNames.has(s.local.name);
146
- return true;
147
- });
148
- ast.program.body = ast.program.body.filter((n) => !t.isImportDeclaration(n) || n.specifiers.length > 0);
149
- };
150
- const generateCode = (ast, origCode, id) => {
151
- const { code, map } = generate(ast, {
152
- sourceMaps: true,
153
- sourceFileName: id
154
- }, origCode);
155
- return {
156
- code,
157
- map
158
- };
159
- };
160
- const transformModule = (code, id, register, sources = DEFAULT_ICON_SOURCES) => {
161
- const ast = parseAst(code, id);
162
- const localNameToImport = collectIconImports(ast, sources);
163
- if (localNameToImport.size === 0) return {
164
- code,
165
- map: null,
166
- anyReplacements: false
167
- };
168
- const { hasIconImport, iconLocalName } = findExistingIconImport(ast);
169
- const { usedLocalNames, anyReplacements } = replaceJsxWithSprite(ast, localNameToImport, iconLocalName, register);
170
- if (!anyReplacements) return {
171
- code,
172
- map: null,
173
- anyReplacements: false
174
- };
175
- if (!hasIconImport) insertIconImport(ast, iconLocalName);
176
- pruneUsedSpecifiers(ast, localNameToImport, usedLocalNames);
177
- return {
178
- ...generateCode(ast, code, id),
179
- anyReplacements
180
- };
181
- };
182
- const PRESENTATION_ATTRS = new Set([
183
- "fill",
184
- "stroke",
185
- "stroke-width",
186
- "stroke-linecap",
187
- "stroke-linejoin",
188
- "stroke-miterlimit",
189
- "stroke-dasharray",
190
- "stroke-dashoffset",
191
- "stroke-opacity",
192
- "fill-rule",
193
- "fill-opacity",
194
- "color",
195
- "opacity",
196
- "shape-rendering",
197
- "vector-effect"
198
- ]);
199
- const ATTR_RE = /([a-zA-Z_:.-]+)\s*=\s*"([^"]*)"/g;
200
- const toKebab = (s) => s.replace(/([a-z0-9])([A-Z])/g, "$1-$2").replace(/([A-Z])([A-Z][a-z])/g, "$1-$2").toLowerCase();
201
- const resolveSpecificImportPath = (pack, exportName) => {
202
- if (/^@mui\/icons-material(?:\/.*)?$/.test(pack)) {
203
- if (pack.split("/").length > 2) return pack;
204
- return `${pack}/${exportName}`;
205
- }
206
- if (/^@radix-ui\/react-icons$/.test(pack)) return `${pack}/${exportName}`;
207
- if (/^@heroicons\/react\/(?:\d{2})\/(?:outline|solid)$/.test(pack)) return `${pack}/${exportName}`;
208
- if (/^@fortawesome\/[\w-]+-svg-icons$/.test(pack)) return `${pack}/${exportName}`;
209
- if (/^lucide-react$/.test(pack)) return `${pack}/icons/${toKebab(exportName)}`;
210
- if (/^@phosphor-icons\/react$/.test(pack)) return `${pack}/dist/ssr/${exportName}.es.js`;
211
- if (/^phosphor-react$/.test(pack)) return `${pack}/dist/icons/${exportName}.esm.js`;
212
- if (/^@tabler\/icons-react$/.test(pack)) return `${pack}/dist/esm/icons/${exportName}.mjs`;
213
- if (/^react-feather$/.test(pack)) return `${pack}/dist/icons/${toKebab(exportName)}`;
214
- if (/^react-bootstrap-icons$/.test(pack)) return `${pack}/dist/icons/${toKebab(exportName)}`;
215
- if (/^@carbon\/icons-react$/.test(pack)) return `${pack}/lib/${exportName}.js`;
216
- return null;
217
- };
218
- const renderOneIcon = async (pack, exportName) => {
219
- let mod;
220
- const specificPath = resolveSpecificImportPath(pack, exportName);
221
- if (specificPath) try {
222
- mod = await import(
223
- /* @vite-ignore */
224
- specificPath
225
- );
226
- if (mod && "default" in mod && Object.keys(mod).length === 1) mod[exportName] = mod.default;
227
- } catch {
228
- mod = await import(
229
- /* @vite-ignore */
230
- pack
231
- );
232
- }
233
- else mod = await import(
234
- /* @vite-ignore */
235
- pack
236
- );
237
- let Comp = mod[exportName] ?? mod.default;
238
- if (pack.includes("fortawesome") && Comp && typeof Comp === "object" && "icon" in Comp && Array.isArray(Comp.icon)) {
239
- const [width, height, , , pathData] = Comp.icon;
240
- const viewBox = `0 0 ${width} ${height}`;
241
- const id = computeIconId(pack, exportName);
242
- return {
243
- id,
244
- symbol: `<symbol id="${id}" viewBox="${viewBox}">${(Array.isArray(pathData) ? pathData : [pathData]).map((d) => `<path d="${d}" />`).join("")}</symbol>`
245
- };
246
- }
247
- if (Comp && typeof Comp === "object" && "default" in Comp && !("$$typeof" in Comp)) Comp = Comp.default;
248
- if (!Comp) throw new Error(`Icon export not found: ${pack} -> ${exportName}`);
249
- const id = computeIconId(pack, exportName);
250
- const html = renderToStaticMarkup(createElement(Comp, {}));
251
- const viewBox = html.match(/viewBox="([^"]+)"/i)?.[1] ?? "0 0 24 24";
252
- const svgAttrsRaw = html.match(/^<svg\b([^>]*)>/i)?.[1] ?? "";
253
- const attrs = [];
254
- for (const [, k, v] of svgAttrsRaw.matchAll(ATTR_RE)) {
255
- const key = k.toLowerCase();
256
- if (PRESENTATION_ATTRS.has(key)) attrs.push(`${key}="${v}"`);
257
- }
258
- const inner = html.replace(/^<svg[^>]*>/i, "").replace(/<\/svg>\s*$/i, "").replace(/<svg[^>]*>/gi, "").replace(/<\/svg>/gi, "");
259
- return {
260
- id,
261
- symbol: `<symbol id="${id}" viewBox="${viewBox}"${attrs.length ? ` ${attrs.join(" ")}` : ""}>${inner}</symbol>`
262
- };
263
- };
264
- const buildSprite = async (icons) => {
265
- return `<svg xmlns="http://www.w3.org/2000/svg"><defs>${(await Promise.all(Array.from(icons).map(({ pack, exportName }) => renderOneIcon(pack, exportName)))).map((r) => r.symbol).join("")}</defs></svg>`;
266
- };
267
- const createCollector = () => {
268
- const set = /* @__PURE__ */ new Map();
269
- return {
270
- add(pack, exportName) {
271
- set.set(`${pack}:${exportName}`, {
272
- pack,
273
- exportName
274
- });
275
- },
276
- toList() {
277
- return Array.from(set.values());
278
- },
279
- clear() {
280
- set.clear();
281
- }
282
- };
283
- };
284
- //#endregion
285
- export { transformModule as i, buildSprite as n, createCollector as r, DEFAULT_ICON_SOURCES as t };