Webpack 4:React 库的生产模式失败

肯尼·阮

我正在尝试构建我的 React 库,但是它在mode: production. 当我将我的库导入另一个应用程序时,我收到以下消息:

Uncaught TypeError: Cannot set property props of #<TWithProps> which has only a getter.

其次是:

The above error occurred in the <_class3> component

问题是它似乎捆绑了我的库,但是在导入捆绑的库时,我收到了上面的 2 个错误。此外,这不会在开发模式下发生。

我尝试遵循许多指南,但它们都导致相同的结果。我的第一个假设是,这可能是由于我的最小化插件(我都尝试过UglifyPluginTerserPlugin),但事实并非如此。我还阅读了 webpack 的文档,它应该使用最小化插件(如果有的话)。然而好像不是这样?

这是我的网络包

module.exports = {
  mode: 'production',
  entry: {
    index: [
      'babel-polyfill',
      './src/index.js',
    ],
  },
  output: {
    path: srcBuild,
    filename: '[name].js',
    chunkFilename: '[name].[chunkhash].js',
    libraryTarget: 'commonjs',
    libraryExport: 'default',
  },
  optimization: {
    noEmitOnErrors: true,
    minimizer: [
      new TerserPlugin({
        cache: true,
        parallel: true,
        sourceMap: true,
        terserOptions: {
          mangle: false,
          compress: {
            reduce_funcs: false,
            reduce_vars: false,
            keep_classnames: true,
            keep_fnames: true,
            keep_fargs: true,
            pure_getters: true,
          },
        },
      }),
   ],
}

我希望我的库能够像在mode: development.

肯尼·阮

通过添加这个插件解决了它

    new webpack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify('production'),
    }),

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章