vue.config.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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: 10002,
  18. },
  19. chainWebpack: config => {
  20. // @ 对应 src目录
  21. config.resolve.alias.set('@', resolve('src'))
  22. },
  23. // 打包时不生成.map文件 避免看到源码
  24. productionSourceMap: false,
  25. // 部署优化
  26. configureWebpack: {
  27. // 使用CDN
  28. externals: {
  29. vue: 'Vue',
  30. 'vue-i18n': 'VueI18n',
  31. axios: 'axios',
  32. 'vue-router': 'VueRouter',
  33. vuex: 'Vuex',
  34. 'view-design': 'iview',
  35. 'vue-apexcharts': 'VueApexCharts',
  36. xlsx: 'XLSX',
  37. dplayer: 'DPlayer',
  38. 'print-js': 'printJS',
  39. html2canvas: 'html2canvas',
  40. 'vue-json-pretty': 'VueJsonPretty',
  41. 'vue-lazyload': 'VueLazyload',
  42. gitalk: 'Gitalk',
  43. 'js-cookie': 'Cookies',
  44. wangEditor: 'wangEditor',
  45. quill: 'Quill',
  46. stompjs: 'Stomp',
  47. 'sockjs-client': 'SockJS',
  48. vuedraggable: 'vuedraggable'
  49. },
  50. // GZIP压缩
  51. plugins: [
  52. new CompressionPlugin({
  53. test: /\.js$|\.html$|\.css/, // 匹配文件
  54. threshold: 10240 // 对超过10k文件压缩
  55. })
  56. ],
  57. optimization: {
  58. runtimeChunk: "single",
  59. splitChunks: {
  60. chunks: "all",
  61. maxInitialRequests: Infinity,
  62. minSize: 20000,
  63. cacheGroups: {
  64. vendor: {
  65. test: /[\\/]node_modules[\\/]/,
  66. name(module) {
  67. const packageName = module.context.match(
  68. /[\\/]node_modules[\\/](.*?)([\\/]|$)/
  69. )[1];
  70. return `npm.${packageName.replace("@", "")}`;
  71. }
  72. }
  73. }
  74. }
  75. }
  76. },
  77. pluginOptions: {
  78. 'style-resources-loader': {
  79. preProcessor: 'scss',
  80. patterns: [path.resolve(__dirname, './src/styles/common.scss')]
  81. }
  82. }
  83. }