vue-pro/yudao-ui-admin-vue3/vite.config.ts

168 lines
4.7 KiB
TypeScript
Raw Normal View History

2022-07-18 19:06:37 +08:00
import { resolve } from 'path'
import { loadEnv } from 'vite'
import type { UserConfig, ConfigEnv } from 'vite'
import Vue from '@vitejs/plugin-vue'
import WindiCSS from 'vite-plugin-windicss'
import VueJsx from '@vitejs/plugin-vue-jsx'
import EslintPlugin from 'vite-plugin-eslint'
import VueI18n from '@intlify/vite-plugin-vue-i18n'
2022-10-19 17:10:19 +08:00
import { createStyleImportPlugin, ElementPlusResolve, VxeTableResolve } from 'vite-plugin-style-import'
2022-11-23 13:27:18 +08:00
import progress from 'vite-plugin-progress'
2022-07-18 19:06:37 +08:00
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import PurgeIcons from 'vite-plugin-purge-icons'
import { createHtmlPlugin } from 'vite-plugin-html'
2022-07-20 16:47:51 +08:00
import viteCompression from 'vite-plugin-compression'
2022-11-17 20:20:09 +08:00
// 当前执行node命令时文件夹的地址(工作目录)
2022-07-18 19:06:37 +08:00
const root = process.cwd()
// 路径查找
function pathResolve(dir: string) {
return resolve(root, '.', dir)
}
// https://vitejs.dev/config/
export default ({ command, mode }: ConfigEnv): UserConfig => {
let env = {} as any
const isBuild = command === 'build'
if (!isBuild) {
env = loadEnv((process.argv[3] === '--mode' ? process.argv[4] : process.argv[3]), root)
} else {
env = loadEnv(mode, root)
}
return {
2022-07-19 22:33:54 +08:00
base: env.VITE_BASE_PATH,
root: root,
2022-07-18 19:06:37 +08:00
// 服务端渲染
server: {
// 是否开启 https
https: false,
// 端口号
port: env.VITE_PORT,
host: "0.0.0.0",
open: env.VITE_OPEN,
// 本地跨域代理
proxy: {
['/dev-api']: {
target: env.VITE_BASE_URL,
ws: false,
changeOrigin: true,
rewrite: (path) => path.replace(new RegExp(`^/dev-api`), ''),
},
},
},
plugins: [
Vue(),
VueJsx(),
WindiCSS(),
2022-11-23 13:27:18 +08:00
progress(),
PurgeIcons(),
2022-08-01 19:03:46 +08:00
createStyleImportPlugin({
2022-10-19 17:10:19 +08:00
resolves: [ElementPlusResolve(),VxeTableResolve()],
2022-07-18 19:06:37 +08:00
libs: [{
libraryName: 'element-plus',
esModule: true,
resolveStyle: (name) => {
return `element-plus/es/components/${name.substring(3)}/style/css`
}
2022-10-19 17:10:19 +08:00
},{
libraryName: 'vxe-table',
esModule: true,
resolveStyle: (name) => {
return `vxe-table/es/${name}/style.css`
}
2022-07-18 19:06:37 +08:00
}]
}),
EslintPlugin({
2022-07-19 22:33:54 +08:00
cache: false,
2022-07-18 19:06:37 +08:00
include: ['src/**/*.vue', 'src/**/*.ts', 'src/**/*.tsx'] // 检查的文件
}),
VueI18n({
runtimeOnly: true,
compositionOnly: true,
include: [resolve(__dirname, 'src/locales/**')]
}),
createSvgIconsPlugin({
iconDirs: [pathResolve('src/assets/svgs')],
symbolId: 'icon-[dir]-[name]',
svgoOptions: true
}),
2022-07-20 16:47:51 +08:00
viteCompression({
verbose: true, // 是否在控制台输出压缩结果
2022-11-23 11:18:03 +08:00
disable: false, // 是否禁用
2022-07-20 16:47:51 +08:00
threshold: 10240, // 体积大于 threshold 才会被压缩,单位 b
algorithm: 'gzip', // 压缩算法,可选 [ 'gzip' , 'brotliCompress' ,'deflate' , 'deflateRaw']
ext: '.gz', // 生成的压缩包后缀
2022-11-23 11:18:03 +08:00
deleteOriginFile: true //压缩后是否删除源文件
2022-07-20 16:47:51 +08:00
}),
2022-07-18 19:06:37 +08:00
createHtmlPlugin({
inject: {
data: {
title: env.VITE_APP_TITLE,
2022-07-20 16:47:51 +08:00
injectScript: `<script src="./inject.js"></script>`
2022-07-18 19:06:37 +08:00
}
}
})
],
css: {
preprocessorOptions: {
2022-11-23 15:18:59 +08:00
scss: {
additionalData: '@import "./src/styles/variables.scss";',
2022-07-18 19:06:37 +08:00
javascriptEnabled: true
}
}
},
resolve: {
2022-11-23 15:18:59 +08:00
extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.scss', '.css'],
2022-07-18 19:06:37 +08:00
alias: [
{
find: 'vue-i18n',
replacement: 'vue-i18n/dist/vue-i18n.cjs.js'
},
{
find: /\@\//,
replacement: `${pathResolve('src')}/`
}
]
},
build: {
minify: 'terser',
outDir: env.VITE_OUT_DIR || 'dist',
sourcemap: env.VITE_SOURCEMAP === 'true' ? 'inline' : false,
2022-07-19 22:33:54 +08:00
// brotliSize: false,
2022-07-18 19:06:37 +08:00
terserOptions: {
compress: {
drop_debugger: env.VITE_DROP_DEBUGGER === 'true',
drop_console: env.VITE_DROP_CONSOLE === 'true'
}
}
},
optimizeDeps: {
include: [
'vue',
'vue-router',
'vue-types',
2022-07-28 12:17:47 +08:00
'vue-i18n',
2022-11-17 10:12:50 +08:00
'vxe-table',
'xe-utils',
2022-11-23 13:27:18 +08:00
'lodash-es',
2022-08-02 09:54:46 +08:00
'element-plus/es',
2022-07-18 19:06:37 +08:00
'element-plus/es/locale/lang/zh-cn',
'element-plus/es/locale/lang/en',
'@iconify/iconify',
'@vueuse/core',
'axios',
'qs',
2022-07-28 12:17:47 +08:00
'dayjs',
2022-07-18 19:06:37 +08:00
'echarts',
'echarts-wordcloud',
'intro.js',
'qrcode',
2022-07-28 12:17:47 +08:00
'pinia',
2022-08-03 16:01:06 +08:00
'crypto-js',
2022-07-18 19:06:37 +08:00
'@wangeditor/editor',
'@wangeditor/editor-for-vue'
]
}
}
}