Merge branch 'feat/async-export-package' into 'develop'

Code review title: feat: 资产包支持一个package从另外一个package异步导出
Code review description: 资产包支持一个package从另外一个package异步导出
Code review Link: https://code.aone.alibaba-inc.com/ali-lowcode/ali-lowcode-engine/codereview/8153374
This commit is contained in:
lihao.ylh 2022-03-29 09:48:30 +08:00
commit 04c3ebf4f5
4 changed files with 34 additions and 10 deletions

View File

@ -69,7 +69,7 @@ import { Project } from '../project';
import { Scroller } from '../designer/scroller'; import { Scroller } from '../designer/scroller';
import { isElementNode, isDOMNodeVisible } from '../utils/misc'; import { isElementNode, isDOMNodeVisible } from '../utils/misc';
export interface LibraryItem extends Package{ export interface LibraryItem extends Package {
package: string; package: string;
library: string; library: string;
urls?: Asset; urls?: Asset;
@ -342,9 +342,11 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
const _library = library || (this.get('library') as LibraryItem[]); const _library = library || (this.get('library') as LibraryItem[]);
const libraryAsset: AssetList = []; const libraryAsset: AssetList = [];
const libraryExportList: string[] = []; const libraryExportList: string[] = [];
const functionCallLibraryExportList: string[] = [];
if (_library && _library.length) { if (_library && _library.length) {
_library.forEach((item) => { _library.forEach((item) => {
const { exportMode, exportSourceLibrary } = item;
this.libraryMap[item.package] = item.library; this.libraryMap[item.package] = item.library;
if (item.async) { if (item.async) {
this.asyncLibraryMap[item.package] = item; this.asyncLibraryMap[item.package] = item;
@ -354,6 +356,11 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
`Object.defineProperty(window,'${item.exportName}',{get:()=>window.${item.library}});`, `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) { if (item.editUrls) {
libraryAsset.push(item.editUrls); libraryAsset.push(item.editUrls);
} else if (item.urls) { } else if (item.urls) {
@ -362,7 +369,7 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
}); });
} }
libraryAsset.unshift(assetItem(AssetType.JSText, libraryExportList.join(''))); libraryAsset.unshift(assetItem(AssetType.JSText, libraryExportList.join('')));
libraryAsset.push(assetItem(AssetType.JSText, functionCallLibraryExportList.join('')));
return libraryAsset; return libraryAsset;
} }
@ -418,6 +425,9 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
if (Object.keys(this.asyncLibraryMap).length > 0) { if (Object.keys(this.asyncLibraryMap).length > 0) {
// 加载异步Library // 加载异步Library
await renderer.loadAsyncLibrary(this.asyncLibraryMap); await renderer.loadAsyncLibrary(this.asyncLibraryMap);
Object.keys(this.asyncLibraryMap).forEach(key => {
delete this.asyncLibraryMap[key];
});
} }
// step 5 ready & render // step 5 ready & render
@ -437,7 +447,14 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
async setupComponents(library) { async setupComponents(library) {
const libraryAsset: AssetList = this.buildLibrary(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() { setupEvents() {

View File

@ -11,6 +11,7 @@ export interface BuiltinSimulatorRenderer {
setNativeSelection(enableFlag: boolean): void; setNativeSelection(enableFlag: boolean): void;
setDraggingState(state: boolean): void; setDraggingState(state: boolean): void;
setCopyState(state: boolean): void; setCopyState(state: boolean): void;
loadAsyncLibrary(asyncMap: { [index: string]: any }): void;
clearState(): void; clearState(): void;
run(): void; run(): void;
} }

View File

@ -8,7 +8,9 @@ export interface Package { // 应该被编辑器默认加载,定义组件大
urls?: string[] | any; // 组件渲染态视图打包后的 CDN url 列表,包含 js 和 css urls?: string[] | any; // 组件渲染态视图打包后的 CDN url 列表,包含 js 和 css
editUrls?: string[] | any; // 组件编辑态视图打包后的 CDN url 列表,包含 js 和 css editUrls?: string[] | any; // 组件编辑态视图打包后的 CDN url 列表,包含 js 和 css
library: string; // 作为全局变量引用时的名称和webpack output.library字段含义一样用来定义全局变量名 library: string; // 作为全局变量引用时的名称和webpack output.library字段含义一样用来定义全局变量名
async?: boolean, async?: boolean;
exportMode?: string;
exportSourceLibrary?: any;
exportName?: string; exportName?: string;
} }

View File

@ -320,18 +320,22 @@ export class AssetLoader {
} }
private async loadAsyncLibrary(asyncLibraryMap) { private async loadAsyncLibrary(asyncLibraryMap) {
const promiseList = []; const libraryKeyList = []; const promiseList = [];
const libraryKeyList = [];
const pkgs = [];
for (const key in asyncLibraryMap) { for (const key in asyncLibraryMap) {
// 需要异步加载 // 需要异步加载
if (asyncLibraryMap[key].async) { if (asyncLibraryMap[key].async) {
promiseList.push(window[asyncLibraryMap[key].library]); promiseList.push(window[asyncLibraryMap[key].library]);
libraryKeyList.push(asyncLibraryMap[key].library); libraryKeyList.push(asyncLibraryMap[key].library);
pkgs.push(asyncLibraryMap[key]);
} }
} }
await Promise.all(promiseList).then((mods) => { await Promise.all(promiseList).then((mods) => {
if (mods.length > 0) { if (mods.length > 0) {
mods.map((item, index) => { 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; return item;
}); });
} }