2026-07-28 16:26:07 +08:00
|
|
|
import { defineConfig, loadEnv, normalizePath } from 'vite'
|
2025-08-21 14:11:58 +08:00
|
|
|
import path from 'path'
|
2026-08-25 14:04:27 +08:00
|
|
|
import { realpathSync } from 'node:fs'
|
2025-08-21 14:11:58 +08:00
|
|
|
import createVitePlugins from './vite/plugins'
|
|
|
|
|
|
2026-08-25 14:04:27 +08:00
|
|
|
const isCommonLinked = (() => {
|
|
|
|
|
try {
|
|
|
|
|
const linkedPath = realpathSync(path.resolve('node_modules/@cmvr/iot-common'))
|
|
|
|
|
const localPath = realpathSync(path.resolve('../cmvr-iot-common'))
|
|
|
|
|
return path.relative(linkedPath, localPath) === ''
|
|
|
|
|
} catch {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
})()
|
|
|
|
|
|
2025-08-21 14:11:58 +08:00
|
|
|
// https://vitejs.dev/config/
|
2026-07-28 16:26:07 +08:00
|
|
|
export default defineConfig(({ mode, command }) => {
|
|
|
|
|
const env = loadEnv(mode, process.cwd())
|
|
|
|
|
const { VITE_APP_ENV, VITE_API_URL, VITE_PORT } = env
|
|
|
|
|
const variablePath = normalizePath(path.resolve('./src/style/variable.scss'))
|
|
|
|
|
return {
|
|
|
|
|
base: VITE_APP_ENV === 'production' ? '/' : '/',
|
|
|
|
|
plugins: createVitePlugins(env, command === 'build'),
|
|
|
|
|
optimizeDeps: {
|
|
|
|
|
exclude: [
|
2026-08-26 08:45:14 +08:00
|
|
|
...(isCommonLinked ? ['@cmvr/iot-common'] : [])
|
2026-07-28 16:26:07 +08:00
|
|
|
],
|
|
|
|
|
include: [
|
2026-08-26 08:45:14 +08:00
|
|
|
'monaco-editor/esm/vs/editor/editor.all.js',
|
|
|
|
|
'monaco-editor/esm/vs/editor/editor.api.js',
|
|
|
|
|
'monaco-editor/esm/vs/basic-languages/javascript/javascript.contribution.js',
|
|
|
|
|
'monaco-editor/esm/vs/language/json/monaco.contribution.js',
|
2026-07-28 16:26:07 +08:00
|
|
|
'monaco-editor/esm/vs/language/typescript/monaco.contribution'
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
resolve: {
|
|
|
|
|
// https://cn.vitejs.dev/config/#resolve-alias
|
|
|
|
|
alias: {
|
|
|
|
|
// 设置路径
|
|
|
|
|
'~': path.resolve(__dirname, './'),
|
|
|
|
|
// 设置别名
|
2026-09-20 09:58:29 +08:00
|
|
|
'@': path.resolve(__dirname, './src'),
|
|
|
|
|
'@cmvr/iot-common': path.resolve(__dirname, '../cmvr-iot-common/dist')
|
2026-07-28 16:26:07 +08:00
|
|
|
},
|
2026-08-25 10:26:33 +08:00
|
|
|
dedupe: ['vue', 'vue-router', 'pinia', 'element-plus', '@element-plus/icons-vue'],
|
2026-07-28 16:26:07 +08:00
|
|
|
// https://cn.vitejs.dev/config/#resolve-extensions
|
|
|
|
|
extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
|
|
|
|
|
},
|
|
|
|
|
// 打包配置
|
|
|
|
|
build: {
|
|
|
|
|
// https://vite.dev/config/build-options.html
|
|
|
|
|
sourcemap: command === 'build' ? false : 'inline',
|
|
|
|
|
outDir: 'dist',
|
|
|
|
|
assetsDir: 'assets',
|
|
|
|
|
chunkSizeWarningLimit: 2000,
|
|
|
|
|
rollupOptions: {
|
|
|
|
|
output: {
|
2026-08-26 08:45:14 +08:00
|
|
|
manualChunks(id) {
|
|
|
|
|
if (id.includes('/node_modules/monaco-editor/')) return 'monaco'
|
2026-07-28 16:26:07 +08:00
|
|
|
},
|
|
|
|
|
chunkFileNames: 'static/js/[name]-[hash].js',
|
|
|
|
|
entryFileNames: 'static/js/[name]-[hash].js',
|
|
|
|
|
assetFileNames: 'static/[ext]/[name]-[hash].[ext]'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
// vite 相关配置
|
|
|
|
|
server: {
|
|
|
|
|
port: Number(VITE_PORT || 80),
|
|
|
|
|
host: true,
|
|
|
|
|
open: true,
|
2026-08-25 14:04:27 +08:00
|
|
|
...(isCommonLinked ? {
|
|
|
|
|
watch: {
|
|
|
|
|
ignored: ['!**/node_modules/@cmvr/iot-common/**']
|
|
|
|
|
}
|
|
|
|
|
} : {}),
|
2026-07-28 16:26:07 +08:00
|
|
|
proxy: {
|
|
|
|
|
// https://cn.vitejs.dev/config/#server-proxy
|
|
|
|
|
'/dev-api': {
|
|
|
|
|
target: VITE_API_URL || 'http://localhost:13080',
|
|
|
|
|
changeOrigin: true,
|
|
|
|
|
rewrite: (p) => p.replace(/^\/dev-api/, '')
|
2026-03-04 14:41:38 +08:00
|
|
|
},
|
2026-07-28 16:26:07 +08:00
|
|
|
// springdoc proxy
|
|
|
|
|
'^/v3/api-docs/(.*)': {
|
|
|
|
|
target: VITE_API_URL || 'http://localhost:13080',
|
|
|
|
|
changeOrigin: true,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
css: {
|
|
|
|
|
postcss: {
|
|
|
|
|
plugins: [
|
|
|
|
|
{
|
|
|
|
|
postcssPlugin: 'internal:charset-removal',
|
|
|
|
|
AtRule: {
|
|
|
|
|
charset: (atRule) => {
|
|
|
|
|
if (atRule.name === 'charset') {
|
|
|
|
|
atRule.remove()
|
2025-08-21 14:11:58 +08:00
|
|
|
}
|
2026-07-28 16:26:07 +08:00
|
|
|
}
|
2025-08-21 14:11:58 +08:00
|
|
|
}
|
2026-07-28 16:26:07 +08:00
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
preprocessorOptions: {
|
|
|
|
|
scss: {
|
|
|
|
|
additionalData: `@use "${variablePath}" as *;`
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-09-11 10:27:38 +08:00
|
|
|
},
|
|
|
|
|
define: {
|
|
|
|
|
// 将环境变量注入到全局常量中
|
|
|
|
|
'__PROJECT_NAME__': JSON.stringify(env.PROJECT_NAME || 'changan'),
|
|
|
|
|
},
|
2026-07-28 16:26:07 +08:00
|
|
|
}
|
2025-08-21 14:11:58 +08:00
|
|
|
})
|