vue.config.js 4.8 KB

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