vue.config.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // vue.config.js
  2. const { defineConfig } = require('@vue/cli-service')
  3. module.exports = defineConfig({
  4. transpileDependencies: true,
  5. //跨域
  6. // --- 核心配置开始 ---
  7. devServer: {
  8. proxy: {
  9. '/api': {
  10. // ⚠️【重要】这里必须改成你真实的后端地址!
  11. // 如果后端在本地,可能是 http://localhost:8000
  12. // 如果是线上测试服,可能是 http://47.100.xx.xx
  13. target: 'http://backend.66linknow.com',
  14. changeOrigin: true, // 允许跨域
  15. },
  16. // 2.【新增】WebSocket 代理配置
  17. '/ws/kline': {
  18. target: 'http://backend.66linknow.com', // 后端 IP
  19. changeOrigin: true,
  20. ws: true // ⚠️ 开启 WebSocket 支持
  21. // 这里是否需要 pathRewrite 取决于后端路径有没有 /ws
  22. }
  23. }
  24. },
  25. // 1. 基础路径 (解决白屏问题)
  26. publicPath: './',
  27. // 2. 关闭 SourceMap (最简单粗暴的减体积)
  28. // 生产环境不生成 .map 文件,体积能减少 50% 以上,且防源码泄露
  29. productionSourceMap: false,
  30. // 3. Webpack 核心优化 (代码分割)
  31. configureWebpack: config => {
  32. // 只有在打包生产环境 (npm run build) 时才运行优化
  33. if (process.env.NODE_ENV === 'production') {
  34. // 开启代码分割 (SplitChunks)
  35. config.optimization.splitChunks = {
  36. chunks: 'all', // 对所有代码进行分割 (无论是异步还是同步)
  37. // 缓存组配置:决定什么代码打包到什么文件里
  38. cacheGroups: {
  39. // 组1: 第三方库 (node_modules)
  40. // 作用:把所有 node_modules 里的东西拆出来
  41. libs: {
  42. name: 'chunk-libs',
  43. test: /[\\/]node_modules[\\/]/,
  44. priority: 10,
  45. chunks: 'initial' // 只打包初始依赖
  46. },
  47. // 组2: Vant UI 组件库 (单独拆分)
  48. // 作用:Vant 体积较大,单独打包有利于缓存
  49. vant: {
  50. name: 'chunk-vant',
  51. priority: 20, // 优先级更高,会先被提取
  52. test: /[\\/]node_modules[\\/]_?vant(.*)/
  53. },
  54. // 组3: 公共代码
  55. // 作用:如果你的多个页面都引用了同一个组件,提取出来公用
  56. commons: {
  57. name: 'chunk-commons',
  58. test: /[\\/]src[\\/]components[\\/]/, // 匹配 src/components 下的组件
  59. minChunks: 2, // 只要被引用2次及以上就提取
  60. priority: 5,
  61. reuseExistingChunk: true
  62. }
  63. }
  64. };
  65. }
  66. },
  67. })