From feaa7d7a9e9d0284319bdd5a10da7ebe9b157ad3 Mon Sep 17 00:00:00 2001 From: johndoe6345789 Date: Sat, 17 Jan 2026 17:46:36 +0000 Subject: [PATCH] Generated by Spark: Make this function smarter, wide variety of search paths and other permissive logic - function createLazyComponent(componentConfig: ComponentConfig) { const loader = () => { if (componentConfig.preloadDependencies) { componentConfig.preloadDependencies.forEach(depName => { const preloader = dependencyPreloaders[depName] if (preloader) { preloader() } }) } return import(componentConfig.path).then(m => ({ default: m[componentConfig.export] })) } --- src/lib/component-registry.ts | 117 +++++++++++++++++++++++++++++++++- 1 file changed, 115 insertions(+), 2 deletions(-) diff --git a/src/lib/component-registry.ts b/src/lib/component-registry.ts index e911421..18a9da0 100644 --- a/src/lib/component-registry.ts +++ b/src/lib/component-registry.ts @@ -36,7 +36,7 @@ const dependencyPreloaders: Record void> = { } function createLazyComponent(componentConfig: ComponentConfig) { - const loader = () => { + const loader = async () => { if (componentConfig.preloadDependencies) { componentConfig.preloadDependencies.forEach(depName => { const preloader = dependencyPreloaders[depName] @@ -46,7 +46,49 @@ function createLazyComponent(componentConfig: ComponentConfig) { }) } - return import(componentConfig.path).then(m => ({ default: m[componentConfig.export] })) + const pathVariants = generatePathVariants(componentConfig.path, componentConfig.name) + + let lastError: Error | null = null + for (const path of pathVariants) { + try { + const module = await import(path) + const exportName = componentConfig.export || 'default' + + if (module[exportName]) { + return { default: module[exportName] } + } + + if (exportName === 'default' && module.default) { + return { default: module.default } + } + + if (module[componentConfig.name]) { + return { default: module[componentConfig.name] } + } + + const firstExport = Object.keys(module).find(key => + key !== '__esModule' && + key !== 'default' && + typeof module[key] === 'function' + ) + + if (firstExport) { + return { default: module[firstExport] } + } + + if (module.default) { + return { default: module.default } + } + + return { default: module } + } catch (err) { + lastError = err as Error + continue + } + } + + console.error(`[REGISTRY] ❌ Failed to load component "${componentConfig.name}" after trying ${pathVariants.length} path variants`) + throw lastError || new Error(`Failed to load component: ${componentConfig.name}`) } if (componentConfig.type === 'dialog' || componentConfig.type === 'pwa') { @@ -56,6 +98,77 @@ function createLazyComponent(componentConfig: ComponentConfig) { return lazyWithPreload(loader, componentConfig.name) } +function generatePathVariants(path: string, componentName: string): string[] { + const variants: string[] = [] + + const cleanPath = path.replace(/\.tsx?$/, '') + + variants.push(path) + + if (!path.endsWith('.tsx') && !path.endsWith('.ts')) { + variants.push(`${cleanPath}.tsx`) + variants.push(`${cleanPath}.ts`) + } + + if (path.startsWith('@/components/')) { + const relativePath = path.replace('@/components/', '') + variants.push(`/src/components/${relativePath}`) + variants.push(`/src/components/${relativePath}.tsx`) + variants.push(`/src/components/${relativePath}.ts`) + variants.push(`./components/${relativePath}`) + variants.push(`./components/${relativePath}.tsx`) + variants.push(`../components/${relativePath}`) + variants.push(`../components/${relativePath}.tsx`) + } + + if (path.startsWith('@/')) { + const relativePath = path.replace('@/', '') + variants.push(`/src/${relativePath}`) + variants.push(`/src/${relativePath}.tsx`) + variants.push(`/src/${relativePath}.ts`) + variants.push(`./${relativePath}`) + variants.push(`./${relativePath}.tsx`) + variants.push(`../${relativePath}`) + variants.push(`../${relativePath}.tsx`) + } + + if (!path.startsWith('@/') && !path.startsWith('/') && !path.startsWith('./') && !path.startsWith('../')) { + variants.push(`@/${path}`) + variants.push(`@/${path}.tsx`) + variants.push(`@/components/${path}`) + variants.push(`@/components/${path}.tsx`) + variants.push(`/src/${path}`) + variants.push(`/src/${path}.tsx`) + variants.push(`/src/components/${path}`) + variants.push(`/src/components/${path}.tsx`) + } + + if (path.endsWith('.tsx')) { + variants.push(path.replace('.tsx', '')) + } + + const pathWithoutExt = cleanPath + const lastSegment = pathWithoutExt.split('/').pop() || '' + + if (lastSegment !== componentName) { + const dirPath = pathWithoutExt.substring(0, pathWithoutExt.lastIndexOf('/')) + if (dirPath) { + variants.push(`${dirPath}/${componentName}`) + variants.push(`${dirPath}/${componentName}.tsx`) + variants.push(`${dirPath}/${componentName}.ts`) + variants.push(`${dirPath}/index.tsx`) + variants.push(`${dirPath}/index.ts`) + } + } + + const seen = new Set() + return variants.filter(v => { + if (seen.has(v)) return false + seen.add(v) + return true + }) +} + function buildRegistry(components: ComponentConfig[]) { return components.reduce((registry, component) => { registry[component.name] = createLazyComponent(component)