vue.config.js 2.8 KB

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