import { readFile, mkdir, writeFile, copyFile, access } from 'node:fs/promises'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { createHash } from 'node:crypto'; import { build } from 'esbuild'; import { format } from 'prettier'; const root = fileURLToPath(new URL('../', import.meta.url)); const source = path.resolve( root, process.argv[2] || 'node_modules/@lobehub/icons', ); const upstream = JSON.parse( await readFile(path.join(source, 'package.json'), 'utf8'), ); if (upstream.name !== '@lobehub/icons' || upstream.version !== '1.97.2') { throw new Error('Use the audited @lobehub/icons@1.97.2 source package.'); } const brands = [ 'OpenAI', 'Claude', 'Gemini', 'Moonshot', 'Zhipu', 'Qwen', 'DeepSeek', 'Minimax', 'Wenxin', 'Spark', 'Midjourney', 'Hunyuan', 'Cohere', 'Cloudflare', 'Ai360', 'Yi', 'Jina', 'Mistral', 'XAI', 'Ollama', 'Doubao', 'Suno', 'Xinference', 'OpenRouter', 'Dify', 'Coze', 'SiliconCloud', 'Kling', 'Perplexity', 'Replicate', ]; const external = new Set(['react', 'react/jsx-runtime']); const entries = []; const primitiveEntries = []; for (const brand of brands) { entries.push(`export { default as ${brand} } from './es/${brand}/index.js';`); for (const variant of ['Mono', 'Color']) { const modulePath = `./es/${brand}/components/${variant}.js`; try { await access(path.join(source, modulePath)); } catch { continue; } primitiveEntries.push( `export { default as ${brand}${variant} } from '${modulePath}';`, ); } } const result = await build({ absWorkingDir: source, entryPoints: { lobehub: 'brand:variants', primitives: 'brand:primitives' }, outdir: path.join(root, 'src/assets/brand-icons'), splitting: true, chunkNames: 'shared-[hash]', bundle: true, write: false, metafile: true, format: 'esm', target: 'es2015', minifySyntax: true, plugins: [ { name: 'brand-primitives-only', setup(build) { build.onResolve({ filter: /^brand:/ }, ({ path }) => ({ path, namespace: 'brand', })); build.onLoad( { filter: /.*/, namespace: 'brand' }, ({ path: name }) => ({ contents: (name === 'brand:variants' ? entries : primitiveEntries ).join('\n'), resolveDir: source, }), ); build.onResolve( { filter: /features\/Icon(Avatar|Combine)$/ }, ({ path: name }) => ({ path: `../../components/common/brand-icons/${path.basename(name)}.jsx`, external: true, }), ); build.onResolve({ filter: /^[^./]/ }, ({ path: name }) => { if (name === 'antd-style') return { path: '../../components/common/brand-icons/useThemeMode.js', external: true, }; if (!external.has(name)) throw new Error(`Unexpected icon dependency: ${name}`); return { path: name, external: true }; }); }, }, ], }); const output = path.join(root, 'src/assets/brand-icons'); await mkdir(output, { recursive: true }); const files = {}; for (const file of result.outputFiles) { const code = await format( '// Generated from @lobehub/icons@1.97.2. See LICENSE and README.md.\n' + file.text, { parser: 'babel', singleQuote: true }, ); await writeFile(file.path, code); files[path.basename(file.path)] = createHash('sha256') .update(code) .digest('hex'); } await copyFile(path.join(source, 'LICENSE'), path.join(output, 'LICENSE')); await writeFile( path.join(output, 'manifest.json'), JSON.stringify( { source: '@lobehub/icons', version: upstream.version, license: upstream.license, brands, moduleCount: Object.keys(result.metafile.inputs).filter( (name) => !name.startsWith('brand:'), ).length, files, }, null, 2, ) + '\n', ); console.log( `Vendored ${brands.length} brands in ${Object.keys(files).length} modules.`, );