@intlayer/config
Version:
Retrieve Intlayer configurations and manage environment variables for both server-side and client-side environments.
56 lines (54 loc) • 2.93 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
//#region src/envVars/envVars.ts
/**
* Converts a camelCase node-type string to SCREAMING_SNAKE_CASE so that
* the generated env-var name matches what the plugin source files check.
*
* @example
* toScreamingSnakeCase('reactNode') // 'REACT_NODE'
* toScreamingSnakeCase('markdown') // 'MARKDOWN'
*/
const toScreamingSnakeCase = (str) => str.replace(/([A-Z])/g, "_$1").toUpperCase().replace(/^_/, "");
/**
* Converts a list of unused NodeType keys into env-var definitions.
* Set to `"false"` so bundlers can eliminate the corresponding plugin code.
*
* @example
* formatNodeTypeToEnvVar(['enumeration'])
* // { 'INTLAYER_NODE_TYPE_ENUMERATION': '"false"' }
*
* formatNodeTypeToEnvVar(['reactNode'], (k) => `process.env.${k}`, (v) => `"${v}"`)
* // { 'process.env.INTLAYER_NODE_TYPE_REACT_NODE': '"false"' }
*/
const formatNodeTypeToEnvVar = (nodeTypes, wrapKey = (key) => key, wrapValue = (value) => value) => nodeTypes.reduce((acc, nodeType) => {
acc[wrapKey(`INTLAYER_NODE_TYPE_${toScreamingSnakeCase(nodeType)}`)] = wrapValue("false");
return acc;
}, {});
/**
* Returns env-var definitions for the full Intlayer config to be injected at
* build time. Allows bundlers to dead-code-eliminate unused routing modes,
* rewrite logic, storage mechanisms, and editor code.
*
* @example
* getConfigEnvVars(config)
* // { INTLAYER_ROUTING_MODE: '"prefix-no-default"', INTLAYER_ROUTING_REWRITE_RULES: '"false"', ... }
*
* getConfigEnvVars(config, true)
* // { 'process.env.INTLAYER_ROUTING_MODE': '"prefix-no-default"', ... }
*/
const getConfigEnvVars = (config, wrapKey = (key) => key, wrapValue = (value) => value) => {
const { routing, editor } = config;
const envVars = { [wrapKey("INTLAYER_ROUTING_MODE")]: wrapValue(routing.mode) };
if (!routing.rewrite) envVars[wrapKey("INTLAYER_ROUTING_REWRITE_RULES")] = wrapValue("false");
if (!routing.domains || Object.keys(routing.domains).length === 0) envVars[wrapKey("INTLAYER_ROUTING_DOMAINS")] = wrapValue("false");
if (!routing.storage.cookies || routing.storage.cookies.length === 0) envVars[wrapKey("INTLAYER_ROUTING_STORAGE_COOKIES")] = wrapValue("false");
if (!routing.storage.localStorage || routing.storage.localStorage.length === 0) envVars[wrapKey("INTLAYER_ROUTING_STORAGE_LOCALSTORAGE")] = wrapValue("false");
if (!routing.storage.sessionStorage || routing.storage.sessionStorage.length === 0) envVars[wrapKey("INTLAYER_ROUTING_STORAGE_SESSIONSTORAGE")] = wrapValue("false");
if (!routing.storage.headers || routing.storage.headers.length === 0) envVars[wrapKey("INTLAYER_ROUTING_STORAGE_HEADERS")] = wrapValue("false");
if (editor?.enabled === false) envVars[wrapKey("INTLAYER_EDITOR_ENABLED")] = wrapValue("false");
return envVars;
};
//#endregion
exports.formatNodeTypeToEnvVar = formatNodeTypeToEnvVar;
exports.getConfigEnvVars = getConfigEnvVars;
//# sourceMappingURL=envVars.cjs.map