1.0.2 • Published 1 year ago

babel-plugin-no-console v1.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

目标

移除代码中的 console 语句。

使用方式

局部安装

# 1. 项目中执行
npm install -D babel-plugin-no-console

# 2. babelrc或 babel.config.js中添加
{
  plugins: ["no-console"]
}

options

PropertyTypeDefaultDescription
excludeArray[]需要保留的方法 。如传入'warn' 则会保留 console.warn()语句,不会被移除,可传值:‘warn’,'log','error'

使用

例子

  1. 使用默认配置
.babelrc 或 babel.config.js

{
  plugins: [
    [
      "no-console"
    ]
  ]
}

转换前:

const x = 12;
const y = 13;
function add(m, n) {
  if (typeof m != number || typeof n != number) {
    console.error("TypeError");
  }
  console.warn("warning");
  return m + n;
}
const result = add(x, y);
console.log(result);

转换后:

const x = 12;
const y = 13;
function add(m, n) {
  if (typeof m != number || typeof n != number) {
  }
  return m + n;
}
const result = add(x, y);
  1. 自定义配置 保留 console.error()
.babelrc 或 babel.config.js

{
  plugins: [
    [
      "no-console",
      {
        exclude:['error']
      }
    ]
  ]
}

转换前:

cconst x = 12;
const y = 13;
function add(m, n) {
  if (typeof m != number || typeof n != number) {
    console.error("TypeError");
  }
  console.warn("warning");
  return m + n;
}
const result = add(x, y);
console.log(result);

转换后:

cconst x = 12;
const y = 13;
function add(m, n) {
  if (typeof m != number || typeof n != number) {
    console.error("TypeError");
  }
  return m + n;
}
const result = add(x, y);

参考文档

https://www.babeljs.cn/

https://astexplorer.net/

https://github.com/jamiebuilds/babel-handbook/blob/master/translations/zh-Hans/plugin-handbook.md

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago