uni.plugin.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. 'use strict';
  2. var debug = require('debug');
  3. var uniCliShared = require('@dcloudio/uni-cli-shared');
  4. function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
  5. var debug__default = /*#__PURE__*/_interopDefault(debug);
  6. function once(fn, ctx = null) {
  7. let res;
  8. return ((...args) => {
  9. if (fn) {
  10. res = fn.apply(ctx, args);
  11. fn = null;
  12. }
  13. return res;
  14. });
  15. }
  16. const uniStatLog = once((text) => {
  17. console.log();
  18. console.warn(text);
  19. console.log();
  20. });
  21. const uniStatDeviceLog = once((text) => {
  22. console.log();
  23. console.warn(text);
  24. console.log();
  25. });
  26. var index = () => [
  27. uniCliShared.defineUniMainJsPlugin((opts) => {
  28. let statVersion = '1';
  29. let isEnable = false;
  30. const stats = {
  31. '@dcloudio/uni-stat': uniCliShared.resolveBuiltIn('@dcloudio/uni-stat/dist/uni-stat.es.js'),
  32. '@dcloudio/uni-cloud-stat': uniCliShared.resolveBuiltIn('@dcloudio/uni-stat/dist/uni-cloud-stat.es.js'),
  33. };
  34. return {
  35. name: 'uni:stat',
  36. enforce: 'pre',
  37. config(config, env) {
  38. const inputDir = process.env.UNI_INPUT_DIR;
  39. const platform = process.env.UNI_PLATFORM;
  40. const titlesJson = Object.create(null);
  41. uniCliShared.parsePagesJson(inputDir, platform).pages.forEach((page) => {
  42. var _a;
  43. const style = page.style || {};
  44. const titleText =
  45. // MP
  46. style.navigationBarTitleText ||
  47. (
  48. // H5 || App
  49. (_a = style.navigationBar) === null || _a === void 0 ? void 0 : _a.titleText) ||
  50. '';
  51. if (titleText) {
  52. titlesJson[page.path] = titleText;
  53. }
  54. });
  55. // ssr 时不开启
  56. if (!uniCliShared.isSsr(env.command, config)) {
  57. const statConfig = uniCliShared.getUniStatistics(inputDir, platform);
  58. isEnable = statConfig.enable === true;
  59. if (isEnable) {
  60. const uniCloudConfig = statConfig.uniCloud || {};
  61. // 获取manifest.json 统计配置,插入环境变量中
  62. process.env.UNI_STATISTICS_CONFIG = JSON.stringify(statConfig);
  63. statVersion = Number(statConfig.version) === 2 ? '2' : '1';
  64. process.env.UNI_STAT_UNI_CLOUD = JSON.stringify(uniCloudConfig);
  65. process.env.UNI_STAT_DEBUG = statConfig.debug ? 'true' : 'false';
  66. if (process.env.NODE_ENV === 'production') {
  67. const manifestJson = uniCliShared.parseManifestJsonOnce(inputDir);
  68. if (!manifestJson.appid) {
  69. uniStatLog(uniCliShared.M['stat.warn.appid']);
  70. isEnable = false;
  71. }
  72. else {
  73. if (!statConfig.version) {
  74. uniStatLog(uniCliShared.M['stat.warn.version']);
  75. }
  76. else {
  77. uniStatLog(`已开启 uni统计${statVersion}.0 版本`);
  78. if (statVersion === '2') {
  79. uniStatDeviceLog('【重要】因 HBuilderX 3.4.9 版本起,uni统计2.0 调整了安卓端 deviceId 获取方式,导致 uni统计2.0 App-Android平台部分统计数据不准确。如使用了HBuilderX 3.4.9 - 3.6.4版本且开通了uni统计2.0的应用,需要使用HBuilderX3.6.7及以上版本重新发布应用并升级 uniAdmin 云函数解决,详见:https://ask.dcloud.net.cn/article/40097');
  80. }
  81. }
  82. }
  83. }
  84. else {
  85. if (!statConfig.version) {
  86. uniStatLog(uniCliShared.M['stat.warn.version']);
  87. }
  88. else {
  89. uniStatLog(uniCliShared.M['stat.warn.tip'].replace('{version}', `${statVersion}.0`));
  90. if (statVersion === '2') {
  91. uniStatDeviceLog('【重要】因 HBuilderX 3.4.9 版本起,uni统计2.0 调整了安卓端 deviceId 获取方式,导致 uni统计2.0 App-Android平台部分统计数据不准确。如使用了HBuilderX 3.4.9 - 3.6.4版本且开通了uni统计2.0的应用,需要使用HBuilderX3.6.7及以上版本重新发布应用并升级 uniAdmin 云函数解决,详见:https://ask.dcloud.net.cn/article/40097');
  92. }
  93. }
  94. }
  95. }
  96. debug__default.default('uni:stat')('isEnable', isEnable);
  97. }
  98. process.env.UNI_STAT_TITLE_JSON = JSON.stringify(titlesJson);
  99. return {
  100. define: {
  101. 'process.env.UNI_STAT_TITLE_JSON': process.env.UNI_STAT_TITLE_JSON,
  102. 'process.env.UNI_STAT_UNI_CLOUD': process.env.UNI_STAT_UNI_CLOUD,
  103. 'process.env.UNI_STAT_DEBUG': process.env.UNI_STAT_DEBUG,
  104. 'process.env.UNI_STATISTICS_CONFIG': process.env.UNI_STATISTICS_CONFIG,
  105. },
  106. };
  107. },
  108. resolveId(id) {
  109. return stats[id] || null;
  110. },
  111. transform(code, id) {
  112. if (isEnable && opts.filter(id)) {
  113. return {
  114. code: code +
  115. `;import '@dcloudio/uni${statVersion === '2' ? '-cloud' : ''}-stat';`,
  116. map: null,
  117. };
  118. }
  119. },
  120. };
  121. }),
  122. ];
  123. module.exports = index;