Geen omschrijving
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vite.icons.plugin.js 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import fs from 'fs/promises';
  2. import path from 'path';
  3. import { getIconsCSS } from '@iconify/utils';
  4. export default function iconifyPlugin() {
  5. return {
  6. name: 'vite-iconify-plugin',
  7. apply: 'build', // Run only during build
  8. async buildStart() {
  9. console.log('🔨 Generating iconify CSS file...');
  10. try {
  11. const iconSetPaths = [
  12. path.resolve(process.cwd(), 'node_modules/@iconify/json/json/bx.json'),
  13. path.resolve(process.cwd(), 'node_modules/@iconify/json/json/bxl.json'),
  14. path.resolve(process.cwd(), 'node_modules/@iconify/json/json/bxs.json')
  15. ];
  16. const iconSets = await Promise.all(
  17. iconSetPaths.map(async filePath => {
  18. const data = await fs.readFile(filePath, 'utf-8');
  19. return JSON.parse(data);
  20. })
  21. );
  22. const allIcons = iconSets
  23. .map(iconSet => {
  24. return getIconsCSS(iconSet, Object.keys(iconSet.icons), {
  25. iconSelector: '.{prefix}-{name}',
  26. commonSelector: '.bx',
  27. format: 'expanded'
  28. });
  29. })
  30. .join('\n');
  31. const outputPath = path.resolve(process.cwd(), 'resources/assets/vendor/fonts/iconify/iconify.css');
  32. const dir = path.dirname(outputPath);
  33. await fs.mkdir(dir, { recursive: true });
  34. await fs.writeFile(outputPath, allIcons, 'utf8');
  35. console.log(`✅ Iconify CSS generated at: ${outputPath}`);
  36. const additionalFiles = [
  37. {
  38. name: 'fontawesome',
  39. filesPath: path.resolve(process.cwd(), 'node_modules/@fortawesome/fontawesome-free/webfonts'),
  40. destPath: path.resolve(process.cwd(), 'resources/assets/vendor/fonts/fontawesome')
  41. },
  42. {
  43. name: 'flags',
  44. filesPath: path.resolve(process.cwd(), 'node_modules/flag-icons/flags'),
  45. destPath: path.resolve(process.cwd(), 'resources/assets/vendor/fonts/flags')
  46. }
  47. ];
  48. for (const file of additionalFiles) {
  49. await fs.mkdir(file.destPath, { recursive: true });
  50. const items = await fs.readdir(file.filesPath, { withFileTypes: true });
  51. for (const item of items) {
  52. const srcPath = path.join(file.filesPath, item.name);
  53. const destPath = path.join(file.destPath, item.name);
  54. if (item.isDirectory()) {
  55. await fs.mkdir(destPath, { recursive: true });
  56. const subItems = await fs.readdir(srcPath);
  57. for (const subItem of subItems) {
  58. await fs.copyFile(path.join(srcPath, subItem), path.join(destPath, subItem));
  59. }
  60. } else {
  61. await fs.copyFile(srcPath, destPath);
  62. }
  63. }
  64. }
  65. } catch (error) {
  66. console.error('❌ Error generating Iconify CSS or copying additional files:', error);
  67. }
  68. }
  69. };
  70. }