vue.config.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. const path = require('path');
  2. const StyleLintPlugin = require('stylelint-webpack-plugin');
  3. const CompressionPlugin = require('compression-webpack-plugin');
  4. const defaultSettings = require('./src/settings.js');
  5. const PreloadPlugin = require('@vue/preload-webpack-plugin');
  6. function resolve(dir) {
  7. return path.join(__dirname, './', dir);
  8. }
  9. const name = defaultSettings.title;
  10. const NODE_ENV = process.env.NODE_ENV;
  11. const port = process.env.port || 7878;
  12. let proxy = {};
  13. if (NODE_ENV === 'development') {
  14. proxy = {
  15. [process.env.VUE_APP_BASE_API]: {
  16. // target: 'https://gcls.utschool.cn/',
  17. target: 'https://gcls.helxsoft.cn/',
  18. // target: 'https://youthchinesedu.blcup.com/',
  19. changeOrigin: true,
  20. pathRewrite: {
  21. [`^${process.env.VUE_APP_BASE_API}`]: ''
  22. }
  23. },
  24. [process.env.VUE_APP_FILE]: {
  25. // target: 'https://file-cs.helxsoft.cn',
  26. target: 'https://file-kf.helxsoft.cn/',
  27. changeOrigin: true,
  28. pathRewrite: {
  29. [`^${process.env.VUE_APP_FILE}`]: ''
  30. }
  31. }
  32. };
  33. }
  34. // 配置项说明 https://cli.vuejs.org/config/
  35. module.exports = {
  36. publicPath: NODE_ENV === 'development' ? '/' : './',
  37. outputDir: 'dist',
  38. assetsDir: 'static',
  39. lintOnSave: false,
  40. runtimeCompiler: true,
  41. productionSourceMap: false,
  42. devServer: {
  43. port,
  44. open: {
  45. target: [`http://localhost:${port}`]
  46. },
  47. client: {
  48. overlay: {
  49. warnings: false,
  50. errors: true
  51. }
  52. },
  53. proxy
  54. },
  55. css: {
  56. loaderOptions: {
  57. scss: {
  58. // 为 scss 配置共享全局变量
  59. additionalData: '@import "./src/styles/variables.scss";'
  60. }
  61. }
  62. },
  63. configureWebpack: {
  64. name,
  65. // 配置路径别名
  66. resolve: {
  67. alias: {
  68. '@': resolve('src')
  69. }
  70. },
  71. devtool: NODE_ENV === 'development' ? 'source-map' : false,
  72. plugins: [
  73. // stylelint 配置
  74. new StyleLintPlugin({
  75. files: ['src/**/*.{vue,html,css,scss}'],
  76. fix: true, // 是否自动修复
  77. failOnError: false
  78. }),
  79. new CompressionPlugin({
  80. algorithm: 'gzip', // 使用gzip压缩
  81. test: /\.js$|\.html$|\.css$/, // 匹配文件名
  82. minRatio: 0.8, // 压缩率小于0.8才会压缩
  83. threshold: 10240, // 对超过10k的数据压缩
  84. deleteOriginalAssets: false // 是否删除未压缩的源文件,谨慎设置,如果希望提供非gzip的资源,可不设置或者设置为false(比如删除打包后的gz后还可以加载到原始资源文件)
  85. })
  86. ]
  87. },
  88. chainWebpack(config) {
  89. // 启用预加载,提高首屏加载速度
  90. config
  91. .plugin('preload')
  92. .use(PreloadPlugin, [
  93. {
  94. rel: 'preload',
  95. include: 'initial',
  96. fileBlacklist: [/\.map$/, /hot-update\.js$/],
  97. as(entry) {
  98. if (/\.css$/.test(entry)) return 'style';
  99. return 'script';
  100. }
  101. }
  102. ])
  103. .after('html');
  104. // 当页面很多时,prefetch 将导致太多无意义的请求,开启这个
  105. config
  106. .plugin('prefetch')
  107. .use(PreloadPlugin, [
  108. {
  109. rel: 'prefetch',
  110. include: 'asyncChunks'
  111. }
  112. ])
  113. .after('html');
  114. // 设置 svg-sprite-loader
  115. config.module.rule('svg').exclude.add(resolve('src/icons')).end();
  116. config.module
  117. .rule('icons')
  118. .test(/\.svg$/)
  119. .include.add(resolve('src/icons'))
  120. .end()
  121. .use('svg-sprite-loader')
  122. .loader('svg-sprite-loader')
  123. .options({
  124. symbolId: 'icon-[name]'
  125. })
  126. .end();
  127. config.when(NODE_ENV !== 'development', () => {
  128. config
  129. .plugin('ScriptExtHtmlWebpackPlugin')
  130. .after('html')
  131. .use('script-ext-html-webpack-plugin', [
  132. {
  133. // `runtime`必须与runtimeChunk名称相同。默认是“运行时”
  134. inline: /runtime\..*\.js$/
  135. }
  136. ])
  137. .end();
  138. config.optimization.splitChunks({
  139. chunks: 'all',
  140. cacheGroups: {
  141. libs: {
  142. name: 'chunk-libs',
  143. test: /[\\/]node_modules[\\/]/,
  144. priority: 10,
  145. chunks: 'initial' // 仅打包最初依赖的第三方
  146. },
  147. elementUI: {
  148. name: 'chunk-elementUI', // 将elementUI拆分为一个包
  149. priority: 20, // 权重必须大于libs和app,否则将被打包到libs或app中
  150. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  151. },
  152. commons: {
  153. name: 'chunk-commons',
  154. test: resolve('src/components'), // 可以自定义规则
  155. minChunks: 3, // 最小公用数
  156. priority: 5,
  157. reuseExistingChunk: true
  158. }
  159. }
  160. });
  161. // https://webpack.js.org/configuration/optimization/#optimizationruntimechunk
  162. config.optimization.runtimeChunk('single');
  163. });
  164. }
  165. };