wuhan-gl/vite.config.js

131 lines
4.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* @Author: 季万俊
* @Date: 2025-09-04 19:58:43
* @Description:
*/
import { defineConfig, loadEnv } from 'vite'
import path from 'path'
import createVitePlugins from './vite/plugins'
const baseUrl = 'http://172.16.1.165:18080'
// https://vitejs.dev/config/
export default defineConfig(({ mode, command }) => {
const env = loadEnv(mode, process.cwd())
const { VITE_APP_ENV } = env
// 获取资源版本(从环境变量)
// 开发模式下默认为空,生产模式下默认为 wuhan
const assetsVersion = process.env.ASSETS_VERSION || (command === 'build' ? 'wuhan' : '')
console.log(`\n📦 构建配置:`)
console.log(` 模式: ${mode}`)
console.log(` 命令: ${command}`)
console.log(` 资源版本: ${assetsVersion || '(空)'}\n`)
return {
// 部署生产环境和开发环境下的URL。
// 默认情况下vite 会假设你的应用是被部署在一个域名的根路径上
// 例如 https://www.ruoyi.vip/。如果应用被部署在一个子路径上,你就需要用这个选项指定这个子路径。例如,如果你的应用被部署在 https://www.ruoyi.vip/admin/,则设置 baseUrl 为 /admin/。
base: VITE_APP_ENV === 'production' ? '/' : '/',
// 注入环境变量到代码中
define: {
'__ASSETS_VERSION__': assetsVersion ? JSON.stringify(assetsVersion) : undefined,
'import.meta.env.VITE_ASSETS_VERSION': assetsVersion ? JSON.stringify(assetsVersion) : undefined
},
plugins: createVitePlugins(env, command === 'build', assetsVersion),
resolve: {
// https://cn.vitejs.dev/config/#resolve-alias
alias: {
// 设置路径
'~': path.resolve(__dirname, './'),
// 设置别名
'@': path.resolve(__dirname, './src'),
// 动态别名:指向当前版本的资源目录(如果有指定版本)
...(assetsVersion ? {
'@assets': path.resolve(__dirname, `./src/assets/${assetsVersion}`)
} : {})
},
// https://cn.vitejs.dev/config/#resolve-extensions
extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
},
// 允许将 fbx 当作静态资产处理,避免被当作 JS 解析
assetsInclude: ['**/*.fbx'],
// 打包配置
build: {
// https://vite.dev/config/build-options.html
sourcemap: command === 'build' ? false : 'inline',
outDir: 'dist',
assetsDir: 'assets',
chunkSizeWarningLimit: 2000,
rollupOptions: {
// 排除不需要的资源文件
external: (id) => {
// 只在有指定版本时才排除
if (!assetsVersion) {
return false
}
// 排除不需要版本的 plan 组件
const excludeVersions = assetsVersion === 'wuhan' ? ['shiyan'] : ['wuhan']
for (const excludeVersion of excludeVersions) {
if (id.includes(`/plan/${excludeVersion}.vue`)) {
console.log(` 🚫 External: plan/${excludeVersion}.vue`)
return true
}
}
return false
},
output: {
chunkFileNames: 'static/js/[name]-[hash].js',
entryFileNames: 'static/js/[name]-[hash].js',
assetFileNames: 'static/[ext]/[name]-[hash].[ext]'
}
}
},
// vite 相关配置
server: {
port: 80,
host: true,
open: true,
proxy: {
// https://cn.vitejs.dev/config/#server-proxy
'/api': {
target: baseUrl,
changeOrigin: true,
// pathRewrite: { '^/dev-api': '' }
},
}
},
css: {
preprocessorOptions: {
scss: {
// 屏蔽Sass的@import deprecation警告
quietDeps: true, // 抑制依赖中的警告
// 可选针对特定警告代码进行屏蔽Sass的@import警告代码为deprecation-module-import
warningSuppress: [
{ code: 'deprecation-module-import' }
]
}
},
postcss: {
plugins: [
{
postcssPlugin: 'internal:charset-removal',
AtRule: {
charset: (atRule) => {
if (atRule.name === 'charset') {
atRule.remove()
}
}
}
}
]
}
}
}
})