From de40ace56fee723d12d4eb50f71832e0cbb73984 Mon Sep 17 00:00:00 2001 From: Istvan Novak Date: Mon, 20 Oct 2025 08:03:12 +0200 Subject: [PATCH] Fix loading code-behind files in standalone mode --- .changeset/twelve-guests-care.md | 5 ++ xmlui/src/components-core/StandaloneApp.tsx | 90 +++++++++------------ 2 files changed, 41 insertions(+), 54 deletions(-) create mode 100644 .changeset/twelve-guests-care.md diff --git a/.changeset/twelve-guests-care.md b/.changeset/twelve-guests-care.md new file mode 100644 index 000000000..906360967 --- /dev/null +++ b/.changeset/twelve-guests-care.md @@ -0,0 +1,5 @@ +--- +"xmlui": patch +--- + +Fix loading code-behind files in standalone mode diff --git a/xmlui/src/components-core/StandaloneApp.tsx b/xmlui/src/components-core/StandaloneApp.tsx index bc0f47067..8ca79c13d 100644 --- a/xmlui/src/components-core/StandaloneApp.tsx +++ b/xmlui/src/components-core/StandaloneApp.tsx @@ -65,7 +65,7 @@ type RuntimeProps = { component?: ComponentDef | CompoundComponentDef; file?: string; src?: string; -} +}; // --- The properties of the standalone app type StandaloneAppProps = { @@ -743,26 +743,19 @@ function useStandalone( errorComponents.push(loadedEntryPoint!.component as ComponentDef); } - let loadedEntryPointCodeBehind = null; - if (loadedEntryPoint.component.props?.codeBehind !== undefined) { - // --- We have a code-behind file for the main component - loadedEntryPointCodeBehind = (await new Promise(async (resolve) => { - try { - const resp = await fetchWithoutCache( - resolvePath( - MAIN_FILE, - loadedEntryPoint.component.props?.codeBehind || MAIN_CODE_BEHIND_FILE, - ), - ); - const codeBehind = await parseCodeBehindResponse(resp); - resolve(codeBehind.hasError ? codeBehind : codeBehind.codeBehind); - } catch (e) { - resolve(null); - } - })) as any; - if (loadedEntryPointCodeBehind.hasError) { - errorComponents.push(loadedEntryPointCodeBehind.component as ComponentDef); + // -- Load the main component's code-behind file if any + const mainCodeBehind = loadedEntryPoint.component.props?.codeBehind || MAIN_CODE_BEHIND_FILE; + let loadedEntryPointCodeBehind = (await new Promise(async (resolve) => { + try { + const resp = await fetchWithoutCache(resolvePath(MAIN_FILE, mainCodeBehind)); + const codeBehind = await parseCodeBehindResponse(resp); + resolve(codeBehind?.hasError ? codeBehind : codeBehind.codeBehind); + } catch (e) { + resolve(null); } + })) as any; + if (loadedEntryPointCodeBehind?.hasError) { + errorComponents.push(loadedEntryPointCodeBehind.component as ComponentDef); } // --- Check if any of the components have markup errors @@ -797,9 +790,6 @@ function useStandalone( } else { if (compWrapper.file) { sources[compWrapper.file] = compWrapper.src; - // resolvedRuntime.projectCompilation.entrypoint.filename = MAIN_FILE; - // resolvedRuntime.projectCompilation.entrypoint.definition = loadedEntryPoint.component; - // resolvedRuntime.projectCompilation.entrypoint.markupSource = loadedEntryPoint.src; } } }); @@ -885,38 +875,30 @@ function useStandalone( definition: compWrapper.component as CompoundComponentDef, }; - let componentCodeBehind = null; - if ( - "codeBehind" in compWrapper.component && - compWrapper.component?.codeBehind !== undefined - ) { - // --- Promises for the component code-behind files - componentCodeBehind = (await new Promise(async (resolve) => { - try { - const codeBehind = await fetchWithoutCache( - resolvePath( - `components/${componentPath}`, - (compWrapper.component as CompoundComponentDef)?.codeBehind || - `${componentPath}.${codeBehindFileExtension}`, - ), - ); - const codeBehindWrapper = await parseCodeBehindResponse(codeBehind); - if (codeBehindWrapper.hasError) { - errorComponents.push(codeBehindWrapper.component as ComponentDef); - } - resolve( - codeBehindWrapper.hasError - ? (codeBehindWrapper.component as CompoundComponentDef) - : codeBehindWrapper, - ); - } catch { - resolve(null); + const componentCodeBehindFile = + (compWrapper.component as CompoundComponentDef)?.codeBehind || + `${componentPath}.${codeBehindFileExtension}`; + let componentCodeBehind = (await new Promise(async (resolve) => { + try { + const codeBehind = await fetchWithoutCache( + resolvePath(`components/${componentPath}`, componentCodeBehindFile), + ); + const codeBehindWrapper = await parseCodeBehindResponse(codeBehind); + if (codeBehindWrapper.hasError) { + errorComponents.push(codeBehindWrapper.component as ComponentDef); } - })) as Promise; - - if (componentCodeBehind && "src" in componentCodeBehind) { - compCompilation.codeBehindSource = componentCodeBehind.src; + resolve( + codeBehindWrapper.hasError + ? (codeBehindWrapper.component as CompoundComponentDef) + : codeBehindWrapper, + ); + } catch { + resolve(null); } + })) as any; + + if (componentCodeBehind && "src" in componentCodeBehind) { + compCompilation.codeBehindSource = (componentCodeBehind as any).src; } resolvedRuntime.projectCompilation.components.push(compCompilation); @@ -927,7 +909,7 @@ function useStandalone( ...(compWrapper.component as any).component, vars: { ...(compWrapper.component as any).component.vars, - ...componentCodeBehind?.component?.vars, + ...componentCodeBehind?.codeBehind?.vars, }, }, };