@umengfe/babel-plugin-uapm-trycatch
v0.0.1
Published
> Babel plugin 可自动对文件中存在的Catch表达式插入友盟WEB/小程序的异常上报的脚本
Downloads
6
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高于全局变量
// 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
}