gohttpdUi/vite.config.ts

178 lines
4.8 KiB
TypeScript
Raw Normal View History

2021-12-07 16:36:16 +08:00
import { resolve } from 'path'
import { loadEnv } from 'vite'
import type { UserConfig, ConfigEnv } from 'vite'
import Vue from '@vitejs/plugin-vue'
import VueJsx from '@vitejs/plugin-vue-jsx'
2022-12-21 09:15:01 +08:00
import progress from 'vite-plugin-progress'
2021-12-07 16:36:16 +08:00
import EslintPlugin from 'vite-plugin-eslint'
import { ViteEjsPlugin } from 'vite-plugin-ejs'
2023-12-10 10:11:14 +08:00
import { viteMockServe } from 'vite-plugin-mock'
2022-12-20 09:42:35 +08:00
import PurgeIcons from 'vite-plugin-purge-icons'
import VueI18nPlugin from '@intlify/unplugin-vue-i18n/vite'
2022-12-20 09:42:35 +08:00
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import { createStyleImportPlugin, ElementPlusResolve } from 'vite-plugin-style-import'
2023-04-17 09:23:03 +08:00
import UnoCSS from 'unocss/vite'
2023-12-21 11:23:41 +08:00
import { visualizer } from 'rollup-plugin-visualizer'
2021-10-10 09:59:52 +08:00
// https://vitejs.dev/config/
2021-12-07 16:36:16 +08:00
const root = process.cwd()
function pathResolve(dir: string) {
return resolve(root, '.', dir)
}
export default ({ command, mode }: ConfigEnv): UserConfig => {
2022-06-24 21:36:33 +08:00
let env = {} as any
2022-01-08 18:38:20 +08:00
const isBuild = command === 'build'
if (!isBuild) {
env = loadEnv(process.argv[3] === '--mode' ? process.argv[4] : process.argv[3], root)
2022-01-07 17:38:24 +08:00
} else {
2021-12-07 16:36:16 +08:00
env = loadEnv(mode, root)
}
return {
base: env.VITE_BASE_PATH,
plugins: [
2023-08-26 10:10:49 +08:00
Vue({
script: {
// 开启defineModel
defineModel: true
}
}),
2021-12-07 16:36:16 +08:00
VueJsx(),
2022-12-21 09:15:01 +08:00
progress(),
2023-12-25 16:27:55 +08:00
env.VITE_USE_ALL_ELEMENT_PLUS_STYLE === 'false'
? createStyleImportPlugin({
resolves: [ElementPlusResolve()],
libs: [
{
libraryName: 'element-plus',
esModule: true,
resolveStyle: (name) => {
if (name === 'click-outside') {
return ''
}
return `element-plus/es/components/${name.replace(/^el-/, '')}/style/css`
}
}
2023-12-25 16:27:55 +08:00
]
})
: undefined,
2023-07-20 16:37:18 +08:00
EslintPlugin({
cache: false,
include: ['src/**/*.vue', 'src/**/*.ts', 'src/**/*.tsx'] // 检查的文件
}),
2022-12-20 09:42:35 +08:00
VueI18nPlugin({
2021-12-08 10:47:33 +08:00
runtimeOnly: true,
compositionOnly: true,
include: [resolve(__dirname, 'src/locales/**')]
}),
createSvgIconsPlugin({
iconDirs: [pathResolve('src/assets/svgs')],
symbolId: 'icon-[dir]-[name]',
svgoOptions: true
}),
2022-01-07 17:38:24 +08:00
PurgeIcons(),
2023-12-25 17:01:33 +08:00
env.VITE_USE_MOCK === 'true'
? viteMockServe({
ignore: /^\_/,
mockPath: 'mock',
localEnabled: !isBuild,
prodEnabled: isBuild,
injectCode: `
2023-12-10 10:11:14 +08:00
import { setupProdMockServer } from '../mock/_createProductionServer'
2022-01-07 17:38:24 +08:00
2023-12-10 10:11:14 +08:00
setupProdMockServer()
`
2023-12-25 17:01:33 +08:00
})
: undefined,
2022-12-05 16:09:14 +08:00
ViteEjsPlugin({
title: env.VITE_APP_TITLE
2023-04-17 09:23:03 +08:00
}),
UnoCSS()
2021-12-07 16:36:16 +08:00
],
css: {
preprocessorOptions: {
less: {
additionalData: '@import "./src/styles/variables.module.less";',
2021-12-07 16:36:16 +08:00
javascriptEnabled: true
}
}
},
resolve: {
extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.less', '.css'],
2021-12-07 16:36:16 +08:00
alias: [
2021-12-15 17:16:53 +08:00
{
find: 'vue-i18n',
replacement: 'vue-i18n/dist/vue-i18n.cjs.js'
},
2021-12-07 16:36:16 +08:00
{
find: /\@\//,
replacement: `${pathResolve('src')}/`
}
]
},
build: {
minify: 'terser',
outDir: env.VITE_OUT_DIR || 'dist',
2023-12-25 15:16:08 +08:00
sourcemap: env.VITE_SOURCEMAP === 'true',
2022-07-16 11:09:30 +08:00
// brotliSize: false,
2021-12-07 16:36:16 +08:00
terserOptions: {
compress: {
drop_debugger: env.VITE_DROP_DEBUGGER === 'true',
drop_console: env.VITE_DROP_CONSOLE === 'true'
}
2023-12-21 11:23:41 +08:00
},
rollupOptions: {
2023-12-25 16:27:55 +08:00
plugins: env.VITE_USE_BUNDLE_ANALYZER === 'true' ? [visualizer()] : undefined,
// 拆包
output: {
manualChunks: {
vue: ['vue', 'vue-router', 'pinia', 'vue-i18n'],
'element-plus': ['element-plus'],
'wang-editor': ['@wangeditor/editor', '@wangeditor/editor-for-vue'],
echarts: ['echarts', 'echarts-wordcloud']
}
}
2021-12-07 16:36:16 +08:00
}
},
server: {
2022-03-26 10:14:24 +08:00
port: 4000,
2021-12-07 16:36:16 +08:00
proxy: {
// 选项写法
2022-06-23 22:37:49 +08:00
'/api': {
target: 'http://127.0.0.1:8000',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, '')
2022-06-23 22:37:49 +08:00
}
},
hmr: {
overlay: false
2022-01-19 16:29:35 +08:00
},
host: '0.0.0.0'
2021-12-07 16:36:16 +08:00
},
optimizeDeps: {
include: [
'vue',
2021-12-10 17:10:51 +08:00
'vue-router',
2021-12-30 17:25:51 +08:00
'vue-types',
2022-03-13 10:31:42 +08:00
'element-plus/es/locale/lang/zh-cn',
'element-plus/es/locale/lang/en',
2022-01-05 17:02:25 +08:00
'@iconify/iconify',
2022-01-08 18:38:20 +08:00
'@vueuse/core',
'axios',
2022-01-23 14:35:46 +08:00
'qs',
'echarts',
'echarts-wordcloud',
'qrcode',
'@wangeditor/editor',
2023-08-26 10:10:49 +08:00
'@wangeditor/editor-for-vue',
2023-09-19 10:15:02 +08:00
'vue-json-pretty',
'@zxcvbn-ts/core',
2023-09-27 20:26:50 +08:00
'dayjs',
2023-11-15 09:59:17 +08:00
'cropperjs'
2021-12-07 16:36:16 +08:00
]
}
}
}