@hadss/hmrouter-plugin 1.0.0-rc.9 → 1.1.1-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (147) hide show
  1. package/LICENSE +176 -77
  2. package/NOTICE +1365 -0
  3. package/README.md +254 -71
  4. package/dist/HMRouterAnalyzer.d.ts +24 -1
  5. package/dist/HMRouterAnalyzer.js +154 -55
  6. package/dist/HMRouterHvigorPlugin.js +23 -4
  7. package/dist/HMRouterPluginConfig.d.ts +1 -0
  8. package/dist/HMRouterPluginConfig.js +7 -3
  9. package/dist/HMRouterPluginHandle.d.ts +1 -0
  10. package/dist/HMRouterPluginHandle.js +81 -55
  11. package/dist/HMRouterPluginManager.d.ts +9 -0
  12. package/dist/HMRouterPluginManager.js +55 -0
  13. package/dist/Index.d.ts +9 -3
  14. package/dist/Index.js +33 -103
  15. package/dist/common/Logger.js +1 -1
  16. package/dist/common/PluginModel.d.ts +4 -1
  17. package/dist/common/PluginModel.js +2 -1
  18. package/dist/constants/CommonConstants.d.ts +10 -5
  19. package/dist/constants/CommonConstants.js +11 -6
  20. package/dist/constants/ConfigConstants.d.ts +2 -1
  21. package/dist/constants/ConfigConstants.js +3 -2
  22. package/dist/constants/TaskConstants.js +1 -1
  23. package/dist/framework/constants/TaskConstants.d.ts +8 -0
  24. package/dist/framework/constants/TaskConstants.js +12 -0
  25. package/dist/framework/constants/index.d.ts +1 -0
  26. package/dist/framework/constants/index.js +17 -0
  27. package/dist/framework/core/PluginExecutionController.d.ts +15 -0
  28. package/dist/framework/core/PluginExecutionController.js +62 -0
  29. package/dist/framework/core/TaskManager.d.ts +12 -0
  30. package/dist/framework/core/TaskManager.js +67 -0
  31. package/dist/framework/core/TaskStage.d.ts +10 -0
  32. package/dist/framework/core/TaskStage.js +14 -0
  33. package/dist/framework/core/index.d.ts +3 -0
  34. package/dist/framework/core/index.js +19 -0
  35. package/dist/framework/extension/ExtensionManager.d.ts +11 -0
  36. package/dist/framework/extension/ExtensionManager.js +33 -0
  37. package/dist/framework/extension/PluginExtension.d.ts +13 -0
  38. package/dist/framework/extension/PluginExtension.js +6 -0
  39. package/dist/framework/extension/context/ExtensionContextImpl.d.ts +23 -0
  40. package/dist/framework/extension/context/ExtensionContextImpl.js +64 -0
  41. package/dist/framework/extension/context/Interface.d.ts +21 -0
  42. package/dist/framework/extension/context/Interface.js +2 -0
  43. package/dist/framework/extension/context/TemplateMetadataImpl.d.ts +12 -0
  44. package/dist/framework/extension/context/TemplateMetadataImpl.js +22 -0
  45. package/dist/framework/extension/index.d.ts +4 -0
  46. package/dist/framework/extension/index.js +20 -0
  47. package/dist/framework/index.d.ts +4 -0
  48. package/dist/framework/index.js +20 -0
  49. package/dist/framework/utils/FileUtil.d.ts +13 -0
  50. package/dist/framework/utils/FileUtil.js +20 -0
  51. package/dist/framework/utils/Logger.d.ts +12 -0
  52. package/dist/framework/utils/Logger.js +31 -0
  53. package/dist/framework/utils/ModuleTypeUtil.d.ts +4 -0
  54. package/dist/framework/utils/ModuleTypeUtil.js +16 -0
  55. package/dist/framework/utils/ObjectUtils.d.ts +3 -0
  56. package/dist/framework/utils/ObjectUtils.js +14 -0
  57. package/dist/framework/utils/PluginStore.d.ts +10 -0
  58. package/dist/framework/utils/PluginStore.js +29 -0
  59. package/dist/framework/utils/StringUtil.d.ts +3 -0
  60. package/dist/framework/utils/StringUtil.js +18 -0
  61. package/dist/framework/utils/TsAstUtil.d.ts +6 -0
  62. package/dist/framework/utils/TsAstUtil.js +19 -0
  63. package/dist/framework/utils/index.d.ts +7 -0
  64. package/dist/framework/utils/index.js +23 -0
  65. package/dist/hmrouter_extension/HMRouterExtension.d.ts +14 -0
  66. package/dist/hmrouter_extension/HMRouterExtension.js +47 -0
  67. package/dist/hmrouter_extension/HMRouterExtensionContext.d.ts +8 -0
  68. package/dist/hmrouter_extension/HMRouterExtensionContext.js +2 -0
  69. package/dist/hmrouter_extension/analyzer/AnnotationAnalyzerRegistry.d.ts +14 -0
  70. package/dist/hmrouter_extension/analyzer/AnnotationAnalyzerRegistry.js +45 -0
  71. package/dist/hmrouter_extension/analyzer/ComponentAnalyzer.d.ts +12 -0
  72. package/dist/hmrouter_extension/analyzer/ComponentAnalyzer.js +52 -0
  73. package/dist/hmrouter_extension/analyzer/RouterAnalyzer.d.ts +11 -0
  74. package/dist/hmrouter_extension/analyzer/RouterAnalyzer.js +79 -0
  75. package/dist/hmrouter_extension/analyzer/interface/AbstractAnnotationAnalyzer.d.ts +9 -0
  76. package/dist/hmrouter_extension/analyzer/interface/AbstractAnnotationAnalyzer.js +9 -0
  77. package/dist/hmrouter_extension/analyzer/interface/IConstantResolver.d.ts +7 -0
  78. package/dist/hmrouter_extension/analyzer/interface/IConstantResolver.js +2 -0
  79. package/dist/hmrouter_extension/analyzer/utils/ConstantAnalyzer.d.ts +9 -0
  80. package/dist/hmrouter_extension/analyzer/utils/ConstantAnalyzer.js +32 -0
  81. package/dist/hmrouter_extension/analyzer/utils/ConstantResolver.d.ts +15 -0
  82. package/dist/hmrouter_extension/analyzer/utils/ConstantResolver.js +217 -0
  83. package/dist/hmrouter_extension/analyzer/utils/DecoratorParser.d.ts +7 -0
  84. package/dist/hmrouter_extension/analyzer/utils/DecoratorParser.js +41 -0
  85. package/dist/hmrouter_extension/analyzer/utils/ImportAnalyzer.d.ts +4 -0
  86. package/dist/hmrouter_extension/analyzer/utils/ImportAnalyzer.js +35 -0
  87. package/dist/hmrouter_extension/config/HMRouterPluginConfig.d.ts +38 -0
  88. package/dist/hmrouter_extension/config/HMRouterPluginConfig.js +64 -0
  89. package/dist/hmrouter_extension/constants/AnnotationConstants.d.ts +8 -0
  90. package/dist/hmrouter_extension/constants/AnnotationConstants.js +11 -0
  91. package/dist/hmrouter_extension/constants/FilePathConstants.d.ts +19 -0
  92. package/dist/hmrouter_extension/constants/FilePathConstants.js +26 -0
  93. package/dist/hmrouter_extension/constants/ObfuscationConstants.d.ts +5 -0
  94. package/dist/hmrouter_extension/constants/ObfuscationConstants.js +8 -0
  95. package/dist/hmrouter_extension/constants/PluginConstants.d.ts +6 -0
  96. package/dist/hmrouter_extension/constants/PluginConstants.js +9 -0
  97. package/dist/hmrouter_extension/constants/PrefixConstants.d.ts +8 -0
  98. package/dist/hmrouter_extension/constants/PrefixConstants.js +11 -0
  99. package/dist/hmrouter_extension/constants/RouterMapConstants.d.ts +5 -0
  100. package/dist/hmrouter_extension/constants/RouterMapConstants.js +8 -0
  101. package/dist/hmrouter_extension/constants/TemplateConstants.d.ts +6 -0
  102. package/dist/hmrouter_extension/constants/TemplateConstants.js +9 -0
  103. package/dist/hmrouter_extension/constants/VersionConstants.d.ts +6 -0
  104. package/dist/hmrouter_extension/constants/VersionConstants.js +9 -0
  105. package/dist/hmrouter_extension/constants/index.d.ts +8 -0
  106. package/dist/hmrouter_extension/constants/index.js +22 -0
  107. package/dist/hmrouter_extension/error/PluginError.d.ts +29 -0
  108. package/dist/hmrouter_extension/error/PluginError.js +75 -0
  109. package/dist/hmrouter_extension/model/HMAnnotationModel.d.ts +28 -0
  110. package/dist/hmrouter_extension/model/HMAnnotationModel.js +2 -0
  111. package/dist/hmrouter_extension/model/RouterInfo.d.ts +6 -0
  112. package/dist/hmrouter_extension/model/RouterInfo.js +2 -0
  113. package/dist/hmrouter_extension/model/TemplateModel.d.ts +8 -0
  114. package/dist/hmrouter_extension/model/TemplateModel.js +2 -0
  115. package/dist/hmrouter_extension/model/index.d.ts +3 -0
  116. package/dist/hmrouter_extension/model/index.js +19 -0
  117. package/dist/hmrouter_extension/processor/CodeGenerationProcessor.d.ts +13 -0
  118. package/dist/hmrouter_extension/processor/CodeGenerationProcessor.js +117 -0
  119. package/dist/hmrouter_extension/processor/CompletionProcessor.d.ts +11 -0
  120. package/dist/hmrouter_extension/processor/CompletionProcessor.js +59 -0
  121. package/dist/hmrouter_extension/processor/ConfigUpdateProcessor.d.ts +11 -0
  122. package/dist/hmrouter_extension/processor/ConfigUpdateProcessor.js +53 -0
  123. package/dist/hmrouter_extension/processor/InitializerProcessor.d.ts +15 -0
  124. package/dist/hmrouter_extension/processor/InitializerProcessor.js +95 -0
  125. package/dist/hmrouter_extension/processor/ObfuscationProcessor.d.ts +13 -0
  126. package/dist/hmrouter_extension/processor/ObfuscationProcessor.js +121 -0
  127. package/dist/hmrouter_extension/processor/ResourceProcessor.d.ts +9 -0
  128. package/dist/hmrouter_extension/processor/ResourceProcessor.js +53 -0
  129. package/dist/hmrouter_extension/processor/RouterMapBuildingProcessor.d.ts +9 -0
  130. package/dist/hmrouter_extension/processor/RouterMapBuildingProcessor.js +122 -0
  131. package/dist/hmrouter_extension/processor/index.d.ts +7 -0
  132. package/dist/hmrouter_extension/processor/index.js +23 -0
  133. package/dist/store/PluginStore.d.ts +14 -0
  134. package/dist/store/PluginStore.js +20 -0
  135. package/dist/utils/FileUtil.d.ts +2 -0
  136. package/dist/utils/ObfuscationUtil.js +7 -1
  137. package/dist/utils/TsAstUtil.d.ts +3 -2
  138. package/dist/utils/TsAstUtil.js +51 -11
  139. package/examples/CustomExtension.ts +256 -0
  140. package/package.json +8 -7
  141. package/template/customBuilder.ejs +25 -0
  142. package/template/viewBuilder.ejs +51 -0
  143. package/{viewBuilder.ejs → template/viewBuilder_v1.ejs} +11 -4
  144. package/dist/common/Constant.d.ts +0 -27
  145. package/dist/common/Constant.js +0 -35
  146. package/dist/utils/ConfusionUtil.d.ts +0 -4
  147. package/dist/utils/ConfusionUtil.js +0 -27
