vue.config.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. css: {
  28. loaderOptions: {
  29. sass: {
  30. sassOptions: {
  31. outputStyle: 'expanded'
  32. }
  33. }
  34. }
  35. },
  36. configureWebpack: config => {
  37. const plugins = [
  38. new webpack.ProvidePlugin({
  39. 'window.Quill': 'quill/dist/quill.js', // 确保 Quill 在全局作用域内可用
  40. Quill: 'quill/dist/quill.js' // 手动引入 Quill
  41. })
  42. ]
  43. if (process.env.NODE_ENV === 'production') {
  44. console.log('正在执行Gzip极限压缩!')
  45. // 在生产环境中加入 Gzip 压缩插件
  46. plugins.push(
  47. new CompressionWebpackPlugin({
  48. filename: '[path].gz[query]',
  49. algorithm: 'gzip',
  50. test: /\.(js|css|html|svg)$/, // 需要压缩的文件类型
  51. threshold: 10240, // 大于10KB的文件才进行压缩
  52. minRatio: 0.8 // 压缩比低于0.8才进行压缩
  53. })
  54. )
  55. }
  56. return {
  57. name: name,
  58. resolve: {
  59. alias: {
  60. '@': resolve('src')
  61. }
  62. },
  63. plugins
  64. }
  65. },
  66. chainWebpack(config) {
  67. // 预加载配置
  68. config.plugin('preload').tap(() => [
  69. {
  70. rel: 'preload',
  71. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  72. include: 'initial'
  73. }
  74. ])
  75. // 删除 prefetch 插件,减少无用请求
  76. config.plugins.delete('prefetch')
  77. // 设置 svg-sprite-loader
  78. config.module
  79. .rule('svg')
  80. .exclude.add(resolve('src/icons'))
  81. .end()
  82. config.module
  83. .rule('icons')
  84. .test(/\.svg$/)
  85. .include.add(resolve('src/icons'))
  86. .end()
  87. .use('svg-sprite-loader')
  88. .loader('svg-sprite-loader')
  89. .options({
  90. symbolId: 'icon-[name]'
  91. })
  92. .end()
  93. config.when(process.env.NODE_ENV !== 'development', config => {
  94. config
  95. .plugin('ScriptExtHtmlWebpackPlugin')
  96. .after('html')
  97. .use('script-ext-html-webpack-plugin', [{
  98. inline: /runtime\..*\.js$/
  99. }])
  100. .end()
  101. config.optimization.splitChunks({
  102. chunks: 'all',
  103. cacheGroups: {
  104. libs: {
  105. name: 'chunk-libs',
  106. test: /[\\/]node_modules[\\/]/,
  107. priority: 10,
  108. chunks: 'initial' // 仅打包初始依赖的第三方
  109. },
  110. elementUI: {
  111. name: 'chunk-elementUI', // 将 elementUI 单独拆分
  112. priority: 20,
  113. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // 为适应 cnpm
  114. },
  115. commons: {
  116. name: 'chunk-commons',
  117. test: resolve('src/components'),
  118. minChunks: 3,
  119. priority: 5,
  120. reuseExistingChunk: true
  121. }
  122. }
  123. })
  124. config.optimization.runtimeChunk('single')
  125. })
  126. }
  127. }