vue.config.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // vue.config.js
  2. const { defineConfig } = require('@vue/cli-service')
  3. module.exports = defineConfig({
  4. transpileDependencies: true,
  5. // 1. 基础路径 (解决白屏问题)
  6. publicPath: './',
  7. // 2. 关闭 SourceMap (最简单粗暴的减体积)
  8. // 生产环境不生成 .map 文件,体积能减少 50% 以上,且防源码泄露
  9. productionSourceMap: false,
  10. // 3. Webpack 核心优化 (代码分割)
  11. configureWebpack: config => {
  12. // 只有在打包生产环境 (npm run build) 时才运行优化
  13. if (process.env.NODE_ENV === 'production') {
  14. // 开启代码分割 (SplitChunks)
  15. config.optimization.splitChunks = {
  16. chunks: 'all', // 对所有代码进行分割 (无论是异步还是同步)
  17. // 缓存组配置:决定什么代码打包到什么文件里
  18. cacheGroups: {
  19. // 组1: 第三方库 (node_modules)
  20. // 作用:把所有 node_modules 里的东西拆出来
  21. libs: {
  22. name: 'chunk-libs',
  23. test: /[\\/]node_modules[\\/]/,
  24. priority: 10,
  25. chunks: 'initial' // 只打包初始依赖
  26. },
  27. // 组2: Vant UI 组件库 (单独拆分)
  28. // 作用:Vant 体积较大,单独打包有利于缓存
  29. vant: {
  30. name: 'chunk-vant',
  31. priority: 20, // 优先级更高,会先被提取
  32. test: /[\\/]node_modules[\\/]_?vant(.*)/
  33. },
  34. // 组3: 公共代码
  35. // 作用:如果你的多个页面都引用了同一个组件,提取出来公用
  36. commons: {
  37. name: 'chunk-commons',
  38. test: /[\\/]src[\\/]components[\\/]/, // 匹配 src/components 下的组件
  39. minChunks: 2, // 只要被引用2次及以上就提取
  40. priority: 5,
  41. reuseExistingChunk: true
  42. }
  43. }
  44. };
  45. }
  46. },
  47. })