babel-plugin-transform-remove-console处于开发模式?

Noi*_*art 6 react-native

我需要运行babel-plugin-transform-remove-console我的开发版本。我所做的是:

npm i babel-plugin-transform-remove-console --save-dev
Run Code Online (Sandbox Code Playgroud)

然后,.babelrc我将其更改为:

{
  "presets": ["react-native"],
  "plugins": ["transform-remove-console"]
}
Run Code Online (Sandbox Code Playgroud)

我也尝试过:

{
  "presets": ["react-native"],
  "env": {
    "development,": {
      "plugins": ["transform-remove-console"]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

但是,控制台日志记录仍在我的开发版本中进行。我在Android上。

有谁知道如何使它在开发模式下工作?

Hus*_*urd 5

也许您使用的是新的 babel 版本 7,您需要更改此文件babel.config.js而不是.babelrc如下:

module.exports = function override(api) {

  var env = api.cache(() => process.env.NODE_ENV);
  var isProd = api.cache(() => process.env.NODE_ENV === "production");

  if (!isProd) {
    config = {
      plugins: [
        ["transform-remove-console"]
      ],
      presets: ["@babel/preset-flow", "module:metro-react-native-babel-preset"]
    };
  }

  return config;
};
Run Code Online (Sandbox Code Playgroud)

这应该删除新 babel 版本中的控制台


Dan*_* DC 0

你可以试试 :

{
 "presets": ["react-native"],
 "plugins": ["dev-expression", "babel-plugin-dev-expression"]
}
Run Code Online (Sandbox Code Playgroud)

希望这有帮助。

  • 根据 https://facebook.github.io/react-native/docs/performance.html#using-consolelog-statements 中给出的,它将从生产模式中删除控制台日志,但我尝试过它根本不起作用。我的结局有什么遗漏吗? (3认同)