const common = require('./webpack.common');
const { merge } = require('webpack-merge');
const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const { parsed: dotenv } = require('dotenv').config({ path: path.resolve(__dirname, '../.env') });
const BASENAME = process.env.BASENAME || '';
const PUBLIC_PATH = `${BASENAME}/`;
const BUILD_PATH = path.join(__dirname, '/../devdist/');
const copyToPath =  path.resolve(__dirname, '../devdist')
const antdTheme = require('./antd-theme');

module.exports = merge(common, {
  mode: 'development',
  devtool: 'eval-cheap-module-source-map',
  stats:{
    errorDetails:true
  },
  entry: {
    app: ['./src/scripts/index.tsx']
  },
  output: {
    path: BUILD_PATH,
    publicPath: PUBLIC_PATH,
    filename: 'js/[name].[fullhash:5].js',
    chunkFilename: 'js/[name].[contenthash:5].js',
    globalObject: 'this'
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        exclude: /node_modules/,
        use: [
          {
            loader: 'style-loader'
          },
          {
            loader: 'css-loader',
            options: {
              importLoaders: 2,
              sourceMap: false,
              modules: {
                localIdentName: '[name]__[local]--[hash:base64:5]',
              },
            },
          }
        ]
      },
      {
        test: /\.scss$/,
        use: ['style-loader', 'css-loader', 'sass-loader']
      },
      {
        test: /\.less$/,
        include: /node_modules/,
        use: [
          'style-loader',
          'css-loader',
          {
            loader: 'less-loader',
            options: {
              lessOptions: {
                javascriptEnabled: true,
                modifyVars: antdTheme
              }
            },
          }
        ]
      },
      {
        test: /\.less$/,
        exclude: /node_modules/,
        use: [
          'style-loader',
          {
            loader: 'css-loader',
            options: {
              importLoaders: 2,
              sourceMap: false,
              modules: {
                localIdentName: '[name]__[local]--[hash:base64:5]',
              },
            },
          },
          {
            loader: 'less-loader'
          }
        ]
      }
    ]
  },
  plugins: [
    new CopyWebpackPlugin({
      patterns: [
        {
          from: path.resolve(__dirname, '../src/favicon.ico'),
          to: copyToPath,
        },
        {
          from: path.resolve(__dirname, '../pwa/'),
          to: copyToPath,
        },
        {
          from: path.resolve(__dirname, '../public/'),
          to: copyToPath,
        },
      ]
    }),
    new BundleAnalyzerPlugin({
      openAnalyzer: false,
      analyzerMode : 'disabled' , 
      // 从 stats 文件中排除模块源，因此不会有任何敏感数据
      statsOptions : {  source : false  } 
    })
  ],
  devServer: {
    host: '0.0.0.0',
    port: 80,
    hot: true,
    open: false,
    allowedHosts:'all',
    static: {
      directory: BUILD_PATH,
    },
    historyApiFallback: {
      rewrites: [{ from: /./, to: `${BASENAME}/index.html` }]
    },
    proxy: {
      '/api/**': {
        changeOrigin: true,
        proxyTimeout: 12 * 3600 * 1000,
        timeout: 12 * 3600 * 10000,
        target: dotenv.SERVER_PATH,
        secure: false
      },
      '/callback': {
        changeOrigin: true,
        proxyTimeout: 12 * 3600 * 1000,
        timeout: 12 * 3600 * 10000,
        target: dotenv.SERVER_PATH,
        secure: false
      },
      '/logout': {
        changeOrigin: true,
        proxyTimeout: 12 * 3600 * 1000,
        timeout: 12 * 3600 * 10000,
        target: dotenv.SERVER_PATH,
        secure: false
      },
      '/login': {
        changeOrigin: true,
        proxyTimeout: 12 * 3600 * 1000,
        timeout: 12 * 3600 * 10000,
        target: dotenv.SERVER_PATH,
        secure: false
      },
      '/socket.io': {
        changeOrigin: true,
        proxyTimeout: 12 * 3600 * 1000,
        timeout: 12 * 3600 * 10000,
        target: dotenv.SERVER_PATH,
        secure: false
      }
    }
  }
});