statistic.js 1.8 KB

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