/**
* @intlayer/dictionaries-entry is a package that only returns the dictionary entry file.
* Using an external package allow to alias it in the bundle configuration (such as webpack).
* The alias allow hot reload the app (such as nextjs) on any dictionary change.
*/
import { existsSync } from 'node:fs';
import { join } from 'node:path';
import { clearModuleCache, configESMxCJSRequire } from '@intlayer/config';
import config from '@intlayer/config/built';
import type { DictionaryRegistry, IntlayerConfig } from '@intlayer/types';
type GetDictionaries = (configuration?: IntlayerConfig) => DictionaryRegistry;
export const getDictionaries: GetDictionaries = (
configuration: IntlayerConfig = config
) => {
const { content, build } = configuration;
// Always use cjs for dictionaries entry as it uses require
const dictionariesPath = join(content.mainDir, `dictionaries.cjs`);
let dictionaries = {};
if (existsSync(dictionariesPath)) {
// Clear cache for dictionaries.cjs and all its dependencies (JSON files)
clearModuleCache(dictionariesPath);
dictionaries = (build.require ?? configESMxCJSRequire)(dictionariesPath);
}
return (dictionaries ?? {}) as DictionaryRegistry;
};