package/README.md CHANGED
@@ -1,84 +1,150 @@
1
- ## 编译插件配置
1
+ # HMRouterPlugin
2
2
 
3
- 1.修改项目的`hvigor/hvigor-config.json`文件,加入路由编译插件
3
+ HMRouter路由框架编译插件,需要配合[HMRouter](https://ohpm.openharmony.cn/#/cn/detail/@hadss%2Fhmrouter)使用
4
+
5
+ ## 插件简介
6
+
7
+ HMRouter编译插件是一个基于hvigor的编译插件,用于:
8
+
9
+ 1. 扫描代码中的`@HMRouter`、`@HMInterceptor`、`@HMLifecycle`、`@HMAnimator`、`@HMService`等注解
10
+ 2. 生成路由表和NavDestination页面代码
11
+ 3. 生成混淆规则
12
+ 4. 支持自定义模板
13
+ 5. 编译时自定义扩展
14
+
15
+ ## 插件安装与配置
16
+
17
+ ### 工程级配置
18
+
19
+ 在工程的`hvigor/hvigor-config.json`文件中添加路由编译插件:
4
20
 
5
21
  ```json5
6
22
  {
7
23
  "dependencies": {
8
- "@hadss/hmrouter-plugin": "^1.0.0-rc.9" // 使用npm仓版本号
24
+ "@hadss/hmrouter-plugin": "^1.1.0-beta.0" // 使用最新版本
9
25
  },
10
- // ...其他配置
26
+ // ...其余配置
11
27
  }
12
28
  ```
13
29
 
14
- 2.在模块中引入路由编译插件,修改`hvigorfile.ts`
30
+ ### 模块级配置
31
+
32
+ 在使用HMRouter的模块中引入路由编译插件,修改`hvigorfile.ts`:
33
+
34
+ ```extendtypescript
35
+ // entry/hvigorfile.ts entry模块的hvigorfile.ts
36
+ import { hapTasks } from '@ohos/hvigor-ohos-plugin';
37
+ import { hapPlugin } from '@hadss/hmrouter-plugin';
38
+
39
+ export default {
40
+ system: hapTasks,
41
+ plugins: [hapPlugin()] // 使用HMRouter标签的模块均需要配置,与模块类型保持一致
42
+ }
43
+
44
+ // libHar/hvigorfile.ts libHar模块的hvigorfile.ts
45
+ import { harTasks } from '@ohos/hvigor-ohos-plugin';
46
+ import { harPlugin } from '@hadss/hmrouter-plugin';
15
47
 
16
- ```typescript
17
- import { hapTasks } from "@ohos/hvigor-ohos-plugin";
18
- import { hapPlugin } from "@hadss/hmrouter-plugin";
48
+ export default {
49
+ system: harTasks,
50
+ plugins:[harPlugin()] // 使用HMRouter标签的模块均需要配置,与模块类型保持一致
51
+ }
52
+
53
+ // libHsp/hvigorfile.ts libHsp模块的hvigorfile.ts
54
+ import { hspTasks } from '@ohos/hvigor-ohos-plugin';
55
+ import { hspPlugin } from '@hadss/hmrouter-plugin';
19
56
 
20
57
  export default {
21
- system: hapTasks,
22
- plugins: [hapPlugin()], // 使用HMRouter框架中标签的模块均需要配置,与模块类型保持一致
23
- };
58
+ system: hspTasks,
59
+ plugins: [hspPlugin()] // 使用HMRouter标签的模块均需要配置,与模块类型保持一致
60
+ }
24
61
  ```
25
62
 
26
- > 请使用与模块类型一致的插件方法,如果模块是 Har 则使用`harPlugin()`, 模块是 Hsp 则使用`hspPlugin()`
63
+ > 注意:模块类型与插件类型必须匹配:
64
+ > - Har模块使用`harPlugin()`
65
+ > - Hsp模块使用`hspPlugin()`
66
+ > - Hap模块使用`hapPlugin()`
67
+
68
+ ## 配置文件详解
27
69
 
28
- 3.项目根目录或者模块目录创建路由编译插件配置文件`hmrouter_config.json`(推荐)
70
+ 在项目根目录或模块目录创建`hmrouter_config.json`文件,配置插件行为:
29
71
 
30
72
  ```json5
