unocss.ts 967 B

1234567891011121314151617181920212223242526272829303132
  1. import process from 'node:process';
  2. import path from 'node:path';
  3. import unocss from '@unocss/vite';
  4. import presetIcons from '@unocss/preset-icons';
  5. import { FileSystemIconLoader } from '@iconify/utils/lib/loader/node-loaders';
  6. export function setupUnocss(viteEnv: Env.ImportMeta) {
  7. const { VITE_ICON_PREFIX, VITE_ICON_LOCAL_PREFIX } = viteEnv;
  8. const localIconPath = path.join(process.cwd(), 'src/assets/svg-icon');
  9. /** The name of the local icon collection */
  10. const collectionName = VITE_ICON_LOCAL_PREFIX.replace(`${VITE_ICON_PREFIX}-`, '');
  11. return unocss({
  12. presets: [
  13. presetIcons({
  14. prefix: `${VITE_ICON_PREFIX}-`,
  15. scale: 1,
  16. extraProperties: {
  17. display: 'inline-block'
  18. },
  19. collections: {
  20. [collectionName]: FileSystemIconLoader(localIconPath, svg =>
  21. svg.replace(/^<svg\s/, '<svg width="1em" height="1em" ')
  22. )
  23. },
  24. warn: true
  25. })
  26. ]
  27. });
  28. }