mirror of
https://github.com/alibaba/lowcode-engine.git
synced 2025-12-12 03:01:16 +00:00
feat: 资产包支持一个package从另一个package异步导出 (#1150)
This commit is contained in:
parent
535b75c04f
commit
8a83fc9b5c
@ -352,9 +352,11 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
|
||||
const _library = library || (this.get('library') as LibraryItem[]);
|
||||
const libraryAsset: AssetList = [];
|
||||
const libraryExportList: string[] = [];
|
||||
const functionCallLibraryExportList: string[] = [];
|
||||
|
||||
if (_library && _library.length) {
|
||||
_library.forEach((item) => {
|
||||
const { exportMode, exportSourceLibrary } = item;
|
||||
this.libraryMap[item.package] = item.library;
|
||||
if (item.async) {
|
||||
this.asyncLibraryMap[item.package] = item;
|
||||
@ -364,6 +366,11 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
|
||||
`Object.defineProperty(window,'${item.exportName}',{get:()=>window.${item.library}});`,
|
||||
);
|
||||
}
|
||||
if (exportMode === 'functionCall' && exportSourceLibrary) {
|
||||
functionCallLibraryExportList.push(
|
||||
`window["${item.library}"] = window["${exportSourceLibrary}"]("${item.library}", "${item.package}");`,
|
||||
);
|
||||
}
|
||||
if (item.editUrls) {
|
||||
libraryAsset.push(item.editUrls);
|
||||
} else if (item.urls) {
|
||||
@ -372,7 +379,7 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
|
||||
});
|
||||
}
|
||||
libraryAsset.unshift(assetItem(AssetType.JSText, libraryExportList.join('')));
|
||||
|
||||
libraryAsset.push(assetItem(AssetType.JSText, functionCallLibraryExportList.join('')));
|
||||
return libraryAsset;
|
||||
}
|
||||
|
||||
@ -428,6 +435,9 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
|
||||
if (Object.keys(this.asyncLibraryMap).length > 0) {
|
||||
// 加载异步Library
|
||||
await renderer.loadAsyncLibrary(this.asyncLibraryMap);
|
||||
Object.keys(this.asyncLibraryMap).forEach(key => {
|
||||
delete this.asyncLibraryMap[key];
|
||||
});
|
||||
}
|
||||
|
||||
// step 5 ready & render
|
||||
@ -447,7 +457,14 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
|
||||
|
||||
async setupComponents(library) {
|
||||
const libraryAsset: AssetList = this.buildLibrary(library);
|
||||
await this.renderer.load(libraryAsset);
|
||||
await this.renderer?.load(libraryAsset);
|
||||
if (Object.keys(this.asyncLibraryMap).length > 0) {
|
||||
// 加载异步Library
|
||||
await this.renderer?.loadAsyncLibrary(this.asyncLibraryMap);
|
||||
Object.keys(this.asyncLibraryMap).forEach(key => {
|
||||
delete this.asyncLibraryMap[key];
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
setupEvents() {
|
||||
|
||||
@ -11,6 +11,7 @@ export interface BuiltinSimulatorRenderer {
|
||||
setNativeSelection(enableFlag: boolean): void;
|
||||
setDraggingState(state: boolean): void;
|
||||
setCopyState(state: boolean): void;
|
||||
loadAsyncLibrary(asyncMap: { [index: string]: any }): void;
|
||||
clearState(): void;
|
||||
run(): void;
|
||||
}
|
||||
|
||||
@ -130,6 +130,14 @@ export interface Package {
|
||||
* @todo 需推进提案 @度城
|
||||
*/
|
||||
async?: boolean;
|
||||
/**
|
||||
* 标识当前 package 从其他 package 的导出方式
|
||||
*/
|
||||
exportMode?: 'functionCall';
|
||||
/**
|
||||
* 标识当前 package 是从 window 上的哪个属性导出来的
|
||||
*/
|
||||
exportSourceLibrary?: any;
|
||||
/**
|
||||
* 组件描述导出名字,可以通过 window[exportName] 获取到组件描述的 Object 内容;
|
||||
*/
|
||||
|
||||
@ -268,17 +268,24 @@ export class AssetLoader {
|
||||
async loadAsyncLibrary(asyncLibraryMap: Record<string, any>) {
|
||||
const promiseList: any[] = [];
|
||||
const libraryKeyList: any[] = [];
|
||||
const pkgs: any[] = [];
|
||||
for (const key in asyncLibraryMap) {
|
||||
// 需要异步加载
|
||||
if (asyncLibraryMap[key].async) {
|
||||
promiseList.push(window[asyncLibraryMap[key].library]);
|
||||
libraryKeyList.push(asyncLibraryMap[key].library);
|
||||
pkgs.push(asyncLibraryMap[key]);
|
||||
}
|
||||
}
|
||||
await Promise.all(promiseList).then((mods) => {
|
||||
if (mods.length > 0) {
|
||||
mods.map((item, index) => {
|
||||
window[libraryKeyList[index]] = item;
|
||||
const { exportMode, exportSourceLibrary, library } = pkgs[index];
|
||||
window[libraryKeyList[index]] =
|
||||
exportMode === 'functionCall' &&
|
||||
(exportSourceLibrary == null || exportSourceLibrary === library)
|
||||
? item()
|
||||
: item;
|
||||
return item;
|
||||
});
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user