Skip to content

Commit 4cfebf0

Browse files
authored
Fix loading code-behind files in standalone mode (#2235)
1 parent de33d91 commit 4cfebf0

File tree

2 files changed

+41
-54
lines changed

2 files changed

+41
-54
lines changed

.changeset/twelve-guests-care.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"xmlui": patch
3+
---
4+
5+
Fix loading code-behind files in standalone mode

xmlui/src/components-core/StandaloneApp.tsx

Lines changed: 36 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ type RuntimeProps = {
6565
component?: ComponentDef | CompoundComponentDef;
6666
file?: string;
6767
src?: string;
68-
}
68+
};
6969

7070
// --- The properties of the standalone app
7171
type StandaloneAppProps = {
@@ -743,26 +743,19 @@ function useStandalone(
743743
errorComponents.push(loadedEntryPoint!.component as ComponentDef);
744744
}
745745

746-
let loadedEntryPointCodeBehind = null;
747-
if (loadedEntryPoint.component.props?.codeBehind !== undefined) {
748-
// --- We have a code-behind file for the main component
749-
loadedEntryPointCodeBehind = (await new Promise(async (resolve) => {
750-
try {
751-
const resp = await fetchWithoutCache(
752-
resolvePath(
753-
MAIN_FILE,
754-
loadedEntryPoint.component.props?.codeBehind || MAIN_CODE_BEHIND_FILE,
755-
),
756-
);
757-
const codeBehind = await parseCodeBehindResponse(resp);
758-
resolve(codeBehind.hasError ? codeBehind : codeBehind.codeBehind);
759-
} catch (e) {
760-
resolve(null);
761-
}
762-
})) as any;
763-
if (loadedEntryPointCodeBehind.hasError) {
764-
errorComponents.push(loadedEntryPointCodeBehind.component as ComponentDef);
746+
// -- Load the main component's code-behind file if any
747+
const mainCodeBehind = loadedEntryPoint.component.props?.codeBehind || MAIN_CODE_BEHIND_FILE;
748+
let loadedEntryPointCodeBehind = (await new Promise(async (resolve) => {
749+
try {
750+
const resp = await fetchWithoutCache(resolvePath(MAIN_FILE, mainCodeBehind));
751+
const codeBehind = await parseCodeBehindResponse(resp);
752+
resolve(codeBehind?.hasError ? codeBehind : codeBehind.codeBehind);
753+
} catch (e) {
754+
resolve(null);
765755
}
756+
})) as any;
757+
if (loadedEntryPointCodeBehind?.hasError) {
758+
errorComponents.push(loadedEntryPointCodeBehind.component as ComponentDef);
766759
}
767760

768761
// --- Check if any of the components have markup errors
@@ -797,9 +790,6 @@ function useStandalone(
797790
} else {
798791
if (compWrapper.file) {
799792
sources[compWrapper.file] = compWrapper.src;
800-
// resolvedRuntime.projectCompilation.entrypoint.filename = MAIN_FILE;
801-
// resolvedRuntime.projectCompilation.entrypoint.definition = loadedEntryPoint.component;
802-
// resolvedRuntime.projectCompilation.entrypoint.markupSource = loadedEntryPoint.src;
803793
}
804794
}
805795
});
@@ -885,38 +875,30 @@ function useStandalone(
885875
definition: compWrapper.component as CompoundComponentDef,
886876
};
887877

888-
let componentCodeBehind = null;
889-
if (
890-
"codeBehind" in compWrapper.component &&
891-
compWrapper.component?.codeBehind !== undefined
892-
) {
893-
// --- Promises for the component code-behind files
894-
componentCodeBehind = (await new Promise(async (resolve) => {
895-
try {
896-
const codeBehind = await fetchWithoutCache(
897-
resolvePath(
898-
`components/${componentPath}`,
899-
(compWrapper.component as CompoundComponentDef)?.codeBehind ||
900-
`${componentPath}.${codeBehindFileExtension}`,
901-
),
902-
);
903-
const codeBehindWrapper = await parseCodeBehindResponse(codeBehind);
904-
if (codeBehindWrapper.hasError) {
905-
errorComponents.push(codeBehindWrapper.component as ComponentDef);
906-
}
907-
resolve(
908-
codeBehindWrapper.hasError
909-
? (codeBehindWrapper.component as CompoundComponentDef)
910-
: codeBehindWrapper,
911-
);
912-
} catch {
913-
resolve(null);
878+
const componentCodeBehindFile =
879+
(compWrapper.component as CompoundComponentDef)?.codeBehind ||
880+
`${componentPath}.${codeBehindFileExtension}`;
881+
let componentCodeBehind = (await new Promise(async (resolve) => {
882+
try {
883+
const codeBehind = await fetchWithoutCache(
884+
resolvePath(`components/${componentPath}`, componentCodeBehindFile),
885+
);
886+
const codeBehindWrapper = await parseCodeBehindResponse(codeBehind);
887+
if (codeBehindWrapper.hasError) {
888+
errorComponents.push(codeBehindWrapper.component as ComponentDef);
914889
}
915-
})) as Promise<CompoundComponentDef | ParsedResponse>;
916-
917-
if (componentCodeBehind && "src" in componentCodeBehind) {
918-
compCompilation.codeBehindSource = componentCodeBehind.src;
890+
resolve(
891+
codeBehindWrapper.hasError
892+
? (codeBehindWrapper.component as CompoundComponentDef)
893+
: codeBehindWrapper,
894+
);
895+
} catch {
896+
resolve(null);
919897
}
898+
})) as any;
899+
900+
if (componentCodeBehind && "src" in componentCodeBehind) {
901+
compCompilation.codeBehindSource = (componentCodeBehind as any).src;
920902
}
921903

922904
resolvedRuntime.projectCompilation.components.push(compCompilation);
@@ -927,7 +909,7 @@ function useStandalone(
927909
...(compWrapper.component as any).component,
928910
vars: {
929911
...(compWrapper.component as any).component.vars,
930-
...componentCodeBehind?.component?.vars,
912+
...componentCodeBehind?.codeBehind?.vars,
931913
},
932914
},
933915
};

0 commit comments

Comments
 (0)