vue.config.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. 'use strict'
  2. const path = require('path')
  3. function resolve(dir) {
  4. return path.join(__dirname, dir)
  5. }
  6. const CompressionPlugin = require('compression-webpack-plugin')
  7. const name = process.env.VUE_APP_TITLE || '小智权限管理系统' // 网页标题
  8. const port = process.env.port || process.env.npm_config_port || 80 // 端口
  9. // vue.config.js 配置说明
  10. //官方vue.config.js 参考文档 https://cli.vuejs.org/zh/config/#css-loaderoptions
  11. // 这里只列一部分,具体配置参考文档
  12. module.exports = {
  13. transpileDependencies: [
  14. // 让 Babel 处理 quill 模块
  15. 'quill'
  16. ],
  17. // 部署生产环境和开发环境下的URL
  18. publicPath: process.env.NODE_ENV === "production" ? "/" : "/",
  19. // 在 npm run build 或 yarn build 时,生成文件的目录名称(要和baseUrl的生产环境路径一致,默认dist)
  20. outputDir: 'dist',
  21. // 用于放置生成的静态资源(js、css、img、fonts)的目录(项目打包之后,静态资源会放在这个文件夹下)
  22. assetsDir: 'static',
  23. lintOnSave: process.env.NODE_ENV === 'development',
  24. productionSourceMap: false,
  25. // webpack-dev-server 相关配置
  26. devServer: {
  27. host: '0.0.0.0',
  28. port: port,
  29. open: true,
  30. // 安全修复:使用环境变量替代硬编码IP
  31. proxy: {
  32. [process.env.VUE_APP_BASE_API]: {
  33. target: process.env.VUE_APP_PROXY_TARGET || 'http://localhost:8090',
  34. changeOrigin: true,
  35. pathRewrite: {
  36. ['^' + process.env.VUE_APP_BASE_API]: ''
  37. }
  38. }
  39. },
  40. allowedHosts: [
  41. 'localhost',
  42. '127.0.0.1',
  43. '.local' // 允许 .local 域名
  44. // 如需添加其他域名,请在此处添加
  45. ]
  46. },
  47. css: {
  48. loaderOptions: {
  49. sass: {
  50. sassOptions: { outputStyle: "expanded" }
  51. }
  52. }
  53. },
  54. configureWebpack: {
  55. name: name,
  56. resolve: {
  57. alias: {
  58. '@': resolve('src')
  59. }
  60. },
  61. plugins: [
  62. // 使用 gzip 解压缩静态文件
  63. new CompressionPlugin({
  64. cache: false, // 不启用文件缓存
  65. test: /\.(js|css|html)?$/i, // 压缩文件格式
  66. filename: '[path].gz[query]', // 压缩后的文件名
  67. algorithm: 'gzip', // 使用 gzip 压缩
  68. minRatio: 0.8 // 压缩率小于 1 才会压缩
  69. })
  70. ],
  71. },
  72. chainWebpack(config) {
  73. config.plugins.delete('preload') // 删除预加载插件
  74. config.plugins.delete('prefetch') // 删除预获取插件
  75. // 设置 svg-sprite-loader
  76. config.module
  77. .rule('svg')
  78. .exclude.add(resolve('src/assets/icons'))
  79. .end()
  80. config.module
  81. .rule('icons')
  82. .test(/\.svg$/)
  83. .include.add(resolve('src/assets/icons'))
  84. .end()
  85. .use('svg-sprite-loader')
  86. .loader('svg-sprite-loader')
  87. .options({
  88. symbolId: 'icon-[name]'
  89. })
  90. .end()
  91. // 安全修复:生产环境自动移除 console.log
  92. config.when(process.env.NODE_ENV === 'production', config => {
  93. // 移除 console 和 debugger
  94. config.optimization.minimizer('terser').tap(args => {
  95. Object.assign(args[0].terserOptions.compress, {
  96. drop_console: true, // 移除所有 console
  97. drop_debugger: true, // 移除 debugger
  98. pure_funcs: ['console.log'] // 移除 console.log
  99. })
  100. return args
  101. })
  102. config
  103. .plugin('ScriptExtHtmlWebpackPlugin')
  104. .after('html')
  105. .use('script-ext-html-webpack-plugin', [{
  106. // runtime 必须与 runtimeChunk 名称相同,默认为 runtime
  107. inline: /runtime\..*\.js$/
  108. }])
  109. .end()
  110. config.optimization.splitChunks({
  111. chunks: 'all',
  112. cacheGroups: {
  113. libs: {
  114. name: 'chunk-libs',
  115. test: /[\\/]node_modules[\\/]/,
  116. priority: 10,
  117. chunks: 'initial' // 仅打包初始依赖的第三方库
  118. },
  119. elementUI: {
  120. name: 'chunk-elementUI', // 将 elementUI 拆分成单独的包
  121. test: /[\\/]node_modules[\\/]_?element-ui(.*)/, // 为了适配 cnpm
  122. priority: 20 // 权重需要大于 libs 和 app,否则会被打包进 libs 或 app
  123. },
  124. commons: {
  125. name: 'chunk-commons',
  126. test: resolve('src/components'), // 可以自定义规则
  127. minChunks: 3, // 最小公共数量
  128. priority: 5,
  129. reuseExistingChunk: true // 重用已存在的块
  130. }
  131. }
  132. })
  133. config.optimization.runtimeChunk('single')
  134. })
  135. }
  136. }