# @umengfe/babel-plugin-uapm-trycatch

> > Babel plugin 可自动对文件中存在的Catch表达式插入友盟WEB/小程序的异常上报的脚本

Latest version **0.0.1** (published 2022-11-08) · ISC license · 0 weekly downloads

## Install

```sh
npm install @umengfe/babel-plugin-uapm-trycatch
pnpm add @umengfe/babel-plugin-uapm-trycatch
yarn add @umengfe/babel-plugin-uapm-trycatch
bun add @umengfe/babel-plugin-uapm-trycatch
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1 |
| Published | 2022-11-08 |
| First published | 2022-11-08 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 9 |
| Unpacked size | 21.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | pdap, umfe |
| Keywords | babel-plugin |

## Links

- npm: https://www.npmjs.com/package/@umengfe/babel-plugin-uapm-trycatch
- npm.io page: https://npm.io/package/@umengfe/babel-plugin-uapm-trycatch

## Dependencies (9)

- [picomatch](https://npm.io/package/picomatch.md) ^2.3.1
- [@babel/core](https://npm.io/package/@babel/core.md) ^7.18.13
- [@babel/types](https://npm.io/package/@babel/types.md) ^7.18.13
- [@babel/parser](https://npm.io/package/@babel/parser.md) ^7.18.13
- [@babel/template](https://npm.io/package/@babel/template.md) ^7.10.4
- [@babel/traverse](https://npm.io/package/@babel/traverse.md) ^7.18.13
- [@babel/generator](https://npm.io/package/@babel/generator.md) ^7.18.13
- [@babel/helper-plugin-utils](https://npm.io/package/@babel/helper-plugin-utils.md) ^7.0.0
- [@types/babel__helper-plugin-utils](https://npm.io/package/@types/babel__helper-plugin-utils.md) ^7.10.0

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.0.1 (latest) — 2022-11-08

## README

## @alife/babel-plugin-uapm-trycatch
> Babel plugin 可自动对文件中存在的Catch表达式插入友盟WEB/小程序的异常上报的脚本


### Usage

Install:
```
npm install --save-dev @alife/babel-plugin-uapm-trycatch
```

### Support Platform

| 支持平台 | 是否支持 |
| --- | --- | 
| WEB/H5 | - [x]  |
| 微信小程序 | - [x]  |
| 支付宝小程序 | - [x]  |


### Options
| 配置参数 | 是否必选 | 参数说明 | 参数类型 | 示例 |
| --- | --- | --- | --- | --- |
| include | 否  | 包含的文件及路径规则 | string[] | include: ['**/*.tsx', '**/*.ts'] |
| exclude | 否  | 排除的文件及路径规则 | string[] | exclude: ['**/node_modules'] |
| platform  | 否  | 目标构建平台,支持(wechat、web、alipay)  | string | platform: 'web' |

> 目标构建平台也可配合构建命令 通过约定的环境变量(`UAPM_PLATFORM`)在package.json的构建命令中进行指定，同时配置取值优先级options高于全局变量
```json
// package.json
{
  "name": "",
  "version": "0.1.0",
  "scripts": {
    "build:h5": "cross-env UAPM_PLATFORM=web npm run build"
  }
}
```

### Example

before:
```ts
const fn = () =>  {
  try {
    console.log(test);
  } catch (error) {}
};
```

after:
```ts
const fn = () =>  {
  try {
    console.log(test);
  } catch (error) {
      // WEB
+     try { window && window._apm && window._apm.captureException && window._apm.captureException(error); } catch(e){}
      // 小程序
+     try { wx && wx.umapm && wx.$umapm.captureException && wx.$umapm.captureException(error); } catch(e){}
      // 支付宝
+     try { my && my.$umapm && my.$umapm.captureException && my.$umapm.captureException(error); } catch(e){}
  }
};

```


## How to config
.babelrc
```diff
{
  presets: [
    '@babel/preset-env',
    '@babel/preset-react',
    '@babel/preset-typescript',
  ],
  plugins: [
    '@babel/plugin-transform-runtime',
+    [
+      @alife/babel-plugin-uapm-trycatch,
+      {
+        include: ['**/*.tsx', '**/*.ts'],
+        platform: 'web'
+      }
+    ]
  ],
};

uniapp babel.config.js
```diff
+ if (process.env.UNI_PLATFORM === 'h5' || process.env.UNI_PLATFORM === 'alipay' || process.env.UNI_PLATFORM === 'wexin') {
+   plugins.push([
+     @alife/babel-plugin-uapm-trycatch,
+     {
+         exclude: ['**/node_modules'],
+         platform: process.env.UAPM_PLATFORM
+     }
+   ])
+ }
module.exports = {
  presets: [
    [
      '@vue/app',
      {
        modules: 'commonjs',
        useBuiltIns: process.env.UNI_PLATFORM === 'h5' ? 'usage' : 'entry'
      }
    ]
  ],
  plugins
}

```

---
_Source: https://npm.io/package/@umengfe/babel-plugin-uapm-trycatch · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
