2023-12-14 21:45:29 +08:00
|
|
|
import process from 'node:process';
|
2023-11-17 08:45:00 +08:00
|
|
|
import path from 'node:path';
|
|
|
|
import type { PluginOption } from 'vite';
|
2025-03-12 22:24:59 +08:00
|
|
|
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
|
2022-03-12 19:55:02 +08:00
|
|
|
import Icons from 'unplugin-icons/vite';
|
|
|
|
import IconsResolver from 'unplugin-icons/resolver';
|
|
|
|
import Components from 'unplugin-vue-components/vite';
|
2024-10-28 19:37:23 +08:00
|
|
|
import { NaiveUiResolver } from 'unplugin-vue-components/resolvers';
|
2022-03-12 19:55:02 +08:00
|
|
|
import { FileSystemIconLoader } from 'unplugin-icons/loaders';
|
|
|
|
|
2023-11-17 08:45:00 +08:00
|
|
|
export function setupUnplugin(viteEnv: Env.ImportMeta) {
|
2023-07-19 23:44:18 +08:00
|
|
|
const { VITE_ICON_PREFIX, VITE_ICON_LOCAL_PREFIX } = viteEnv;
|
2022-06-16 01:17:31 +08:00
|
|
|
|
2023-11-17 08:45:00 +08:00
|
|
|
const localIconPath = path.join(process.cwd(), 'src/assets/svg-icon');
|
2022-06-16 01:17:31 +08:00
|
|
|
|
2023-12-14 21:45:29 +08:00
|
|
|
/** The name of the local icon collection */
|
2023-07-19 23:44:18 +08:00
|
|
|
const collectionName = VITE_ICON_LOCAL_PREFIX.replace(`${VITE_ICON_PREFIX}-`, '');
|
2022-09-23 00:15:00 +08:00
|
|
|
|
2023-11-17 08:45:00 +08:00
|
|
|
const plugins: PluginOption[] = [
|
2022-09-23 00:15:00 +08:00
|
|
|
Icons({
|
|
|
|
compiler: 'vue3',
|
|
|
|
customCollections: {
|
2023-02-02 23:25:00 +08:00
|
|
|
[collectionName]: FileSystemIconLoader(localIconPath, svg =>
|
2023-02-09 23:25:00 +08:00
|
|
|
svg.replace(/^<svg\s/, '<svg width="1em" height="1em" ')
|
2023-02-02 23:25:00 +08:00
|
|
|
)
|
2022-09-23 00:15:00 +08:00
|
|
|
},
|
|
|
|
scale: 1,
|
2022-09-24 17:22:08 +08:00
|
|
|
defaultClass: 'inline-block'
|
2022-09-23 00:15:00 +08:00
|
|
|
}),
|
|
|
|
Components({
|
|
|
|
dts: 'src/typings/components.d.ts',
|
|
|
|
types: [{ from: 'vue-router', names: ['RouterLink', 'RouterView'] }],
|
|
|
|
resolvers: [
|
|
|
|
NaiveUiResolver(),
|
2023-07-19 23:44:18 +08:00
|
|
|
IconsResolver({ customCollections: [collectionName], componentPrefix: VITE_ICON_PREFIX })
|
2022-09-23 00:15:00 +08:00
|
|
|
]
|
|
|
|
}),
|
|
|
|
createSvgIconsPlugin({
|
|
|
|
iconDirs: [localIconPath],
|
2023-07-19 23:44:18 +08:00
|
|
|
symbolId: `${VITE_ICON_LOCAL_PREFIX}-[dir]-[name]`,
|
2022-09-23 00:15:00 +08:00
|
|
|
inject: 'body-last',
|
|
|
|
customDomId: '__SVG_ICON_LOCAL__'
|
|
|
|
})
|
|
|
|
];
|
2023-11-17 08:45:00 +08:00
|
|
|
|
|
|
|
return plugins;
|
2022-09-23 00:15:00 +08:00
|
|
|
}
|