vue.config.js 4.6 KB

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