statistic.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = exports._statistic_build_ = void 0;
  7. exports.merge = merge;
  8. exports.statistic = void 0;
  9. var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
  10. var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
  11. var enableStatistic = process.env.NODE_ENV !== 'production' || typeof CSSINJS_STATISTIC !== 'undefined';
  12. var recording = true;
  13. /**
  14. * This function will do as `Object.assign` in production. But will use Object.defineProperty:get to
  15. * pass all value access in development. To support statistic field usage with alias token.
  16. */
  17. function merge() {
  18. for (var _len = arguments.length, objs = new Array(_len), _key = 0; _key < _len; _key++) {
  19. objs[_key] = arguments[_key];
  20. }
  21. /* istanbul ignore next */
  22. if (!enableStatistic) {
  23. return Object.assign.apply(Object, [{}].concat(objs));
  24. }
  25. recording = false;
  26. var ret = {};
  27. objs.forEach(function (obj) {
  28. if ((0, _typeof2.default)(obj) !== 'object') {
  29. return;
  30. }
  31. var keys = Object.keys(obj);
  32. keys.forEach(function (key) {
  33. Object.defineProperty(ret, key, {
  34. configurable: true,
  35. enumerable: true,
  36. get: function get() {
  37. return obj[key];
  38. }
  39. });
  40. });
  41. });
  42. recording = true;
  43. return ret;
  44. }
  45. /** @internal Internal Usage. Not use in your production. */
  46. var statistic = exports.statistic = {};
  47. /** @internal Internal Usage. Not use in your production. */
  48. var _statistic_build_ = exports._statistic_build_ = {};
  49. /* istanbul ignore next */
  50. function noop() {}
  51. /** Statistic token usage case. Should use `merge` function if you do not want spread record. */
  52. var statisticToken = function statisticToken(token) {
  53. var tokenKeys;
  54. var proxy = token;
  55. var flush = noop;
  56. if (enableStatistic && typeof Proxy !== 'undefined') {
  57. tokenKeys = new Set();
  58. proxy = new Proxy(token, {
  59. get: function get(obj, prop) {
  60. if (recording) {
  61. var _tokenKeys;
  62. (_tokenKeys = tokenKeys) === null || _tokenKeys === void 0 || _tokenKeys.add(prop);
  63. }
  64. return obj[prop];
  65. }
  66. });
  67. flush = function flush(componentName, componentToken) {
  68. var _statistic$componentN;
  69. statistic[componentName] = {
  70. global: Array.from(tokenKeys),
  71. component: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, (_statistic$componentN = statistic[componentName]) === null || _statistic$componentN === void 0 ? void 0 : _statistic$componentN.component), componentToken)
  72. };
  73. };
  74. }
  75. return {
  76. token: proxy,
  77. keys: tokenKeys,
  78. flush: flush
  79. };
  80. };
  81. var _default = exports.default = statisticToken;