vite.config.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { defineConfig, loadEnv } from 'vite';
  2. import createPlugins from './vite/plugins';
  3. import autoprefixer from 'autoprefixer'; // css自动添加兼容性前缀
  4. import path from 'path';
  5. export default defineConfig(({ mode, command }) => {
  6. const env = loadEnv(mode, process.cwd());
  7. const isBuild = command === 'build';
  8. // CI / 小内存机器:限制并行、跳过 gzip 插件(产物外层已 tar.gz)
  9. const lowMem = process.env.CI === 'true' || process.env.VITE_LOW_MEM === '1';
  10. return {
  11. // 部署生产环境和开发环境下的URL。
  12. // 默认情况下,vite 会假设你的应用是被部署在一个域名的根路径上
  13. // 例如 https://www.ruoyi.vip/。如果应用被部署在一个子路径上,你就需要用这个选项指定这个子路径。例如,如果你的应用被部署在 https://www.ruoyi.vip/admin/,则设置 baseUrl 为 /admin/。
  14. base: env.VITE_APP_CONTEXT_PATH || '/',
  15. resolve: {
  16. alias: {
  17. '@': path.resolve(__dirname, './src')
  18. },
  19. extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
  20. },
  21. // https://cn.vitejs.dev/config/#resolve-extensions
  22. plugins: createPlugins(env, isBuild),
  23. server: {
  24. host: '0.0.0.0',
  25. port: Number(env.VITE_APP_PORT),
  26. open: true,
  27. proxy: {
  28. [env.VITE_APP_BASE_API]: {
  29. target: 'http://localhost:8080',
  30. changeOrigin: true,
  31. ws: true,
  32. rewrite: (path) => path.replace(new RegExp('^' + env.VITE_APP_BASE_API), '')
  33. }
  34. }
  35. },
  36. css: {
  37. preprocessorOptions: {
  38. scss: {
  39. // additionalData: '@use "@/assets/styles/variables.module.scss as *";'
  40. // javascriptEnabled: true
  41. api: 'modern-compiler'
  42. }
  43. },
  44. postcss: {
  45. plugins: [
  46. // 浏览器兼容性
  47. autoprefixer(),
  48. {
  49. postcssPlugin: 'internal:charset-removal',
  50. AtRule: {
  51. charset: (atRule) => {
  52. atRule.remove();
  53. }
  54. }
  55. }
  56. ]
  57. }
  58. },
  59. esbuild: {
  60. legalComments: 'none'
  61. },
  62. build: {
  63. sourcemap: false,
  64. // 控制台 gzip 体积统计会再压缩一遍全部产物,大项目很吃内存
  65. reportCompressedSize: false,
  66. chunkSizeWarningLimit: 2000,
  67. // esbuild 比 terser 省内存、更快
  68. minify: 'esbuild',
  69. cssMinify: 'esbuild',
  70. target: 'es2020',
  71. // 小内存 / CI:串行读文件,显著降低 transform 峰值
  72. rollupOptions: {
  73. maxParallelFileOps: lowMem ? 1 : 2
  74. }
  75. },
  76. // 预编译
  77. optimizeDeps: {
  78. include: [
  79. 'vue',
  80. 'vue-router',
  81. 'pinia',
  82. 'axios',
  83. '@vueuse/core',
  84. 'echarts',
  85. 'vue-i18n',
  86. '@vueup/vue-quill',
  87. 'image-conversion'
  88. ]
  89. }
  90. };
  91. });