mirror of
https://github.com/alibaba/lowcode-engine.git
synced 2026-01-18 21:38:14 +00:00
refactor: buildCompoents should get from utils
This commit is contained in:
parent
4372bb1366
commit
e20b5102f2
@ -1,4 +1,5 @@
|
|||||||
import { ReactProvider, Utils } from '@ali/lowcode-runtime';
|
import { ReactProvider } from '@ali/lowcode-runtime';
|
||||||
|
import { buildComponents } from '@ali/lowcode-utils';
|
||||||
import appConfig from '../config/app';
|
import appConfig from '../config/app';
|
||||||
import builtInComps from '../config/components';
|
import builtInComps from '../config/components';
|
||||||
import componentsMap from '../config/componentsMap';
|
import componentsMap from '../config/componentsMap';
|
||||||
@ -31,7 +32,7 @@ export default class Preview extends ReactProvider {
|
|||||||
layout,
|
layout,
|
||||||
routes,
|
routes,
|
||||||
containerId,
|
containerId,
|
||||||
components: { ...builtInComps, ...Utils.buildComponents({ '@alifd/next': 'Next' }, componentsMap) },
|
components: { ...builtInComps, ...buildComponents({ '@alifd/next': 'Next' }, componentsMap) },
|
||||||
componentsMap,
|
componentsMap,
|
||||||
utils: utils,
|
utils: utils,
|
||||||
constants,
|
constants,
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
import Provider from './core/provider';
|
import Provider from './core/provider';
|
||||||
import app from './core';
|
import app from './core';
|
||||||
import * as Utils from './utils';
|
|
||||||
|
|
||||||
export { app, Provider, Utils };
|
export { app, Provider };
|
||||||
|
|||||||
@ -1,101 +0,0 @@
|
|||||||
function isESModule(obj: any): obj is { [key: string]: any } {
|
|
||||||
return obj && obj.__esModule;
|
|
||||||
}
|
|
||||||
|
|
||||||
function accessLibrary(library: string | object) {
|
|
||||||
if (typeof library !== 'string') {
|
|
||||||
return library;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (window as any)[library];
|
|
||||||
}
|
|
||||||
|
|
||||||
function getSubComponent(library: any, paths: string[]) {
|
|
||||||
const l = paths.length;
|
|
||||||
if (l < 1 || !library) {
|
|
||||||
return library;
|
|
||||||
}
|
|
||||||
let i = 0;
|
|
||||||
let component: any;
|
|
||||||
while (i < l) {
|
|
||||||
const key = paths[i]!;
|
|
||||||
let ex: any;
|
|
||||||
try {
|
|
||||||
component = library[key];
|
|
||||||
} catch (e) {
|
|
||||||
ex = e;
|
|
||||||
component = null;
|
|
||||||
}
|
|
||||||
if (i === 0 && component == null && key === 'default') {
|
|
||||||
if (ex) {
|
|
||||||
return l === 1 ? library : null;
|
|
||||||
}
|
|
||||||
component = library;
|
|
||||||
} else if (component == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
library = component;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
return component;
|
|
||||||
}
|
|
||||||
|
|
||||||
function findComponent(libraryMap: LibraryMap, componentName: string, npm?: NpmInfo) {
|
|
||||||
if (!npm) {
|
|
||||||
return accessLibrary(componentName);
|
|
||||||
}
|
|
||||||
// libraryName the key access to global
|
|
||||||
// export { exportName } from xxx exportName === global.libraryName.exportName
|
|
||||||
// export exportName from xxx exportName === global.libraryName.default || global.libraryName
|
|
||||||
// export { exportName as componentName } from package
|
|
||||||
// if exportName == null exportName === componentName;
|
|
||||||
// const componentName = exportName.subName, if exportName empty subName donot use
|
|
||||||
const exportName = npm.exportName || npm.componentName || componentName;
|
|
||||||
const libraryName = libraryMap[npm.package] || exportName;
|
|
||||||
const library = accessLibrary(libraryName);
|
|
||||||
const paths = npm.exportName && npm.subName ? npm.subName.split('.') : [];
|
|
||||||
if (npm.destructuring) {
|
|
||||||
paths.unshift(exportName);
|
|
||||||
} else if (isESModule(library)) {
|
|
||||||
paths.unshift('default');
|
|
||||||
}
|
|
||||||
return getSubComponent(library, paths);
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface LibraryMap {
|
|
||||||
[key: string]: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface NpmInfo {
|
|
||||||
componentName?: string;
|
|
||||||
package: string;
|
|
||||||
version: string;
|
|
||||||
destructuring?: boolean;
|
|
||||||
exportName?: string;
|
|
||||||
subName?: string;
|
|
||||||
main?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function buildComponents(
|
|
||||||
libraryMap: LibraryMap,
|
|
||||||
componentsMap: { [componentName: string]: NpmInfo } | NpmInfo[],
|
|
||||||
) {
|
|
||||||
const components: any = {};
|
|
||||||
if (componentsMap && Array.isArray(componentsMap)) {
|
|
||||||
const compMapObj: any = {};
|
|
||||||
componentsMap.forEach((item: NpmInfo) => {
|
|
||||||
if (!item || !item.componentName) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
compMapObj[item.componentName] = item;
|
|
||||||
});
|
|
||||||
componentsMap = compMapObj;
|
|
||||||
}
|
|
||||||
Object.keys(componentsMap).forEach((componentName) => {
|
|
||||||
const component = findComponent(libraryMap, componentName, (componentsMap as any)[componentName]);
|
|
||||||
if (component) {
|
|
||||||
components[componentName] = component;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return components;
|
|
||||||
}
|
|
||||||
@ -1 +0,0 @@
|
|||||||
export * from './assets';
|
|
||||||
Loading…
x
Reference in New Issue
Block a user