如何在使用create-react-app创建的React应用程序的生产版本中删除console.log? [英] How can remove console.log in the production build of a React application created using create-react-app?

查看:474
本文介绍了如何在使用create-react-app创建的React应用程序的生产版本中删除console.log?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在使用create-react-app创建的React应用的生产版本中删除console.log?

How can remove console.log in the production build of a React application created using create-react-app?

推荐答案

这个问题的简单解决方案是帮助面临相同问题的其他人。

The simple solution to the question is to help others who are facing the same issue.

引用: https://stackoverflow.com/a/41041580/3574669

参考文献中的建议,它需要在文件 webpack.config.js中进行更改;。 create-react-app在内部使用webpack和配置文件,但无法在应用程序根目录中添加webpack.config.js来应用更改。这将需要离开create-react-app设置,并为Webpack和配置构建自己的设置。经过大量研究并编写示例代码后,这对我来说并不容易。

The suggestion in the reference, it needs to do a change in the file "webpack.config.js". The create-react-app uses the webpack and the config file internally but it is not possible to add webpack.config.js in my application root for applying the change. This would need to leave the create-react-app setup and build own setup for webpack and configuration. It does not find easy for me after exploring a lot and writing sample code.

由于我对create-react-app非常满意,所以也不想保留它的好处。

Since I am very much satisfied with create-react-app so also don't want to keep aside its benefits.

最后,我对 node_modules / react-scripts / config / webpack.config.js 通过添加行 drop_console:true,如参考中所述。参考中建议的代码如下,

Finally, I did a simple change in the node_modules/react-scripts/config/webpack.config.js by adding a line drop_console: true, as mentioned in the reference. The suggested code in the reference is as below,

module.exports = {
  optimization: {
    minimizer: [
      new TerserPlugin({
        sourceMap: true, // Must be set to true if using source-maps in production
        terserOptions: {
          compress: {
            drop_console: true, // << this needs only to remove console.log //
          },
        },
      }),
    ],
  },
};

对我来说这很好,通过简单的更改,生产构建应用程序中就没有控制台日志。

This works fine to me and there is no console log in my production build application by the simple change.

我正在使用 react-scripts: 3.0.1,

注意:每当您重新安装 react-scripts时,都会清除此新行。后来。因此,在这种情况下,需要再次进行相同的更改。

这篇关于如何在使用create-react-app创建的React应用程序的生产版本中删除console.log?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