@lx-frontend/taro-plugin-monitor
Version:
taro 小程序监控插件,收集生命周期、事件、错误等数据
112 lines (104 loc) • 4.06 kB
JavaScript
Object.defineProperty(exports, '__esModule', { value: true });
var shared = require('@tarojs/shared');
var path = require('path');
function _interopNamespace(e) {
if (e && e.__esModule) return e;
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () { return e[k]; }
});
}
});
}
n["default"] = e;
return Object.freeze(n);
}
var path__namespace = /*#__PURE__*/_interopNamespace(path);
// 编译时到运行时的配置,不能放到 index.ts 中去加载,否则就会出现命名打包排除了
// 然后又 require ,出现顺序错误加载失败问题。
var defaultMonitorSetting = {
/**
* 允许触发事件的节点名称
*/
allowNodeNames: ['view', 'button', 'text', 'image'],
/**
* 是否开启节点监控
*/
enableNodeMonitor: true,
/**
* 是否开启 API 监控
*/
enableApiMonitor: true,
/**
* 是否开启生命周期监控
*/
enableLifecycleMonitor: true,
/**
* 是否开启错误监控
*/
enableErrorMonitor: true,
};
var index = (function (ctx, options) {
var fs = ctx.helper.fs;
ctx.registerMethod({
name: 'onSetupClose',
fn: function (platform) {
var monitor = options.monitor;
var template = platform.template;
if (!template)
return;
if (!shared.isObject(monitor)) {
throw new Error('monitor must be an object, eq: { allowNodeNames: ["view", "button", "text", "image"], enableApiMonitor: true, enableLifecycleMonitor: true, enableErrorMonitor: true }');
}
// 如果配置了 enableTid,则开启生成 data-tid 属性
// 遍历所有组件,增加 data-tid 属性,便于自动化测试
// TODO: 后面继续研究
// if (monitor.enableTid ?? defaultMonitorSetting.enableTid) {
// // Notice:当前发现部分组件对新增 data-tid 属性不友好,如果增加了自定义属性,可能导致页面渲染少一部分内容,或者属性错乱。
// // 'i.dataTid' 同名表示动态值,有值小程序才会渲染。
// template.mergeComponents(ctx, {
// View: {
// 'tid': 'i.tid'
// }
// })
// }
// 把监控配置转换成运行时配置。
injectMonitorSetting(fs, monitor);
// 注入 runtime path
// taro 支持多个 runtime:platform.runtimePath = [platform.runtimePath, injectedPath]
// 通过这种方式,可以 动态扩展 hostConfig 和修改各种构建配置参数。
// packages/taro-webpack5-runner/src/plugins/MiniPlugin.ts
// runner 会把 runtimePath 传给 @taro/loader 进行处理
// link: packages/taro-loader/src/app.ts
injectRuntimePath(platform);
},
});
});
function injectRuntimePath(platform) {
var injectedPath = "@lx-frontend/taro-plugin-monitor/dist/runtime";
if (shared.isArray(platform.runtimePath)) {
platform.runtimePath.push(injectedPath);
}
else if (shared.isString(platform.runtimePath)) {
platform.runtimePath = [platform.runtimePath, injectedPath];
}
}
/**
* 把编译时参数转换成运行时配置
* @param fs 文件系统
* @param monitorSetting 监控配置
*/
function injectMonitorSetting(fs, monitorSetting) {
// 合并配置
var mergedMonitorSetting = Object.assign(defaultMonitorSetting, monitorSetting);
// 写入文件
fs.writeFileSync(path__namespace.resolve(__dirname, '../dist/monitorSetting.js'), "\nexport const monitorSetting = ".concat(JSON.stringify(mergedMonitorSetting), ";\n"));
}
exports["default"] = index;
//# sourceMappingURL=index.js.map
;