feat: 资产包支持一个package从另一个package异步导出 (#1150)

This commit is contained in:
SoberZ 2022-10-19 19:59:52 +08:00 committed by LeoYuan 袁力皓
parent 535b75c04f
commit 8a83fc9b5c
4 changed files with 36 additions and 3 deletions

View File

@ -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() {

View File

@ -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;
}

View File

@ -130,6 +130,14 @@ export interface Package {
* @todo @度城
*/
async?: boolean;
/**
* package package
*/
exportMode?: 'functionCall';
/**
* package window
*/
exportSourceLibrary?: any;
/**
* window[exportName] Object
*/

View File

@ -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;
});
}