反应打字稿Webpack块大小

时间:2020-08-02 02:08:09

标签: reactjs typescript webpack webpack-4

我一直试图减小数据块的大小,但没有解决方案。我正在使用happypack,ForkTsCheckerWebpackPlugin,TerserPlugin作为最小化程序。

对于性能配置,我正在使用

performance: {
        hints: process.env.NODE_ENV === 'production' ? 'error' : false,
        maxEntrypointSize: 200 * KILOBYTES,
        maxAssetSize: 100 * KILOBYTES,
        assetFilter: (fileName) =>
            !fileName.endsWith('.css') && !fileName.endsWith('.scss') && !fileName.endsWith('.map'),
    }

用于优化配置

optimization: {
        runtimeChunk: 'single',
        noEmitOnErrors: true,
        minimize: true,
        splitChunks: {
            cacheGroups: {
                default: false,
                vendors: false,
                vendor: {
                    test: /[\\/]node_modules[\\/]/,
                    name(module, chunks, cacheGroupKey) {
                        const moduleFileName = module
                            .identifier()
                            .split('/')
                            .reduceRight((item) => item);
                        const allChunksNames = chunks.map((item) => item.name).join('~');
                        return `${cacheGroupKey}-${allChunksNames}-${moduleFileName}`;
                    },
                    chunks: 'all',
                    minChunks: 2,
                    priority: 20
                },
                common: {
                    name: 'common',
                    minChunks: 2,
                    chunks: 'async',
                    priority: 10,
                    reuseExistingChunk: true,
                    enforce: true
                }
            },
        },
        minimizer: [
            new TerserPlugin({
                terserOptions: {
                    parse: {
                        ecma: 8
                    },
                    compress: {
                        ecma: 5,
                        warnings: false,
                        inline: 2
                    },
                    mangle: {
                        safari10: true
                    },
                    output: {
                        ecma: 5,
                        comments: false,
                        ascii_only: true
                    },
                },
                cache: true,
                parallel: true,
                sourceMap: isSourceMap,
            }),
        ],
    }

这是我得到的输出

enter image description here

只有一大块。我究竟做错了什么?怎样减小大小并增加块数?

谢谢

1 个答案:

答案 0 :(得分:0)

也许您配置错误。所有的js文件都是单个块。 您可以参考我的示例代码:

  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',
    sideEffects: true,
    concatenateModules: true,
    runtimeChunk: 'single',
    splitChunks: {
      chunks: 'all',
      maxInitialRequests: 10,
      minSize: 0,
      cacheGroups: {
        vendor: {
          test: /[\\/]node_modules[\\/]/,
          name(module) {
            const packageName = module.context.match(
              /[\\/]node_modules[\\/](.*?)([\\/]|$)/,
            )[1];
            return `npm.${packageName.replace('@', '')}`;
          },
        },
      },
    },
  }