vue.config.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. const path = require("path");
  2. const CompressionPlugin = require("compression-webpack-plugin");
  3. const resolve = dir => {
  4. return path.join(__dirname, dir);
  5. };
  6. module.exports = {
  7. publicPath: "./",
  8. css: {
  9. loaderOptions: {
  10. // 向 CSS 相关的 loader 传递选项
  11. less: {
  12. lessOptions: {
  13. javascriptEnabled: true
  14. }
  15. }
  16. }
  17. },
  18. devServer: {
  19. port: 10003
  20. },
  21. chainWebpack: config => {
  22. // @ 对应 src目录
  23. config.resolve.alias.set("@", resolve("src"));
  24. },
  25. // 打包时不生成.map文件 避免看到源码
  26. productionSourceMap: false,
  27. // 部署优化
  28. configureWebpack: {
  29. // 使用CDN
  30. externals: {
  31. vue: "Vue",
  32. "vue-i18n": "VueI18n",
  33. axios: "axios",
  34. "vue-router": "VueRouter",
  35. vuex: "Vuex",
  36. "view-design": "iview",
  37. xlsx: "XLSX",
  38. dplayer: "DPlayer",
  39. "print-js": "printJS",
  40. html2canvas: "html2canvas",
  41. "vue-json-pretty": "VueJsonPretty",
  42. "vue-lazyload": "VueLazyload",
  43. gitalk: "Gitalk",
  44. "js-cookie": "Cookies",
  45. wangEditor: "wangEditor",
  46. quill: "Quill",
  47. stompjs: "Stomp",
  48. "sockjs-client": "SockJS",
  49. vuedraggable: "vuedraggable"
  50. },
  51. // GZIP压缩
  52. plugins: [
  53. new CompressionPlugin({
  54. test: /\.js$|\.html$|\.css/, // 匹配文件
  55. threshold: 10240 // 对超过10k文件压缩
  56. })
  57. ],
  58. optimization: {
  59. runtimeChunk: "single",
  60. splitChunks: {
  61. chunks: "all",
  62. maxInitialRequests: Infinity,
  63. minSize: 20000,
  64. cacheGroups: {
  65. vendor: {
  66. test: /[\\/]node_modules[\\/]/,
  67. name(module) {
  68. // get the name. E.g. node_modules/packageName/not/this/part.js
  69. // or node_modules/packageName
  70. const packageName = module.context.match(
  71. /[\\/]node_modules[\\/](.*?)([\\/]|$)/
  72. )[1];
  73. // npm package names are URL-safe, but some servers don't like @ symbols
  74. return `npm.${packageName.replace("@", "")}`;
  75. }
  76. }
  77. }
  78. }
  79. }
  80. },
  81. pluginOptions: {
  82. "style-resources-loader": {
  83. preProcessor: "scss",
  84. patterns: [path.resolve(__dirname, "./src/styles/common.scss")]
  85. }
  86. }
  87. };