unplugin.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import VueMacros from 'unplugin-vue-macros/vite';
  2. import Icons from 'unplugin-icons/vite';
  3. import IconsResolver from 'unplugin-icons/resolver';
  4. import Components from 'unplugin-vue-components/vite';
  5. import { NaiveUiResolver } from 'unplugin-vue-components/resolvers';
  6. import { FileSystemIconLoader } from 'unplugin-icons/loaders';
  7. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
  8. import { getSrcPath } from '../utils';
  9. export default function unplugin(viteEnv: ImportMetaEnv) {
  10. const { VITE_ICON_PREFFIX, VITE_ICON_LOCAL_PREFFIX } = viteEnv;
  11. const srcPath = getSrcPath();
  12. const localIconPath = `${srcPath}/assets/svg-icon`;
  13. /** 本地svg图标集合名称 */
  14. const collectionName = VITE_ICON_LOCAL_PREFFIX.replace(`${VITE_ICON_PREFFIX}-`, '');
  15. return [
  16. VueMacros({}),
  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. IconsResolver({ customCollections: [collectionName], componentPrefix: VITE_ICON_PREFFIX })
  33. ]
  34. }),
  35. createSvgIconsPlugin({
  36. iconDirs: [localIconPath],
  37. symbolId: `${VITE_ICON_LOCAL_PREFFIX}-[dir]-[name]`,
  38. inject: 'body-last',
  39. customDomId: '__SVG_ICON_LOCAL__'
  40. })
  41. ];
  42. }