unplugin.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import process from 'node:process';
  2. import path from 'node:path';
  3. import type { PluginOption } from 'vite';
  4. import Icons from 'unplugin-icons/vite';
  5. import IconsResolver from 'unplugin-icons/resolver';
  6. import Components from 'unplugin-vue-components/vite';
  7. import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers';
  8. import { FileSystemIconLoader } from 'unplugin-icons/loaders';
  9. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
  10. export function setupUnplugin(viteEnv: Env.ImportMeta) {
  11. const { VITE_ICON_PREFIX, VITE_ICON_LOCAL_PREFIX } = viteEnv;
  12. const localIconPath = path.join(process.cwd(), 'src/assets/svg-icon');
  13. /** The name of the local icon collection */
  14. const collectionName = VITE_ICON_LOCAL_PREFIX.replace(`${VITE_ICON_PREFIX}-`, '');
  15. const plugins: PluginOption[] = [
  16. Icons({
  17. compiler: 'vue3',
  18. customCollections: {
  19. [collectionName]: FileSystemIconLoader(localIconPath, svg =>
  20. svg.replace(/^<svg\s/, '<svg width="1em" height="1em" ')
  21. )
  22. },
  23. scale: 1,
  24. defaultClass: 'inline-block'
  25. }),
  26. Components({
  27. dts: 'src/typings/components.d.ts',
  28. types: [{ from: 'vue-router', names: ['RouterLink', 'RouterView'] }],
  29. resolvers: [
  30. AntDesignVueResolver({
  31. importStyle: false
  32. }),
  33. IconsResolver({ customCollections: [collectionName], componentPrefix: VITE_ICON_PREFIX })
  34. ]
  35. }),
  36. createSvgIconsPlugin({
  37. iconDirs: [localIconPath],
  38. symbolId: `${VITE_ICON_LOCAL_PREFIX}-[dir]-[name]`,
  39. inject: 'body-last',
  40. customDomId: '__SVG_ICON_LOCAL__'
  41. })
  42. ];
  43. return plugins;
  44. }