编辑 /vue.config.js

为了提高单页打开速度、分文件打包 dist js

  1. module.exports = {
  2. ...
  3. configureWebpack: (config) => {
  4. if (process.env.NODE_ENV === "production") {
  5. // 开启分离js
  6. config.optimization = {
  7. runtimeChunk: "single",
  8. splitChunks: {
  9. chunks: "all",
  10. maxInitialRequests: Infinity,
  11. minSize: 20000,
  12. cacheGroups: {
  13. vendor: {
  14. test: /[\\/]node_modules[\\/]/,
  15. name(module) {
  16. // get the name. E.g. node_modules/packageName/not/this/part.js
  17. // or node_modules/packageName
  18. const packageName = module.context.match(
  19. /[\\/]node_modules[\\/](.*?)([\\/]|$)/
  20. )[1];
  21. // npm package names are URL-safe, but some servers don't like @ symbols
  22. return `npm.${packageName.replace("@", "")}`;
  23. },
  24. },
  25. },
  26. },
  27. };
  28. // end
  29. }
  30. },