Skip to content

Commit 6b78157

Browse files
authored
feat: 资产包支持一个package从另一个package异步导出 (alibaba#1150)
1 parent 6576346 commit 6b78157

File tree

4 files changed

+36
-3
lines changed

4 files changed

+36
-3
lines changed

packages/designer/src/builtin-simulator/host.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,11 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
352352
const _library = library || (this.get('library') as LibraryItem[]);
353353
const libraryAsset: AssetList = [];
354354
const libraryExportList: string[] = [];
355+
const functionCallLibraryExportList: string[] = [];
355356

356357
if (_library && _library.length) {
357358
_library.forEach((item) => {
359+
const { exportMode, exportSourceLibrary } = item;
358360
this.libraryMap[item.package] = item.library;
359361
if (item.async) {
360362
this.asyncLibraryMap[item.package] = item;
@@ -364,6 +366,11 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
364366
`Object.defineProperty(window,'${item.exportName}',{get:()=>window.${item.library}});`,
365367
);
366368
}
369+
if (exportMode === 'functionCall' && exportSourceLibrary) {
370+
functionCallLibraryExportList.push(
371+
`window["${item.library}"] = window["${exportSourceLibrary}"]("${item.library}", "${item.package}");`,
372+
);
373+
}
367374
if (item.editUrls) {
368375
libraryAsset.push(item.editUrls);
369376
} else if (item.urls) {
@@ -372,7 +379,7 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
372379
});
373380
}
374381
libraryAsset.unshift(assetItem(AssetType.JSText, libraryExportList.join('')));
375-
382+
libraryAsset.push(assetItem(AssetType.JSText, functionCallLibraryExportList.join('')));
376383
return libraryAsset;
377384
}
378385

@@ -428,6 +435,9 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
428435
if (Object.keys(this.asyncLibraryMap).length > 0) {
429436
// 加载异步Library
430437
await renderer.loadAsyncLibrary(this.asyncLibraryMap);
438+
Object.keys(this.asyncLibraryMap).forEach(key => {
439+
delete this.asyncLibraryMap[key];
440+
});
431441
}
432442

433443
// step 5 ready & render
@@ -447,7 +457,14 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
447457

448458
async setupComponents(library) {
449459
const libraryAsset: AssetList = this.buildLibrary(library);
450-
await this.renderer.load(libraryAsset);
460+
await this.renderer?.load(libraryAsset);
461+
if (Object.keys(this.asyncLibraryMap).length > 0) {
462+
// 加载异步Library
463+
await this.renderer?.loadAsyncLibrary(this.asyncLibraryMap);
464+
Object.keys(this.asyncLibraryMap).forEach(key => {
465+
delete this.asyncLibraryMap[key];
466+
});
467+
}
451468
}
452469

453470
setupEvents() {

packages/designer/src/builtin-simulator/renderer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface BuiltinSimulatorRenderer {
1111
setNativeSelection(enableFlag: boolean): void;
1212
setDraggingState(state: boolean): void;
1313
setCopyState(state: boolean): void;
14+
loadAsyncLibrary(asyncMap: { [index: string]: any }): void;
1415
clearState(): void;
1516
run(): void;
1617
}

packages/types/src/assets.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ export interface Package {
130130
* @todo 需推进提案 @度城
131131
*/
132132
async?: boolean;
133+
/**
134+
* 标识当前 package 从其他 package 的导出方式
135+
*/
136+
exportMode?: 'functionCall';
137+
/**
138+
* 标识当前 package 是从 window 上的哪个属性导出来的
139+
*/
140+
exportSourceLibrary?: any;
133141
/**
134142
* 组件描述导出名字,可以通过 window[exportName] 获取到组件描述的 Object 内容;
135143
*/

packages/utils/src/asset.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,17 +268,24 @@ export class AssetLoader {
268268
async loadAsyncLibrary(asyncLibraryMap: Record<string, any>) {
269269
const promiseList: any[] = [];
270270
const libraryKeyList: any[] = [];
271+
const pkgs: any[] = [];
271272
for (const key in asyncLibraryMap) {
272273
// 需要异步加载
273274
if (asyncLibraryMap[key].async) {
274275
promiseList.push(window[asyncLibraryMap[key].library]);
275276
libraryKeyList.push(asyncLibraryMap[key].library);
277+
pkgs.push(asyncLibraryMap[key]);
276278
}
277279
}
278280
await Promise.all(promiseList).then((mods) => {
279281
if (mods.length > 0) {
280282
mods.map((item, index) => {
281-
window[libraryKeyList[index]] = item;
283+
const { exportMode, exportSourceLibrary, library } = pkgs[index];
284+
window[libraryKeyList[index]] =
285+
exportMode === 'functionCall' &&
286+
(exportSourceLibrary == null || exportSourceLibrary === library)
287+
? item()
288+
: item;
282289
return item;
283290
});
284291
}

0 commit comments

Comments
 (0)