设想咱们的若干源组件【souce components】知足这样的目录结构关系,全部组件都仅仅经过一个 index.ts 向外暴露接口,相互之间仅经过 index.ts 引用依赖——这个是一个很重要的原则,isolate:node
而这些源组件存在以下的依赖关系:git
经过 bit add src/source/zoo --id demo/zoo 对 Zoo 组件进行追踪,bit 会把组件目录、文件等相关信息会被写入到 .bitmap。github
bit 会在执行 tag 时候才会解析组件的依赖,因此当 bit tag demo/zoo 时,会抛错,并同时打印两个告警信息:typescript
对于告警 1,咱们须要在 bit.json 里配置别名,告诉 bit 如何去查找 @source 依赖:shell
{
"resolveModules": {
"aliases": {
"@source": "src/source"
}
}
}
复制代码
而告警 2 则须要 bit add、bit tag Camel & Elephant 两个组件:npm
bit add src/source/camel --id=demo/camel
bit add src/source/elephant --id=demo/elephant
bit tag demo/camel
bit tag demo/elephant
复制代码
而后就能够再次 bit tag demo/zoo,而且成功—— bit 会把对 Camel、Elephant 组件的依赖映射为对 demo/camel 、demo/elephant 的依赖【Relative Source】——接下来就能够经过 bit export scopeName 导出组件。json
更直观的体验一下,能够配置 bit.json:bash
{
"env": {},
"componentsDefaultDirectory": "components/{name}",
"packageManager": "npm"
}
复制代码
而后:ssh
bit init;
bit remote add ssh://bit@你的私服:/data/bit/my-test;
bit import my-test/demo/zoo;
复制代码
最终的 bit 建立目录树是这样的:spa
其中,components 目录下:
若是:
import Elephant from '@source/Elephant/Elephant';
复制代码
可是在对应的 node_modules/@source/Elephant 并无 Elephant.tsx,必然会是错误的——这就是为何组件之间要 isolate,由于 bit 就这么限制了——奇怪的是这里为何不把 Elephant 直接软链到 node_modules/@bit/my-test.demo.elephant?
而后,node_modules/@bit 目录下会有软链到 components/demo/zoo 的 my-test.demo.zoo,这样在业务代码里就能够经过 @bit/my-test.demo.zoo 引入 Zoo 组件。
组件之间 isolate——组件全部的成员,包括逻辑、类型都只能经过惟一的出口,好比 index.ts 导出;对其余组件依赖的引用,也必须经过该组件惟一的出口,进行引用。如此就能够避免踩到 bit 依赖处理这块的坑。