引入
ts
import {
normalizePath,
flattenUsingComponentMap,
genIterativeComponentMap,
getRelativePath
} from 't-comm';
// 不支持 tree-shaking 的项目
import {
normalizePath,
flattenUsingComponentMap,
genIterativeComponentMap,
getRelativePath
} from 't-comm/lib/path/index';
// 只支持 ESM 的项目
import {
normalizePath,
flattenUsingComponentMap,
genIterativeComponentMap,
getRelativePath
} from 't-comm/es/path/index';normalizePath
描述:格式化路径
参数:
| 参数名 | 描述 |
|---|---|
| path | 文件路径,或目录路径 |
返回: 格式化后的路径
示例
ts
normalizePath('xxx/xxx/xxx');
normalizePath('xxx\\xxx\\xxx');flattenUsingComponentMap(rawMap)
描述:将嵌套的组件依赖关系打平为一维映射 递归遍历组件依赖树,将每个页面/组件的所有直接和间接依赖打平为数组
参数:
| 参数名 | 类型 | 描述 |
|---|---|---|
| rawMap | Record.<string, any> | 原始组件依赖映射,key 为页面/组件名,value 为其直接依赖的组件映射 |
返回: ComponentMapList
打平后的组件列表映射,key 为页面/组件名,value 为所有依赖组件名数组
示例
ts
const rawMap = {
'pages/index': { 'comp-a': true, 'comp-b': true },
'comp-a': { 'comp-c': true },
};
const result = flattenUsingComponentMap(rawMap);
// { 'pages/index': ['comp-a', 'comp-b', 'comp-c'], 'comp-a': ['comp-c'] }genIterativeComponentMap(usingComponentsMap)
描述:生成迭代式组件映射,将子组件的依赖合并到父组件中 遍历组件映射,如果某个组件本身也在映射中,则将其依赖合并到引用它的父组件中
参数:
| 参数名 | 类型 | 描述 |
|---|---|---|
| usingComponentsMap | IterativeComponentMap | 组件依赖映射,key 为页面/组件名,value 为其依赖的组件对象 |
示例
ts
const map = {
'pages/index': { 'comp-a': {}, 'comp-b': {} },
'comp-a': { 'comp-c': {} },
};
genIterativeComponentMap(map);
// map['pages/index']['comp-a'] 现在包含了 comp-c 的依赖getRelativePath(pathA, pathB)
描述:A引用B时,拿引用路径
参数:
| 参数名 | 描述 |
|---|---|
| pathA | 路径A |
| pathB | 路径B |
返回: 相对路径
示例
ts
getRelativePath('a', 'b');
// './b'