const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const webpack = require('webpack');

module.exports = {
  mode: 'development',
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  },
  
  // 添加处理Node.js模块的配置
  resolve: {
    fallback: {
      "fs": false,
      "path": require.resolve("path-browserify"),
      "buffer": require.resolve("buffer/"),
      "stream": require.resolve("stream-browserify"),
      "util": require.resolve("util/"),
      "assert": require.resolve("assert/"),
      "os": require.resolve("os-browserify/browser"),
      "process": require.resolve("process/browser")
    },
    // 添加别名配置，修复ESM模块中的process/browser引用问题
    alias: {
      'process/browser': require.resolve('process/browser')
    }
  },
 
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env', '@babel/preset-react']
          }
        }
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']
      },
      {
        test: /\.(png|svg|jpg|jpeg|gif|woff|woff2)$/i,
        type: 'asset/resource'
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './public/index.html',
      favicon: './public/logo.ico'
    }),
    // 添加Buffer polyfill
    new webpack.ProvidePlugin({
      Buffer: ['buffer', 'Buffer'],
    }),
    // 添加process polyfill
    new webpack.ProvidePlugin({
      process: 'process/browser',
    }),
    // 添加全局变量定义，确保process.browser设置为true
    new webpack.DefinePlugin({
      'process.browser': true
    })
  ],
  devServer: {
    static: {
      directory: path.join(__dirname, 'public'),
    },
    compress: true,
    port: 9000,
    open: true
  }
};
