UNPKG

@intlayer/chokidar

Version:

Uses chokidar to scan and build Intlayer declaration files into dictionaries based on Intlayer configuration.

61 lines (59 loc) 3.03 kB
import { readDictionariesFromDisk } from "../utils/readDictionariesFromDisk.mjs"; import { writeDynamicDictionary } from "./writeDynamicDictionary.mjs"; import { writeFetchDictionary } from "./writeFetchDictionary.mjs"; import { writeMergedDictionaries } from "./writeMergedDictionary.mjs"; import { writeUnmergedDictionaries } from "./writeUnmergedDictionary.mjs"; import { IMPORT_MODE, OUTPUT_FORMAT } from "@intlayer/config/defaultValues"; //#region src/buildIntlayerDictionary/buildIntlayerDictionary.ts const defaultOptions = { formats: OUTPUT_FORMAT, importOtherDictionaries: true, env: "dev" }; /** * This function transpile the bundled code to to make dictionaries as JSON files */ const buildDictionary = async (localDictionariesEntries, configuration, options) => { const importMode = configuration?.build?.importMode ?? configuration?.dictionary?.importMode ?? IMPORT_MODE; const { importOtherDictionaries, env, formats } = { ...defaultOptions, ...options }; const unmergedDictionariesToUpdate = [...localDictionariesEntries]; if (importOtherDictionaries) { const prevUnmergedDictionaries = readDictionariesFromDisk(configuration.system.unmergedDictionariesDir); for (const dictionaryToWrite of localDictionariesEntries) { const allPrebuiltUnmergedDictionaries = prevUnmergedDictionaries[dictionaryToWrite.key]; if (allPrebuiltUnmergedDictionaries?.length > 0) { const otherUnmergedDictionaries = allPrebuiltUnmergedDictionaries.filter((unmergedDictionary) => unmergedDictionary.localId !== dictionaryToWrite.localId); unmergedDictionariesToUpdate.push(...otherUnmergedDictionaries); } } } const unmergedDictionaries = await writeUnmergedDictionaries(unmergedDictionariesToUpdate, configuration, env); const mergedDictionaries = await writeMergedDictionaries(unmergedDictionaries, configuration); const dictionariesToBuildDynamic = {}; const keysToBuildFetch = /* @__PURE__ */ new Set(); for (const [key, mergedResult] of Object.entries(mergedDictionaries)) { const mode = mergedResult.dictionary.importMode ?? importMode; if (mode === "dynamic" || mode === "fetch") dictionariesToBuildDynamic[key] = mergedResult; if (mode === "fetch") keysToBuildFetch.add(key); } let dynamicDictionaries = null; if (Object.keys(dictionariesToBuildDynamic).length > 0) dynamicDictionaries = await writeDynamicDictionary(dictionariesToBuildDynamic, configuration, formats); let fetchDictionaries = null; if (dynamicDictionaries && keysToBuildFetch.size > 0) { const dictionariesToBuildFetch = {}; for (const key of keysToBuildFetch) if (dynamicDictionaries[key]) dictionariesToBuildFetch[key] = dynamicDictionaries[key]; if (Object.keys(dictionariesToBuildFetch).length > 0) fetchDictionaries = await writeFetchDictionary(dictionariesToBuildFetch, configuration, formats); } return { unmergedDictionaries, mergedDictionaries, dynamicDictionaries, fetchDictionaries }; }; //#endregion export { buildDictionary }; //# sourceMappingURL=buildIntlayerDictionary.mjs.map