@hadss/hmrouter-plugin 1.1.0-beta.0 → 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 (120) hide show
  1. package/README.md +246 -67
  2. package/dist/HMRouterPluginManager.d.ts +9 -0
  3. package/dist/HMRouterPluginManager.js +55 -0
  4. package/dist/Index.d.ts +9 -3
  5. package/dist/Index.js +29 -109
  6. package/dist/framework/constants/TaskConstants.d.ts +8 -0
  7. package/dist/framework/constants/TaskConstants.js +12 -0
  8. package/dist/framework/constants/index.d.ts +1 -0
  9. package/dist/framework/constants/index.js +17 -0
  10. package/dist/framework/core/PluginExecutionController.d.ts +15 -0
  11. package/dist/framework/core/PluginExecutionController.js +62 -0
  12. package/dist/framework/core/TaskManager.d.ts +12 -0
  13. package/dist/framework/core/TaskManager.js +67 -0
  14. package/dist/framework/core/TaskStage.d.ts +10 -0
  15. package/dist/framework/core/TaskStage.js +14 -0
  16. package/dist/framework/core/index.d.ts +3 -0
  17. package/dist/framework/core/index.js +19 -0
  18. package/dist/framework/extension/ExtensionManager.d.ts +11 -0
  19. package/dist/framework/extension/ExtensionManager.js +33 -0
  20. package/dist/framework/extension/PluginExtension.d.ts +13 -0
  21. package/dist/framework/extension/PluginExtension.js +6 -0
  22. package/dist/framework/extension/context/ExtensionContextImpl.d.ts +23 -0
  23. package/dist/framework/extension/context/ExtensionContextImpl.js +64 -0
  24. package/dist/framework/extension/context/Interface.d.ts +21 -0
  25. package/dist/framework/extension/context/Interface.js +2 -0
  26. package/dist/framework/extension/context/TemplateMetadataImpl.d.ts +12 -0
  27. package/dist/framework/extension/context/TemplateMetadataImpl.js +22 -0
  28. package/dist/framework/extension/index.d.ts +4 -0
  29. package/dist/framework/extension/index.js +20 -0
  30. package/dist/framework/index.d.ts +4 -0
  31. package/dist/framework/index.js +20 -0
  32. package/dist/framework/utils/FileUtil.d.ts +13 -0
  33. package/dist/framework/utils/FileUtil.js +20 -0
  34. package/dist/framework/utils/Logger.d.ts +12 -0
  35. package/dist/framework/utils/Logger.js +31 -0
  36. package/dist/framework/utils/ModuleTypeUtil.d.ts +4 -0
  37. package/dist/framework/utils/ModuleTypeUtil.js +16 -0
  38. package/dist/framework/utils/ObjectUtils.d.ts +3 -0
  39. package/dist/framework/utils/ObjectUtils.js +14 -0
  40. package/dist/framework/utils/PluginStore.d.ts +10 -0
  41. package/dist/framework/utils/PluginStore.js +29 -0
  42. package/dist/framework/utils/StringUtil.d.ts +3 -0
  43. package/dist/framework/utils/StringUtil.js +18 -0
  44. package/dist/framework/utils/TsAstUtil.d.ts +6 -0
  45. package/dist/framework/utils/TsAstUtil.js +19 -0
  46. package/dist/framework/utils/index.d.ts +7 -0
  47. package/dist/framework/utils/index.js +23 -0
  48. package/dist/hmrouter_extension/HMRouterExtension.d.ts +14 -0
  49. package/dist/hmrouter_extension/HMRouterExtension.js +47 -0
  50. package/dist/hmrouter_extension/HMRouterExtensionContext.d.ts +8 -0
  51. package/dist/hmrouter_extension/HMRouterExtensionContext.js +2 -0
  52. package/dist/hmrouter_extension/analyzer/AnnotationAnalyzerRegistry.d.ts +14 -0
  53. package/dist/hmrouter_extension/analyzer/AnnotationAnalyzerRegistry.js +45 -0
  54. package/dist/hmrouter_extension/analyzer/ComponentAnalyzer.d.ts +12 -0
  55. package/dist/hmrouter_extension/analyzer/ComponentAnalyzer.js +52 -0
  56. package/dist/hmrouter_extension/analyzer/RouterAnalyzer.d.ts +11 -0
  57. package/dist/hmrouter_extension/analyzer/RouterAnalyzer.js +79 -0
  58. package/dist/hmrouter_extension/analyzer/interface/AbstractAnnotationAnalyzer.d.ts +9 -0
  59. package/dist/hmrouter_extension/analyzer/interface/AbstractAnnotationAnalyzer.js +9 -0
  60. package/dist/hmrouter_extension/analyzer/interface/IConstantResolver.d.ts +7 -0
  61. package/dist/hmrouter_extension/analyzer/interface/IConstantResolver.js +2 -0
  62. package/dist/hmrouter_extension/analyzer/utils/ConstantAnalyzer.d.ts +9 -0
  63. package/dist/hmrouter_extension/analyzer/utils/ConstantAnalyzer.js +32 -0
  64. package/dist/hmrouter_extension/analyzer/utils/ConstantResolver.d.ts +15 -0
  65. package/dist/hmrouter_extension/analyzer/utils/ConstantResolver.js +217 -0
  66. package/dist/hmrouter_extension/analyzer/utils/DecoratorParser.d.ts +7 -0
  67. package/dist/hmrouter_extension/analyzer/utils/DecoratorParser.js +41 -0
  68. package/dist/hmrouter_extension/analyzer/utils/ImportAnalyzer.d.ts +4 -0
  69. package/dist/hmrouter_extension/analyzer/utils/ImportAnalyzer.js +35 -0
  70. package/dist/hmrouter_extension/config/HMRouterPluginConfig.d.ts +38 -0
  71. package/dist/hmrouter_extension/config/HMRouterPluginConfig.js +64 -0
  72. package/dist/hmrouter_extension/constants/AnnotationConstants.d.ts +8 -0
  73. package/dist/hmrouter_extension/constants/AnnotationConstants.js +11 -0
  74. package/dist/hmrouter_extension/constants/FilePathConstants.d.ts +19 -0
  75. package/dist/hmrouter_extension/constants/FilePathConstants.js +26 -0
  76. package/dist/hmrouter_extension/constants/ObfuscationConstants.d.ts +5 -0
  77. package/dist/hmrouter_extension/constants/ObfuscationConstants.js +8 -0
  78. package/dist/hmrouter_extension/constants/PluginConstants.d.ts +6 -0
  79. package/dist/hmrouter_extension/constants/PluginConstants.js +9 -0
  80. package/dist/hmrouter_extension/constants/PrefixConstants.d.ts +8 -0
  81. package/dist/hmrouter_extension/constants/PrefixConstants.js +11 -0
  82. package/dist/hmrouter_extension/constants/RouterMapConstants.d.ts +5 -0
  83. package/dist/hmrouter_extension/constants/RouterMapConstants.js +8 -0
  84. package/dist/hmrouter_extension/constants/TemplateConstants.d.ts +6 -0
  85. package/dist/hmrouter_extension/constants/TemplateConstants.js +9 -0
  86. package/dist/hmrouter_extension/constants/VersionConstants.d.ts +6 -0
  87. package/dist/hmrouter_extension/constants/VersionConstants.js +9 -0
  88. package/dist/hmrouter_extension/constants/index.d.ts +8 -0
  89. package/dist/hmrouter_extension/constants/index.js +22 -0
  90. package/dist/hmrouter_extension/error/PluginError.d.ts +29 -0
  91. package/dist/hmrouter_extension/error/PluginError.js +75 -0
  92. package/dist/hmrouter_extension/model/HMAnnotationModel.d.ts +28 -0
  93. package/dist/hmrouter_extension/model/HMAnnotationModel.js +2 -0
  94. package/dist/hmrouter_extension/model/RouterInfo.d.ts +6 -0
  95. package/dist/hmrouter_extension/model/RouterInfo.js +2 -0
  96. package/dist/hmrouter_extension/model/TemplateModel.d.ts +8 -0
  97. package/dist/hmrouter_extension/model/TemplateModel.js +2 -0
  98. package/dist/hmrouter_extension/model/index.d.ts +3 -0
  99. package/dist/hmrouter_extension/model/index.js +19 -0
  100. package/dist/hmrouter_extension/processor/CodeGenerationProcessor.d.ts +13 -0
  101. package/dist/hmrouter_extension/processor/CodeGenerationProcessor.js +117 -0
  102. package/dist/hmrouter_extension/processor/CompletionProcessor.d.ts +11 -0
  103. package/dist/hmrouter_extension/processor/CompletionProcessor.js +59 -0
  104. package/dist/hmrouter_extension/processor/ConfigUpdateProcessor.d.ts +11 -0
  105. package/dist/hmrouter_extension/processor/ConfigUpdateProcessor.js +53 -0
  106. package/dist/hmrouter_extension/processor/InitializerProcessor.d.ts +15 -0
  107. package/dist/hmrouter_extension/processor/InitializerProcessor.js +95 -0
  108. package/dist/hmrouter_extension/processor/ObfuscationProcessor.d.ts +13 -0
  109. package/dist/hmrouter_extension/processor/ObfuscationProcessor.js +121 -0
  110. package/dist/hmrouter_extension/processor/ResourceProcessor.d.ts +9 -0
  111. package/dist/hmrouter_extension/processor/ResourceProcessor.js +53 -0
  112. package/dist/hmrouter_extension/processor/RouterMapBuildingProcessor.d.ts +9 -0
  113. package/dist/hmrouter_extension/processor/RouterMapBuildingProcessor.js +122 -0
  114. package/dist/hmrouter_extension/processor/index.d.ts +7 -0
  115. package/dist/hmrouter_extension/processor/index.js +23 -0
  116. package/examples/CustomExtension.ts +256 -0
  117. package/package.json +2 -2
  118. package/template/customBuilder.ejs +1 -0
  119. package/template/viewBuilder.ejs +1 -0
  120. package/template/viewBuilder_v1.ejs +104 -0
