UNPKG

@lucidcms/core

Version:

The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.

1 lines 3.34 kB
{"version":3,"file":"load-config-module.mjs","names":[],"sources":["../../../../src/libs/config/utils/load-config-module.ts"],"sourcesContent":["import path from \"node:path\";\nimport type { Jiti, NodeModule } from \"jiti\";\n\n/** Returns the cached module graph rooted at a loaded config entry. */\nconst getConfigModuleGraph = (entryPath: string, cache: Jiti[\"cache\"]) => {\n\tconst modules = new Set<string>();\n\tconst visit = (module: NodeModule | undefined) => {\n\t\tif (!module || modules.has(module.filename)) return;\n\t\tmodules.add(module.filename);\n\t\tfor (const child of module.children) visit(child);\n\t};\n\n\tvisit(cache[entryPath]);\n\treturn modules;\n};\n\n/** Imports a module and clears cache entries created by that import. */\nconst importModule = async <T>(props: {\n\tloader: Jiti;\n\tspecifier: string;\n\tclearCache: boolean;\n\tonLoad?: () => void;\n}) => {\n\tconst cachedModules = props.clearCache\n\t\t? new Set(Object.keys(props.loader.cache))\n\t\t: undefined;\n\ttry {\n\t\tconst module = await props.loader.import<T>(props.specifier);\n\t\tprops.onLoad?.();\n\t\treturn module;\n\t} finally {\n\t\tif (cachedModules) {\n\t\t\tfor (const modulePath of Object.keys(props.loader.cache)) {\n\t\t\t\tif (!cachedModules.has(modulePath)) {\n\t\t\t\t\tdelete props.loader.cache[modulePath];\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n};\n\n/** Loads a config module and optionally reports its local dependencies. */\nconst loadConfigModule = async <T>(props: {\n\tloader: Jiti;\n\tspecifier: string;\n\tdependencyEntryPath?: string;\n\tclearCache?: boolean;\n}) => {\n\tconst dependencyEntryPath = props.dependencyEntryPath;\n\tlet moduleGraph = new Set<string>();\n\tconst module = await importModule<T>({\n\t\tloader: props.loader,\n\t\tspecifier: props.specifier,\n\t\tclearCache: props.clearCache ?? false,\n\t\tonLoad: dependencyEntryPath\n\t\t\t? () => {\n\t\t\t\t\tmoduleGraph = getConfigModuleGraph(\n\t\t\t\t\t\tdependencyEntryPath,\n\t\t\t\t\t\tprops.loader.cache,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t: undefined,\n\t});\n\tconst dependencies = dependencyEntryPath\n\t\t? Array.from(moduleGraph).filter(\n\t\t\t\t(modulePath) =>\n\t\t\t\t\tmodulePath !== dependencyEntryPath &&\n\t\t\t\t\t!modulePath.includes(`${path.sep}node_modules${path.sep}`) &&\n\t\t\t\t\t!modulePath.endsWith(\".d.ts\"),\n\t\t\t)\n\t\t: [];\n\n\treturn { module, dependencies };\n};\n\nexport default loadConfigModule;\n"],"mappings":"yBAIA,MAAM,GAAwB,EAAmB,IAAyB,CACzE,IAAM,EAAU,IAAI,IACd,EAAS,GAAmC,CAC7C,MAAC,GAAU,EAAQ,IAAI,EAAO,QAAQ,GAC1C,GAAQ,IAAI,EAAO,QAAQ,EAC3B,IAAK,IAAM,KAAS,EAAO,SAAU,EAAM,CAAK,CADrB,CAE5B,EAGA,OADA,EAAM,EAAM,EAAU,EACf,CACR,EAGM,EAAe,KAAU,IAKzB,CACL,IAAM,EAAgB,EAAM,WACzB,IAAI,IAAI,OAAO,KAAK,EAAM,OAAO,KAAK,CAAC,EACvC,IAAA,GACH,GAAI,CACH,IAAM,EAAS,MAAM,EAAM,OAAO,OAAU,EAAM,SAAS,EAE3D,OADA,EAAM,SAAS,EACR,CACR,QAAU,CACT,GAAI,MACE,IAAM,KAAc,OAAO,KAAK,EAAM,OAAO,KAAK,EACjD,EAAc,IAAI,CAAU,GAChC,OAAO,EAAM,OAAO,MAAM,EAI9B,CACD,EAGM,EAAmB,KAAU,IAK7B,CACL,IAAM,EAAsB,EAAM,oBAC9B,EAAc,IAAI,IAuBtB,MAAO,CAAE,OAAA,MAtBY,EAAgB,CACpC,OAAQ,EAAM,OACd,UAAW,EAAM,UACjB,WAAY,EAAM,YAAc,GAChC,OAAQ,MACC,CACN,EAAc,EACb,EACA,EAAM,OAAO,KACd,CACD,EACC,IAAA,EACJ,CAAC,EAUgB,aATI,EAClB,MAAM,KAAK,CAAW,CAAC,CAAC,OACvB,GACA,IAAe,GACf,CAAC,EAAW,SAAS,GAAG,EAAK,IAAI,cAAc,EAAK,KAAK,GACzD,CAAC,EAAW,SAAS,OAAO,CAC9B,EACC,CAAC,CAE0B,CAC/B"}