You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
3.8 KiB
86 lines
3.8 KiB
4 days ago
|
# 路由映射插件模块
|
||
|
|
||
|
这个目录包含了模块化后的路由映射插件的各个功能模块。
|
||
|
|
||
|
## 模块结构
|
||
|
|
||
|
### 1. `api-collector.js` - API收集模块
|
||
|
- **功能**: 从service文件中收集API信息
|
||
|
- **主要方法**:
|
||
|
- `collectApiMappings(moduleDirs)` - 收集所有模块的API映射
|
||
|
- `collectModuleApiMappings(moduleName)` - 收集单个模块的API映射
|
||
|
- `analyzeServiceFile(servicePath, serviceName, moduleName)` - 分析单个服务文件
|
||
|
- `extractApiMapping(methodPath, methodName, serviceName, servicePath)` - 从AST节点中提取API映射信息
|
||
|
|
||
|
### 2. `ast-analyzer.js` - AST分析模块
|
||
|
- **功能**: 负责Babel和Vue模板的AST分析
|
||
|
- **主要方法**:
|
||
|
- `findMethodsCallingServiceWithBabel(content, serviceName, apiMethodName)` - 使用Babel AST分析查找调用指定服务方法的方法
|
||
|
- `checkMethodCallsServiceWithBabel(methodPath, serviceName, apiMethodName)` - 检查方法是否调用了指定的service方法
|
||
|
- `findTriggersInTemplateWithAST(templateContent, componentName, apiMethodNames, triggerSources, filePath)` - 使用AST分析模板中的事件绑定
|
||
|
- `generateUniqueButtonName(componentName, clickHandler)` - 生成唯一的按钮名称
|
||
|
|
||
|
### 3. `trigger-analyzer.js` - 触发器分析模块
|
||
|
- **功能**: 负责分析组件中的按钮和事件触发器
|
||
|
- **主要方法**:
|
||
|
- `findTriggerSourcesForApiMappings(apiMappings)` - 查找API方法的触发器源
|
||
|
- `findTriggerSourcesForApiMethod(serviceName, apiMethodName, moduleName)` - 查找API方法的触发器源
|
||
|
- `analyzeComponentWithAST(componentName, serviceName, apiMethodName)` - 使用AST分析组件
|
||
|
- `analyzeMethodTriggerSources(content, componentName, methodNames, filePath)` - 分析方法的触发源
|
||
|
|
||
|
### 4. `file-generator.js` - 文件生成模块
|
||
|
- **功能**: 负责生成最终的映射文件
|
||
|
- **主要方法**:
|
||
|
- `generateMappingFile(routes, apiMappings)` - 生成映射文件
|
||
|
- `generateFileContent(routes, apiMappings)` - 生成文件内容
|
||
|
- `optimizeApiMappings(apiMappings)` - 优化API映射
|
||
|
- `deduplicateTriggerSources(triggerSources)` - 去重触发器源
|
||
|
|
||
|
### 5. `route-analyzer.js` - 路由分析模块
|
||
|
- **功能**: 负责分析路由配置
|
||
|
- **主要方法**:
|
||
|
- `analyzeRoutes()` - 分析路由配置
|
||
|
- `parseRouteConfig(routeContent)` - 解析路由配置
|
||
|
- `getModuleDirs()` - 获取模块列表
|
||
|
|
||
|
## 使用方式
|
||
|
|
||
|
```javascript
|
||
|
const { ApiCollector, TriggerAnalyzer, FileGenerator, RouteAnalyzer } = require('./modules')
|
||
|
|
||
|
// 创建实例
|
||
|
const apiCollector = new ApiCollector()
|
||
|
const triggerAnalyzer = new TriggerAnalyzer()
|
||
|
const fileGenerator = new FileGenerator()
|
||
|
const routeAnalyzer = new RouteAnalyzer()
|
||
|
|
||
|
// 使用模块
|
||
|
const routes = routeAnalyzer.analyzeRoutes()
|
||
|
const moduleDirs = routeAnalyzer.getModuleDirs()
|
||
|
const apiMappings = apiCollector.collectApiMappings(moduleDirs)
|
||
|
const enhancedApiMappings = triggerAnalyzer.findTriggerSourcesForApiMappings(apiMappings)
|
||
|
const optimizedApiMappings = fileGenerator.optimizeApiMappings(enhancedApiMappings)
|
||
|
fileGenerator.generateMappingFile(routes, optimizedApiMappings)
|
||
|
```
|
||
|
|
||
|
## 优势
|
||
|
|
||
|
1. **模块化**: 每个模块负责特定的功能,职责清晰
|
||
|
2. **可维护性**: 代码结构清晰,易于理解和修改
|
||
|
3. **可测试性**: 每个模块可以独立测试
|
||
|
4. **可复用性**: 模块可以在其他地方复用
|
||
|
5. **可扩展性**: 新功能可以作为新模块添加
|
||
|
|
||
|
## 文件大小对比
|
||
|
|
||
|
- **原始文件**: `route-mapping-plugin.js` (2425行)
|
||
|
- **模块化后**:
|
||
|
- `route-mapping-plugin-modular.js` (约200行)
|
||
|
- `api-collector.js` (约150行)
|
||
|
- `ast-analyzer.js` (约200行)
|
||
|
- `trigger-analyzer.js` (约300行)
|
||
|
- `file-generator.js` (约100行)
|
||
|
- `route-analyzer.js` (约80行)
|
||
|
|
||
|
总计约1030行,比原始文件减少了约57%的代码量,同时提高了可维护性。
|