webpack.prod.babel.js 2.46 KB
Newer Older
dengxiaofeng's avatar
dengxiaofeng committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Important modules this config uses
const path = require('path');
const { HashedModuleIdsPlugin } = 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
16 17 18 19
  // output: {
  //   filename: '[name].[chunkhash].js',
  //   chunkFilename: '[name].[chunkhash].chunk.js',
  // },
dengxiaofeng's avatar
dengxiaofeng committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

  optimization: {
    minimize: true,
    minimizer: [
      new TerserPlugin({
        terserOptions: {
          warnings: false,
          compress: {
            comparisons: false,
          },
          parse: {},
          mangle: true,
          output: {
            comments: false,
            ascii_only: true,
          },
        },
        parallel: true,
        cache: true,
        sourceMap: true,
      }),
    ],
    nodeEnv: 'production',
43 44
    namedModules: true,
    usedExports: true,
dengxiaofeng's avatar
dengxiaofeng committed
45 46 47 48 49
    sideEffects: true,
    concatenateModules: true,
    runtimeChunk: 'single',
    splitChunks: {
      cacheGroups: {
50 51 52 53
        vendor: { 
          chunks: 'all',
          minSize: 0,
          maxInitialRequests: 35,
dengxiaofeng's avatar
dengxiaofeng committed
54 55 56 57 58 59 60 61
          test: /[\\/]node_modules[\\/]/,
          name(module) {
            const packageName = module.context.match(
              /[\\/]node_modules[\\/](.*?)([\\/]|$)/,
            )[1];
            return `npm.${packageName.replace('@', '')}`;
          },
        },
62 63 64 65 66 67 68 69 70 71 72 73 74 75
        bizComponent: {
          chunks: 'all',
          minSize: 0,
          maxInitialRequests: 10,
          test: /[\\/]src[\\/]components[\\/]/,
          name: 'biz-component',
        },
        react: {
          chunks: 'all',
          minSize: 0,
          maxInitialRequests: 10,
          test: /[\\/]node_modules[\\/]react-dom[\\/]/,
          name: 'react-dom',
        }
dengxiaofeng's avatar
dengxiaofeng committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
      },
    },
  },

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

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

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