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.
|
3 days ago | |
---|---|---|
.. | ||
README.md | 3 days ago | |
api-collector.js | 3 days ago | |
ast-analyzer.js | 3 days ago | |
file-generator.js | 3 days ago | |
index.js | 3 days ago | |
route-analyzer.js | 3 days ago | |
trigger-analyzer.js | 3 days ago |
README.md
路由映射插件模块
这个目录包含了模块化后的路由映射插件的各个功能模块。
模块结构
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()
- 获取模块列表
使用方式
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)
优势
- 模块化: 每个模块负责特定的功能,职责清晰
- 可维护性: 代码结构清晰,易于理解和修改
- 可测试性: 每个模块可以独立测试
- 可复用性: 模块可以在其他地方复用
- 可扩展性: 新功能可以作为新模块添加
文件大小对比
- 原始文件:
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%的代码量,同时提高了可维护性。