vue.config.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. 'use strict'
  2. const path = require('path')
  3. const defaultSettings = require('./src/settings.js')
  4. const webpack = require('webpack')
  5. const CompressionWebpackPlugin = require('compression-webpack-plugin') // 引入 Gzip 压缩插件
  6. function resolve(dir) {
  7. return path.join(__dirname, dir)
  8. }
  9. const name = defaultSettings.title || 'vue Element Admin' // page title
  10. const port = process.env.port || process.env.npm_config_port || 9527 // dev port
  11. module.exports = {
  12. publicPath: '/',
  13. outputDir: 'dist',
  14. assetsDir: 'static',
  15. //lintOnSave: process.env.NODE_ENV === 'development',
  16. lintOnSave: false,
  17. productionSourceMap: false,
  18. devServer: {
  19. port: port,
  20. open: true,
  21. overlay: {
  22. warnings: false,
  23. errors: true
  24. }
  25. // 如有需要,可在此处配置代理
  26. },
  27. configureWebpack: config => {
  28. const plugins = [
  29. new webpack.ProvidePlugin({
  30. 'window.Quill': 'quill/dist/quill.js', // 确保 Quill 在全局作用域内可用
  31. Quill: 'quill/dist/quill.js' // 手动引入 Quill
  32. })
  33. ]
  34. if (process.env.NODE_ENV === 'production') {
  35. console.log('正在执行Gzip极限压缩!')
  36. // 在生产环境中加入 Gzip 压缩插件
  37. plugins.push(
  38. new CompressionWebpackPlugin({
  39. filename: '[path].gz[query]',
  40. algorithm: 'gzip',
  41. test: /\.(js|css|html|svg)$/, // 需要压缩的文件类型
  42. threshold: 10240, // 大于10KB的文件才进行压缩
  43. minRatio: 0.8 // 压缩比低于0.8才进行压缩
  44. })
  45. )
  46. }
  47. return {
  48. name: name,
  49. resolve: {
  50. alias: {
  51. '@': resolve('src')
  52. }
  53. },
  54. plugins
  55. }
  56. },
  57. chainWebpack(config) {
  58. // 预加载配置
  59. config.plugin('preload').tap(() => [
  60. {
  61. rel: 'preload',
  62. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  63. include: 'initial'
  64. }
  65. ])
  66. // 删除 prefetch 插件,减少无用请求
  67. config.plugins.delete('prefetch')
  68. // 设置 svg-sprite-loader
  69. config.module
  70. .rule('svg')
  71. .exclude.add(resolve('src/icons'))
  72. .end()
  73. config.module
  74. .rule('icons')
  75. .test(/\.svg$/)
  76. .include.add(resolve('src/icons'))
  77. .end()
  78. .use('svg-sprite-loader')
  79. .loader('svg-sprite-loader')
  80. .options({
  81. symbolId: 'icon-[name]'
  82. })
  83. .end()
  84. config.when(process.env.NODE_ENV !== 'development', config => {
  85. config
  86. .plugin('ScriptExtHtmlWebpackPlugin')
  87. .after('html')
  88. .use('script-ext-html-webpack-plugin', [{
  89. inline: /runtime\..*\.js$/
  90. }])
  91. .end()
  92. config.optimization.splitChunks({
  93. chunks: 'all',
  94. cacheGroups: {
  95. libs: {
  96. name: 'chunk-libs',
  97. test: /[\\/]node_modules[\\/]/,
  98. priority: 10,
  99. chunks: 'initial' // 仅打包初始依赖的第三方
  100. },
  101. elementUI: {
  102. name: 'chunk-elementUI', // 将 elementUI 单独拆分
  103. priority: 20,
  104. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // 为适应 cnpm
  105. },
  106. commons: {
  107. name: 'chunk-commons',
  108. test: resolve('src/components'),
  109. minChunks: 3,
  110. priority: 5,
  111. reuseExistingChunk: true
  112. }
  113. }
  114. })
  115. config.optimization.runtimeChunk('single')
  116. })
  117. }
  118. }