package/README.md CHANGED
@@ -2,89 +2,149 @@
2
2
 
3
3
  HMRouter路由框架编译插件,需要配合[HMRouter](https://ohpm.openharmony.cn/#/cn/detail/@hadss%2Fhmrouter)使用
4
4
 
5
- ## 编译插件配置
5
+ ## 插件简介
6
6
 
7
- 1.修改项目的`hvigor/hvigor-config.json`文件,加入路由编译插件
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`文件中添加路由编译插件:
8
20
 
9
21
  ```json5
10
22
  {
11
23
  "dependencies": {
12
- "@hadss/hmrouter-plugin": "^1.0.0-rc.10" // 使用npm仓版本号
24
+ "@hadss/hmrouter-plugin": "^1.1.0-beta.0" // 使用最新版本
13
25
  },
14
26
  // ...其余配置
15
27
  }
16
28
  ```
17
29
 
18
- 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';
47
+
48
+ export default {
49
+ system: harTasks,
50
+ plugins:[harPlugin()] // 使用HMRouter标签的模块均需要配置,与模块类型保持一致
51
+ }
19
52
 
20
- ```typescript
21
- import { hapTasks } from "@ohos/hvigor-ohos-plugin";
22
- import { hapPlugin } from "@hadss/hmrouter-plugin";
53
+ // libHsp/hvigorfile.ts libHsp模块的hvigorfile.ts
54
+ import { hspTasks } from '@ohos/hvigor-ohos-plugin';
55
+ import { hspPlugin } from '@hadss/hmrouter-plugin';
23
56
 
24
57
  export default {
25
- system: hapTasks,
26
- plugins: [hapPlugin()], // 使用HMRouter框架中标签的模块均需要配置,与模块类型保持一致
27
- };
58
+ system: hspTasks,
59
+ plugins: [hspPlugin()] // 使用HMRouter标签的模块均需要配置,与模块类型保持一致
60
+ }
28
61
  ```
29
62
 
30
- > 请使用与模块类型一致的插件方法,如果模块是 Har 则使用`harPlugin()`, 模块是 Hsp 则使用`hspPlugin()`
63
+ > 注意:模块类型与插件类型必须匹配:
64
+ > - Har模块使用`harPlugin()`
65
+ > - Hsp模块使用`hspPlugin()`
66
+ > - Hap模块使用`hapPlugin()`
67
+
68
+ ## 配置文件详解
31
69
 
32
- 3.项目根目录或者模块目录创建路由编译插件配置文件`hmrouter_config.json`(推荐)
70
+ 在项目根目录或模块目录创建`hmrouter_config.json`文件,配置插件行为:
33
71
 
34
72
  ```json5
