1.改造为函数
默认模板创建的vite.config.ts
文件是这样的
我们在上一节也提到过,这样将使得vite的一些配置十分杂乱,无法统一进行管理,因此我们可以将其函数化
export default defineConfig(configEnv => {
const viteEnv = loadEnv(configEnv.mode , process.cwd()) as unknown as ImportMetaEnv
return{
}
})
2.创建build文件夹——用于配置vite
config
——用于配置vite相关(包括网络代理等)plugins
——项目有关插件utils
——与项目build配置相关工具
每个小模块都由对应的index.ts
管理
最大级的index
export * from './plugins';
export * from './config';
export * from './utils';
utils-index
获取相关路由
import path from 'path';
/**
* 获取项目根路径
* @descrition 末尾不带斜杠
*/
export function getRootPath() {
return path.resolve(process.cwd());
}
/**
* 获取项目src路径
* @param srcName - src目录名称(默认: "src")
* @descrition 末尾不带斜杠
*/
export function getSrcPath(srcName = 'src') {
const rootPath = getRootPath();
return `${rootPath}/${srcName}`;
}
3.完成插件配置
在 build
-> plugins
目录下创建unplugin.ts文件,用于插件的解析
根据报错安装对应的依赖即可
- 自动生成component.d.ts
- 自动下载icon
- 解析本地icon-svg
import Icons from 'unplugin-icons/vite';
import IconsResolver from 'unplugin-icons/resolver';
import Components from 'unplugin-vue-components/vite';
import { NaiveUiResolver } from 'unplugin-vue-components/resolvers';
import { FileSystemIconLoader } from 'unplugin-icons/loaders';
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
import { getSrcPath } from '../utils';
export default function unplugin(viteEnv: ImportMetaEnv) {
const { VITE_ICON_PREFFIX, VITE_ICON_LOCAL_PREFFIX } = viteEnv;
const srcPath = getSrcPath();
const localIconPath = `${srcPath}/assets/svg-icon`;
/** 本地svg图标集合名称 */
const collectionName = VITE_ICON_LOCAL_PREFFIX.replace(`${VITE_ICON_PREFFIX}-`, '');
return [
Icons({
compiler: 'vue3',
customCollections: {
[collectionName]: FileSystemIconLoader(localIconPath)
},
scale: 1,
defaultClass: 'inline-block'
}),
Components({
dts: 'src/typings/components.d.ts',
types: [{ from: 'vue-router', names: ['RouterLink', 'RouterView'] }],
resolvers: [
NaiveUiResolver(),
IconsResolver({ customCollections: [collectionName], componentPrefix: VITE_ICON_PREFFIX })
]
}),
createSvgIconsPlugin({
iconDirs: [localIconPath],
symbolId: `${VITE_ICON_LOCAL_PREFFIX}-[dir]-[name]`,
inject: 'body-last',
customDomId: '__SVG_ICON_LOCAL__'
})
];
}
plugins-index
导入插件
import type { PluginOption } from 'vite';
import vue from '@vitejs/plugin-vue';
import unocss from '@unocss/vite';
import unplugin from './unplugin';
/**
* vite插件
* @param viteEnv - 环境变量配置
*/
export function setupVitePlugins(viteEnv: ImportMetaEnv): (PluginOption | PluginOption[])[] {
const plugins = [vue(),...unplugin(viteEnv), unocss()];
return plugins;
}
创建env文件
在根目录创建env文件,设置vite环境变量
VITE_BASE_URL=/
VITE_APP_NAME=Job-Collect
VITE_APP_TITLE=作业管理系统
# iconify图标作为组件的前缀
VITE_ICON_PREFFIX=icon
# 本地SVG图标作为组件的前缀, 请注意一定要包含 VITE_ICON_PREFFIX
# 格式 {VITE_ICON_PREFFIX}-{本地图标集合名称}
VITE_ICON_LOCAL_PREFFIX=icon-local
4.完成vite.config.ts构建
import { defineConfig, loadEnv } from 'vite'
import { getRootPath, getSrcPath, setupVitePlugins } from './src/build';
export default defineConfig(configEnv => {
const viteEnv = loadEnv(configEnv.mode , process.cwd()) as unknown as ImportMetaEnv
const rootPath = getRootPath();
const srcPath = getSrcPath();
return{
base: viteEnv.VITE_BASE_URL,
resolve: {
alias: {
'~': rootPath,
'@': srcPath,
}
},
plugins: setupVitePlugins(viteEnv),
server: {
host: '0.0.0.0',
port: 3200,
open: true,
},
}
})
// // https://vitejs.dev/config/
// export default defineConfig({
// plugins: [
// vue(),
// Unocss({ // 使用Unocss
// presets: [
// presetUno(),
// presetAttributify(),
// presetIcons()],
// })
// ],
// })
5.创建uno.config.ts
import { defineConfig } from '@unocss/vite';
import presetUno from '@unocss/preset-uno';
export default defineConfig({
exclude: ['node_modules', 'dist', '.git', '.husky', '.vscode', 'public', 'build', 'mock', './stats.html'],
presets: [presetUno({ dark: 'class' })],
shortcuts: {
'wh-full': 'w-full h-full',
'flex-center': 'flex justify-center items-center',
'flex-col-center': 'flex-center flex-col',
'flex-x-center': 'flex justify-center',
'flex-y-center': 'flex items-center',
'i-flex-center': 'inline-flex justify-center items-center',
'i-flex-x-center': 'inline-flex justify-center',
'i-flex-y-center': 'inline-flex items-center',
'flex-col': 'flex flex-col',
'flex-col-stretch': 'flex-col items-stretch',
'i-flex-col': 'inline-flex flex-col',
'i-flex-col-stretch': 'i-flex-col items-stretch',
'flex-1-hidden': 'flex-1 overflow-hidden',
'absolute-lt': 'absolute left-0 top-0',
'absolute-lb': 'absolute left-0 bottom-0',
'absolute-rt': 'absolute right-0 top-0',
'absolute-rb': 'absolute right-0 bottom-0',
'absolute-tl': 'absolute-lt',
'absolute-tr': 'absolute-rt',
'absolute-bl': 'absolute-lb',
'absolute-br': 'absolute-rb',
'absolute-center': 'absolute-lt flex-center wh-full',
'fixed-lt': 'fixed left-0 top-0',
'fixed-lb': 'fixed left-0 bottom-0',
'fixed-rt': 'fixed right-0 top-0',
'fixed-rb': 'fixed right-0 bottom-0',
'fixed-tl': 'fixed-lt',
'fixed-tr': 'fixed-rt',
'fixed-bl': 'fixed-lb',
'fixed-br': 'fixed-rb',
'fixed-center': 'fixed-lt flex-center wh-full',
'nowrap-hidden': 'whitespace-nowrap overflow-hidden',
'ellipsis-text': 'nowrap-hidden overflow-ellipsis',
'transition-base': 'transition-all duration-300 ease-in-out'
},
theme: {
colors: {
primary: 'var(--primary-color)',
primary_hover: 'var(--primary-color-hover)',
primary_pressed: 'var(--primary-color-pressed)',
primary_active: 'var(--primary-color-active)',
info: 'var(--info-color)',
info_hover: 'var(--info-color-hover)',
info_pressed: 'var(--info-color-pressed)',
info_active: 'var(--info-color-active)',
success: 'var(--success-color)',
success_hover: 'var(--success-color-hover)',
success_pressed: 'var(--success-color-pressed)',
success_active: 'var(--success-color-active)',
warning: 'var(--warning-color)',
warning_hover: 'var(--warning-color-hover)',
warning_pressed: 'var(--warning-color-pressed)',
warning_active: 'var(--warning-color-active)',
error: 'var(--error-color)',
error_hover: 'var(--error-color-hover)',
error_pressed: 'var(--error-color-pressed)',
error_active: 'var(--error-color-active)',
dark: '#18181c'
}
}
});
最终效果
参考开源项目:GitHub - honghuangdc/soybean-admin: A fresh and elegant admin template, based on Vue3,Vite3,TypeScript,NaiveUI and UnoCSS [一个基于Vue3、Vite3、TypeScript、NaiveUI 和 UnoCSS的清新优雅的中后台模版]