@core-rc/cssinjs-utils
Version:
A cssinjs util library to support @core-rc and its ecosystem libraries.
64 lines (63 loc) • 2.06 kB
JavaScript
const enableStatistic = process.env.NODE_ENV !== 'production' || typeof CSSINJS_STATISTIC !== 'undefined';
let recording = true;
/**
* This function will do as `Object.assign` in production. But will use Object.defineProperty:get to
* pass all value access in development. To support statistic field usage with alias token.
*/
export function merge(...objs) {
/* istanbul ignore next */
if (!enableStatistic) {
return Object.assign({}, ...objs);
}
recording = false;
const ret = {};
objs.forEach((obj) => {
if (typeof obj !== 'object') {
return;
}
const keys = Object.keys(obj);
keys.forEach((key) => {
Object.defineProperty(ret, key, {
configurable: true,
enumerable: true,
get: () => obj[key],
});
});
});
recording = true;
return ret;
}
/** @internal Internal Usage. Not use in your production. */
export const statistic = {};
/** @internal Internal Usage. Not use in your production. */
export const _statistic_build_ = {};
/* istanbul ignore next */
function noop() { }
/** Statistic token usage case. Should use `merge` function if you do not want spread record. */
const statisticToken = (token) => {
let tokenKeys;
let proxy = token;
let flush = noop;
if (enableStatistic && typeof Proxy !== 'undefined') {
tokenKeys = new Set();
proxy = new Proxy(token, {
get(obj, prop) {
if (recording) {
tokenKeys?.add(prop);
}
return obj[prop];
},
});
flush = (componentName, componentToken) => {
statistic[componentName] = {
global: Array.from(tokenKeys),
component: {
...statistic[componentName]?.component,
...componentToken,
},
};
};
}
return { token: proxy, keys: tokenKeys, flush };
};
export default statisticToken;