vue.config.js 5.1 KB

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