Files
dyf-vue-ui/vite.config.ts

127 lines
4.0 KiB
TypeScript
Raw Permalink Normal View History

2025-06-26 15:29:07 +08:00
import { defineConfig, loadEnv } from 'vite';
import createPlugins from './vite/plugins';
import autoprefixer from 'autoprefixer'; // css自动添加兼容性前缀
import path from 'path';
export default defineConfig(({ mode, command }) => {
const env = loadEnv(mode, process.cwd());
2025-07-10 09:14:44 +08:00
// 参考eladmin-web的部署方式使用相对路径
let basePath = '/';
if (mode === 'production') {
// 生产环境:如果设置了上下文路径则使用,否则使用相对路径
basePath = env.VITE_APP_CONTEXT_PATH || './';
} else {
// 开发环境:使用上下文路径或默认根路径
basePath = env.VITE_APP_CONTEXT_PATH || '/';
}
2025-06-26 15:29:07 +08:00
return {
// 部署生产环境和开发环境下的URL。
// 默认情况下vite 会假设你的应用是被部署在一个域名的根路径上
// 例如 https://www.ruoyi.vip/。如果应用被部署在一个子路径上,你就需要用这个选项指定这个子路径。例如,如果你的应用被部署在 https://www.ruoyi.vip/admin/,则设置 baseUrl 为 /admin/。
2025-07-10 09:14:44 +08:00
base: basePath,
2025-06-26 15:29:07 +08:00
resolve: {
alias: {
'@': path.resolve(__dirname, './src')
},
extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
},
// https://cn.vitejs.dev/config/#resolve-extensions
plugins: createPlugins(env, command === 'build'),
server: {
host: '0.0.0.0',
port: Number(env.VITE_APP_PORT),
open: true,
proxy: {
[env.VITE_APP_BASE_API]: {
2025-07-10 09:14:44 +08:00
target: env.VITE_APP_PROXY_TARGET || 'http://localhost:8001',
2025-06-26 15:29:07 +08:00
changeOrigin: true,
ws: true,
rewrite: (path) => path.replace(new RegExp('^' + env.VITE_APP_BASE_API), '')
}
}
},
css: {
preprocessorOptions: {
scss: {
// additionalData: '@use "@/assets/styles/variables.module.scss as *";'
// javascriptEnabled: true
api: 'modern-compiler'
}
},
postcss: {
plugins: [
// 浏览器兼容性
autoprefixer(),
{
postcssPlugin: 'internal:charset-removal',
AtRule: {
charset: (atRule) => {
atRule.remove();
}
}
}
]
}
},
2025-07-10 09:14:44 +08:00
// 构建配置
build: {
outDir: 'dist',
assetsDir: 'static',
// 生产环境关闭source map
sourcemap: mode === 'development',
// 构建时清理输出目录
emptyOutDir: true,
// 大文件警告限制
chunkSizeWarningLimit: 2000,
// 代码分割配置
rollupOptions: {
output: {
// 静态资源分类打包
chunkFileNames: 'static/js/[name]-[hash].js',
entryFileNames: 'static/js/[name]-[hash].js',
assetFileNames: 'static/[ext]/[name]-[hash].[ext]',
// 代码分割
manualChunks: {
// 将 Vue 相关的包打包到一个 chunk 中
vue: ['vue', 'vue-router', 'pinia'],
// 将 Element Plus 相关的包打包到一个 chunk 中
elementPlus: ['element-plus', '@element-plus/icons-vue'],
// 将 ECharts 相关的包打包到一个 chunk 中
echarts: ['echarts'],
// 将工具库打包到一个 chunk 中
utils: ['axios', '@vueuse/core', 'crypto-js', 'js-cookie', 'jsencrypt'],
// 将编辑器相关的包打包到一个 chunk 中
editor: ['@vueup/vue-quill', 'vue-json-pretty']
}
}
},
// 压缩配置
minify: 'terser',
terserOptions: {
compress: {
// 生产环境移除console
drop_console: mode === 'production',
drop_debugger: mode === 'production'
}
}
},
2025-06-26 15:29:07 +08:00
// 预编译
optimizeDeps: {
include: [
'vue',
'vue-router',
'pinia',
'axios',
'@vueuse/core',
'echarts',
'vue-i18n',
'@vueup/vue-quill',
'image-conversion',
'element-plus/es/components/**/css'
]
}
};
});