import fs from 'node:fs' import path from 'node:path' import * as icons from '@element-plus/icons-vue' // Statically used icons are tree-shaken named imports. All other names remain // registered as lazy fallbacks so menu/config-driven icons continue to work. export default function elementIcons() { const id = 'virtual:business-element-icons' let root function collect(directory) { return fs.readdirSync(directory, { withFileTypes: true }).flatMap(entry => { const file = path.join(directory, entry.name) return entry.isDirectory() ? collect(file) : /\.(vue|js)$/.test(file) ? [fs.readFileSync(file, 'utf8')] : [] }).join('\n') } return { name: 'business-element-icons', configResolved(config) { root = config.root }, resolveId(source) { if (source === id) return '\0' + id }, load(source) { if (source !== '\0' + id) return const text = collect(path.join(root, 'src')) const names = Object.keys(icons) const used = names.filter(name => { const kebab = name.replace(/[A-Z]/g, (letter, index) => (index ? '-' : '') + letter.toLowerCase()) return new RegExp(`(?:<|["'])(${name}|${kebab})(?:[\\s/>"'])`).test(text) }) return `import { defineAsyncComponent } from 'vue';\n` + `import { ${used.join(',')} } from '@element-plus/icons-vue';\n` + `const components = { ${used.join(',')} };\n` + // The separate official distribution keeps the fallback out of the // eager named-import module (Rollup otherwise merges both into entry). `for (const name of ${JSON.stringify(names.filter(name => !used.includes(name)))}) components[name] = defineAsyncComponent(() => import('@element-plus/icons-vue/global').then(icons => icons[name]));\n` + `export default components;` } } }