@@ -56,12 +56,20 @@ export const copyObject = (node) => {
5656}
5757
5858/**
59- * 动态导入组件,缓存组件对象
60- * @param {object } param0 组件的依赖: { package: 包名,script:js文件cdn, components:组件id和导出组件名的映射关系}
59+ * 动态导入获取组件库模块
60+ * @param {* } pkg 模块名称
61+ * @param {* } script 模块的cdn地址
6162 * @returns
6263 */
63- export const dynamicImportComponents = async ( { package : pkg , script, components } ) => {
64- if ( ! script ) return
64+ const dynamicImportComponentLib = async ( { pkg, script } ) => {
65+ if ( window . TinyComponentLibs [ pkg ] ) {
66+ return window . TinyComponentLibs [ pkg ]
67+ }
68+
69+ if ( ! script ) {
70+ return { }
71+ }
72+
6573 const href = window . parent . location . href || location . href // 这里要取父窗口的地址,因为在iframe中href是about:srcdoc
6674 const scriptUrl = script . startsWith ( '.' ) ? new URL ( script , href ) . href : script
6775
@@ -71,27 +79,22 @@ export const dynamicImportComponents = async ({ package: pkg, script, components
7179 window . TinyComponentLibs [ pkg ] = modules
7280 }
7381
74- Object . entries ( components ) . forEach ( ( [ componentId , exportName ] ) => {
75- const modules = window . TinyComponentLibs [ pkg ]
76-
77- if ( ! window . TinyLowcodeComponent [ componentId ] ) {
78- window . TinyLowcodeComponent [ componentId ] = modules [ exportName ]
79- }
80- } )
82+ return window . TinyComponentLibs [ pkg ]
8183}
8284
8385/**
84- * 更新区块/组件依赖
85- * @param {object } param0 依赖的CDN信息
86+ * 获取组件对象并缓存,组件渲染时使用
87+ * @param {object } param0 组件的依赖: { package: 包名,script:js文件cdn, components:组件id和导出组件名的映射关系}
88+ * @returns
8689 */
87- export const updateDependencies = ( { detail } ) => {
88- const { scripts = [ ] , styles = [ ] } = detail || { }
89- const { styles : canvasStyles } = window . thirdPartyDeps
90- const newStyles = [ ...styles ] . filter ( ( item ) => ! canvasStyles . has ( item ) )
90+ export const getComponents = async ( { package : pkg , script, components } ) => {
91+ if ( ! pkg ) return
9192
92- newStyles . forEach ( ( item ) => canvasStyles . add ( item ) )
93+ const modules = await dynamicImportComponentLib ( { pkg , script } )
9394
94- const promises = [ ...newStyles ] . map ( ( src ) => addStyle ( src ) ) . concat ( scripts . map ( dynamicImportComponents ) )
95-
96- Promise . allSettled ( promises )
95+ Object . entries ( components ) . forEach ( ( [ componentId , exportName ] ) => {
96+ if ( ! window . TinyLowcodeComponent [ componentId ] ) {
97+ window . TinyLowcodeComponent [ componentId ] = modules [ exportName ]
98+ }
99+ } )
97100}
0 commit comments