增加获取importModules方法及loadModule后的moduleInfo#469
增加获取importModules方法及loadModule后的moduleInfo#469maoxiaoke merged 6 commits intoice-lab:release/2.7.1from xiazhiqiang:master
Conversation
| mount, | ||
| unmount, | ||
| component, | ||
| moduleInfo, |
There was a problem hiding this comment.
在加载微模块后,需要获取已加载的模块中对应的方法。通过获取到微模块的方法,用来定制业务中的方法,从而实现方法的定制扩展。
There was a problem hiding this comment.
也就是說,getImportModules 並不消費
There was a problem hiding this comment.
此处可以有2种方式,如果loadModule不增加返回moduleInfo,await loadModule后通过getImportModule获取已加载的模块信息,如果有返回,则读取返回值。2中方式都是读取的方式。
There was a problem hiding this comment.
需要2中方式的。loadModule每次调用会渲染css。对于微模块配置了url: ['xxx.js', 'yyy.css'],js中有default模块及暴露的其他方法实现。这种首次加载模块后渲染模块没问题。但如果需要基于已加载的模块中调用方法,就不需要重新渲染样式了,直接读取importModule中的已加载的模块信息去调用对应的方法即可。也就是先判断importModule有没有,如果有已加载的模块信息,则直接调用,如果没有再调用loadModule方法。
There was a problem hiding this comment.
用法是 moduleA.moduleInfo.自定义方法 ?
There was a problem hiding this comment.
是的,跟目前的设计不冲突(目前mount和unmount用的是umd包里面的mount和unmount方法,component参数用的是umd暴露的default方法)。业务拿到moduleInfo后回去调用自定义方法判断的。
There was a problem hiding this comment.
这里建议根据 rfc 修改下,
{
component,
...moduleInfo,
mount,
unmount,
}
| export const getImportModules = function (name: string = '') { | ||
| return name ? importModules[name] || {} : importModules; | ||
| }; | ||
|
|
There was a problem hiding this comment.
一个方法两用,函数命名和实现不一致。建议分成两个方法实现
There was a problem hiding this comment.
好的,这个可拆成2个函数,一个是getImportModules,一个是getImportModuleByName
There was a problem hiding this comment.
-
getImportedModule
-
getAllImportedModules
不会更好一些?
|
邮件已收到~~
|
|
需求简单整理下 背景目前根据规范,模块默认导出 Component、mount 、unmount 方法,此外模块还可能暴露其他能力。目前,我们可以通过 store 来处理。比如,模块注册方法: import { store } from '@ice/stark-data';
store.set('method1', () => { })应用消费 store 数据: const method1 = store.get('methods')
method1(1);这个方案存在的问题:
用法方式一: const {
mount,
unmount,
Component,
method1 // 自定义方法
} = loadModule({
name: 'moduleName',
url: 'https://localhost/module.js',
})方式二: import { getImportedModule } from '@ice/stark-module'
loadModule({
name: 'moduleName',
url: 'https://localhost/module.js',
})
const { moduleInfo: { method1 } } = getImportedModule('moduleName') |
|
store 方案本身没问题。store方案我认为主要是共享不同应用的数据、触发/响应公共事件及回调,通过store也能实现。 |
我认为 moduleInfo 内也包含了 mount、unmount 的内容,从 loadModule 这个方法的行为来看,其实是加载模块返回的信息,额外存在 moduleInfo 在含义上非常奇怪 |
这个确实有点。moduleInfo中确实包含mount和unmount和default。但是为了isv写的方法不影响loadModule的实现,所以直接把moduleInfo返回了,而没有将moduleInfo剔除default、mount和unmount方法后的其他属性值打平作为loadModule的返回。个人觉得loadModule可以不感知这些。看你们的理解了。 |
从目前的函数方法来理解,不建议增加一个 moduleInfo 判断,直接返回就可以了 |
| * get import module by name | ||
| */ | ||
| export const getImportedModule = function(name: string = '') { | ||
| return name ? importModules[name] : {}; |
There was a problem hiding this comment.
如果 importModules[name] 不存在,跟没传入 name 的时候返回不一样。建议:
if (typeof name !== 'string') {
console.error(`[icestark-module]: should be string, but get ${typeof name}`)
return;
}
return importModules[name];| mount, | ||
| unmount, | ||
| component, | ||
| moduleInfo, |
There was a problem hiding this comment.
这里建议根据 rfc 修改下,
{
component,
...moduleInfo,
mount,
unmount,
}
在微模块umd包除了mount、unmount和default外,还有其他方法。所以需要获取已经加载的模块信息及loadModule信息。