'use strict'
const path = require('path')
const defaultSettings = require('./src/settings.js')
const webpack = require('webpack')
const CompressionWebpackPlugin = require('compression-webpack-plugin') // 引入 Gzip 压缩插件

function resolve(dir) {
  return path.join(__dirname, dir)
}

const name = defaultSettings.title || 'vue Element Admin' // page title
const port = process.env.port || process.env.npm_config_port || 9527 // dev port

module.exports = {
  publicPath: '/',
  outputDir: 'dist',
  assetsDir: 'static',
  //lintOnSave: process.env.NODE_ENV === 'development',
  lintOnSave: false,
  productionSourceMap: false,
  devServer: {
    port: port,
    open: true,
    overlay: {
      warnings: false,
      errors: true
    }
    // 如有需要，可在此处配置代理
  },
  css: {
    loaderOptions: {
      sass: {
        sassOptions: {
          outputStyle: 'expanded'
        }
      }
    }
  },
  configureWebpack: config => {
    const plugins = [
      new webpack.ProvidePlugin({
        'window.Quill': 'quill/dist/quill.js',  // 确保 Quill 在全局作用域内可用
        Quill: 'quill/dist/quill.js'            // 手动引入 Quill
      })
    ]

    if (process.env.NODE_ENV === 'production') {
      console.log('正在执行Gzip极限压缩！')
      // 在生产环境中加入 Gzip 压缩插件
      plugins.push(
        new CompressionWebpackPlugin({
          filename: '[path].gz[query]',
          algorithm: 'gzip',
          test: /\.(js|css|html|svg)$/, // 需要压缩的文件类型
          threshold: 10240, // 大于10KB的文件才进行压缩
          minRatio: 0.8     // 压缩比低于0.8才进行压缩
        })
      )
    }

    return {
      name: name,
      resolve: {
        alias: {
          '@': resolve('src')
        }
      },
      plugins
    }
  },
  chainWebpack(config) {
    // 预加载配置
    config.plugin('preload').tap(() => [
      {
        rel: 'preload',
        fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
        include: 'initial'
      }
    ])

    // 删除 prefetch 插件，减少无用请求
    config.plugins.delete('prefetch')

    // 设置 svg-sprite-loader
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()

    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()

    config.when(process.env.NODE_ENV !== 'development', config => {
      config
        .plugin('ScriptExtHtmlWebpackPlugin')
        .after('html')
        .use('script-ext-html-webpack-plugin', [{
          inline: /runtime\..*\.js$/
        }])
        .end()

      config.optimization.splitChunks({
        chunks: 'all',
        cacheGroups: {
          libs: {
            name: 'chunk-libs',
            test: /[\\/]node_modules[\\/]/,
            priority: 10,
            chunks: 'initial' // 仅打包初始依赖的第三方
          },
          elementUI: {
            name: 'chunk-elementUI', // 将 elementUI 单独拆分
            priority: 20,
            test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // 为适应 cnpm
          },
          commons: {
            name: 'chunk-commons',
            test: resolve('src/components'), 
            minChunks: 3, 
            priority: 5,
            reuseExistingChunk: true
          }
        }
      })
      config.optimization.runtimeChunk('single')
    })
  }
}
