102 lines
2.6 KiB
JavaScript
102 lines
2.6 KiB
JavaScript
import { defineConfig } from 'vite'
|
|
import vue from '@vitejs/plugin-vue'
|
|
import AutoImport from 'unplugin-auto-import/vite'
|
|
import Components from 'unplugin-vue-components/vite'
|
|
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
|
|
import { resolve } from 'path'
|
|
import { fileURLToPath, URL } from 'node:url'
|
|
|
|
export default defineConfig(({ mode }) => {
|
|
// 根据构建模式确定后端服务器地址
|
|
const getBackendUrl = () => {
|
|
if (mode === 'production') {
|
|
return 'http://192.168.1.36:8080' // 生产环境后端地址
|
|
}
|
|
return 'http://localhost:8080' // 开发环境后端地址
|
|
}
|
|
|
|
const backendUrl = getBackendUrl()
|
|
const apiBaseUrl = `${backendUrl}/api`
|
|
console.log(`构建模式: ${mode}, 后端地址: ${backendUrl}, API基础地址: ${apiBaseUrl}`)
|
|
|
|
return {
|
|
plugins: [
|
|
vue(),
|
|
AutoImport({
|
|
resolvers: [ElementPlusResolver()],
|
|
}),
|
|
Components({
|
|
resolvers: [ElementPlusResolver()],
|
|
}),
|
|
],
|
|
resolve: {
|
|
alias: {
|
|
'@': fileURLToPath(new URL('./src', import.meta.url))
|
|
},
|
|
},
|
|
define: {
|
|
// 为了兼容一些依赖包
|
|
global: 'globalThis',
|
|
// 定义环境变量
|
|
__VITE_APP_BASE_API__: JSON.stringify(apiBaseUrl),
|
|
},
|
|
server: {
|
|
host: '0.0.0.0',
|
|
port: 3000,
|
|
open: true,
|
|
proxy: {
|
|
'/api': {
|
|
target: backendUrl,
|
|
changeOrigin: true,
|
|
secure: false,
|
|
},
|
|
'/ws': {
|
|
target: backendUrl.replace('http', 'ws'),
|
|
ws: true,
|
|
changeOrigin: true,
|
|
},
|
|
},
|
|
},
|
|
build: {
|
|
outDir: 'dist',
|
|
sourcemap: false,
|
|
chunkSizeWarningLimit: 1600,
|
|
rollupOptions: {
|
|
input: {
|
|
main: resolve(fileURLToPath(new URL('./', import.meta.url)), 'index.html')
|
|
},
|
|
output: {
|
|
manualChunks(id) {
|
|
if (id.includes('node_modules')) {
|
|
return id.toString().split('node_modules/')[1].split('/')[0].toString()
|
|
}
|
|
},
|
|
},
|
|
},
|
|
},
|
|
optimizeDeps: {
|
|
include: [
|
|
'vue',
|
|
'vue-router',
|
|
'vuex',
|
|
'axios',
|
|
'element-plus',
|
|
'@element-plus/icons-vue',
|
|
'echarts',
|
|
'dayjs',
|
|
'dayjs/plugin/customParseFormat',
|
|
'dayjs/plugin/advancedFormat',
|
|
'dayjs/plugin/localeData',
|
|
'dayjs/plugin/weekOfYear',
|
|
'dayjs/plugin/weekYear',
|
|
'dayjs/plugin/dayOfYear',
|
|
'dayjs/plugin/isSameOrAfter',
|
|
'dayjs/plugin/isSameOrBefore',
|
|
'dayjs/plugin/utc',
|
|
'dayjs/plugin/timezone'
|
|
],
|
|
exclude: [],
|
|
force: true
|
|
},
|
|
}
|
|
})
|