zb-admin/vite.config.ts

97 lines
2.7 KiB
TypeScript
Raw Normal View History

2023-12-22 09:46:27 +00:00
import { defineConfig, ConfigEnv, UserConfig } from 'vite'
2022-09-13 10:34:54 +00:00
import path from 'path'
2022-08-31 14:07:41 +00:00
// vite.config.ts中无法使用import.meta.env 所以需要引入
2022-08-03 10:49:04 +00:00
import vue from '@vitejs/plugin-vue'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
// 增加 vue文件 script name值
import vueSetupExtend from 'vite-plugin-vue-setup-extend'
2022-09-13 10:34:54 +00:00
// 生产gz文件
2022-08-28 14:11:05 +00:00
import viteCompression from 'vite-plugin-compression'
2022-09-29 14:04:17 +00:00
// 按需加载
// import AutoImport from 'unplugin-auto-import/vite'
// import Components from 'unplugin-vue-components/vite'
//import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
2022-09-13 10:34:54 +00:00
2023-12-22 09:46:27 +00:00
function resolve(dir) {
2022-08-03 10:49:04 +00:00
return path.join(__dirname, '.', dir)
}
// https://vitejs.dev/config/
2022-08-28 14:11:05 +00:00
export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
return {
2023-12-22 09:46:27 +00:00
plugins: [
vue(),
2022-08-28 14:11:05 +00:00
vueSetupExtend(),
// AutoImport({
// resolvers: [ElementPlusResolver()],
// }),
// Components({
// resolvers: [ElementPlusResolver()],
// }),
// * 使用 svg 图标
createSvgIconsPlugin({
// 指定需要缓存的图标文件夹
iconDirs: [path.resolve(process.cwd(), 'src/icons/svg')],
// 指定symbolId格式
symbolId: 'icon-[dir]-[name]',
}),
// gzip压缩 生产环境生成 .gz 文件
2023-12-22 09:46:27 +00:00
mode === 'production' &&
viteCompression({
verbose: true,
disable: false,
threshold: 10240,
algorithm: 'gzip',
ext: '.gz',
}),
2022-08-28 14:11:05 +00:00
],
css: {
preprocessorOptions: {
scss: {
2023-12-22 09:46:27 +00:00
additionalData: `@use "./src/styles/index.scss" as *;`,
},
},
2022-08-03 10:49:04 +00:00
},
2022-08-28 14:11:05 +00:00
// 配置别名
resolve: {
alias: {
2023-12-22 09:46:27 +00:00
'@': resolve('src'),
static: resolve('public/static'),
2022-08-28 14:11:05 +00:00
},
// 忽略后缀名的配置选项, 添加 .vue 选项时要记得原本默认忽略的选项也要手动写入
extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue'],
},
//启动服务配置
server: {
// 服务器主机名,如果允许外部访问,可设置为 "0.0.0.0" 也可设置成你的ip地址
host: '0.0.0.0',
2022-11-10 05:12:12 +00:00
port: 8080,
2022-08-28 14:11:05 +00:00
open: true,
https: false,
cors: true,
// 代理跨域(模拟示例)
proxy: {
// "/api": {
// target: "", // easymock
// changeOrigin: true,
// rewrite: path => path.replace(/^\/api/, "")
// }
2023-12-22 09:46:27 +00:00
},
2022-08-28 14:11:05 +00:00
},
2022-08-29 08:08:26 +00:00
// 生产环境打包配置
//去除 console debugger
2022-08-31 14:07:41 +00:00
// esbuild: {
// pure:mode==='production' ? ["console.log", "debugger"] : []
// },
2022-08-29 08:08:26 +00:00
2022-08-28 14:11:05 +00:00
// build: {
// terserOptions: {
// compress: {
// drop_console: true,
// drop_debugger: true,
// },
// },
// },
}
2022-08-03 10:49:04 +00:00
})