const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');

module.exports = {
  // 入口文件，指定项目的起点
  entry: './bin/www',
  // 输出配置
  output: {
    // 输出文件的路径
    path: path.resolve(__dirname, '../dist'),
    // 输出文件名
    filename: 'server.js'
  },
  externals: {
    mysql: 'commonjs mysql',
    axios: 'commonjs axios',
  },
  target: 'node',
  node: {
    __dirname: false,
    fs: 'empty',
    net: 'empty',
    tls: 'empty',
  },
  // 模块规则，用于处理各种类型的文件
  module: {
    rules: [
      {
        // 处理JavaScript文件
        test: /\.js$/,
        // 排除node_modules目录下的文件
        exclude: /node_modules\/(?!http-proxy-middleware)/, // 处理 http-proxy-middleware 包
        // 使用babel-loader进行转译
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env'],
            plugins: ['@babel/plugin-proposal-optional-chaining']
          }
        }
      },
      {
        // 处理CSS文件
        test: /\.css$/,
        // 使用style-loader和css-loader加载CSS
        use: ['style-loader', 'css-loader']
      }
    ]
  },
  plugins: [
    new CleanWebpackPlugin(), 
    new CopyWebpackPlugin({
      patterns: [
        { from: path.resolve(__dirname, '../web/dist'), to: path.resolve(__dirname, '../dist/dist') },
        { from: path.resolve(__dirname, './build'), to: path.resolve(__dirname, '../dist') }
      ]
    })
  ]
};