vite.config.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. import mockDevServer from 'vite-plugin-mock-dev-server';
  14. // 引入svg需要用到的插件, 安装 yarn add vite-plugin-svg-icons
  15. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
  16. // 导入低码代理配置和忽略编译配置
  17. import { watchOptionsIgnored } from '@yuxp/previewer/src/integration/server';
  18. import createYuxpServerPlugin from '@yuxp/integration/yuxpServer/vite-plugin-yuxp-server';
  19. import { getProxyConfig } from './build-proxy';
  20. import insertYuxpProxy, { VITE_APP_YUXP_WATCH_IGNORED } from './src/config/yuxp/proxyConfig.js';
  21. // 根据运行模式加载环境变量,并配置Vite
  22. export default ({ mode }) => {
  23. // 加载环境变量
  24. const loadEnvVar = loadEnv(mode, process.cwd());
  25. const {
  26. VITE_APP_DEV_PORT,
  27. VITE_APP_BASE_URL,
  28. VITE_APP_PROXY_API,
  29. VITE_APP_BASE_API,
  30. VITE_APP_BASE_AUTH,
  31. VITE_APP_PROXY_SHUFFLE,
  32. } = loadEnvVar;
  33. const proxy = getProxyConfig(loadEnvVar, insertYuxpProxy);
  34. const viteHost = '0.0.0.0';
  35. // 返回Vite配置对象
  36. return defineConfig({
  37. define: {
  38. 'process.env': {},
  39. },
  40. base: VITE_APP_BASE_URL, // 设置项目基路径
  41. plugins: [
  42. vue2(),
  43. vue2Jsx(),
  44. // mockDevServer(),
  45. createSvgIconsPlugin({
  46. // 配置svg图标
  47. iconDirs: [
  48. resolve(process.cwd(), 'src/style/svg'),
  49. resolve(process.cwd(), 'src/views/@xdjf/card/style/svg'),
  50. resolve(process.cwd(), 'src/views/@xdjf/intelligent-due-diligence/assets/icons/svg'),
  51. resolve(process.cwd(), 'src/views/@xdjf/intelligent-due-diligence/assets/icons/chatSvg'),
  52. resolve(process.cwd(), 'src/views/@xdjf/financial-analysis/assets/icons/svg'),
  53. ],
  54. symbolId: 'icon-[name]',
  55. }),
  56. // 低码yuxpServer服务插件
  57. //createYuxpServerPlugin(),
  58. viteExternalsPlugin({
  59. echarts: 'echarts',
  60. }),
  61. ], // 使用的插件
  62. resolve: {
  63. alias: {
  64. package: resolve(__dirname, 'package.json'),
  65. 'yuxp-web': resolve('src'),
  66. assets: resolve(__dirname, 'src/assets'),
  67. static: resolve(__dirname, 'public/static'),
  68. '@': resolve(__dirname, 'src'), // 设置别名为src目录
  69. '@xdjf/demo-web': resolve(__dirname, 'src/views/@xdjf/demo-web'),
  70. '@xdjf/card': resolve(__dirname, 'src/views/@xdjf/card'),
  71. '@xdjf/idd': resolve(__dirname, 'src/views/@xdjf/intelligent-due-diligence'), // 智能尽调
  72. '@xdjf/fa': resolve(__dirname, 'src/views/@xdjf/financial-analysis'), // 财务分析
  73. vue: resolve('node_modules/vue/dist/vue.esm.js'),
  74. '@yufp/store': resolve(__dirname, 'node_modules/@yufp/store/vuex/'),
  75. },
  76. // 省略后缀
  77. extensions: ['.vue', '.js', '.json', '.ts'],
  78. },
  79. // scss全局变量的配置
  80. css: {
  81. preprocessorOptions: {
  82. scss: {
  83. additionalData: '@import "./src/style/variables.scss";',
  84. },
  85. },
  86. },
  87. optimizeDeps: {
  88. // 其他常用依赖
  89. include: [
  90. 'vue',
  91. 'echarts',
  92. 'vue-router',
  93. 'vuex',
  94. 'element-ui',
  95. 'axios',
  96. 'lodash',
  97. // shuffle 集成
  98. 'codemirror/addon/selection/active-line',
  99. 'codemirror/addon/hint/show-hint',
  100. 'codemirror/addon/edit/matchbrackets',
  101. 'codemirror/addon/edit/closebrackets',
  102. 'codemirror/mode/javascript/javascript',
  103. 'codemirror/mode/sql/sql',
  104. 'codemirror/lib/codemirror',
  105. ],
  106. esbuildOptions: {
  107. target: 'es2015',
  108. },
  109. exclude: ['@ggzj/shuffle-lite-web'],
  110. },
  111. server: {
  112. // 服务器配置
  113. port: VITE_APP_DEV_PORT, // 设置开发服务器端口号
  114. host: viteHost, // 监听所有网络接口
  115. cors: true, // 启用跨域请求
  116. proxy: proxy, // 使用定义的代理规则
  117. watch: {
  118. ignored: (filePath) => watchOptionsIgnored(filePath, VITE_APP_YUXP_WATCH_IGNORED),
  119. },
  120. },
  121. build: {
  122. // 构建配置
  123. target: 'es2015', // 设置目标浏览器兼容版本
  124. sourcemap: false, // 是否生成源码映射
  125. chunkSizeWarningLimit: 2000, // 设置块大小警告限制
  126. reportCompressedSize: false, // 是否报告压缩后的大小
  127. minify: 'terser',
  128. cssMinify: false,
  129. terserOptions: {
  130. compress: {
  131. drop_debugger: true,
  132. drop_console: true,
  133. },
  134. },
  135. },
  136. });
  137. };