Skip to content

Commit 2be73d4

Browse files
committed
feat(react-icons): update to create static SVGs
1 parent 2b36c61 commit 2be73d4

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

packages/react-icons/scripts/writeIcons.mjs

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import { join } from 'path';
2-
import { outputFileSync } from 'fs-extra/esm';
2+
import { outputFileSync, ensureDirSync } from 'fs-extra/esm';
33
import { generateIcons } from './generateIcons.mjs';
4+
import { createIcon } from '../dist/esm/createIcon.js';
5+
import React from 'react';
6+
import { renderToString } from 'react-dom/server';
47

58
import * as url from 'url';
69
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
710

811
const outDir = join(__dirname, '../dist');
12+
const staticDir = join(outDir, 'static');
913

1014
const removeSnake = (s) => s.toUpperCase().replace('-', '').replace('_', '');
1115
const toCamel = (s) => `${s[0].toUpperCase()}${s.substr(1).replace(/([-_][\w])/gi, removeSnake)}`;
@@ -72,6 +76,50 @@ export default ${jsName};
7276
outputFileSync(join(outDir, 'esm/icons', filename), text);
7377
};
7478

79+
/**
80+
* Generates a static SVG string from icon data using createIcon
81+
* @param {string} iconName The name of the icon
82+
* @param {object} icon The icon data object
83+
* @returns {string} Static SVG markup
84+
*/
85+
function generateStaticSVG(iconName, icon) {
86+
const jsName = `${toCamel(iconName)}Icon`;
87+
88+
// Create icon component using createIcon
89+
const IconComponent = createIcon({
90+
name: jsName,
91+
width: icon.width,
92+
height: icon.height,
93+
svgPath: icon.svgPathData,
94+
xOffset: icon.xOffset || 0,
95+
yOffset: icon.yOffset || 0,
96+
svgClassName: icon.svgClassName
97+
});
98+
99+
// Render the component to string
100+
const svgString = renderToString(React.createElement(IconComponent));
101+
102+
// Convert React's className to class for static SVG
103+
return svgString.replace(/className=/g, 'class=');
104+
}
105+
106+
/**
107+
* Writes static SVG files to dist/static directory
108+
* @param {object} icons icons from generateIcons
109+
*/
110+
function writeStaticSVGs(icons) {
111+
ensureDirSync(staticDir);
112+
113+
Object.entries(icons).forEach(([iconName, icon]) => {
114+
const svgContent = generateStaticSVG(iconName, icon);
115+
const svgFileName = `${iconName}.svg`;
116+
outputFileSync(join(staticDir, svgFileName), svgContent, 'utf-8');
117+
});
118+
119+
// eslint-disable-next-line no-console
120+
console.log(`Wrote ${Object.keys(icons).length} static SVG files to ${staticDir}`);
121+
}
122+
75123
/**
76124
* Writes CJS and ESM icons to `dist` directory
77125
*
@@ -114,4 +162,6 @@ ${index
114162
console.log('Wrote', index.length * 3 + 3, 'icon files.');
115163
}
116164

117-
writeIcons(generateIcons());
165+
const icons = generateIcons();
166+
writeIcons(icons);
167+
writeStaticSVGs(icons);

0 commit comments

Comments
 (0)