vite.config.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * @Author: yufp
  3. * @Date: 2024-03-25 10:33:01
  4. * @LastEditors: zhanglin3
  5. * @LastEditTime: 2024-10-17 17:42:03
  6. * @Description: 定义开发和构建的配置
  7. */
  8. import { resolve } from 'path';
  9. import { defineConfig, loadEnv } from 'vite';
  10. import vue2 from '@vitejs/plugin-vue2';
  11. import vue2Jsx from '@vitejs/plugin-vue2-jsx';
  12. import { viteExternalsPlugin } from 'vite-plugin-externals';
  13. // 引入svg需要用到的插件, 安装 yarn add vite-plugin-svg-icons
  14. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
  15. // 导入低码代理配置和忽略编译配置
  16. import { getProxyConfig } from './build-proxy';
  17. import insertYuxpProxy from './src/config/yuxp/proxyConfig.js';
  18. // 根据运行模式加载环境变量,并配置Vite
  19. export default ({ mode }) => {
  20. // 加载环境变量
  21. const loadEnvVar = loadEnv(mode, process.cwd());
  22. const {
  23. VITE_APP_DEV_PORT,
  24. VITE_APP_BASE_URL,
  25. } = loadEnvVar;
  26. const proxy = getProxyConfig(loadEnvVar, insertYuxpProxy);
  27. const viteHost = '0.0.0.0';
  28. // 判断是否为开发模式
  29. const isDev = mode === 'development';
  30. // 返回Vite配置对象
  31. return defineConfig({
  32. define: {
  33. 'process.env': {},
  34. },
  35. base: VITE_APP_BASE_URL, // 设置项目基路径
  36. plugins: [
  37. vue2(),
  38. vue2Jsx(),
  39. createSvgIconsPlugin({
  40. // 配置svg图标
  41. iconDirs: [
  42. resolve(process.cwd(), 'src/style/svg'),
  43. ],
  44. symbolId: 'icon-[name]',
  45. }),
  46. // 生产模式下使用 externals
  47. !isDev && viteExternalsPlugin({
  48. echarts: 'echarts',
  49. }),
  50. ].filter(Boolean), // 过滤掉 false 值
  51. resolve: {
  52. alias: {
  53. package: resolve(__dirname, 'package.json'),
  54. 'yuxp-web': resolve('src'),
  55. assets: resolve(__dirname, 'src/assets'),
  56. static: resolve(__dirname, 'public/static'),
  57. '@': resolve(__dirname, 'src'), // 设置别名为src目录
  58. vue: resolve('node_modules/vue/dist/vue.esm.js'),
  59. '@yufp/store': resolve(__dirname, 'node_modules/@yufp/store/vuex/'),
  60. },
  61. // 省略后缀
  62. extensions: ['.vue', '.js', '.json', '.ts'],
  63. },
  64. // scss全局变量的配置
  65. css: {
  66. preprocessorOptions: {
  67. scss: {
  68. additionalData: '@import "./src/style/variables.scss";',
  69. },
  70. },
  71. },
  72. optimizeDeps: {
  73. // 其他常用依赖
  74. include: [
  75. 'vue',
  76. 'echarts',
  77. 'vue-router',
  78. 'vuex',
  79. 'element-ui',
  80. 'axios',
  81. 'lodash',
  82. // shuffle 集成
  83. 'codemirror/addon/selection/active-line',
  84. 'codemirror/addon/hint/show-hint',
  85. 'codemirror/addon/edit/matchbrackets',
  86. 'codemirror/addon/edit/closebrackets',
  87. 'codemirror/mode/javascript/javascript',
  88. 'codemirror/mode/sql/sql',
  89. 'codemirror/lib/codemirror',
  90. ],
  91. esbuildOptions: {
  92. target: 'es2015',
  93. },
  94. exclude: ['@ggzj/shuffle-lite-web'],
  95. },
  96. server: {
  97. // 服务器配置
  98. port: VITE_APP_DEV_PORT, // 设置开发服务器端口号
  99. host: viteHost, // 监听所有网络接口
  100. cors: true, // 启用跨域请求
  101. proxy: proxy, // 使用定义的代理规则
  102. // 优化热更新配置
  103. hmr: {
  104. overlay: false, // 关闭错误遮罩,提高响应速度
  105. },
  106. // 优化文件监听配置
  107. watch: {
  108. // 使用数组形式的忽略规则,性能更好
  109. ignored: [
  110. '**/node_modules/**',
  111. '**/.git/**',
  112. '**/dist/**',
  113. '**/coverage/**',
  114. '**/*.log',
  115. '**/.temp/**',
  116. '**/.cache/**',
  117. '**/public/**',
  118. '**/.vscode/**',
  119. '**/.idea/**',
  120. ],
  121. // 使用原生监听,提高性能
  122. usePolling: false,
  123. // 增加防抖延迟,减少频繁触发
  124. interval: 500,
  125. },
  126. },
  127. build: {
  128. // 构建配置
  129. target: 'es2015', // 设置目标浏览器兼容版本
  130. sourcemap: false, // 是否生成源码映射
  131. chunkSizeWarningLimit: 2000, // 设置块大小警告限制
  132. reportCompressedSize: false, // 是否报告压缩后的大小
  133. // 开发模式下禁用压缩,提高构建速度
  134. minify: isDev ? false : 'terser',
  135. cssMinify: !isDev,
  136. terserOptions: {
  137. compress: {
  138. drop_debugger: true,
  139. drop_console: true,
  140. },
  141. },
  142. },
  143. });
  144. };