react-icons-sprite 0.9.1 → 1.0.0-rc.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.
- package/dist/build-sprite-BZCizCDt.mjs +73 -0
- package/dist/{collector-BvJbH8Da.mjs → collector-wdoob7qt.mjs} +1 -1
- package/dist/compute-icon-id-DKiwIqyH.mjs +73 -0
- package/dist/transform-module-Dn8lfegG.mjs +410 -0
- package/dist/vite/plugin.mjs +4 -2
- package/dist/webpack/loader.mjs +2 -2
- package/dist/webpack/plugin.mjs +2 -2
- package/package.json +12 -13
- package/dist/core-B0kAb7AT.mjs +0 -404
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { r as resolveIconImport, t as computeIconId } from "./compute-icon-id-DKiwIqyH.mjs";
|
|
2
|
+
import { createElement } from "react";
|
|
3
|
+
import { renderToStaticMarkup } from "react-dom/server";
|
|
4
|
+
//#region src/sprite/render-icon.ts
|
|
5
|
+
const SVG_INNER_RE = /<svg\b[^>]*>([\s\S]*?)<\/svg>/i;
|
|
6
|
+
const VIEWBOX_RE = /viewBox=["']([^"']+)["']/i;
|
|
7
|
+
const SVG_OPEN_RE = /<svg\b([^>]*)>/i;
|
|
8
|
+
const SVG_ATTR_RE = /([:\w-]+)=("[^"]*"|'[^']*')/g;
|
|
9
|
+
const OMITTED_SVG_ATTRIBUTES = new Set([
|
|
10
|
+
"xmlns",
|
|
11
|
+
"viewBox",
|
|
12
|
+
"width",
|
|
13
|
+
"height"
|
|
14
|
+
]);
|
|
15
|
+
const extractSymbolAttributes = (svgMarkup) => {
|
|
16
|
+
const openingAttributes = SVG_OPEN_RE.exec(svgMarkup)?.[1];
|
|
17
|
+
if (!openingAttributes) return "";
|
|
18
|
+
const attributes = [];
|
|
19
|
+
for (const [, name, value] of openingAttributes.matchAll(SVG_ATTR_RE)) {
|
|
20
|
+
if (OMITTED_SVG_ATTRIBUTES.has(name)) continue;
|
|
21
|
+
attributes.push(`${name}=${value}`);
|
|
22
|
+
}
|
|
23
|
+
return attributes.join(" ");
|
|
24
|
+
};
|
|
25
|
+
const pickExport = (moduleExports, exportName) => {
|
|
26
|
+
if (exportName === "default") return moduleExports.default;
|
|
27
|
+
return moduleExports[exportName] ?? moduleExports.default;
|
|
28
|
+
};
|
|
29
|
+
const isRenderableComponent = (value) => {
|
|
30
|
+
if (typeof value === "function") return true;
|
|
31
|
+
if (!value || typeof value !== "object") return false;
|
|
32
|
+
return "$$typeof" in value;
|
|
33
|
+
};
|
|
34
|
+
const isFontAwesomeIconDefinition = (value) => {
|
|
35
|
+
if (!value || typeof value !== "object") return false;
|
|
36
|
+
const icon = value.icon;
|
|
37
|
+
if (!Array.isArray(icon) || icon.length < 5) return false;
|
|
38
|
+
return typeof icon[0] === "number" && typeof icon[1] === "number";
|
|
39
|
+
};
|
|
40
|
+
const renderFontAwesomeIconDefinition = (iconDefinition) => {
|
|
41
|
+
const [width, height, , , svgPathData] = iconDefinition.icon;
|
|
42
|
+
return {
|
|
43
|
+
symbolBody: (Array.isArray(svgPathData) ? svgPathData : [svgPathData]).map((d) => `<path d="${d}"/>`).join(""),
|
|
44
|
+
viewBox: `0 0 ${width} ${height}`,
|
|
45
|
+
symbolAttributes: ""
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
const renderIcon = async (pack, exportName) => {
|
|
49
|
+
const iconComponent = pickExport(await import(resolveIconImport(pack, exportName)), exportName);
|
|
50
|
+
if (isFontAwesomeIconDefinition(iconComponent)) return renderFontAwesomeIconDefinition(iconComponent);
|
|
51
|
+
if (!isRenderableComponent(iconComponent)) throw new Error(`[react-icons-sprite] Unable to render icon "${exportName}" from "${pack}". Expected a React component export.`);
|
|
52
|
+
const svgMarkup = renderToStaticMarkup(createElement(iconComponent));
|
|
53
|
+
const svgInner = SVG_INNER_RE.exec(svgMarkup)?.[1];
|
|
54
|
+
if (!svgInner) throw new Error(`[react-icons-sprite] Unable to extract SVG content for "${exportName}" from "${pack}".`);
|
|
55
|
+
return {
|
|
56
|
+
symbolBody: svgInner,
|
|
57
|
+
viewBox: VIEWBOX_RE.exec(svgMarkup)?.[1] ?? "0 0 24 24",
|
|
58
|
+
symbolAttributes: extractSymbolAttributes(svgMarkup)
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
//#endregion
|
|
62
|
+
//#region src/sprite/build-sprite.ts
|
|
63
|
+
const buildSprite = async (icons) => {
|
|
64
|
+
if (!icons.length) return "<svg xmlns=\"http://www.w3.org/2000/svg\" style=\"display:none\"></svg>";
|
|
65
|
+
return `<svg xmlns="http://www.w3.org/2000/svg" style="display:none">${(await Promise.all(icons.map(async ({ pack, exportName }) => {
|
|
66
|
+
const rendered = await renderIcon(pack, exportName);
|
|
67
|
+
const id = computeIconId(pack, exportName);
|
|
68
|
+
const symbolAttributes = rendered.symbolAttributes ? ` ${rendered.symbolAttributes}` : "";
|
|
69
|
+
return `<symbol id="${id}" viewBox="${rendered.viewBox}"${symbolAttributes}>${rendered.symbolBody}</symbol>`;
|
|
70
|
+
}))).join("")}</svg>`;
|
|
71
|
+
};
|
|
72
|
+
//#endregion
|
|
73
|
+
export { buildSprite as t };
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
//#region src/collector/create-collector.ts
|
|
2
|
+
const createCollector = () => {
|
|
3
|
+
const collected = /* @__PURE__ */ new Set();
|
|
4
|
+
return {
|
|
5
|
+
add(pack, exportName) {
|
|
6
|
+
collected.add(`${pack}:${exportName}`);
|
|
7
|
+
},
|
|
8
|
+
toList() {
|
|
9
|
+
return [...collected].map((key) => {
|
|
10
|
+
const [pack, exportName] = key.split(":");
|
|
11
|
+
return {
|
|
12
|
+
pack,
|
|
13
|
+
exportName
|
|
14
|
+
};
|
|
15
|
+
});
|
|
16
|
+
},
|
|
17
|
+
clear() {
|
|
18
|
+
collected.clear();
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/utils/kebab-case.ts
|
|
24
|
+
const kebabCase = (value) => {
|
|
25
|
+
return value.replace(/([a-z0-9])([A-Z])/g, "$1-$2").replace(/[^a-z0-9]+/gi, "-").replace(/-{2,}/g, "-").replace(/^-|-$/g, "").toLowerCase();
|
|
26
|
+
};
|
|
27
|
+
//#endregion
|
|
28
|
+
//#region src/packs/icon-resolvers.ts
|
|
29
|
+
const DEFAULT_ICON_SOURCES = [
|
|
30
|
+
/^react-icons\/[\w-]+$/,
|
|
31
|
+
/^@fortawesome\/[\w-]+-svg-icons$/,
|
|
32
|
+
/^lucide-react$/,
|
|
33
|
+
/^@heroicons\/react(?:\/.*)?$/,
|
|
34
|
+
/^@tabler\/icons-react$/,
|
|
35
|
+
/^@radix-ui\/react-icons$/,
|
|
36
|
+
/^phosphor-react$/,
|
|
37
|
+
/^@phosphor-icons\/react$/,
|
|
38
|
+
/^react-feather$/,
|
|
39
|
+
/^react-bootstrap-icons$/,
|
|
40
|
+
/^grommet-icons$/,
|
|
41
|
+
/^@remixicon\/react$/,
|
|
42
|
+
/^devicons-react$/,
|
|
43
|
+
/^@mui\/icons-material(?:\/.*)?$/,
|
|
44
|
+
/^@carbon\/icons-react$/
|
|
45
|
+
];
|
|
46
|
+
const exactResolvers = {
|
|
47
|
+
"lucide-react": (pack, name) => `${pack}/dist/esm/icons/${kebabCase(name)}.js`,
|
|
48
|
+
"@radix-ui/react-icons": (pack, name) => `${pack}/${name}`,
|
|
49
|
+
"@tabler/icons-react": (pack, name) => `${pack}/dist/esm/icons/${name}.mjs`,
|
|
50
|
+
"@phosphor-icons/react": (pack, name) => `${pack}/dist/ssr/${name}.es.js`,
|
|
51
|
+
"phosphor-react": (pack, name) => `${pack}/dist/icons/${name}.esm.js`,
|
|
52
|
+
"react-feather": (pack, name) => `${pack}/dist/icons/${kebabCase(name)}`,
|
|
53
|
+
"react-bootstrap-icons": (pack, name) => `${pack}/dist/icons/${kebabCase(name)}`,
|
|
54
|
+
"@carbon/icons-react": (pack, name) => `${pack}/lib/${name}.js`
|
|
55
|
+
};
|
|
56
|
+
const resolveIconImport = (pack, exportName) => {
|
|
57
|
+
const exactResolver = exactResolvers[pack];
|
|
58
|
+
if (exactResolver) return exactResolver(pack, exportName);
|
|
59
|
+
if (/^@mui\/icons-material(?:\/.*)?$/.test(pack)) return pack.split("/").length > 2 ? pack : `${pack}/${exportName}`;
|
|
60
|
+
if (/^@heroicons\/react\/(?:\d{2})\/(?:outline|solid)$/.test(pack)) return `${pack}/${exportName}`;
|
|
61
|
+
if (/^@fortawesome\/[\w-]+-svg-icons$/.test(pack)) return `${pack}/${exportName}`;
|
|
62
|
+
return pack;
|
|
63
|
+
};
|
|
64
|
+
//#endregion
|
|
65
|
+
//#region src/utils/compute-icon-id.ts
|
|
66
|
+
const normalizePackAlias = (pack) => {
|
|
67
|
+
return kebabCase(pack.replace(/^@/, ""));
|
|
68
|
+
};
|
|
69
|
+
const computeIconId = (pack, iconName) => {
|
|
70
|
+
return `ri-${normalizePackAlias(pack)}-${iconName}`;
|
|
71
|
+
};
|
|
72
|
+
//#endregion
|
|
73
|
+
export { createCollector as i, DEFAULT_ICON_SOURCES as n, resolveIconImport as r, computeIconId as t };
|
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
import { n as DEFAULT_ICON_SOURCES, t as computeIconId } from "./compute-icon-id-DKiwIqyH.mjs";
|
|
2
|
+
import MagicString from "magic-string";
|
|
3
|
+
//#region src/transform/edit-applier.ts
|
|
4
|
+
const applyEdits = (code, edits) => {
|
|
5
|
+
const magicString = new MagicString(code);
|
|
6
|
+
for (const edit of edits) {
|
|
7
|
+
if (edit.type === "replace") magicString.overwrite(edit.from, edit.to, edit.value);
|
|
8
|
+
if (edit.type === "insert") magicString.appendLeft(edit.pos, edit.value);
|
|
9
|
+
if (edit.type === "remove") magicString.remove(edit.from, edit.to);
|
|
10
|
+
}
|
|
11
|
+
return magicString;
|
|
12
|
+
};
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region src/transform/edit-builder.ts
|
|
15
|
+
const buildEdits = (usages, componentName, usedSymbols, register) => {
|
|
16
|
+
const edits = [];
|
|
17
|
+
const iconIdCache = /* @__PURE__ */ new Map();
|
|
18
|
+
for (const usage of usages) {
|
|
19
|
+
edits.push({
|
|
20
|
+
type: "replace",
|
|
21
|
+
from: usage.range[0],
|
|
22
|
+
to: usage.range[1],
|
|
23
|
+
value: componentName
|
|
24
|
+
});
|
|
25
|
+
if (usage.kind === "opening") {
|
|
26
|
+
if (!usage.hasIconId) {
|
|
27
|
+
const key = `${usage.pack}:${usage.exportName}`;
|
|
28
|
+
let iconId = iconIdCache.get(key);
|
|
29
|
+
if (!iconId) {
|
|
30
|
+
iconId = computeIconId(usage.pack, usage.exportName);
|
|
31
|
+
iconIdCache.set(key, iconId);
|
|
32
|
+
}
|
|
33
|
+
edits.push({
|
|
34
|
+
type: "insert",
|
|
35
|
+
pos: usage.range[1],
|
|
36
|
+
value: ` iconId="${iconId}"`
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
usedSymbols.add(usage.local);
|
|
40
|
+
register(usage.pack, usage.exportName);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return edits;
|
|
44
|
+
};
|
|
45
|
+
//#endregion
|
|
46
|
+
//#region src/transform/fast-filter.ts
|
|
47
|
+
const fastFilter = (code) => {
|
|
48
|
+
if (!code.includes("<")) return false;
|
|
49
|
+
if (!code.includes("import")) return false;
|
|
50
|
+
return true;
|
|
51
|
+
};
|
|
52
|
+
//#endregion
|
|
53
|
+
//#region src/transform/transform-module.ts
|
|
54
|
+
const ICON_SOURCE = "react-icons-sprite";
|
|
55
|
+
const ICON_COMPONENT_NAME = "ReactIconsSpriteIcon";
|
|
56
|
+
const FONTAWESOME_REACT_PACK = "@fortawesome/react-fontawesome";
|
|
57
|
+
const isFontAwesomeIconPack = (pack) => {
|
|
58
|
+
return /^@fortawesome\/[\w-]+-svg-icons$/.test(pack);
|
|
59
|
+
};
|
|
60
|
+
const sourceMatches = (source, pack) => {
|
|
61
|
+
source.lastIndex = 0;
|
|
62
|
+
return source.test(pack);
|
|
63
|
+
};
|
|
64
|
+
const IMPORT_RE = /import\s+([^;]+?)\s+from\s+(['"])([^'"]+)\2\s*;?/g;
|
|
65
|
+
const trimRange = (value, offset) => {
|
|
66
|
+
let start = 0;
|
|
67
|
+
let end = value.length;
|
|
68
|
+
while (start < end && /\s/.test(value[start])) start += 1;
|
|
69
|
+
while (end > start && /\s/.test(value[end - 1])) end -= 1;
|
|
70
|
+
return start === end ? null : [offset + start, offset + end];
|
|
71
|
+
};
|
|
72
|
+
const parseNamedSpecifiers = (specifier, specifierOffset, imports) => {
|
|
73
|
+
const start = specifier.indexOf("{");
|
|
74
|
+
const end = specifier.lastIndexOf("}");
|
|
75
|
+
if (start === -1 || end === -1 || end <= start) return;
|
|
76
|
+
let segmentStart = start + 1;
|
|
77
|
+
for (let index = start + 1; index <= end; index += 1) {
|
|
78
|
+
if (index !== end && specifier[index] !== ",") continue;
|
|
79
|
+
const range = trimRange(specifier.slice(segmentStart, index), specifierOffset + segmentStart);
|
|
80
|
+
segmentStart = index + 1;
|
|
81
|
+
if (!range) continue;
|
|
82
|
+
const text = specifier.slice(range[0] - specifierOffset, range[1] - specifierOffset);
|
|
83
|
+
if (text.startsWith("type ")) continue;
|
|
84
|
+
const aliasMatch = /^(.*?)\s+as\s+([A-Za-z_$][\w$]*)$/.exec(text);
|
|
85
|
+
const exportName = aliasMatch ? aliasMatch[1].trim() : text.trim();
|
|
86
|
+
const local = aliasMatch ? aliasMatch[2] : exportName;
|
|
87
|
+
if (exportName && local) imports.push({
|
|
88
|
+
local,
|
|
89
|
+
exportName,
|
|
90
|
+
range
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
const countImportSpecifiers = (specifier) => {
|
|
95
|
+
let count = 0;
|
|
96
|
+
const braceStart = specifier.indexOf("{");
|
|
97
|
+
if ((braceStart === -1 ? specifier : specifier.slice(0, braceStart)).replace(/,$/, "").trim()) count += 1;
|
|
98
|
+
const braceEnd = specifier.lastIndexOf("}");
|
|
99
|
+
if (braceStart !== -1 && braceEnd > braceStart) {
|
|
100
|
+
const named = specifier.slice(braceStart + 1, braceEnd);
|
|
101
|
+
for (const segment of named.split(",")) if (segment.trim()) count += 1;
|
|
102
|
+
}
|
|
103
|
+
return count;
|
|
104
|
+
};
|
|
105
|
+
const scanImportsDetailed = (code, sources) => {
|
|
106
|
+
const imports = [];
|
|
107
|
+
IMPORT_RE.lastIndex = 0;
|
|
108
|
+
for (const match of code.matchAll(IMPORT_RE)) {
|
|
109
|
+
const [statement, specifier, , pack] = match;
|
|
110
|
+
if (!sources.some((source) => sourceMatches(source, pack))) continue;
|
|
111
|
+
if (specifier.trim().startsWith("type ")) continue;
|
|
112
|
+
const matchStart = match.index;
|
|
113
|
+
const specifierOffset = matchStart + statement.indexOf(specifier);
|
|
114
|
+
const specifiers = [];
|
|
115
|
+
const braceStart = specifier.indexOf("{");
|
|
116
|
+
const defaultRange = trimRange((braceStart === -1 ? specifier : specifier.slice(0, braceStart)).replace(/,$/, ""), specifierOffset);
|
|
117
|
+
if (defaultRange && !specifier.slice(defaultRange[0] - specifierOffset, defaultRange[1] - specifierOffset).startsWith("type ")) {
|
|
118
|
+
const local = specifier.slice(defaultRange[0] - specifierOffset, defaultRange[1] - specifierOffset);
|
|
119
|
+
if (local && /^[A-Za-z_$][\w$]*$/.test(local)) specifiers.push({
|
|
120
|
+
local,
|
|
121
|
+
exportName: "default",
|
|
122
|
+
range: defaultRange
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
parseNamedSpecifiers(specifier, specifierOffset, specifiers);
|
|
126
|
+
if (specifiers.length) imports.push({
|
|
127
|
+
pack,
|
|
128
|
+
declarationRange: [matchStart, matchStart + statement.length],
|
|
129
|
+
specifiers,
|
|
130
|
+
specifierCount: countImportSpecifiers(specifier)
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
return imports;
|
|
134
|
+
};
|
|
135
|
+
const buildScannedSymbolTable = (imports) => {
|
|
136
|
+
const table = /* @__PURE__ */ new Map();
|
|
137
|
+
for (const item of imports) for (const specifier of item.specifiers) table.set(specifier.local, {
|
|
138
|
+
pack: item.pack,
|
|
139
|
+
exportName: specifier.exportName
|
|
140
|
+
});
|
|
141
|
+
return table;
|
|
142
|
+
};
|
|
143
|
+
const scanSpriteIconImport = (code) => {
|
|
144
|
+
IMPORT_RE.lastIndex = 0;
|
|
145
|
+
for (const match of code.matchAll(IMPORT_RE)) {
|
|
146
|
+
const [, specifier, , source] = match;
|
|
147
|
+
if (source !== "react-icons-sprite") continue;
|
|
148
|
+
const specifiers = [];
|
|
149
|
+
parseNamedSpecifiers(specifier, match.index + match[0].indexOf(specifier), specifiers);
|
|
150
|
+
for (const item of specifiers) if (item.exportName === "ReactIconsSpriteIcon") return {
|
|
151
|
+
hasImport: true,
|
|
152
|
+
localName: item.local
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
return {
|
|
156
|
+
hasImport: false,
|
|
157
|
+
localName: ICON_COMPONENT_NAME
|
|
158
|
+
};
|
|
159
|
+
};
|
|
160
|
+
const escapeRegExp = (value) => {
|
|
161
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
162
|
+
};
|
|
163
|
+
const scanJsxIconUsages = (code, symbols) => {
|
|
164
|
+
if (!symbols.size) return [];
|
|
165
|
+
const names = [...symbols.keys()].map(escapeRegExp).join("|");
|
|
166
|
+
const tagRe = new RegExp(`<\\s*(/?)\\s*(${names})\\b`, "g");
|
|
167
|
+
const usages = [];
|
|
168
|
+
for (const match of code.matchAll(tagRe)) {
|
|
169
|
+
const [, closing, local] = match;
|
|
170
|
+
const symbol = symbols.get(local);
|
|
171
|
+
if (!symbol) continue;
|
|
172
|
+
const localStart = match.index + match[0].lastIndexOf(local);
|
|
173
|
+
const kind = closing ? "closing" : "opening";
|
|
174
|
+
let hasIconId = false;
|
|
175
|
+
if (!closing) {
|
|
176
|
+
const tagEnd = findJsxOpeningTagEnd(code, localStart + local.length);
|
|
177
|
+
hasIconId = tagEnd !== -1 && /\biconId\s*=/.test(code.slice(localStart + local.length, tagEnd));
|
|
178
|
+
}
|
|
179
|
+
usages.push({
|
|
180
|
+
local,
|
|
181
|
+
range: [localStart, localStart + local.length],
|
|
182
|
+
pack: symbol.pack,
|
|
183
|
+
exportName: symbol.exportName,
|
|
184
|
+
kind,
|
|
185
|
+
hasIconId
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
return usages;
|
|
189
|
+
};
|
|
190
|
+
const findJsxOpeningTagEnd = (code, start) => {
|
|
191
|
+
let quote = null;
|
|
192
|
+
let braceDepth = 0;
|
|
193
|
+
for (let index = start; index < code.length; index += 1) {
|
|
194
|
+
const char = code[index];
|
|
195
|
+
if (quote) {
|
|
196
|
+
if (char === quote && code[index - 1] !== "\\") quote = null;
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
if (char === "\"" || char === "'" || char === "`") {
|
|
200
|
+
quote = char;
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
if (char === "{") {
|
|
204
|
+
braceDepth += 1;
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
if (char === "}") {
|
|
208
|
+
braceDepth -= 1;
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
if (char === ">" && braceDepth === 0) return index;
|
|
212
|
+
}
|
|
213
|
+
return -1;
|
|
214
|
+
};
|
|
215
|
+
const scanFontAwesomeComponents = (code) => {
|
|
216
|
+
const locals = /* @__PURE__ */ new Set();
|
|
217
|
+
IMPORT_RE.lastIndex = 0;
|
|
218
|
+
for (const match of code.matchAll(IMPORT_RE)) {
|
|
219
|
+
const [, specifier, , source] = match;
|
|
220
|
+
if (source !== FONTAWESOME_REACT_PACK) continue;
|
|
221
|
+
const specifiers = [];
|
|
222
|
+
parseNamedSpecifiers(specifier, match.index + match[0].indexOf(specifier), specifiers);
|
|
223
|
+
for (const item of specifiers) if (item.exportName === "FontAwesomeIcon") locals.add(item.local);
|
|
224
|
+
}
|
|
225
|
+
return locals;
|
|
226
|
+
};
|
|
227
|
+
const scanFontAwesomeUsages = (code, symbols, componentLocals) => {
|
|
228
|
+
if (!componentLocals.size) return [];
|
|
229
|
+
const names = [...componentLocals].map(escapeRegExp).join("|");
|
|
230
|
+
const tagRe = new RegExp(`<\\s*(${names})\\b`, "g");
|
|
231
|
+
const usages = [];
|
|
232
|
+
for (const match of code.matchAll(tagRe)) {
|
|
233
|
+
const componentLocal = match[1];
|
|
234
|
+
const componentStart = match.index + match[0].lastIndexOf(componentLocal);
|
|
235
|
+
const tagEnd = findJsxOpeningTagEnd(code, componentStart + componentLocal.length);
|
|
236
|
+
if (tagEnd === -1) continue;
|
|
237
|
+
const attributes = code.slice(componentStart + componentLocal.length, tagEnd);
|
|
238
|
+
const iconMatch = /\sicon\s*=\s*\{\s*([A-Za-z_$][\w$]*)\s*\}/.exec(attributes);
|
|
239
|
+
if (!iconMatch || iconMatch.index === void 0) continue;
|
|
240
|
+
const symbol = symbols.get(iconMatch[1]);
|
|
241
|
+
if (!symbol || !isFontAwesomeIconPack(symbol.pack)) continue;
|
|
242
|
+
const attributeStart = componentStart + componentLocal.length + iconMatch.index;
|
|
243
|
+
usages.push({
|
|
244
|
+
componentLocal,
|
|
245
|
+
componentRange: [componentStart, componentStart + componentLocal.length],
|
|
246
|
+
iconAttributeRange: [attributeStart, attributeStart + iconMatch[0].length],
|
|
247
|
+
hasIconId: /\biconId\s*=/.test(attributes),
|
|
248
|
+
iconLocal: iconMatch[1],
|
|
249
|
+
pack: symbol.pack,
|
|
250
|
+
exportName: symbol.exportName
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
return usages;
|
|
254
|
+
};
|
|
255
|
+
const cleanupScannedImports = (code, imports, usedLocals) => {
|
|
256
|
+
const edits = [];
|
|
257
|
+
for (const item of imports) {
|
|
258
|
+
const usedSpecifiers = item.specifiers.filter((specifier) => usedLocals.has(specifier.local));
|
|
259
|
+
if (!usedSpecifiers.length) continue;
|
|
260
|
+
if (usedSpecifiers.length === item.specifiers.length && item.specifierCount === item.specifiers.length) {
|
|
261
|
+
edits.push({
|
|
262
|
+
type: "remove",
|
|
263
|
+
from: item.declarationRange[0],
|
|
264
|
+
to: extendToLineEnd(code, item.declarationRange[1])
|
|
265
|
+
});
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
for (const specifier of usedSpecifiers) {
|
|
269
|
+
const [from, to] = specifierRemovalRange(code, specifier.range);
|
|
270
|
+
edits.push({
|
|
271
|
+
type: "remove",
|
|
272
|
+
from,
|
|
273
|
+
to
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
return edits;
|
|
278
|
+
};
|
|
279
|
+
const cleanupScannedFontAwesomeComponentImports = (code, usedLocals) => {
|
|
280
|
+
const edits = [];
|
|
281
|
+
IMPORT_RE.lastIndex = 0;
|
|
282
|
+
for (const match of code.matchAll(IMPORT_RE)) {
|
|
283
|
+
const [statement, specifier, , source] = match;
|
|
284
|
+
if (source !== FONTAWESOME_REACT_PACK) continue;
|
|
285
|
+
const specifiers = [];
|
|
286
|
+
parseNamedSpecifiers(specifier, match.index + statement.indexOf(specifier), specifiers);
|
|
287
|
+
const removableSpecifiers = specifiers.filter((item) => item.exportName === "FontAwesomeIcon" && usedLocals.has(item.local));
|
|
288
|
+
if (!removableSpecifiers.length) continue;
|
|
289
|
+
if (removableSpecifiers.length === specifiers.length) {
|
|
290
|
+
edits.push({
|
|
291
|
+
type: "remove",
|
|
292
|
+
from: match.index,
|
|
293
|
+
to: extendToLineEnd(code, match.index + statement.length)
|
|
294
|
+
});
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
for (const specifier of removableSpecifiers) {
|
|
298
|
+
const [from, to] = specifierRemovalRange(code, specifier.range);
|
|
299
|
+
edits.push({
|
|
300
|
+
type: "remove",
|
|
301
|
+
from,
|
|
302
|
+
to
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
return edits;
|
|
307
|
+
};
|
|
308
|
+
const extendToLineEnd = (code, end) => {
|
|
309
|
+
let to = end;
|
|
310
|
+
while (to < code.length && /[ \t]/.test(code[to])) to += 1;
|
|
311
|
+
if (code[to] === "\r" && code[to + 1] === "\n") return to + 2;
|
|
312
|
+
if (code[to] === "\n") return to + 1;
|
|
313
|
+
return to;
|
|
314
|
+
};
|
|
315
|
+
const specifierRemovalRange = (code, [start, end]) => {
|
|
316
|
+
let from = start;
|
|
317
|
+
let to = end;
|
|
318
|
+
let before = start - 1;
|
|
319
|
+
while (before >= 0 && /\s/.test(code[before])) before -= 1;
|
|
320
|
+
if (before >= 0 && code[before] === ",") {
|
|
321
|
+
from = before;
|
|
322
|
+
return [from, to];
|
|
323
|
+
}
|
|
324
|
+
let after = end;
|
|
325
|
+
while (after < code.length && /\s/.test(code[after])) after += 1;
|
|
326
|
+
if (after < code.length && code[after] === ",") to = consumeTrailingWhitespace(code, after + 1);
|
|
327
|
+
return [from, to];
|
|
328
|
+
};
|
|
329
|
+
const consumeTrailingWhitespace = (code, start) => {
|
|
330
|
+
let to = start;
|
|
331
|
+
while (to < code.length && /\s/.test(code[to])) to += 1;
|
|
332
|
+
return to;
|
|
333
|
+
};
|
|
334
|
+
const transformModule = (code, id, register, sources = DEFAULT_ICON_SOURCES, options = {}) => {
|
|
335
|
+
const { sourceMap = false } = options;
|
|
336
|
+
if (!fastFilter(code)) return {
|
|
337
|
+
code,
|
|
338
|
+
map: null,
|
|
339
|
+
anyReplacements: false
|
|
340
|
+
};
|
|
341
|
+
const scannedImports = scanImportsDetailed(code, sources);
|
|
342
|
+
if (!scannedImports.length) return {
|
|
343
|
+
code,
|
|
344
|
+
map: null,
|
|
345
|
+
anyReplacements: false
|
|
346
|
+
};
|
|
347
|
+
const hasPotentialFontAwesomeUsage = code.includes(FONTAWESOME_REACT_PACK) && scannedImports.some((item) => isFontAwesomeIconPack(item.pack));
|
|
348
|
+
const table = buildScannedSymbolTable(scannedImports);
|
|
349
|
+
if (!table.size) return {
|
|
350
|
+
code,
|
|
351
|
+
map: null,
|
|
352
|
+
anyReplacements: false
|
|
353
|
+
};
|
|
354
|
+
const spriteIconImport = scanSpriteIconImport(code);
|
|
355
|
+
const usages = scanJsxIconUsages(code, table);
|
|
356
|
+
const fontAwesomeUsages = hasPotentialFontAwesomeUsage ? scanFontAwesomeUsages(code, table, scanFontAwesomeComponents(code)) : [];
|
|
357
|
+
if (!usages.length && !fontAwesomeUsages.length) return {
|
|
358
|
+
code,
|
|
359
|
+
map: null,
|
|
360
|
+
anyReplacements: false
|
|
361
|
+
};
|
|
362
|
+
const used = /* @__PURE__ */ new Set();
|
|
363
|
+
const usedFontAwesomeComponents = /* @__PURE__ */ new Set();
|
|
364
|
+
const edits = buildEdits(usages, spriteIconImport.localName, used, register);
|
|
365
|
+
const registeredFontAwesomeIcons = /* @__PURE__ */ new Set();
|
|
366
|
+
for (const usage of fontAwesomeUsages) {
|
|
367
|
+
edits.push({
|
|
368
|
+
type: "replace",
|
|
369
|
+
from: usage.componentRange[0],
|
|
370
|
+
to: usage.componentRange[1],
|
|
371
|
+
value: spriteIconImport.localName
|
|
372
|
+
});
|
|
373
|
+
if (!usage.hasIconId) edits.push({
|
|
374
|
+
type: "insert",
|
|
375
|
+
pos: usage.componentRange[1],
|
|
376
|
+
value: ` iconId="${computeIconId(usage.pack, usage.exportName)}"`
|
|
377
|
+
});
|
|
378
|
+
edits.push({
|
|
379
|
+
type: "remove",
|
|
380
|
+
from: usage.iconAttributeRange[0],
|
|
381
|
+
to: consumeTrailingWhitespace(code, usage.iconAttributeRange[1])
|
|
382
|
+
});
|
|
383
|
+
used.add(usage.iconLocal);
|
|
384
|
+
usedFontAwesomeComponents.add(usage.componentLocal);
|
|
385
|
+
const key = `${usage.pack}:${usage.exportName}`;
|
|
386
|
+
if (!registeredFontAwesomeIcons.has(key)) {
|
|
387
|
+
registeredFontAwesomeIcons.add(key);
|
|
388
|
+
register(usage.pack, usage.exportName);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
const cleanupEdits = cleanupScannedImports(code, scannedImports, used);
|
|
392
|
+
const cleanupFontAwesomeEdits = cleanupScannedFontAwesomeComponentImports(code, usedFontAwesomeComponents);
|
|
393
|
+
const magicString = applyEdits(code, [
|
|
394
|
+
...edits,
|
|
395
|
+
...cleanupEdits,
|
|
396
|
+
...cleanupFontAwesomeEdits
|
|
397
|
+
]);
|
|
398
|
+
const importPrefix = `import { ${ICON_COMPONENT_NAME} } from "${ICON_SOURCE}";\n`;
|
|
399
|
+
if (!spriteIconImport.hasImport) magicString.prepend(importPrefix);
|
|
400
|
+
return {
|
|
401
|
+
code: magicString.toString(),
|
|
402
|
+
map: sourceMap ? magicString.generateMap({
|
|
403
|
+
source: id,
|
|
404
|
+
hires: true
|
|
405
|
+
}) : null,
|
|
406
|
+
anyReplacements: true
|
|
407
|
+
};
|
|
408
|
+
};
|
|
409
|
+
//#endregion
|
|
410
|
+
export { transformModule as t };
|
package/dist/vite/plugin.mjs
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { REACT_ICONS_SPRITE_URL_PLACEHOLDER } from "../index.mjs";
|
|
2
|
-
import { i as
|
|
2
|
+
import { i as createCollector, n as DEFAULT_ICON_SOURCES } from "../compute-icon-id-DKiwIqyH.mjs";
|
|
3
|
+
import { t as buildSprite } from "../build-sprite-BZCizCDt.mjs";
|
|
4
|
+
import { t as transformModule } from "../transform-module-Dn8lfegG.mjs";
|
|
3
5
|
import { createHash } from "node:crypto";
|
|
4
6
|
//#region src/vite/plugin.ts
|
|
5
7
|
const reactIconsSprite = (options = {}) => {
|
|
@@ -18,7 +20,7 @@ const reactIconsSprite = (options = {}) => {
|
|
|
18
20
|
try {
|
|
19
21
|
const { code: next, map, anyReplacements } = transformModule(code, id, (pack, exportName) => {
|
|
20
22
|
collector.add(pack, exportName);
|
|
21
|
-
}, DEFAULT_ICON_SOURCES
|
|
23
|
+
}, DEFAULT_ICON_SOURCES);
|
|
22
24
|
if (!anyReplacements) return null;
|
|
23
25
|
return {
|
|
24
26
|
code: next,
|
package/dist/webpack/loader.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { t as collector } from "../collector-
|
|
1
|
+
import { t as transformModule } from "../transform-module-Dn8lfegG.mjs";
|
|
2
|
+
import { t as collector } from "../collector-wdoob7qt.mjs";
|
|
3
3
|
//#region src/webpack/loader.ts
|
|
4
4
|
const reactIconsSpriteLoader = async function(source) {
|
|
5
5
|
if (this.mode === "development") return source;
|
package/dist/webpack/plugin.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { REACT_ICONS_SPRITE_URL_PLACEHOLDER } from "../index.mjs";
|
|
2
|
-
import {
|
|
3
|
-
import { t as collector } from "../collector-
|
|
2
|
+
import { t as buildSprite } from "../build-sprite-BZCizCDt.mjs";
|
|
3
|
+
import { t as collector } from "../collector-wdoob7qt.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,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://www.schemastore.org/package.json",
|
|
3
3
|
"name": "react-icons-sprite",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "1.0.0-rc.1",
|
|
5
5
|
"type": "module",
|
|
6
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>",
|
|
@@ -65,21 +65,20 @@
|
|
|
65
65
|
"react-dom": ">= 16"
|
|
66
66
|
},
|
|
67
67
|
"dependencies": {
|
|
68
|
-
"magic-string": "0.30.21"
|
|
69
|
-
"oxc-parser": "0.119.0"
|
|
68
|
+
"magic-string": "0.30.21"
|
|
70
69
|
},
|
|
71
70
|
"devDependencies": {
|
|
72
|
-
"@carbon/icons-react": "11.
|
|
73
|
-
"@types/node": "25.
|
|
71
|
+
"@carbon/icons-react": "11.81.0",
|
|
72
|
+
"@types/node": "25.9.1",
|
|
74
73
|
"@types/react-dom": "19.2.3",
|
|
75
|
-
"@typescript/native-preview": "7.0.0-dev.
|
|
76
|
-
"react": "19.2.
|
|
77
|
-
"react-dom": "19.2.
|
|
78
|
-
"tsdown": "0.
|
|
79
|
-
"typescript": "
|
|
80
|
-
"vite": "8.0.
|
|
81
|
-
"vitest": "4.1.
|
|
82
|
-
"webpack": "5.
|
|
74
|
+
"@typescript/native-preview": "7.0.0-dev.20260521.1",
|
|
75
|
+
"react": "19.2.6",
|
|
76
|
+
"react-dom": "19.2.6",
|
|
77
|
+
"tsdown": "0.22.0",
|
|
78
|
+
"typescript": "6.0.3",
|
|
79
|
+
"vite": "8.0.14",
|
|
80
|
+
"vitest": "4.1.7",
|
|
81
|
+
"webpack": "5.107.1"
|
|
83
82
|
},
|
|
84
83
|
"keywords": [
|
|
85
84
|
"vite",
|
package/dist/core-B0kAb7AT.mjs
DELETED
|
@@ -1,404 +0,0 @@
|
|
|
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 };
|