uni.plugin.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. var index = () => [
  22. uniCliShared.defineUniMainJsPlugin((opts) => {
  23. let statVersion = '1';
  24. let isEnable = false;
  25. const stats = {
  26. '@dcloudio/uni-stat': uniCliShared.resolveBuiltIn('@dcloudio/uni-stat/dist/uni-stat.es.js'),
  27. '@dcloudio/uni-cloud-stat': uniCliShared.resolveBuiltIn('@dcloudio/uni-stat/dist/uni-cloud-stat.es.js'),
  28. };
  29. return {
  30. name: 'uni:stat',
  31. enforce: 'pre',
  32. config(config, env) {
  33. if (process.env.UNI_COMPILE_TARGET === 'uni_modules') {
  34. // 不需要统计
  35. return;
  36. }
  37. const inputDir = process.env.UNI_INPUT_DIR;
  38. const platform = process.env.UNI_PLATFORM;
  39. const titlesJson = Object.create(null);
  40. uniCliShared.parsePagesJson(inputDir, platform).pages.forEach((page) => {
  41. var _a;
  42. const style = page.style || {};
  43. const titleText =
  44. // MP
  45. style.navigationBarTitleText ||
  46. (
  47. // H5 || App
  48. (_a = style.navigationBar) === null || _a === void 0 ? void 0 : _a.titleText) ||
  49. '';
  50. if (titleText) {
  51. titlesJson[page.path] = titleText;
  52. }
  53. });
  54. // ssr 时不开启
  55. if (!uniCliShared.isSsr(env.command, config)) {
  56. const statConfig = uniCliShared.getUniStatistics(inputDir, platform);
  57. isEnable = statConfig.enable === true;
  58. if (isEnable) {
  59. const uniCloudConfig = statConfig.uniCloud || {};
  60. // 获取manifest.json 统计配置,插入环境变量中
  61. process.env.UNI_STATISTICS_CONFIG = JSON.stringify(statConfig);
  62. statVersion = Number(statConfig.version) === 2 ? '2' : '1';
  63. process.env.UNI_STAT_UNI_CLOUD = JSON.stringify(uniCloudConfig);
  64. process.env.UNI_STAT_DEBUG = statConfig.debug ? 'true' : 'false';
  65. if (process.env.NODE_ENV === 'production') {
  66. const manifestJson = uniCliShared.parseManifestJsonOnce(inputDir);
  67. if (!manifestJson.appid) {
  68. uniStatLog(uniCliShared.M['stat.warn.appid']);
  69. isEnable = false;
  70. }
  71. else {
  72. if (!statConfig.version) {
  73. uniStatLog(uniCliShared.M['stat.warn.version']);
  74. }
  75. else {
  76. uniStatLog(`已开启 uni统计${statVersion}.0 版本`);
  77. }
  78. }
  79. }
  80. else {
  81. if (!statConfig.version) {
  82. uniStatLog(uniCliShared.M['stat.warn.version']);
  83. }
  84. else {
  85. uniStatLog(uniCliShared.M['stat.warn.tip'].replace('{version}', `${statVersion}.0`));
  86. }
  87. }
  88. }
  89. debug__default.default('uni:stat')('isEnable', isEnable);
  90. }
  91. process.env.UNI_STAT_TITLE_JSON = JSON.stringify(titlesJson);
  92. return {
  93. define: {
  94. 'process.env.UNI_STAT_TITLE_JSON': process.env.UNI_STAT_TITLE_JSON,
  95. 'process.env.UNI_STAT_UNI_CLOUD': process.env.UNI_STAT_UNI_CLOUD,
  96. 'process.env.UNI_STAT_DEBUG': process.env.UNI_STAT_DEBUG,
  97. 'process.env.UNI_STATISTICS_CONFIG': process.env.UNI_STATISTICS_CONFIG,
  98. },
  99. };
  100. },
  101. resolveId(id) {
  102. return stats[id] || null;
  103. },
  104. transform(code, id) {
  105. if (isEnable && opts.filter(id)) {
  106. return {
  107. code: code +
  108. `;import '@dcloudio/uni${statVersion === '2' ? '-cloud' : ''}-stat';`,
  109. map: null,
  110. };
  111. }
  112. },
  113. };
  114. }),
  115. ];
  116. module.exports = index;