Skip to content

Commit

Permalink
feat: support app config of runApp (#6398)
Browse files Browse the repository at this point in the history
  • Loading branch information
ClarkXia authored Aug 2, 2023
1 parent f0d7906 commit 67eae5c
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 24 deletions.
6 changes: 6 additions & 0 deletions .changeset/violet-coats-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@ice/runtime': patch
'@ice/app': patch
---

feat: support app config of runApp
4 changes: 4 additions & 0 deletions examples/basic-project/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@ export const dataLoader = defineDataLoader(() => {
});
});
});

export const runApp = (render) => {
render();
};
6 changes: 5 additions & 1 deletion packages/ice/src/createService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ async function createService({ rootDir, command, commandArgs }: CreateServiceOpt
command,
});

let entryCode = 'render();';
let entryCode = `if (app.runApp) {
app.runApp(render, renderOptions);
} else {
render();
}`;

const generatorAPI = {
addExport: (declarationData: Omit<DeclarationData, 'declarationType'>) => {
Expand Down
46 changes: 26 additions & 20 deletions packages/ice/templates/core/entry.client.tsx.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import createRoutes from './routes';
<% } -%>
<%- runtimeOptions.imports %>
<% if(dataLoaderImport.imports) {-%><%-dataLoaderImport.imports%><% } -%>
import type { RunClientAppOptions } from '@ice/runtime';

const getRouterBasename = () => {
const appConfig = getAppConfig(app);
return appConfig?.router?.basename ?? <%- basename %> ?? '';
Expand All @@ -27,31 +29,35 @@ let dataLoaderDecorator = (dataLoader) => {
}
<% } -%>

const renderOptions: RunClientAppOptions = {
app,
runtimeModules: {
commons,
statics,
},
<% if (enableRoutes) { %>createRoutes,<% } %>
basename: getRouterBasename(),
hydrate: <%- hydrate %>,
memoryRouter: <%- memoryRouter || false %>,
dataLoaderFetcher,
dataLoaderDecorator,
runtimeOptions: {
<% if (runtimeOptions.exports) { -%>
<%- runtimeOptions.exports %>
<% } -%>
<% if (locals.customRuntimeOptions) { -%>...<%- JSON.stringify(customRuntimeOptions) %>,<% } -%>
},
};

const render = (customOptions: Record<string, any> = {}) => {
const appProps = {
app,
runtimeModules: {
commons,
statics,
},
<% if (enableRoutes) { %>createRoutes,<% } %>
basename: getRouterBasename(),
hydrate: <%- hydrate %>,
memoryRouter: <%- memoryRouter || false %>,
dataLoaderFetcher,
dataLoaderDecorator,
return runClientApp({
...renderOptions,
...customOptions,
runtimeOptions: {
<% if (runtimeOptions.exports) { -%>
<%- runtimeOptions.exports %>
<% } -%>
<% if (locals.customRuntimeOptions) { -%>
...<%- JSON.stringify(customRuntimeOptions) %>,
<% } -%>
...(renderOptions.runtimeOptions || {}),
...customOptions.runtimeOptions,
},
};
return runClientApp(appProps);
});
};

<%- entryCode %>
Expand Down
4 changes: 3 additions & 1 deletion packages/ice/templates/core/routes.tsx.ejs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { createRouteLoader, WrapRouteComponent, RouteErrorComponent } from '@ice/runtime';
import type { CreateRoutes } from '@ice/runtime';
<%- routeImports.length ? routeImports.join('\n') + '\n\n' : ''; -%>
export default ({
const createRoutes: CreateRoutes = ({
requestContext,
renderMode,
}) => ([
<%- routeDefinition %>
]);
export default createRoutes;
3 changes: 2 additions & 1 deletion packages/runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import type {
} from './types.js';
import Runtime from './runtime.js';
import runClientApp from './runClientApp.js';
import type { RunClientAppOptions } from './runClientApp.js';
import type { RunClientAppOptions, CreateRoutes } from './runClientApp.js';
import { useAppContext as useInternalAppContext, useAppData, AppContextProvider } from './AppContext.js';
import { getAppData } from './appData.js';
import { useData, useConfig } from './RouteContext.js';
Expand Down Expand Up @@ -155,4 +155,5 @@ export type {
ScriptsType,
DataType,
MainType,
CreateRoutes,
};
4 changes: 3 additions & 1 deletion packages/runtime/src/runClientApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import { AppContextProvider } from './AppContext.js';
import { deprecatedHistory } from './utils/deprecatedHistory.js';
import reportRecoverableError from './reportRecoverableError.js';

export type CreateRoutes = (options: Pick<RouteLoaderOptions, 'renderMode' | 'requestContext'>) => RouteItem[];

export interface RunClientAppOptions {
app: AppExport;
runtimeModules: RuntimeModules;
createRoutes?: (options: Pick<RouteLoaderOptions, 'renderMode' | 'requestContext'>) => RouteItem[];
createRoutes?: CreateRoutes;
hydrate?: boolean;
basename?: string;
memoryRouter?: boolean;
Expand Down

0 comments on commit 67eae5c

Please sign in to comment.