UNPKG

mdxlayer

Version:

Transform your MDX content into typed, JSON-powered data with flexible schema validation.

41 lines (38 loc) 1.36 kB
import path from 'node:path'; import { cache } from '../../cache/index.js'; import { transformFile } from '../../utils/transform.js'; import { toIndexDts } from './index-d.js'; const getExpName = (file, docType) => { const first = path.dirname(file).split(path.sep)[0]; const main = first === "." ? "all" : first; return `${main}${docType}`; }; const toIndexMjs = (files, docType) => { const isChanged = cache.changed(JSON.stringify(files), "index.js"); if (isChanged) { const exportsMap = {}; const out = []; for (const filePath of files) { const filename = filePath.replace(/\.mdx$/, ""); const impName = filename.replace(/[-.&/]/g, "_"); const moduleName = `./${docType}/${filename}.json`; const expName = getExpName(filePath, docType); const esmLine = `import ${impName} from '${moduleName}' assert { type: 'json' }`; out.push(esmLine); exportsMap[expName] ??= []; exportsMap[expName].push(impName); } Object.entries(exportsMap).forEach(([key, value]) => { const joinedValues = value.join(", "); const esmLine = `export const ${key} = [${joinedValues}];`; out.push(esmLine); }); transformFile({ doc: out.join("\n"), filename: "index.js", subpath: "generated" }); toIndexDts(exportsMap, docType); } }; export { toIndexMjs };