// Important modules this config uses
const path = require('path');
const webpack = require('webpack');
const TerserPlugin = require('terser-webpack-plugin');
const CompressionPlugin = require('compression-webpack-plugin');
module.exports = require('./webpack.base.babel')({
  mode: 'production',

  // In production, we skip all hot-reloading stuff
  entry: [
    require.resolve('react-app-polyfill/ie11'),
    path.join(process.cwd(), 'src/app.js'),
  ],

  // Utilize long-term caching by adding content hashes (not compilation hashes) to compiled assets
  // output: {
  //   filename: '[name].[chunkhash].js',
  //   chunkFilename: '[name].[chunkhash].chunk.js',
  // },

  optimization: {
    minimize: true,
    minimizer: [
      new TerserPlugin({
        terserOptions: {
          warnings: false,
          compress: {
            comparisons: false,
          },
          drop_debugger: true,
          drop_console: true,
          pure_funcs: ['console.log'],
          parse: {},
          mangle: true,
          output: {
            comments: false,
            ascii_only: true,
          },
        },
        parallel: true,
        cache: true,
        sourceMap: true,
      }),
    ],
    nodeEnv: 'production',
    chunkIds: 'deterministic',
    moduleIds: 'deterministic',
    usedExports: true,
    sideEffects: true,
    concatenateModules: true,
    runtimeChunk: 'single',
    splitChunks: {
      minSize: 20000,
      minRemainingSize: 0,
      minChunks: 1,
      maxAsyncRequests: 30,// webpack4,5区别较大
      maxInitialRequests: 30,// webpack4,5区别较大
      enforceSizeThreshold: 50000,
      cacheGroups: {
        vendor: {
          chunks: 'all',
          maxInitialRequests: 5,
          test: /[\\/]node_modules[\\/]/,
          name(module) {
            const packageName = module.context.match(
              /[\\/]node_modules[\\/](.*?)([\\/]|$)/,
            )[1];
            return `npm.${packageName.replace('@', '')}`;
          },
        },
        bizComponent: {
          chunks: 'all',
          minSize: 0,
          maxInitialRequests: 10,
          test: /[\\/]src[\\/]components[\\/]/,
          name: 'biz-component',
        },
        arcgis_views: {
          chunks: 'all',
          minSize: 0,
          maxInitialRequests: 5,
          priority: 20,
          test: /[\\/]node_modules[\\/]@arcgis[\\/]core[\\/]views[\\/]/,
          name: 'arcgis_views',
        },
        arcgis_views_3d: {
          chunks: 'all',
          minSize: 0,
          maxInitialRequests: 5,
          priority: 22,
          test: /[\\/]node_modules[\\/]@arcgis[\\/]core[\\/]views[\\/]3d[\\/]/,
          name: 'arcgis_views_3d',
        },
        arcgis_layers: {
          chunks: 'all',
          minSize: 0,
          maxInitialRequests: 5,
          priority: 10,
          test: /[\\/]node_modules[\\/]@arcgis[\\/]core[\\/]layers[\\/]/,
          name: 'arcgis_layers',
        },
        arcgis_chunks: {
          chunks: 'all',
          minSize: 0,
          maxInitialRequests: 5,
          priority: 30,
          test: /[\\/]node_modules[\\/]@arcgis[\\/]core[\\/]chunks[\\/]/,
          name: 'arcgis_chunks',
        }
      },
    },
  },

  plugins: [
    new CompressionPlugin({
      algorithm: 'gzip',
      test: /\.js$|\.css$|\.html$/,
      threshold: 10240,
      minRatio: 0.8,
    }),

    // new webpack.ids.HashedModuleIdsPlugin({
    //   hashFunction: 'sha256',
    //   hashDigest: 'hex',
    //   hashDigestLength: 20,
    // }),
  ],

  performance: {
    assetFilter: assetFilename =>
      !/(\.map$)|(^(main\.|favicon\.))/.test(assetFilename),
  },
});
