diff --git a/packages/designer/src/builtin-simulator/host.ts b/packages/designer/src/builtin-simulator/host.ts index 0671cc730..795473a82 100644 --- a/packages/designer/src/builtin-simulator/host.ts +++ b/packages/designer/src/builtin-simulator/host.ts @@ -69,7 +69,7 @@ import { Project } from '../project'; import { Scroller } from '../designer/scroller'; import { isElementNode, isDOMNodeVisible } from '../utils/misc'; -export interface LibraryItem extends Package{ +export interface LibraryItem extends Package { package: string; library: string; urls?: Asset; @@ -342,9 +342,11 @@ export class BuiltinSimulatorHost implements ISimulatorHost { + const { exportMode, exportSourceLibrary } = item; this.libraryMap[item.package] = item.library; if (item.async) { this.asyncLibraryMap[item.package] = item; @@ -354,6 +356,11 @@ export class BuiltinSimulatorHost implements ISimulatorHostwindow.${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) { @@ -362,7 +369,7 @@ export class BuiltinSimulatorHost implements ISimulatorHost 0) { // 加载异步Library await renderer.loadAsyncLibrary(this.asyncLibraryMap); + Object.keys(this.asyncLibraryMap).forEach(key => { + delete this.asyncLibraryMap[key]; + }); } // step 5 ready & render @@ -437,7 +447,14 @@ export class BuiltinSimulatorHost implements ISimulatorHost 0) { + // 加载异步Library + await this.renderer?.loadAsyncLibrary(this.asyncLibraryMap); + Object.keys(this.asyncLibraryMap).forEach(key => { + delete this.asyncLibraryMap[key]; + }); + } } setupEvents() { @@ -1605,4 +1622,4 @@ function getMatched(elements: Array, selector: string): Element } } return firstQueried; -} +} \ No newline at end of file diff --git a/packages/designer/src/builtin-simulator/renderer.ts b/packages/designer/src/builtin-simulator/renderer.ts index f3b3de79b..03fb2e289 100644 --- a/packages/designer/src/builtin-simulator/renderer.ts +++ b/packages/designer/src/builtin-simulator/renderer.ts @@ -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; } diff --git a/packages/types/src/assets.ts b/packages/types/src/assets.ts index 3afed1221..5d3ab79b6 100644 --- a/packages/types/src/assets.ts +++ b/packages/types/src/assets.ts @@ -8,7 +8,9 @@ export interface Package { // 应该被编辑器默认加载,定义组件大 urls?: string[] | any; // 组件渲染态视图打包后的 CDN url 列表,包含 js 和 css editUrls?: string[] | any; // 组件编辑态视图打包后的 CDN url 列表,包含 js 和 css library: string; // 作为全局变量引用时的名称,和webpack output.library字段含义一样,用来定义全局变量名 - async?: boolean, + async?: boolean; + exportMode?: string; + exportSourceLibrary?: any; exportName?: string; } diff --git a/packages/utils/src/asset.ts b/packages/utils/src/asset.ts index 5e428ae7c..b0ee9eed7 100644 --- a/packages/utils/src/asset.ts +++ b/packages/utils/src/asset.ts @@ -320,21 +320,25 @@ export class AssetLoader { } private async loadAsyncLibrary(asyncLibraryMap) { - const promiseList = []; const libraryKeyList = []; + const promiseList = []; + const libraryKeyList = []; + const pkgs = []; 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; }); } }); } -} +} \ No newline at end of file