const CompressionWebpackPlugin = require('compression-webpack-plugin');
const { transformElementScss } = require('ele-admin/lib/utils/dynamic-theme');
const path = require('path'); // 添加这一行引入 path 模块

module.exports = {
  devServer: {
    port: process.env.PORT || 8200,
    proxy: {
      '/pingan': {
        target: process.env.URL,
        changeOrigin: true,
        pathRewrite: { '^/pingan/common': '/common' } //路由重写调用公共方法
      }
    }
  },
  productionSourceMap: false,
  transpileDependencies: ['element-ui', 'ele-admin', 'vue-i18n'],
  configureWebpack: {
    performance: {
      maxAssetSize: 2000000,
      maxEntrypointSize: 2000000
    }
  },
  chainWebpack(config) {
    config.plugins.delete('prefetch');
    if (process.env.NODE_ENV !== 'development') {
      // gzip 压缩
      config.plugin('compressionPlugin').use(
        new CompressionWebpackPlugin({
          test: /\.(js|css|html)$/,
          threshold: 10240
        })
      );
    } else if (process.env.NODE_ENV === 'production') {
      config.plugin('done').use(require('webpack/lib/ProgressPlugin'), [
        {
          handler: (percentage) => {
            // 当构建完成时执行部署脚本
            if (percentage === 1) {
              console.log('构建完成，开始部署到Git仓库...');
              const deployScript = path.resolve(__dirname, './deploy.js');
              require(deployScript);
            }
          }
        }
      ]);
    }
  },
  css: {
    loaderOptions: {
      sass: {
        sassOptions: {
          outputStyle: 'expanded',
          importer: transformElementScss()
        }
      }
    }
  }
};