35
73
  {
36
- // 如果不配置则扫描src/main/ets目录,对代码进行全量扫描,如果配置则数组不能为空,建议配置指定目录可缩短编译耗时
74
+ // 扫描目录配置,如果不配置则扫描src/main/ets目录
37
75
  "scanDir": [
38
76
  "src/main/ets/components",
39
77
  "src/main/ets/interceptors"
40
78
  ],
41
- // 默认为false,调试排除错误时可以改成true,不删除编译产物
79
+
80
+ // 是否保留生成文件,默认为false
42
81
  "saveGeneratedFile": false,
43
- // 默认为false,不自动配置混淆规则,只会生成hmrouter_obfuscation_rules.txt文件帮助开发者配置混淆文件;如果设置为true,会自动配置混淆规则,并删除hmrouter_obfuscation_rules.txt文件
82
+
83
+ // 是否自动配置混淆规则,默认为false
44
84
  "autoObfuscation": false,
85
+
45
86
  // 默认模板文件,不配置时使用插件内置模板
46
87
  "defaultPageTemplate": "./templates/defaultTemplate.ejs",
88
+
47
89
  // 特殊页面模版文件,匹配原则支持文件通配符
48
90
  "customPageTemplate": [
49
91
  {
50
- "srcPath": ["**/component/Home/**/*.ets"],
51
- "templatePath": "./templates/home_shopping_template.ejs"
92
+ "srcPath": [
93
+ "**/component/Home/**/*.ets"
94
+ ],
95
+ "templatePath": "templates/home_shopping_template.ejs"
52
96
  },
53
97
  {
54
- "srcPath": ["**/common/**/*.ets"],
55
- "templatePath": "./templates/common_template.ejs"
56
- },
57
- {
58
- "srcPath": ["**/live/**/*.ets"],
59
- "templatePath": "./templates/live_template.ejs"
98
+ "srcPath": [
99
+ "**/live/**/*.ets"
100
+ ],
101
+ "templatePath": "templates/live_template.ejs"
60
102
  }
61
103
  ]
62
104
  }
