/babel-plugin-function-try-catch

Based on ast, a babel plugin that automatically adds try/catch to functions.(基于ast,为函数自动添加 try/catch 的 babel 插件)

Primary LanguageJavaScript

babel-plugin-function-try-catch

Babel plugin that can add try/catch to function automatically.

Usage

Install:

yarn add babel-plugin-function-try-catch
or
npm install babel-plugin-function-try-catch

Example

before:

var fn = function(){
  console.log('hello world');
}

after:

var fn = function () {
  try {
    console.log(2);
  } catch (error) {
    // you can do some stuff here, for example, report this error with monitor sdk.
    console.log(error);
  }
};

How to config

webpack:

rules: [{
  test: /\.js$/,
  exclude: /node_modules/,
  use: [
+    "babel-plugin-function-try-catch",
    "babel-loader",
  ]
}]