statistic.js 2.4 KB

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