63
105
  ```
64
106
 
65
- ### 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为模版路径,可以实现不同的代码使用不同的模版来生成 |
66
116
 
67
- `hmrouter_config.json`文件用于配置该插件在编译期的行为
117
+ ### 配置文件读取规则
68
118
 
69
119
  > 配置文件读取规则为 模块 > 工程 > 默认
70
120
  >
71
- > 优先使用本模块内的配置,如果没有配置,则找模块目录的上级目录(最多找三层目录,找到则停止),若找不到则使用默认配置
121
+ > 优先使用本模块内的配置,如果没有配置,则使用工程根目录配置,若都找不到则使用默认配置
72
122
 
73
123
  > 1.0.0-rc.6 版本开始,支持混淆配置`autoObfuscation`
74
124
  >
75
125
  > 1.0.0-rc.9 版本开始,支持自定义模版配置`customPageTemplate`
76
126
 
77
- | 配置项 | 类型 | 是否必填 | 说明 |
78
- |---------------------|---------|------|----------------------------------------------------------------------------------------------------------------------------------------|
79
- | scanDir | array | 否 | 指定扫描当前模块路径,默认值为`src/main/ets` |
80
- | saveGeneratedFile | boolean | 否 | 默认为 false,不保留插件自动生成的代码,如果需要保留,需要设置为 true |
81
- | autoObfuscation | boolean | 否 | 默认为 false,不自动配置混淆规则,只会生成`hmrouter_obfuscation_rules.txt`文件帮助开发者配置混淆规则;如果设置为 true,会自动配置混淆规则,并在编译完成后删除`hmrouter_obfuscation_rules.txt`文件 |
82
- | defaultPageTemplate | string | 否 | 默认模版路径,相对于hmrouter_config.json文件,例如:`./templates/defaultTemplate.ejs` |
83
- | 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
+ > 注意:当前多模块配置时不会做字段合并,即模块配置会直接覆盖工程配置
84
142
 
85
143
  ## 自定义模板使用
86
144
 
87
- 在 `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)
88
148
 
