|
1 | 1 | import { join } from 'path'; |
2 | | -import { outputFileSync } from 'fs-extra/esm'; |
| 2 | +import { outputFileSync, ensureDirSync } from 'fs-extra/esm'; |
3 | 3 | import { generateIcons } from './generateIcons.mjs'; |
| 4 | +import { createElement } from 'react'; |
| 5 | +import { renderToString } from 'react-dom/server'; |
4 | 6 |
|
5 | 7 | import * as url from 'url'; |
6 | 8 | const __dirname = url.fileURLToPath(new URL('.', import.meta.url)); |
7 | 9 |
|
| 10 | +// Import createIcon from compiled dist (build:esm must run first) |
| 11 | +const createIconModule = await import('../dist/esm/createIcon.js'); |
| 12 | +const createIcon = createIconModule.createIcon; |
| 13 | + |
8 | 14 | const outDir = join(__dirname, '../dist'); |
| 15 | +const staticDir = join(outDir, 'static'); |
9 | 16 |
|
10 | 17 | const removeSnake = (s) => s.toUpperCase().replace('-', '').replace('_', ''); |
11 | 18 | const toCamel = (s) => `${s[0].toUpperCase()}${s.substr(1).replace(/([-_][\w])/gi, removeSnake)}`; |
@@ -72,6 +79,50 @@ export default ${jsName}; |
72 | 79 | outputFileSync(join(outDir, 'esm/icons', filename), text); |
73 | 80 | }; |
74 | 81 |
|
| 82 | +/** |
| 83 | + * Generates a static SVG string from icon data using createIcon |
| 84 | + * @param {string} iconName The name of the icon |
| 85 | + * @param {object} icon The icon data object |
| 86 | + * @returns {string} Static SVG markup |
| 87 | + */ |
| 88 | +function generateStaticSVG(iconName, icon) { |
| 89 | + const jsName = `${toCamel(iconName)}Icon`; |
| 90 | + |
| 91 | + // Create icon component using createIcon |
| 92 | + const IconComponent = createIcon({ |
| 93 | + name: jsName, |
| 94 | + width: icon.width, |
| 95 | + height: icon.height, |
| 96 | + svgPath: icon.svgPathData, |
| 97 | + xOffset: icon.xOffset || 0, |
| 98 | + yOffset: icon.yOffset || 0, |
| 99 | + svgClassName: icon.svgClassName |
| 100 | + }); |
| 101 | + |
| 102 | + // Render the component to string |
| 103 | + const svgString = renderToString(createElement(IconComponent)); |
| 104 | + |
| 105 | + // Convert React's className to class for static SVG |
| 106 | + return svgString.replace(/className=/g, 'class='); |
| 107 | +} |
| 108 | + |
| 109 | +/** |
| 110 | + * Writes static SVG files to dist/static directory |
| 111 | + * @param {object} icons icons from generateIcons |
| 112 | + */ |
| 113 | +function writeStaticSVGs(icons) { |
| 114 | + ensureDirSync(staticDir); |
| 115 | + |
| 116 | + Object.entries(icons).forEach(([iconName, icon]) => { |
| 117 | + const svgContent = generateStaticSVG(iconName, icon); |
| 118 | + const svgFileName = `${iconName}.svg`; |
| 119 | + outputFileSync(join(staticDir, svgFileName), svgContent, 'utf-8'); |
| 120 | + }); |
| 121 | + |
| 122 | + // eslint-disable-next-line no-console |
| 123 | + console.log(`Wrote ${Object.keys(icons).length} static SVG files to ${staticDir}`); |
| 124 | +} |
| 125 | + |
75 | 126 | /** |
76 | 127 | * Writes CJS and ESM icons to `dist` directory |
77 | 128 | * |
@@ -114,4 +165,6 @@ ${index |
114 | 165 | console.log('Wrote', index.length * 3 + 3, 'icon files.'); |
115 | 166 | } |
116 | 167 |
|
117 | | -writeIcons(generateIcons()); |
| 168 | +const icons = generateIcons(); |
| 169 | +writeIcons(icons); |
| 170 | +writeStaticSVGs(icons); |
0 commit comments