31
73
  {
32
- // 如果不配置则扫描src/main/ets目录,对代码进行全量扫描,如果配置则数组不能为空,建议配置指定目录可缩短编译耗时
74
+ // 扫描目录配置,如果不配置则扫描src/main/ets目录
33
75
  "scanDir": [
34
76
  "src/main/ets/components",
35
77
  "src/main/ets/interceptors"
36
78
  ],
37
- // 默认为false,调试排除错误时可以改成true,不删除编译产物
79
+
80
+ // 是否保留生成文件,默认为false
38
81
  "saveGeneratedFile": false,
39
- // 默认为false,不自动配置混淆规则,只会生成hmrouter_obfuscation_rules.txt文件帮助开发者配置混淆文件;如果设置为true,会自动配置混淆规则,并删除hmrouter_obfuscation_rules.txt文件
82
+
83
+ // 是否自动配置混淆规则,默认为false
40
84
  "autoObfuscation": false,
85
+
41
86
  // 默认模板文件,不配置时使用插件内置模板
42
87
  "defaultPageTemplate": "./templates/defaultTemplate.ejs",
88
+
43
89
  // 特殊页面模版文件,匹配原则支持文件通配符
44
90
  "customPageTemplate": [
45
91
  {
46
- "srcPath": ["**/component/Home/**/*.ets"],
47
- "templatePath": "./templates/home_shopping_template.ejs"
48
- },
49
- {
50
- "srcPath": ["**/common/**/*.ets"],
51
- "templatePath": "./templates/common_template.ejs"
92
+ "srcPath": [
93
+ "**/component/Home/**/*.ets"
94
+ ],
95
+ "templatePath": "templates/home_shopping_template.ejs"
52
96
  },
53
97
  {
54
- "srcPath": ["**/live/**/*.ets"],
55
- "templatePath": "./templates/live_template.ejs"
98
+ "srcPath": [
99
+ "**/live/**/*.ets"
100
+ ],
101
+ "templatePath": "templates/live_template.ejs"
56
102
  }
57
103
  ]
58
104
  }
