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 _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;
@ -364,6 +366,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) {
@ -372,7 +379,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;
} }
@ -428,6 +435,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
@ -447,7 +457,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

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

View File

@ -268,17 +268,24 @@ export class AssetLoader {
async loadAsyncLibrary(asyncLibraryMap: Record<string, any>) { async loadAsyncLibrary(asyncLibraryMap: Record<string, any>) {
const promiseList: any[] = []; const promiseList: any[] = [];
const libraryKeyList: any[] = []; const libraryKeyList: any[] = [];
const pkgs: any[] = [];
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;
}); });
} }