unplugin.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import Icons from 'unplugin-icons/vite';
  2. import IconsResolver from 'unplugin-icons/resolver';
  3. import Components from 'unplugin-vue-components/vite';
  4. import { NaiveUiResolver } from 'unplugin-vue-components/resolvers';
  5. import { FileSystemIconLoader } from 'unplugin-icons/loaders';
  6. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
  7. import { getSrcPath } from '../utils';
  8. export default function unplugin(viteEnv: ImportMetaEnv) {
  9. const { VITE_ICON_PREFFIX, VITE_ICON_LOCAL_PREFFIX } = viteEnv;
  10. const srcPath = getSrcPath();
  11. const localIconPath = `${srcPath}/assets/svg-icon`;
  12. /** 本地svg图标集合名称 */
  13. const collectionName = VITE_ICON_LOCAL_PREFFIX.replace(`${VITE_ICON_PREFFIX}-`, '');
  14. return [
  15. Icons({
  16. compiler: 'vue3',
  17. customCollections: {
  18. [collectionName]: FileSystemIconLoader(localIconPath, svg =>
  19. svg.replace(/^<svg\s/, '<svg width="1em" height="1em" ')
  20. )
  21. },
  22. scale: 1,
  23. defaultClass: 'inline-block'
  24. }),
  25. Components({
  26. dts: 'src/typings/components.d.ts',
  27. types: [{ from: 'vue-router', names: ['RouterLink', 'RouterView'] }],
  28. resolvers: [
  29. NaiveUiResolver(),
  30. IconsResolver({ customCollections: [collectionName], componentPrefix: VITE_ICON_PREFFIX })
  31. ]
  32. }),
  33. createSvgIconsPlugin({
  34. iconDirs: [localIconPath],
  35. symbolId: `${VITE_ICON_LOCAL_PREFFIX}-[dir]-[name]`,
  36. inject: 'body-last',
  37. customDomId: '__SVG_ICON_LOCAL__'
  38. })
  39. ];
  40. }