0.0.1 • Published 2 years ago

@umengfe/babel-plugin-uapm-trycatch v0.0.1

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

@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)stringplatform: 'web'

目标构建平台也可配合构建命令 通过约定的环境变量(UAPM_PLATFORM)在package.json的构建命令中进行指定,同时配置取值优先级options高于全局变量

// package.json
{
  "name": "",
  "version": "0.1.0",
  "scripts": {
    "build:h5": "cross-env UAPM_PLATFORM=web npm run build"
  }
}

Example

before:

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

after:

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

{
  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
}