59
105
  ```
60
106
 
61
- ### hmrouter_config.json 配置
107
+ ### 配置选项说明
108
+
109
+ | 配置项 | 类型 | 是否必填 | 说明 |
110
+ |---------------------|---------|------|----------------------------------------------------------------------------------------------------------------------------------------|
111
+ | scanDir | array | 否 | 指定扫描当前模块路径,默认值为`src/main/ets` |
112
+ | saveGeneratedFile | boolean | 否 | 默认为 false,不保留插件自动生成的代码,如果需要保留,需要设置为 true |
113
+ | autoObfuscation | boolean | 否 | 默认为 false,不自动配置混淆规则,只会生成`hmrouter_obfuscation_rules.txt`文件帮助开发者配置混淆规则;如果设置为 true,会自动配置混淆规则,并在编译完成后删除`hmrouter_obfuscation_rules.txt`文件,详细说明请参考[混淆配置](Obfuscation.md)文档。 |
114
+ | defaultPageTemplate | string | 否 | 默认模版路径,相对于hmrouter_config.json文件,例如:`./templates/defaultTemplate.ejs` |
115
+ | customPageTemplate | object | 否 | srcPath为匹配的代码文件路径,支持通配符,templatePath为模版路径,可以实现不同的代码使用不同的模版来生成 |
62
116
 
63
- `hmrouter_config.json`文件用于配置该插件在编译期的行为
117
+ ### 配置文件读取规则
64
118
 
65
119
  > 配置文件读取规则为 模块 > 工程 > 默认
66
- > 优先使用本模块内的配置,如果没有配置,则找模块目录的上级目录(最多找三层目录,找到则停止),若找不到则使用默认配置
120
+ >
121
+ > 优先使用本模块内的配置,如果没有配置,则使用工程根目录配置,若都找不到则使用默认配置
67
122
 
68
123
  > 1.0.0-rc.6 版本开始,支持混淆配置`autoObfuscation`
124
+ >
69
125
  > 1.0.0-rc.9 版本开始,支持自定义模版配置`customPageTemplate`
70
126
 
71
- | 配置项 | 类型 | 是否必填 | 说明 |
72
- |---------------------|---------|------|----------------------------------------------------------------------------------------------------------------------------------------|
73
- | scanDir | array | 否 | 指定扫描当前模块路径,默认值为`src/main/ets` |
74
- | saveGeneratedFile | boolean | 否 | 默认为 false,不保留插件自动生成的代码,如果需要保留,需要设置为 true |
75
- | autoObfuscation | boolean | 否 | 默认为 false,不自动配置混淆规则,只会生成`hmrouter_obfuscation_rules.txt`文件帮助开发者配置混淆规则;如果设置为 true,会自动配置混淆规则,并在编译完成后删除`hmrouter_obfuscation_rules.txt`文件 |
76
- | defaultPageTemplate | string | 否 | 默认模版路径,相对于hmrouter_config.json文件,例如:`./templates/defaultTemplate.ejs` |
77
- | customPageTemplate | object | 否 | srcPath为匹配的代码文件路径,支持通配符,templatePath为模版路径,可以实现不同的代码使用不同的模版来生成 |
127
+ ## 多模块配置
128
+
129
+ 在多模块项目中,每个使用HMRouter的模块都需要配置插件。
130
+
131
+ ### 方式一:每个模块单独配置
132
+
133
+ 在每个模块的根目录下创建`hmrouter_config.json`文件,分别配置。
134
+
135
+ ### 方式二:共享配置
136
+
137
+ 在项目根目录创建`hmrouter_config.json`文件,所有模块共享配置。
138
+
139
+ 如果需要为不同模块指定不同的配置,可以在模块根目录创建`hmrouter_config.json`文件,覆盖共享配置。
140
+
141
+ > 注意:当前多模块配置时不会做字段合并,即模块配置会直接覆盖工程配置
78
142
 
79
143
  ## 自定义模板使用
80
144
 
81
- 在 `HMRouterPlugin` 中,EJS模板用于生成动态页面或组件,你可以在模板文件中使用 EJS 的语法
145
+ 在 `HMRouterPlugin` 中,EJS模板用于生成动态页面或组件,可以在模板文件中使用 EJS 的语法
146
+
147
+ [自定义模板使用场景](https://gitcode.com/openharmony-sig/ohrouter/blob/master/docs/Scene.md#%E8%87%AA%E5%AE%9A%E4%B9%89%E6%A8%A1%E6%9D%BF)
82
148
 
83
149
  > EJS语法可阅读参考[官网链接](https://github.com/mde/ejs)
84
150
 
@@ -110,30 +176,79 @@ export struct <%= componentName %>Generated {
110
176
 
111
177
  ### 默认模板介绍
112
178
 
113
- 插件默认会根据如下模板来生成`NavDestination`页面代码,如有自定义模板的需求,建议先阅读内置模板的介绍在做更改,书写自定义模板时建议在内置模板基础上添加代码,删除内置模板相关代码可能会导致**编译失败、生命周期生效、转场动画失效等问题**。
179
+ 插件默认模板根据HMRouter版本不同有两种实现:
180
+
181
+ #### 1.1.0及以上版本
114
182
 
115
- **默认模版`viewBuilder.ejs`:**
183
+ 1.1.0及以上版本使用`NavDestinationHelper`类来处理页面生命周期和手势动画:
116
184
 
117
185
  ```ejs
