@@ -246,15 +246,15 @@ function generateArticleComponentsCode(articles) {
246246 for ( const locale of locales ) {
247247 const localeArticles = articles [ locale ] ;
248248 const componentEntries = localeArticles . map ( article =>
249- ` '${ article . slug } ': require( '@content/articles/${ locale } /${ article . slug } .mdx').default ,`
249+ ` '${ article . slug } ': () => import( '@content/articles/${ locale } /${ article . slug } .mdx'),`
250250 ) . join ( '\n' ) ;
251251
252252 if ( componentEntries ) {
253253 componentLines . push ( ` '${ locale } ': {\n${ componentEntries } \n },` ) ;
254254 }
255255 }
256256
257- return `const articleComponents: Record<string, Record<string, React.ComponentType>> = {\n${ componentLines . join ( '\n' ) } \n};` ;
257+ return `const articleComponents: Record<string, Record<string, () => Promise<{ default: React.ComponentType }> >> = {\n${ componentLines . join ( '\n' ) } \n};` ;
258258}
259259
260260// Generate component imports for docs
@@ -265,15 +265,15 @@ function generateDocComponentsCode(docs) {
265265 for ( const locale of locales ) {
266266 const localeDocs = docs [ locale ] ;
267267 const componentEntries = localeDocs . map ( doc =>
268- ` '${ doc . slug } ': require( '@content/docs/${ locale } /${ doc . slug } .mdx').default ,`
268+ ` '${ doc . slug } ': () => import( '@content/docs/${ locale } /${ doc . slug } .mdx'),`
269269 ) . join ( '\n' ) ;
270270
271271 if ( componentEntries ) {
272272 componentLines . push ( ` '${ locale } ': {\n${ componentEntries } \n },` ) ;
273273 }
274274 }
275275
276- return `const docComponents: Record<string, Record<string, React.ComponentType>> = {\n${ componentLines . join ( '\n' ) } \n};` ;
276+ return `const docComponents: Record<string, Record<string, () => Promise<{ default: React.ComponentType }> >> = {\n${ componentLines . join ( '\n' ) } \n};` ;
277277}
278278
279279// Generate component import for manifesto (single index.mdx per locale)
@@ -283,11 +283,11 @@ function generateManifestoComponentsCode() {
283283 for ( const locale of SUPPORTED_LOCALES ) {
284284 const manifestoIndex = path . join ( rootDir , `content/manifesto/${ locale } /index.mdx` ) ;
285285 if ( fs . existsSync ( manifestoIndex ) ) {
286- componentLines . push ( ` '${ locale } ': require( '@content/manifesto/${ locale } /index.mdx').default ,` ) ;
286+ componentLines . push ( ` '${ locale } ': () => import( '@content/manifesto/${ locale } /index.mdx'),` ) ;
287287 }
288288 }
289289
290- return ` const components: Record<string, React.ComponentType> = {\n${ componentLines . join ( '\n' ) } \n };` ;
290+ return ` const components: Record<string, () => Promise<{ default: React.ComponentType }> > = {\n${ componentLines . join ( '\n' ) } \n };` ;
291291}
292292
293293// Main execution
@@ -359,12 +359,16 @@ export function getArticleBySlug(slug: string, locale: string = 'en'): ArticleMe
359359 return localeArticles.find((article) => article.slug === slug);
360360}
361361
362- // MDX components mapping for all locales (webpack will handle this at build time )
362+ // MDX components mapping for all locales (dynamic imports )
363363${ articlesComponentsCode }
364364
365- // Get components for a specific locale with fallback to English
366- export function getArticleComponents(locale: string = 'en'): Record<string, React.ComponentType> {
367- return articleComponents[locale] || articleComponents['en'] || {};
365+ // Get a specific article component for a given locale and slug
366+ export async function getArticleComponent(locale: string = 'en', slug: string): Promise<React.ComponentType | null> {
367+ const loaders = articleComponents[locale] || articleComponents['en'];
368+ const loader = loaders?.[slug];
369+ if (!loader) return null;
370+ const mdxModule = await loader();
371+ return mdxModule.default;
368372}
369373` ;
370374
@@ -397,12 +401,16 @@ export function getDocBySlug(slug: string, locale: string = 'en'): DocSection |
397401 return sections.find((doc) => doc.slug === slug);
398402}
399403
400- // MDX components mapping for all locales (webpack will handle this at build time )
404+ // MDX components mapping for all locales (dynamic imports )
401405${ docsComponentsCode }
402406
403- // Get components for a specific locale with fallback to English
404- export function getDocComponents(locale: string = 'en'): Record<string, React.ComponentType> {
405- return docComponents[locale] || docComponents['en'] || {};
407+ // Get a specific doc component for a given locale and slug
408+ export async function getDocComponent(locale: string = 'en', slug: string): Promise<React.ComponentType | null> {
409+ const loaders = docComponents[locale] || docComponents['en'];
410+ const loader = loaders?.[slug];
411+ if (!loader) return null;
412+ const mdxModule = await loader();
413+ return mdxModule.default;
406414}
407415` ;
408416
@@ -417,10 +425,12 @@ export function getDocComponents(locale: string = 'en'): Record<string, React.Co
417425 * Return the Manifesto MDX React component for a given locale.
418426 * Falls back to the default locale ('en') when the requested locale is missing.
419427 */
420- export function getManifestoComponent(locale: string = 'en'): React.ComponentType {
428+ export async function getManifestoComponent(locale: string = 'en'): Promise< React.ComponentType> {
421429${ manifestoComponentsCode }
422430
423- return components[locale] || components['en'];
431+ const loader = components[locale] || components['en'];
432+ const mdxModule = await loader();
433+ return mdxModule.default;
424434}
425435` ;
426436
0 commit comments