Skip to content

Commit 31159e7

Browse files
committed
wip: build atri app
1 parent d3fcb70 commit 31159e7

File tree

7 files changed

+73
-26
lines changed

7 files changed

+73
-26
lines changed

‎packages/atri-app-core/src/prod-entries/loadRoutes.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import React from "react";
66
export function loadRoutes(
77
props: Pick<
88
ProdAppEntryOptions,
9-
"PageWrapper" | "routes" | "styles" | "entryRouteObjectPath"
9+
"PageWrapper" | "routes" | "styles" | "entryRouteObjectPath" | "entryPageFC"
1010
>,
1111
routerOpts: Parameters<typeof atriRouter.setPages>["1"]
1212
) {
@@ -18,7 +18,7 @@ export function loadRoutes(
1818
element: (
1919
<FinalPageComponent
2020
PageWrapper={props.PageWrapper}
21-
Page={route.PageFC}
21+
Page={props.entryPageFC}
2222
styleStr={props.styles}
2323
/>
2424
),

‎packages/atri-app-core/src/prod-entries/renderAppOrReturnPageFC.tsx‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,29 @@ declare global {
1515
window.__APP_STATUS = "started";
1616

1717
export default function renderAppOrReturnPageFC(options: ProdAppEntryOptions) {
18-
const { PageWrapper, entryRouteStore, entryRouteObjectPath, routes, styles } =
19-
options;
18+
const {
19+
PageWrapper,
20+
entryRouteStore,
21+
entryRouteObjectPath,
22+
routes,
23+
styles,
24+
entryPageFC,
25+
} = options;
2026
// add to store
2127
addPageToStore(entryRouteObjectPath, entryRouteStore);
2228
if (window.__APP_STATUS === "loaded") {
2329
// return Page Component
2430
return (
2531
<FinalPageComponent
2632
PageWrapper={PageWrapper}
27-
Page={
28-
routes.find((curr) => {
29-
return curr.path === entryRouteObjectPath;
30-
})!.PageFC
31-
}
33+
Page={entryPageFC}
3234
styleStr={styles}
3335
/>
3436
);
3537
} else {
3638
// configure rotues here
3739
loadRoutes(
38-
{ routes, PageWrapper, styles, entryRouteObjectPath },
40+
{ routes, PageWrapper, styles, entryRouteObjectPath, entryPageFC },
3941
{ initialEntries: [entryRouteObjectPath], initialIndex: 0 }
4042
);
4143
window.__APP_STATUS = "loading";

‎packages/atri-app-core/src/prod-entries/renderPageServerSide.tsx‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,38 @@ import { renderToString } from "react-dom/server";
77
import { createMemoryRouter, RouterProvider } from "react-router-dom";
88
import { atriRouter } from "../entries/atriRouter";
99
import { loadRoutes } from "./loadRoutes";
10+
import { addPageToStore } from "./store";
1011

1112
export function renderPageServerSide(
1213
options: {
1314
srcs: string[];
1415
DocFn: React.FC;
15-
entryRouteObjectPath: string;
16-
} & Pick<ProdAppEntryOptions, "PageWrapper" | "routes" | "styles">
16+
} & Pick<
17+
ProdAppEntryOptions,
18+
| "PageWrapper"
19+
| "routes"
20+
| "styles"
21+
| "entryRouteStore"
22+
| "entryRouteObjectPath"
23+
| "entryPageFC"
24+
>
1725
) {
18-
const { srcs, PageWrapper, DocFn, routes, entryRouteObjectPath, styles } =
19-
options;
26+
const {
27+
srcs,
28+
PageWrapper,
29+
DocFn,
30+
routes,
31+
entryRouteObjectPath,
32+
styles,
33+
entryRouteStore,
34+
entryPageFC,
35+
} = options;
2036
atriRouter.setRouterFactory(createMemoryRouter);
2137
loadRoutes(
22-
{ routes, PageWrapper, styles, entryRouteObjectPath },
38+
{ routes, PageWrapper, styles, entryRouteObjectPath, entryPageFC },
2339
{ initialEntries: [entryRouteObjectPath], initialIndex: 0 }
2440
);
41+
addPageToStore(entryRouteObjectPath, entryRouteStore);
2542
return renderToString(
2643
<AtriScriptsContext.Provider value={srcs}>
2744
<MainAppContext.Provider

‎packages/atri-app-core/src/types.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ export interface LiveApiSocketData {}
123123
export type RepeatingContextData = { indices: number[]; lengths: number[] };
124124

125125
export type ProdAppEntryOptions = {
126-
routes: { path: string; PageFC: React.FC<any> }[];
126+
routes: { path: string }[];
127+
entryPageFC: React.FC<any>;
127128
entryRouteObjectPath: string;
128129
entryRouteStore: {
129130
custom: { [alias: string]: any };

‎packages/commands/src/scripts/build-atri-app/createServerEntry.ts‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,21 @@ export async function createServerEntry() {
66
const entry: Entry = {
77
_error: { import: "./pages/_error" },
88
};
9-
const pagePaths = await getAllPages();
10-
pagePaths.forEach((filepath) => {
11-
const entryName = filepath.replace(/^\//, "");
9+
const pageInfo = await getAllPages();
10+
pageInfo.forEach(({ pagePath, routeObjectPath }) => {
11+
const entryName = pagePath.replace(/^\//, "");
1212
const srcs: string[] = [];
1313
entry[entryName] = {
1414
import: `atri-pages-server-loader?${stringify({
15-
filepath,
15+
pagePath,
1616
srcs,
17-
// TODO: add other options
17+
reactRouteObjectPath: routeObjectPath,
18+
routes: pageInfo.map(({ routeObjectPath }) => {
19+
return { path: routeObjectPath };
20+
}),
21+
// TODO: add actual style & route stores
22+
styles: "",
23+
entryRouteStores: {},
1824
})}!`,
1925
};
2026
});

‎packages/commands/src/scripts/build-atri-app/loaders/atri-pages-server-loader.js‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
function atriPagesServerLoader() {
22
const options = this.getOptions();
3-
const { srcs, filepath, reactRouteObjectPath, routes, styles } = options;
3+
const {
4+
srcs,
5+
filepath,
6+
reactRouteObjectPath,
7+
routes,
8+
styles,
9+
entryRouteStores,
10+
} = options;
411
if (srcs === undefined || filepath === undefined) {
512
const err = Error();
613
err.name = "ValueError";
@@ -14,9 +21,15 @@ function atriPagesServerLoader() {
1421
import { renderPageServerSide } from "@atrilabs/atri-app-core/src/prod-entries/renderPageServerSide";
1522
1623
function renderPage(){
17-
return renderPageServerSide({PageFn, PageWrapper, DocFn, srcs: ${JSON.stringify(
24+
return renderPageServerSide({entryPageFC: PageFn, PageWrapper, DocFn, srcs: ${JSON.stringify(
1825
srcs
19-
)}, routes, styles, reactRouteObjectPath})
26+
)}, routes: ${routes.map((route) => {
27+
return { path: route };
28+
})}, styles: ${JSON.stringify(
29+
styles
30+
)}, reactRouteObjectPath: ${JSON.stringify(
31+
reactRouteObjectPath
32+
)}, entryRouteStores: ${JSON.stringify(entryRouteStores)}})
2033
}
2134
2235
export default { renderPage };`;
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
import { readDirStructure } from "@atrilabs/atri-app-core";
1+
import {
2+
dirStructureToIR,
3+
pathsIRToRouteObjectPaths,
4+
readDirStructure,
5+
} from "@atrilabs/atri-app-core";
26
import path from "path";
37

48
export async function getAllPages() {
59
const pagePaths = await readDirStructure(path.resolve("pages"));
6-
return pagePaths;
10+
const irs = dirStructureToIR(pagePaths);
11+
const routeObjectPaths = pathsIRToRouteObjectPaths(irs);
12+
return pagePaths.map((pagePath, index) => {
13+
return { pagePath, routeObjectPath: routeObjectPaths[index] };
14+
});
715
}

0 commit comments

Comments
 (0)