118
- import { <%= componentName %> } from '<%= importPath %>'
119
- import { TemplateService, TranslateOption, ScaleOption, OpacityOption } from '@hadss/hmrouter'
186
+ // auto-generated <%= componentName %>Builder.ets by HMRouter
187
+ import <%if(isDefaultExport) {%> <%= componentName %> <% } else {%> { <%= componentName %> } <% } %> from '<%= importPath %>'
188
+ import { NavDestinationHelper } from '@hadss/hmrouter';
189
+
190
+ @Builder
191
+ export function <%= componentName %>Builder(name: string, param: Object) {
192
+ <%= componentName %>Generated()
193
+ }
194
+
195
+ export const <%= componentName %>BuilderWrapBuilder = wrapBuilder(<%= componentName %>Builder)
196
+
197
+ @Component
198
+ export struct <%= componentName %>Generated {
199
+ private helper: NavDestinationHelper = new NavDestinationHelper(this);
200
+
201
+ build() {
202
+ NavDestination() {
203
+ <%= componentName %>()
204
+ }
205
+ <% if(dialog){ %>.mode(NavDestinationMode.DIALOG)<% } %>
206
+ .hideTitleBar(true)
207
+ .attributeModifier(this.helper.modifier)
208
+ .gestureModifier(this.helper.gestureModifier)
209
+ .onWillAppear(()=>{this.helper.onWillAppear()})
210
+ .onAppear(() => {this.helper.onAppear()})
211
+ .onWillShow(()=>{this.helper.onWillAppear()})
212
+ .onShown(()=>{this.helper.onShown()})
213
+ .onWillHide(()=>{this.helper.onWillHide()})
214
+ .onHidden(()=>{this.helper.onHidden()})
215
+ .onWillDisappear(()=>{this.helper.onWillDisappear()})
216
+ .onDisAppear(()=>{this.helper.onDisAppear()})
217
+ .onReady((ctx)=>{this.helper.onReady(ctx)})
218
+ .onBackPressed(()=> this.helper.onBackPressed())
219
+ }
220
+ }
221
+ ```
222
+
223
+ #### 1.0.0版本
224
+
225
+ 1.0.0版本使用`TemplateService`类来处理页面生命周期和动画:
226
+
227
+ ```ejs
228
+ // auto-generated <%= componentName %>Builder.ets by HMRouter
229
+ import <%if(isDefaultExport) {%> <%= componentName %> <% } else {%> { <%= componentName %> } <% } %> from '<%= importPath %>'
230
+ import { TemplateService, TranslateOption, ScaleOption, OpacityOption, HMRouterMgr } from '@hadss/hmrouter'
120
231
 
