feat: 支持编译渲染

This commit is contained in:
wuyue.xht 2020-04-19 22:39:56 +08:00
parent 94690c389b
commit 0a421518cf
3 changed files with 59 additions and 39 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@ali/lowcode-runtime", "name": "@ali/lowcode-runtime",
"version": "0.8.9", "version": "0.8.12",
"description": "Runtime for Ali lowCode engine", "description": "Runtime for Ali lowCode engine",
"files": [ "files": [
"es", "es",
@ -25,7 +25,8 @@
}, },
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@ali/recore": "^1.6.9" "@ali/recore": "^1.6.9",
"@ali/offline-events": "^1.0.0"
}, },
"devDependencies": { "devDependencies": {
"@alib/build-scripts": "^0.1.18", "@alib/build-scripts": "^0.1.18",

View File

@ -1,4 +1,4 @@
import { createElement, ReactElement } from 'react'; import { createElement, ReactType, ReactElement } from 'react';
import { Router } from '@ali/recore'; import { Router } from '@ali/recore';
import app from '../../index'; import app from '../../index';
import Provider from '..'; import Provider from '..';
@ -7,33 +7,66 @@ import LazyComponent from './lazy-component';
export default class ReactProvider extends Provider { export default class ReactProvider extends Provider {
// 定制构造根组件的逻辑,如切换路由机制 // 定制构造根组件的逻辑,如切换路由机制
createApp() { createApp() {
const RouterView = this.getRouterView();
let App;
const layoutConfig = this.getLayoutConfig();
if (!layoutConfig || !layoutConfig.componentName) {
App = (props: any) => (RouterView ? createElement(RouterView, { ...props }) : null);
return App;
}
const { componentName: layoutName, props: layoutProps } = layoutConfig;
const { content: Layout, props: extraLayoutProps } = app.getLayout(layoutName) || {};
const sectionalRender = this.isSectionalRender();
if (!sectionalRender && Layout) {
App = (props: any) =>
createElement(
Layout,
{ ...layoutProps, ...extraLayoutProps },
RouterView ? createElement(RouterView, props) : null,
);
} else {
App = (props: any) => (RouterView ? createElement(RouterView, props) : null);
}
return App;
}
// 内置实现 for 动态化渲染
getRouterView(): ReactType | null {
const routerConfig = this.getRouterConfig(); const routerConfig = this.getRouterConfig();
if (!routerConfig) { if (!routerConfig) {
return; return null;
} }
const routes: Array<{ path: string; children: any; exact: boolean; keepAlive: boolean }> = []; const routes: Array<{
let homePageId = ''; path: string;
children: any;
exact: boolean;
defined: { keepAlive: boolean; [key: string]: any };
}> = [];
let homePageId = this.getHomePage();
Object.keys(routerConfig).forEach((pageId: string, idx: number) => { Object.keys(routerConfig).forEach((pageId: string, idx: number) => {
if (!pageId) { if (!pageId) {
return; return;
} }
const path = routerConfig[pageId]; const path = routerConfig[pageId];
if (idx === 0 || path === '/') {
homePageId = pageId;
}
routes.push({ routes.push({
path, path,
children: (props: any) => this.getLazyComponent(pageId, props), children: (props: any) => this.getLazyComponent(pageId, props),
exact: true, exact: true,
keepAlive: true, defined: { keepAlive: true },
}); });
if (homePageId) {
return;
}
if (idx === 0 || path === '/') {
homePageId = pageId;
}
}); });
if (homePageId) { if (homePageId) {
routes.push({ routes.push({
path: '**', path: '**',
children: (props: any) => this.getLazyComponent(homePageId, { ...props }), children: (props: any) => this.getLazyComponent(homePageId, { ...props }),
exact: true, exact: true,
keepAlive: true, defined: { keepAlive: true },
}); });
} }
const RouterView = (props: any) => { const RouterView = (props: any) => {
@ -45,20 +78,7 @@ export default class ReactProvider extends Provider {
...props, ...props,
}); });
}; };
let App; return RouterView;
const layoutConfig = this.getLayoutConfig();
if (!layoutConfig || !layoutConfig.componentName) {
App = (props: any) => createElement(RouterView, { ...props });
return App;
}
const { componentName: layoutName, props: layoutProps } = layoutConfig;
const Layout = app.getLayout(layoutName);
if (Layout) {
App = (props: any) => createElement(Layout, layoutProps, RouterView({ props }));
} else {
App = (props: any) => createElement(RouterView, props);
}
return App;
} }
getLazyComponent(pageId: string, props: any): ReactElement | null { getLazyComponent(pageId: string, props: any): ReactElement | null {

View File

@ -46,21 +46,20 @@ function transformConfig(config: IAppConfig | (() => IAppConfig)): IRecoreAppCon
}; };
} }
export default function run(config?: IAppConfig | (() => IAppConfig)) { export default function run() {
const provider = app.getProvider(); const provider = app.getProvider();
if (config) { if (!provider) {
config = transformConfig(config); throw new Error('');
const App = provider.createApp();
runApp(App, config);
return;
} }
const promise = provider.async(); provider.onReady(() => {
promise.then((config: IAppConfig) => { const promise = provider.async();
if (!config) { promise.then((config: IAppConfig) => {
return; if (!config) {
} return;
const App = provider.createApp(); }
config = transformConfig(config); const App = provider.createApp();
runApp(App, config); config = transformConfig(config);
runApp(App, config);
});
}); });
} }