babel.config.js 如何配置删除 console.log?

文档 https://reactnative.cn/docs/p...

参照文档修改如下配置后并没有起作用, 请问改如何配置呢?

目前环境

  • "react-native": "0.61.5",
  • "react": "16.9.0",
  • "@babel/core": "^7.7.5",
  • "babel-plugin-transform-remove-console": "^6.9.4",

babel.config.js

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    '@babel/plugin-proposal-nullish-coalescing-operator',
    '@babel/plugin-proposal-optional-chaining',
  ],
  env: {
    production: {
      plugins: ['transform-remove-console'],
    },
  },
};
阅读 5.3k
2 个回答

Uglify 也可以:

new UglifyJsPlugin({
  uglifyOptions: {
    compress: {
      drop_console: true, // 去掉注释内容
      drop_debugger: true
    },
    warnings: false,
    output: {
      comments: false 
    }
  },
  sourceMap: false, 
  parallel: true
})

没记错的话删除 console.log 应该是 terser-webpack-plugin 的工作,类似这样:

module.exports = {
  optimization: {
    minimizer: [
      new TerserPlugin({
        cache: true,
        parallel: true,
        exclude: /node_modules/,
        terserOptions: {
          ecma: 6,
          toplevel: true,
          compress: {
            drop_console: true, // 就这个
          },
        },
      }),
    ],
  },
};
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题