121
232
  @Builder
122
233
  export function <%= componentName %>Builder(name: string, param: Object) {
123
234
  <%= componentName %>Generated()
124
235
  }
125
236
 
237
+ export const <%= componentName %>BuilderWrapBuilder = wrapBuilder(<%= componentName %>Builder)
238
+
126
239
  @Component
127
240
  export struct <%= componentName %>Generated {
128
241
  @State translateOption: TranslateOption = new TranslateOption()
129
242
  @State scaleOption: ScaleOption = new ScaleOption()
130
243
  @State opacityOption: OpacityOption = new OpacityOption()
131
- private pageUrl: string = '<%= pageUrl %>'
244
+ private pageUrl: string = ''
132
245
  private ndId: string = ''
133
246
  private navigationId: string = ''
134
247
 
135
248
  aboutToAppear(): void {
136
249
  this.navigationId = this.queryNavigationInfo()!.navigationId;
250
+ const allPathName = HMRouterMgr.getPathStack(this.navigationId)?.getAllPathName();
251
+ this.pageUrl = allPathName ? allPathName[allPathName.length - 1] : '';
137
252
  TemplateService.aboutToAppear(this.navigationId, this.pageUrl, <%= dialog %>,
138
253
  this.translateOption, this.scaleOption, this.opacityOption)
139
254
  }
@@ -158,6 +273,9 @@ export struct <%= componentName %>Generated {
158
273
  .onActionEnd((event: GestureEvent) =>{
159
274
  TemplateService.interactiveFinish(this.navigationId, this.ndId, event)
160
275
  })
276
+ .onActionCancel(() =>{
277
+ TemplateService.interactiveCancel(this.navigationId, this.ndId)
278
+ })
161
279
  )
162
280
  .translate(this.translateOption)
163
281
  .scale(this.scaleOption)
@@ -197,6 +315,23 @@ export struct <%= componentName %>Generated {
197
315
  }
198
316
  ```
199
317
 
318
+ ### 版本差异说明
319
+
320
+ 1. **NavDestinationHelper vs TemplateService**:
321
+ - 1.1.0版本中,`TemplateService`被`NavDestinationHelper`类替代
322
+ - `NavDestinationHelper`采用实例化方式使用,而`TemplateService`则采用静态方法调用
323
+ - `NavDestinationHelper`通过`modifier`和`gestureModifier`简化了动画和手势处理
324
+
325
+ 2. **生命周期处理**:
326
+ - 1.1.0版本使用更简洁的代理模式处理生命周期回调
327
+ - 1.0.0版本需要手动调用每个静态方法并传递参数
328
+
329
+ 3. **动画处理**:
330
+ - 1.1.0版本通过`attributeModifier`和`gestureModifier`处理动画
331
+ - 1.0.0版本需要手动管理`TranslateOption`、`ScaleOption`和`OpacityOption`
332
+
333
+ > 注意:自定义模板时,请确保使用的API版本与HMRouter版本匹配。1.1.0及以上版本应使用NavDestinationHelper,1.0.0版本应使用TemplateService。
334
+
200
335
  ### 模板变量
201
336
 
202
337
  | 属性 | 描述 |
@@ -205,44 +340,49 @@ export struct <%= componentName %>Generated {
205
340
  | importPath | 原组件的导入路径 |
206
341
  | componentName | 原组件名 |
207
342
  | dialog | 是否dialog页面 |
208
- | generatorViewName | 生成的文件名 |
209
-
210
- ### TemplateService内置模版方法
211
-
212
- 该类中封装了一系列在模板中需要用到的注册、初始化、事件回调接口
213
-
214
- | 接口 | 参数 | 返回值 | 接口描述 |
215
- |----------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------|------|-----------------------|
216
- | static aboutToAppear | navigationId: string,pageUrl: string, dialog: boolean, translateOption: TranslateOption, scaleOption: ScaleOption, opacityOption: OpacityOption | void | 注册接口,用于模板代码中注册动画与生命周期 |
217
- | static aboutToDisappear | navigationId: string, pageUrl: string, ndId: string | void | 销毁,用户销毁一个页面的动画与生命周期实例 |
218
- | static onDisAppear | navigationId: string, pageUrl: string, navId: string | void | NavDestination生命周期 |
219
- | static onAppear | navigationId: string, pageUrl: string, navId: string | void | NavDestination生命周期 |
220
- | static onShown | navigationId: string, pageUrl: string, navId: string | void | NavDestination生命周期 |
221
- | static onHidden | navigationId: string, pageUrl: string, navId: string | void | NavDestination生命周期 |
222
- | static onWillAppear | navigationId: string, pageUrl: string | void | NavDestination生命周期 |
223
- | static onWillDisappear | navigationId: string, pageUrl: string, navId: string | void | NavDestination生命周期 |
224
- | static onWillShow | navigationId: string, pageUrl: string, navId: string | void | NavDestination生命周期 |
225
- | static onWillHide | navigationId: string, pageUrl: string, navId: string | void | NavDestination生命周期 |
226
- | static onReady | navigationId: string, pageUrl: string, navContext: NavDestinationContext | void | NavDestination生命周期 |
227
- | static onBackPressed | navigationId: string, pageUrl: string, navId: string | void | NavDestination生命周期 |
228
- | static interactiveStart | navigationId: string, ndId: string, event: GestureEvent | void | 手势转场动画触发 |
229
- | static interactiveFinish | navigationId: string, ndId: string, event: GestureEvent | void | 手势转场动画更新 |
230
- | static interactiveProgress | navigationId: string, ndId: string, event: GestureEvent | void | 手势转场动画结束 |
231
-
232
- ## 依赖版本
233
-
234
- 最低版本
343
+
344
+ ### 文件通配符说明
345
+
346
+ - `*`:匹配任意数量的字符,不包括目录分隔符
347
+ - `**`:匹配任意数量的字符,包括目录分隔符
348
+ - `?`:匹配单个字符
349
+ - `[abc]`:匹配方括号中的任意一个字符
350
+ - `[a-z]`:匹配a到z范围内的任意一个字符
351
+ - `{a,b,c}`:匹配花括号中的任意一个模式
352
+ - `!`:否定模式,排除匹配的文件
353
+
354
+ ### 匹配优先级
355
+
356
+ - customPageTemplate > defaultPageTemplate > 库中携带的模板
357
+ - 如遇到customPageTemplate中的多个srcPath都能匹配上,优先取前面的模板文件
358
+
359
+ ## 插件自定义扩展
360
+
361
+ 见[插件扩展指南](https://gitcode.com/openharmony-sig/ohrouter/blob/master/docs/PluginExtension.md)
362
+
363
+ ## 编译产物
364
+
365
+ HMRouter编译插件生成的文件包括:
366
+
367
+ 1. **路由表文件**:`src/main/resources/base/profile/hm_router_map.json`,包含所有页面的路由信息
368
+ 2. **rawfile资源文件**:`src/main/resources/rawfile/hm_router_map.json`, 包含所有页面的路由信息与本次编译中包含是hsp模块包名信息
369
+ 3. **页面代码文件**:`src/main/ets/generated/HM{PageName}{Hash}.ets`,为每个页面生成的NavDestination包装代码, 命名格式为`HM + 组件名 + pageUrl的hash值`
370
+ 4. **混淆规则文件**:`hmrouter_obfuscation_rules.txt`,包含需要保留的类名
371
+
372
+ 在`hmrouter_config.json`中设置`saveGeneratedFile: true`可保留所有编译产物
373
+
374
+ ## 系统版本要求
375
+
376
+ 最低版本:
235
377
 
236
378
  ```text
237
- HarmonyOS NEXT Developer Beta5版本及以上
238
379
  DevEco Studio 5.0.3.700及以上
239
380
  hvigor 5.5.1及以上
240
381
  ```
241
382
 
242
- **推荐使用**
383
+ **推荐使用**:
243
384
 
244
385
  ```text
245
- HarmonyOS NEXT Beta1版本及以上
246
386
  DevEco Studio 5.0.3.800及以上
247
387
  hvigor 5.7.3及以上
248
388
  ```
@@ -253,6 +393,49 @@ hvigor 5.7.3及以上
253
393
  > [HMRouterPlugin] Your DevEco Studio version less than 5.0.3.800, may cause remote hsp dependencies get failed
254
394
  > ```
255
395
 
256
- ## HMRouter 使用
396
+ ## 常见问题
397
+
398
+ ### 编译失败,提示重复的routerMap对象名称
399
+
400
+ ```
401
+ hvigorERROR: Duplicate 'routerMap' object names detected.
402
+
403
+ * Try the following:
404
+ > Change the 'routerMap' object names listed below under routerMap in the respective router configuration files. Make sure the names are unique across the current module and the modules on which it depends.
405
+ {"home":["BootView"]}
406
+ ```
407
+
408
+ 解决方案:
409
+ 1. 检查是否有多个页面使用了相同的pageUrl
410
+ 2. 升级DevEco Studio版本,已知Build Version: 5.0.3.600版本会偶现该问题
411
+ 3. 清理项目并重新编译
412
+
413
+ ### 编译成功但找不到生成的文件
414
+
415
+ 解决方案:
416
+ 1. 在`hmrouter_config.json`中设置`saveGeneratedFile: true`,保留生成文件
417
+ 2. 检查生成的文件是否正确,通常位于`src/main/ets/generated`目录
418
+ 3. 确认编译插件配置正确,每个使用HMRouter的模块都需要配置对应的插件
419
+
420
+ ### 多target问题
421
+
422
+ 出现编译报错`The "to" argument must be of type string. Received undefined`
423
+
424
+ 解决方案:
425
+ 1. 确保一个`target`对应一个`product`,不要出现多余的`target`定义
426
+ 2. 不要有一个模块中多个`target`给一个`product`的情况
427
+ 3. 遵循官方说明:同一个module的不同target不能打包到同一个product中
428
+
429
+ ## 使用建议
430
+
431
+ 1. **指定扫描目录**:配置`scanDir`,减少编译时间
432
+ 2. **开发阶段保留生成文件**:设置`saveGeneratedFile: true`,方便排查问题
433
+ 3. **启用自动混淆配置**:设置`autoObfuscation: true`,自动处理混淆规则
434
+ 4. **使用自定义模板**:根据项目需求,自定义页面模板
435
+ 5. **模块类型与插件类型匹配**:确保使用正确的插件类型
436
+ 6. **定期清理生成文件**:在发布前,设置`saveGeneratedFile: false`,清理生成文件
437
+ 7. **注意版本兼容性**:确保自定义模板与HMRouter版本匹配
438
+
439
+ ## HMRouter路由框架使用
257
440
 
258
441
  详见[@hadss/hmrouter](https://ohpm.openharmony.cn/#/cn/detail/@hadss%2Fhmrouter)
@@ -3,6 +3,29 @@ import { HMRouterPluginConfig } from './HMRouterPluginConfig';
3
3
  export declare class AnalyzerController {
4
4
  private analyzeResult;
5
5
  analyzeFile(sourceFilePath: string, config: HMRouterPluginConfig): void;
6
- parsePageUrl(): void;
6
+ parseConstants(): void;
7
+ private parsePropertyValue;
7
8
  getAnalyzeResultSet(): Set<AnalyzerResultLike>;
9
+ clearAnalyzeResultSet(): void;
10
+ }
11
+ export declare class AnalyzerService {
12
+ private readonly sourceFilePath;
13
+ private sourceFile;
14
+ private config;
15
+ private analyzerResultSet;
16
+ private importMap;
17
+ constructor(sourceFilePath: string, config: HMRouterPluginConfig);
18
+ start(): void;
19
+ getResult(): Set<AnalyzerResultLike>;
20
+ private analyzeImport;
21
+ private analyzeRouter;
22
+ private parseFileByLineOrder;
23
+ private analyzeComponent;
24
+ private addToResultSet;
25
+ private parseDecorator;
26
+ private parseDecoratorArguments;
27
+ private parseIdentifierPropertyValue;
28
+ private parsePrimitiveValue;
29
+ private getVariableFilePath;
30
+ private getOtherModuleVariableFilePath;
8
31
  }