@intlayer/config
Version:
Retrieve Intlayer configurations and manage environment variables for both server-side and client-side environments.
72 lines (70 loc) • 3.28 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('../_virtual/_rolldown/runtime.cjs');
let _intlayer_types_nodeType = require("@intlayer/types/nodeType");
//#region src/utils/getUsedNodeTypes.ts
/**
* Detect whether a plain object looks like a serialized React element.
* React serializes JSX as: { key, props, _owner, _store }.
* Preact serializes VNodes similarly with a `_store` field.
*/
const isReactLikeElement = (obj) => typeof obj.props !== "undefined" && "key" in obj && typeof obj._store !== "undefined";
/**
* Detect whether a plain object looks like a serialized Solid element.
* Solid JSX is serialized as: { type, props } without the React-specific
* `_store` / `_owner` internal fields.
*/
const isSolidLikeElement = (obj) => typeof obj.props !== "undefined" && typeof obj._store === "undefined" && typeof obj.nodeType === "undefined" && "type" in obj;
/** Recursively collect every `nodeType` string found in a value. */
const collectNodeTypes = (value, result) => {
if (!value || typeof value !== "object") return;
if (Array.isArray(value)) {
for (const item of value) collectNodeTypes(item, result);
return;
}
const obj = value;
if (typeof obj.nodeType === "string") result.add(obj.nodeType);
else if (isReactLikeElement(obj)) {
result.add(_intlayer_types_nodeType.REACT_NODE);
result.add(_intlayer_types_nodeType.PREACT_NODE);
} else if (isSolidLikeElement(obj)) result.add(_intlayer_types_nodeType.SOLID_NODE);
for (const key of Object.keys(obj)) collectNodeTypes(obj[key], result);
};
/**
* Returns the set of NodeType strings actually used across the given
* built dictionaries.
*
* @example
* const used = getUsedNodeTypes(getDictionaries(config));
* // Set { 'translation', 'enumeration' }
*/
const getUsedNodeTypes = (dictionaries) => {
const result = /* @__PURE__ */ new Set();
const dicts = Array.isArray(dictionaries) ? dictionaries : Object.values(dictionaries);
for (const dict of dicts) collectNodeTypes(dict.content, result);
return [...result];
};
const getUnusedNodeTypes = (dictionaries) => {
const usedNodeTypes = getUsedNodeTypes(dictionaries);
return _intlayer_types_nodeType.PLUGIN_NODE_TYPES.filter((nodeType) => !usedNodeTypes.includes(nodeType));
};
const getUsedNodeTypesAsync = async (dictionaries) => {
const dictionariesArray = Array.isArray(dictionaries) ? dictionaries : Object.values(dictionaries);
const results = await Promise.all(dictionariesArray.map(async (dictionary) => {
const result = /* @__PURE__ */ new Set();
collectNodeTypes(dictionary.content, result);
return result;
}));
const finalResult = /* @__PURE__ */ new Set();
for (const res of results) for (const val of res) finalResult.add(val);
return [...finalResult];
};
const getUnusedNodeTypesAsync = async (dictionaries) => {
const usedNodeTypes = await getUsedNodeTypesAsync(dictionaries);
return _intlayer_types_nodeType.PLUGIN_NODE_TYPES.filter((nodeType) => !usedNodeTypes.includes(nodeType));
};
//#endregion
exports.getUnusedNodeTypes = getUnusedNodeTypes;
exports.getUnusedNodeTypesAsync = getUnusedNodeTypesAsync;
exports.getUsedNodeTypes = getUsedNodeTypes;
exports.getUsedNodeTypesAsync = getUsedNodeTypesAsync;
//# sourceMappingURL=getUsedNodeTypes.cjs.map