89
149
  > EJS语法可阅读参考[官网链接](https://github.com/mde/ejs)
90
150
 
@@ -116,30 +176,79 @@ export struct <%= componentName %>Generated {
116
176
 
117
177
  ### 默认模板介绍
118
178
 
119
- 插件默认会根据如下模板来生成`NavDestination`页面代码,如有自定义模板的需求,建议先阅读内置模板的介绍在做更改,书写自定义模板时建议在内置模板基础上添加代码,删除内置模板相关代码可能会导致**编译失败、生命周期生效、转场动画失效等问题**。
179
+ 插件默认模板根据HMRouter版本不同有两种实现:
120
180
 
121
- **默认模版`viewBuilder.ejs`:**
181
+ #### 1.1.0及以上版本
182
+
183
+ 1.1.0及以上版本使用`NavDestinationHelper`类来处理页面生命周期和手势动画:
122
184
 
123
185
  ```ejs
124
- import { <%= componentName %> } from '<%= importPath %>'
125
- 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'
126
231
 
127
232
  @Builder
128
233
  export function <%= componentName %>Builder(name: string, param: Object) {
129
234
  <%= componentName %>Generated()
130
235
  }
131
236
 
237
+ export const <%= componentName %>BuilderWrapBuilder = wrapBuilder(<%= componentName %>Builder)
238
+
132
239
  @Component
133
240
  export struct <%= componentName %>Generated {
134
241
  @State translateOption: TranslateOption = new TranslateOption()
135
242
  @State scaleOption: ScaleOption = new ScaleOption()
136
243
  @State opacityOption: OpacityOption = new OpacityOption()
137
- private pageUrl: string = '<%= pageUrl %>'
244
+ private pageUrl: string = ''
138
245
  private ndId: string = ''
139
246
  private navigationId: string = ''
140
247
 
141
248
  aboutToAppear(): void {
142
249
  this.navigationId = this.queryNavigationInfo()!.navigationId;
250
+ const allPathName = HMRouterMgr.getPathStack(this.navigationId)?.getAllPathName();
251
+ this.pageUrl = allPathName ? allPathName[allPathName.length - 1] : '';
143
252
  TemplateService.aboutToAppear(this.navigationId, this.pageUrl, <%= dialog %>,
144
253
  this.translateOption, this.scaleOption, this.opacityOption)
145
254
  }
@@ -164,6 +273,9 @@ export struct <%= componentName %>Generated {
164
273
  .onActionEnd((event: GestureEvent) =>{
165
274
  TemplateService.interactiveFinish(this.navigationId, this.ndId, event)
166
275
  })
276
+ .onActionCancel(() =>{
277
+ TemplateService.interactiveCancel(this.navigationId, this.ndId)
278
+ })
167
279
  )
168
280
  .translate(this.translateOption)
169
281
  .scale(this.scaleOption)
@@ -203,6 +315,23 @@ export struct <%= componentName %>Generated {
203
315
  }
204
316
  ```
205
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
+
206
335
  ### 模板变量
207
336
 
208
337
  | 属性 | 描述 |
@@ -211,40 +340,47 @@ export struct <%= componentName %>Generated {
211
340
  | importPath | 原组件的导入路径 |
212
341
  | componentName | 原组件名 |
213
342
  | dialog | 是否dialog页面 |
214
- | generatorViewName | 生成的文件名 |
215
-
216
- ### TemplateService内置模版方法
217
-
218
- 该类中封装了一系列在模板中需要用到的注册、初始化、事件回调接口
219
-
220
- | 接口 | 参数 | 返回值 | 接口描述 |
221
- |----------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------|------|-----------------------|
222
- | static aboutToAppear | navigationId: string,pageUrl: string, dialog: boolean, translateOption: TranslateOption, scaleOption: ScaleOption, opacityOption: OpacityOption | void | 注册接口,用于模板代码中注册动画与生命周期 |
223
- | static aboutToDisappear | navigationId: string, pageUrl: string, ndId: string | void | 销毁,用户销毁一个页面的动画与生命周期实例 |
224
- | static onDisAppear | navigationId: string, pageUrl: string, navId: string | void | NavDestination生命周期 |
225
- | static onAppear | navigationId: string, pageUrl: string, navId: string | void | NavDestination生命周期 |
226
- | static onShown | navigationId: string, pageUrl: string, navId: string | void | NavDestination生命周期 |
227
- | static onHidden | navigationId: string, pageUrl: string, navId: string | void | NavDestination生命周期 |
228
- | static onWillAppear | navigationId: string, pageUrl: string | void | NavDestination生命周期 |
229
- | static onWillDisappear | navigationId: string, pageUrl: string, navId: string | void | NavDestination生命周期 |
230
- | static onWillShow | navigationId: string, pageUrl: string, navId: string | void | NavDestination生命周期 |
231
- | static onWillHide | navigationId: string, pageUrl: string, navId: string | void | NavDestination生命周期 |
232
- | static onReady | navigationId: string, pageUrl: string, navContext: NavDestinationContext | void | NavDestination生命周期 |
233
- | static onBackPressed | navigationId: string, pageUrl: string, navId: string | void | NavDestination生命周期 |
234
- | static interactiveStart | navigationId: string, ndId: string, event: GestureEvent | void | 手势转场动画触发 |
235
- | static interactiveFinish | navigationId: string, ndId: string, event: GestureEvent | void | 手势转场动画更新 |
236
- | static interactiveProgress | navigationId: string, ndId: string, event: GestureEvent | void | 手势转场动画结束 |
237
-
238
- ## 依赖系统版本
239
-
240
- 最低版本
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
+ 最低版本:
241
377
 
242
378
  ```text
243
379
  DevEco Studio 5.0.3.700及以上
244
380
  hvigor 5.5.1及以上
245
381
  ```
246
382
 
247
- **推荐使用**
383
+ **推荐使用**:
248
384
 
249
385
  ```text
250
386
  DevEco Studio 5.0.3.800及以上
@@ -257,6 +393,49 @@ hvigor 5.7.3及以上
257
393
  > [HMRouterPlugin] Your DevEco Studio version less than 5.0.3.800, may cause remote hsp dependencies get failed
258
394
  > ```
259
395
 
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
+
260
439
  ## HMRouter路由框架使用
261
440
 
262
441
  详见[@hadss/hmrouter](https://ohpm.openharmony.cn/#/cn/detail/@hadss%2Fhmrouter)
@@ -0,0 +1,9 @@
1
+ import { HvigorNode } from '@ohos/hvigor';
2
+ import { PluginParam } from './Index';
3
+ export declare class HMRouterPluginManager {
4
+ private static instance;
5
+ private controllerSet;
6
+ private constructor();
7
+ static getInstance(): HMRouterPluginManager;
8
+ registerPlugin(node: HvigorNode, pluginId: string, options: PluginParam): void;
9
+ }
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HMRouterPluginManager = void 0;
4
+ const hvigor_1 = require("@ohos/hvigor");
5
+ const PluginError_1 = require("./hmrouter_extension/error/PluginError");
6
+ const HMRouterExtension_1 = require("./hmrouter_extension/HMRouterExtension");
7
+ const framework_1 = require("./framework");
8
+ class HMRouterPluginManager {
9
+ constructor() {
10
+ this.controllerSet = new Set();
11
+ framework_1.Logger.info('', 'HMRouterPlugin starting...');
12
+ (0, framework_1.registerPluginExtension)(new HMRouterExtension_1.HMRouterDefaultExtension());
13
+ hvigor_1.hvigor.nodesEvaluated(() => {
14
+ this.controllerSet.forEach((controller) => {
15
+ controller.start();
16
+ });
17
+ });
18
+ hvigor_1.hvigor.buildFinished(() => {
19
+ this.controllerSet.forEach((controller) => {
20
+ controller.complete();
21
+ });
22
+ HMRouterPluginManager.instance = null;
23
+ framework_1.PluginStore.destroy();
24
+ framework_1.ExtensionManager.destroy();
25
+ this.controllerSet.clear();
26
+ framework_1.TsAstUtil.clearProject();
27
+ });
28
+ }
29
+ static getInstance() {
30
+ if (!HMRouterPluginManager.instance) {
31
+ HMRouterPluginManager.instance = new HMRouterPluginManager();
32
+ }
33
+ return HMRouterPluginManager.instance;
34
+ }
35
+ registerPlugin(node, pluginId, options) {
36
+ const moduleContext = node.getContext(pluginId);
37
+ if (!moduleContext) {
38
+ throw PluginError_1.PluginError.create(PluginError_1.ErrorCode.ERROR_CONFIG, node.getNodeName(), node.getNodePath());
39
+ }
40
+ const controller = new framework_1.PluginExecutionController(node, moduleContext, options.extensions);
41
+ this.controllerSet.add(controller);
42
+ if ((0, framework_1.isHspModule)(node)) {
43
+ let packageJsonPath = framework_1.PluginFileUtil.pathResolve(node.getNodePath(), 'oh-package.json5');
44
+ let packageJsonContent = framework_1.PluginFileUtil.readFileSync(packageJsonPath);
45
+ let packageJson = JSON.parse(packageJsonContent.toString());
46
+ let hspNameArr = framework_1.PluginStore.getInstance().get('hspModuleNames');
47
+ if (!hspNameArr) {
48
+ framework_1.PluginStore.getInstance().set('hspModuleNames', []);
49
+ }
50
+ framework_1.PluginStore.getInstance().get('hspModuleNames')?.push(packageJson.name);
51
+ }
52
+ }
53
+ }
54
+ exports.HMRouterPluginManager = HMRouterPluginManager;
55
+ HMRouterPluginManager.instance = null;
package/dist/Index.d.ts CHANGED
@@ -1,4 +1,10 @@
1
1
  import { HvigorPlugin } from '@ohos/hvigor';
2
- export declare function hapPlugin(): HvigorPlugin;
3
- export declare function hspPlugin(): HvigorPlugin;
4
- export declare function harPlugin(): HvigorPlugin;
2
+ import { PluginExtension } from './framework';
3
+ export * from './framework/utils';
4
+ export * from './framework/extension';
5
+ export interface PluginParam {
6
+ extensions?: PluginExtension[];
7
+ }
8
+ export declare function hapPlugin(options?: PluginParam): HvigorPlugin;
9
+ export declare function hspPlugin(options?: PluginParam): HvigorPlugin;
10
+ export declare function harPlugin(options?: PluginParam): HvigorPlugin;