Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/twelve-guests-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"xmlui": patch
---

Fix loading code-behind files in standalone mode
90 changes: 36 additions & 54 deletions xmlui/src/components-core/StandaloneApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type RuntimeProps = {
component?: ComponentDef | CompoundComponentDef;
file?: string;
src?: string;
}
};

// --- The properties of the standalone app
type StandaloneAppProps = {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
});
Expand Down Expand Up @@ -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<CompoundComponentDef | ParsedResponse>;

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);
Expand All @@ -927,7 +909,7 @@ function useStandalone(
...(compWrapper.component as any).component,
vars: {
...(compWrapper.component as any).component.vars,
...componentCodeBehind?.component?.vars,
...componentCodeBehind?.codeBehind?.vars,
},
},
};
Expand Down
Loading