1.0.2 • Published 2 years ago

remove-debugger-plugin v1.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

目标

移除上线前 debug 代码的 plugins

使用方式

局部安装

# 1. 项目中执行
npm install -D remove-debugger-plugin

# 2. babelrc中添加
{
  plugins: ["no-debugging"]
}

这个插件默认会移除 debugger; 和 console 调用。

这个插件可以移除 debugger 、 console 、 alert 和 自定义的调试函数调用和定义。

为保证在开发阶段不转换代码,记得将这个插件只配置在发布阶段:

# babelrc
{
 {
  "env": {
    "publish": {
      "presets": [
        "@babel/preset-env"
      ],
      "plugins": ["no-debugging"]
    }
  }
}

# package.json script
{
  "scripts": {
    "build": "cross-env BABEL_ENV=publish webpack", # 类似
  },
}

options

PropertyTypeDefaultDescription
debuggerBooleantrue移除断点调试 debugger 代码
consoleBooleantrue函数调用
alertBooleannull函数调用
debugFnStringnull指定的自定义调试代码函数(包括调试函数声明和调用)

使用

例子

  1. 使用默认配置
.babelrc

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

转换前:

const a = 12
const b = 13

function add(m, n) {
  debugger
  return m + n
}

const result = add(a, b)

console.log(result)

转换后:

const a = 12
const b = 13

function add(m, n) {
  return m + n
}

const result = add(a, b)
  1. 自定义配置 移除 alert
.babelrc

{
  plugins: [
    [
      "no-debugging",
      {
        alert: true,
        console: false
      }
    ]
  ]
}

转换前:

const a = 12
const b = 13

function add(m, n) {
  debugger
  return m + n
}

alert(result)

const result = add(a, b)

console.log(result)

转换后:

const a = 12
const b = 13

function add(m, n) {
  return m + n
}

const result = add(a, b)

console.log(result)

NPM

项目已自动更新至 NPM,请移步至remove-debugger-plugin