statistic.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports._statistic_build_ = void 0;
  7. exports.default = statisticToken;
  8. exports.merge = merge;
  9. exports.statistic = void 0;
  10. var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
  11. const enableStatistic = process.env.NODE_ENV !== 'production' || typeof CSSINJS_STATISTIC !== 'undefined';
  12. let 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 (0, _extends2.default)({}, ...objs);
  24. }
  25. recording = false;
  26. const ret = {};
  27. objs.forEach(obj => {
  28. const keys = Object.keys(obj);
  29. keys.forEach(key => {
  30. Object.defineProperty(ret, key, {
  31. configurable: true,
  32. enumerable: true,
  33. get: () => obj[key]
  34. });
  35. });
  36. });
  37. recording = true;
  38. return ret;
  39. }
  40. /** @private Internal Usage. Not use in your production. */
  41. const statistic = exports.statistic = {};
  42. /** @private Internal Usage. Not use in your production. */
  43. // eslint-disable-next-line camelcase
  44. const _statistic_build_ = exports._statistic_build_ = {};
  45. /* istanbul ignore next */
  46. function noop() {}
  47. /** Statistic token usage case. Should use `merge` function if you do not want spread record. */
  48. function statisticToken(token) {
  49. let tokenKeys;
  50. let proxy = token;
  51. let flush = noop;
  52. if (enableStatistic) {
  53. tokenKeys = new Set();
  54. proxy = new Proxy(token, {
  55. get(obj, prop) {
  56. if (recording) {
  57. tokenKeys.add(prop);
  58. }
  59. return obj[prop];
  60. }
  61. });
  62. flush = (componentName, componentToken) => {
  63. statistic[componentName] = {
  64. global: Array.from(tokenKeys),
  65. component: componentToken
  66. };
  67. };
  68. }
  69. return {
  70. token: proxy,
  71. keys: tokenKeys,
  72. flush
  73. };
  74. }