unplugin.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import process from 'node:process';
  2. import path from 'node:path';
  3. import type { PluginOption } from 'vite';
  4. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
  5. import Icons from 'unplugin-icons/vite';
  6. import IconsResolver from 'unplugin-icons/resolver';
  7. import Components from 'unplugin-vue-components/vite';
  8. import { NaiveUiResolver } from 'unplugin-vue-components/resolvers';
  9. import { ProNaiveUIResolver } from 'pro-naive-ui-resolver';
  10. import { FileSystemIconLoader } from 'unplugin-icons/loaders';
  11. export function setupUnplugin(viteEnv: Env.ImportMeta) {
  12. const { VITE_ICON_PREFIX, VITE_ICON_LOCAL_PREFIX } = viteEnv;
  13. const localIconPath = path.join(process.cwd(), 'src/assets/svg-icon');
  14. /** The name of the local icon collection */
  15. const collectionName = VITE_ICON_LOCAL_PREFIX.replace(`${VITE_ICON_PREFIX}-`, '');
  16. const plugins: PluginOption[] = [
  17. Icons({
  18. compiler: 'vue3',
  19. customCollections: {
  20. [collectionName]: FileSystemIconLoader(localIconPath, svg =>
  21. svg.replace(/^<svg\s/, '<svg width="1em" height="1em" ')
  22. )
  23. },
  24. scale: 1,
  25. defaultClass: 'inline-block'
  26. }),
  27. Components({
  28. dts: 'src/typings/components.d.ts',
  29. types: [{ from: 'vue-router', names: ['RouterLink', 'RouterView'] }],
  30. resolvers: [
  31. NaiveUiResolver(),
  32. ProNaiveUIResolver(),
  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. }