From a69933a6d1f21468d6f2d9dd8e63fda0c6476dbc Mon Sep 17 00:00:00 2001 From: "xuanji.w" Date: Mon, 28 Mar 2022 11:20:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=B5=84=E4=BA=A7=E5=8C=85=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E4=B8=80=E4=B8=AApackage=E4=BB=8E=E5=8F=A6=E5=A4=96?= =?UTF-8?q?=E4=B8=80=E4=B8=AApackage=E5=BC=82=E6=AD=A5=E5=AF=BC=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../designer/src/builtin-simulator/host.ts | 29 +++++++++++++++---- .../src/builtin-simulator/renderer.ts | 1 + packages/types/src/assets.ts | 4 ++- packages/utils/src/asset.ts | 10 +++++-- 4 files changed, 34 insertions(+), 10 deletions(-) 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