@hadss/hmrouter-plugin 1.1.0-beta.0 → 1.2.0-beta.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 +308 -67
  2. package/dist/HMRouterPluginManager.d.ts +11 -0
  3. package/dist/HMRouterPluginManager.js +66 -0
  4. package/dist/Index.d.ts +14 -3
  5. package/dist/Index.js +58 -110
  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 +68 -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 +15 -0
  49. package/dist/hmrouter_extension/HMRouterExtension.js +102 -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 +51 -0
  56. package/dist/hmrouter_extension/analyzer/RouterAnalyzer.d.ts +12 -0
  57. package/dist/hmrouter_extension/analyzer/RouterAnalyzer.js +87 -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 +7 -0
  79. package/dist/hmrouter_extension/constants/PluginConstants.js +10 -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 +96 -0
  108. package/dist/hmrouter_extension/processor/ObfuscationProcessor.d.ts +12 -0
  109. package/dist/hmrouter_extension/processor/ObfuscationProcessor.js +118 -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 +117 -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 +3 -2
  118. package/template/customBuilder.ejs +1 -0
  119. package/template/viewBuilder.ejs +2 -1
  120. package/template/viewBuilder_v1.ejs +104 -0
package/README.md CHANGED
@@ -2,89 +2,123 @@
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. **支持插件扩展**,开发者可以在HMRouterPlugin的编译中进行自定义解析
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.2.0-beta.0" // 使用最新版本
13
25
  },
14
26
  // ...其余配置
15
27
  }
16
28
  ```
17
29
 
18
- 2.在模块中引入路由编译插件,修改`hvigorfile.ts`
30
+ 修改工程根目录下的`hvigorfile.ts`,使用路由编译插件
19
31
 
20
32
  ```typescript
21
- import { hapTasks } from "@ohos/hvigor-ohos-plugin";
22
- import { hapPlugin } from "@hadss/hmrouter-plugin";
33
+ // 工程根目录/hvigorfile.ts
34
+ import { appTasks } from '@ohos/hvigor-ohos-plugin';
35
+ import { appPlugin } from "@hadss/hmrouter-plugin";
23
36
 
24
37
  export default {
25
- system: hapTasks,
26
- plugins: [hapPlugin()], // 使用HMRouter框架中标签的模块均需要配置,与模块类型保持一致
38
+ system: appTasks,
39
+ plugins: [appPlugin({ ignoreModuleNames: [ /** 不需要扫描的模块 **/ ] })]
27
40
  };
28
41
  ```
29
42
 
30
- > 请使用与模块类型一致的插件方法,如果模块是 Har 则使用`harPlugin()`, 模块是 Hsp 则使用`hspPlugin()`
43
+ ### 模块级配置
31
44
 
32
- 3.项目根目录或者模块目录创建路由编译插件配置文件`hmrouter_config.json`(推荐)
45
+ 在使用HMRouter的模块中单独配置路由编译插件,修改模块目录下的`hvigorfile.ts`:
46
+
47
+ ```typescript
48
+ // entry/hvigorfile.ts entry模块的hvigorfile.ts
49
+ import { hapTasks } from '@ohos/hvigor-ohos-plugin';
50
+ import { modulePlugin } from '@hadss/hmrouter-plugin';
51
+
52
+ export default {
53
+ system: hapTasks,
54
+ plugins: [modulePlugin()] // 使用HMRouter标签的模块均需要配置,与模块类型保持一致
55
+ }
56
+ ```
57
+
58
+ ## 配置文件详解
59
+
60
+ 在项目根目录或模块目录创建`hmrouter_config.json`文件,配置插件行为:
33
61
 
34
62
  ```json5
35
63
  {
36
- // 如果不配置则扫描src/main/ets目录,对代码进行全量扫描,如果配置则数组不能为空,建议配置指定目录可缩短编译耗时
64
+ // 扫描目录配置,如果不配置则扫描src/main/ets目录
37
65
  "scanDir": [
38
66
  "src/main/ets/components",
39
67
  "src/main/ets/interceptors"
40
68
  ],
41
- // 默认为false,调试排除错误时可以改成true,不删除编译产物
69
+
70
+ // 是否保留生成文件,默认为false
42
71
  "saveGeneratedFile": false,
43
- // 默认为false,不自动配置混淆规则,只会生成hmrouter_obfuscation_rules.txt文件帮助开发者配置混淆文件;如果设置为true,会自动配置混淆规则,并删除hmrouter_obfuscation_rules.txt文件
72
+
73
+ // 是否自动配置混淆规则,默认为false
44
74
  "autoObfuscation": false,
75
+
45
76
  // 默认模板文件,不配置时使用插件内置模板
46
77
  "defaultPageTemplate": "./templates/defaultTemplate.ejs",
78
+
47
79
  // 特殊页面模版文件,匹配原则支持文件通配符
48
80
  "customPageTemplate": [
49
81
  {
50
- "srcPath": ["**/component/Home/**/*.ets"],
51
- "templatePath": "./templates/home_shopping_template.ejs"
52
- },
53
- {
54
- "srcPath": ["**/common/**/*.ets"],
55
- "templatePath": "./templates/common_template.ejs"
82
+ "srcPath": [
83
+ "**/component/Home/**/*.ets"
84
+ ],
85
+ "templatePath": "templates/home_shopping_template.ejs"
56
86
  },
57
87
  {
58
- "srcPath": ["**/live/**/*.ets"],
59
- "templatePath": "./templates/live_template.ejs"
88
+ "srcPath": [
89
+ "**/live/**/*.ets"
90
+ ],
91
+ "templatePath": "templates/live_template.ejs"
60
92
  }
61
93
  ]
62
94
  }
63
95
  ```
64
96
 
65
- ### hmrouter_config.json 配置
97
+ ### 配置选项说明
98
+
99
+ | 配置项 | 类型 | 是否必填 | 说明 |
100
+ |---------------------|---------|------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
101
+ | scanDir | array | 否 | 指定扫描当前模块路径,默认值为`src/main/ets` |
102
+ | saveGeneratedFile | boolean | 否 | 默认为 false,不保留插件自动生成的代码,如果需要保留,需要设置为 true |
103
+ | autoObfuscation | boolean | 否 | 默认为 false,不自动配置混淆规则,只会生成`hmrouter_obfuscation_rules.txt`文件帮助开发者配置混淆规则;如果设置为 true,会自动配置混淆规则,并在编译完成后删除`hmrouter_obfuscation_rules.txt`文件,详细说明请参考[混淆配置](Obfuscation.md)文档。 |
104
+ | defaultPageTemplate | string | 否 | 默认模版路径,相对于hmrouter_config.json文件,例如:`./templates/defaultTemplate.ejs` |
105
+ | customPageTemplate | object | 否 | srcPath为匹配的代码文件路径,支持通配符,templatePath为模版路径,可以实现不同的代码使用不同的模版来生成 |
66
106
 
67
- `hmrouter_config.json`文件用于配置该插件在编译期的行为
107
+ ### 配置文件读取规则
68
108
 
69
- > 配置文件读取规则为 模块 > 工程 > 默认
109
+ > 配置文件会自动合并,读取优先级为 模块 > 工程 > 默认
70
110
  >
71
- > 优先使用本模块内的配置,如果没有配置,则找模块目录的上级目录(最多找三层目录,找到则停止),若找不到则使用默认配置
111
+ > 优先使用本模块内的配置,如果没有配置,则使用工程根目录配置,若都找不到则使用默认配置
72
112
 
73
113
  > 1.0.0-rc.6 版本开始,支持混淆配置`autoObfuscation`
74
114
  >
75
115
  > 1.0.0-rc.9 版本开始,支持自定义模版配置`customPageTemplate`
76
116
 
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为模版路径,可以实现不同的代码使用不同的模版来生成 |
84
-
85
117
  ## 自定义模板使用
86
118
 
87
- 在 `HMRouterPlugin` 中,EJS模板用于生成动态页面或组件,你可以在模板文件中使用 EJS 的语法
119
+ 在 `HMRouterPlugin` 中,EJS模板用于生成动态页面或组件,可以在模板文件中使用 EJS 的语法
120
+
121
+ [自定义模板使用场景](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
122
 
89
123
  > EJS语法可阅读参考[官网链接](https://github.com/mde/ejs)
90
124
 
@@ -116,30 +150,79 @@ export struct <%= componentName %>Generated {
116
150
 
117
151
  ### 默认模板介绍
118
152
 
119
- 插件默认会根据如下模板来生成`NavDestination`页面代码,如有自定义模板的需求,建议先阅读内置模板的介绍在做更改,书写自定义模板时建议在内置模板基础上添加代码,删除内置模板相关代码可能会导致**编译失败、生命周期生效、转场动画失效等问题**。
153
+ 插件默认模板根据HMRouter版本不同有两种实现:
120
154
 
121
- **默认模版`viewBuilder.ejs`:**
155
+ #### 1.1.0及以上版本
156
+
157
+ 1.1.0及以上版本使用`NavDestinationHelper`类来处理页面生命周期和手势动画:
122
158
 
123
159
  ```ejs
124
- import { <%= componentName %> } from '<%= importPath %>'
125
- import { TemplateService, TranslateOption, ScaleOption, OpacityOption } from '@hadss/hmrouter'
160
+ // auto-generated <%= componentName %>Builder.ets by HMRouter
161
+ import <%if(isDefaultExport) {%> <%= componentName %> <% } else {%> { <%= componentName %> } <% } %> from '<%= importPath %>'
162
+ import { NavDestinationHelper } from '@hadss/hmrouter';
163
+
164
+ @Builder
165
+ export function <%= componentName %>Builder(name: string, param: Object) {
166
+ <%= componentName %>Generated()
167
+ }
168
+
169
+ export const <%= componentName %>BuilderWrapBuilder = wrapBuilder(<%= componentName %>Builder)
170
+
171
+ @Component
172
+ export struct <%= componentName %>Generated {
173
+ private helper: NavDestinationHelper = new NavDestinationHelper(this);
174
+
175
+ build() {
176
+ NavDestination() {
177
+ <%= componentName %>()
178
+ }
179
+ <% if(dialog){ %>.mode(NavDestinationMode.DIALOG)<% } %>
180
+ .hideTitleBar(true)
181
+ .attributeModifier(this.helper.modifier)
182
+ .gestureModifier(this.helper.gestureModifier)
183
+ .onWillAppear(()=>{this.helper.onWillAppear()})
184
+ .onAppear(() => {this.helper.onAppear()})
185
+ .onWillShow(()=>{this.helper.onWillAppear()})
186
+ .onShown(()=>{this.helper.onShown()})
187
+ .onWillHide(()=>{this.helper.onWillHide()})
188
+ .onHidden(()=>{this.helper.onHidden()})
189
+ .onWillDisappear(()=>{this.helper.onWillDisappear()})
190
+ .onDisAppear(()=>{this.helper.onDisAppear()})
191
+ .onReady((ctx)=>{this.helper.onReady(ctx)})
192
+ .onBackPressed(()=> this.helper.onBackPressed())
193
+ }
194
+ }
195
+ ```
196
+
197
+ #### 1.0.0版本
198
+
199
+ 1.0.0版本使用`TemplateService`类来处理页面生命周期和动画:
200
+
201
+ ```ejs
202
+ // auto-generated <%= componentName %>Builder.ets by HMRouter
203
+ import <%if(isDefaultExport) {%> <%= componentName %> <% } else {%> { <%= componentName %> } <% } %> from '<%= importPath %>'
204
+ import { TemplateService, TranslateOption, ScaleOption, OpacityOption, HMRouterMgr } from '@hadss/hmrouter'
126
205
 
127
206
  @Builder
128
207
  export function <%= componentName %>Builder(name: string, param: Object) {
129
208
  <%= componentName %>Generated()
130
209
  }
131
210
 
211
+ export const <%= componentName %>BuilderWrapBuilder = wrapBuilder(<%= componentName %>Builder)
212
+
132
213
  @Component
133
214
  export struct <%= componentName %>Generated {
134
215
  @State translateOption: TranslateOption = new TranslateOption()
135
216
  @State scaleOption: ScaleOption = new ScaleOption()
136
217
  @State opacityOption: OpacityOption = new OpacityOption()
137
- private pageUrl: string = '<%= pageUrl %>'
218
+ private pageUrl: string = ''
138
219
  private ndId: string = ''
139
220
  private navigationId: string = ''
140
221
 
141
222
  aboutToAppear(): void {
142
223
  this.navigationId = this.queryNavigationInfo()!.navigationId;
224
+ const allPathName = HMRouterMgr.getPathStack(this.navigationId)?.getAllPathName();
225
+ this.pageUrl = allPathName ? allPathName[allPathName.length - 1] : '';
143
226
  TemplateService.aboutToAppear(this.navigationId, this.pageUrl, <%= dialog %>,
144
227
  this.translateOption, this.scaleOption, this.opacityOption)
145
228
  }
@@ -164,6 +247,9 @@ export struct <%= componentName %>Generated {
164
247
  .onActionEnd((event: GestureEvent) =>{
165
248
  TemplateService.interactiveFinish(this.navigationId, this.ndId, event)
166
249
  })
250
+ .onActionCancel(() =>{
251
+ TemplateService.interactiveCancel(this.navigationId, this.ndId)
252
+ })
167
253
  )
168
254
  .translate(this.translateOption)
169
255
  .scale(this.scaleOption)
@@ -203,6 +289,23 @@ export struct <%= componentName %>Generated {
203
289
  }
204
290
  ```
205
291
 
292
+ ### 版本差异说明
293
+
294
+ 1. **NavDestinationHelper vs TemplateService**:
295
+ - 1.1.0版本中,`TemplateService`被`NavDestinationHelper`类替代
296
+ - `NavDestinationHelper`采用实例化方式使用,而`TemplateService`则采用静态方法调用
297
+ - `NavDestinationHelper`通过`modifier`和`gestureModifier`简化了动画和手势处理
298
+
299
+ 2. **生命周期处理**:
300
+ - 1.1.0版本使用更简洁的代理模式处理生命周期回调
301
+ - 1.0.0版本需要手动调用每个静态方法并传递参数
302
+
303
+ 3. **动画处理**:
304
+ - 1.1.0版本通过`attributeModifier`和`gestureModifier`处理动画
305
+ - 1.0.0版本需要手动管理`TranslateOption`、`ScaleOption`和`OpacityOption`
306
+
307
+ > 注意:自定义模板时,请确保使用的API版本与HMRouter版本匹配。1.1.0及以上版本应使用NavDestinationHelper,1.0.0版本应使用TemplateService。
308
+
206
309
  ### 模板变量
207
310
 
208
311
  | 属性 | 描述 |
@@ -211,40 +314,116 @@ export struct <%= componentName %>Generated {
211
314
  | importPath | 原组件的导入路径 |
212
315
  | componentName | 原组件名 |
213
316
  | 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
- 最低版本
317
+
318
+ ### 文件通配符说明
319
+
320
+ - `*`:匹配任意数量的字符,不包括目录分隔符
321
+ - `**`:匹配任意数量的字符,包括目录分隔符
322
+ - `?`:匹配单个字符
323
+ - `[abc]`:匹配方括号中的任意一个字符
324
+ - `[a-z]`:匹配a到z范围内的任意一个字符
325
+ - `{a,b,c}`:匹配花括号中的任意一个模式
326
+ - `!`:否定模式,排除匹配的文件
327
+
328
+ ### 匹配优先级
329
+
330
+ - customPageTemplate > defaultPageTemplate > 库中携带的模板
331
+ - 如遇到customPageTemplate中的多个srcPath都能匹配上,优先取前面的模板文件
332
+
333
+ ## 插件扩展
334
+
335
+ HMRouterPlugin提供了完善的扩展机制,允许开发者在插件执行的不同阶段注入自定义逻辑。通过继承`PluginExtension`抽象类并实现特定方法来扩展功能。
336
+
337
+ ### 编写扩展类
338
+
339
+ 创建一个继承自`PluginExtension`的类,并实现需要的生命周期方法:
340
+
341
+ ```typescript
342
+ // MyCustomExtension.ts
343
+ import { PluginExtension, ExtensionContext } from '@hadss/hmrouter-plugin';
344
+ import { SourceFile } from 'ts-morph';
345
+
346
+ export class MyCustomExtension extends PluginExtension {
347
+ // 可选:扩展名称,主要用于调试日志
348
+ get name(): string {
349
+ return 'MyCustomExtension';
350
+ }
351
+
352
+ // hmrouter_extension初始化阶段后执行
353
+ afterInitialize(context: ExtensionContext): void {
354
+ console.log('Extension initialization');
355
+ // 初始化设置逻辑
356
+ }
357
+
358
+ // 注解分析阶段后执行
359
+ afterAnnotationAnalysis(sourceFile: SourceFile, filePath: string, context: ExtensionContext): void {
360
+ // 在源文件上执行分析逻辑
361
+ console.log(`Analyzing file: ${filePath}`);
362
+ // 可以提取信息并添加到上下文
363
+ const results = [
364
+ // 分析结果...
365
+ ];
366
+
367
+ // 将结果添加到上下文
368
+ context.addAnalyzeResults(results);
369
+ }
370
+
371
+ // 代码生成阶段后执行
372
+ afterCodeGeneration(context: ExtensionContext): void {
373
+ // 自定义代码生成逻辑
374
+ console.log('Custom code generation');
375
+ }
376
+
377
+ // 其它阶段方法...
378
+ // afterRouterMapBuilding
379
+ // afterConfigUpdate
380
+ // afterObfuscationProcess
381
+ // afterResourceProcess
382
+ // afterCompletion
383
+ }
384
+ ```
385
+
386
+ > 如需要进行自定义解析,请将`ts-morph`加入到`hvigor-config.json`依赖中
387
+
388
+ ### 注册扩展
389
+ ```typescript
390
+ import { appTasks } from '@ohos/hvigor-ohos-plugin';
391
+ import { registerPluginExtension } from '@hadss/hmrouter-plugin';
392
+ import { MyCustomExtension } from './MyCustomExtension';
393
+ import { appPlugin } from "@hadss/hmrouter-plugin";
394
+
395
+ // 注册扩展实例
396
+ registerPluginExtension(new MyCustomExtension());
397
+
398
+ export default {
399
+ system: appTasks,
400
+ plugins: [appPlugin({ ignoreModuleNames: [] })]
401
+ };
402
+ ```
403
+
404
+ > 更多使用介绍请访问[插件扩展](https://gitcode.com/openharmony-sig/ohrouter/blob/master/docs/PluginExtension.md)
405
+
406
+ ## 编译产物
407
+
408
+ HMRouter编译插件生成的文件包括:
409
+
410
+ 1. **路由表文件**:`src/main/resources/base/profile/hm_router_map.json`,包含所有页面的路由信息
411
+ 2. **rawfile资源文件**:`src/main/resources/rawfile/hm_router_map.json`, 包含所有页面的路由信息与本次编译中包含是hsp模块包名信息
412
+ 3. **页面代码文件**:`src/main/ets/generated/HM{PageName}{Hash}.ets`,为每个页面生成的NavDestination包装代码, 命名格式为`HM + 组件名 + pageUrl的hash值`
413
+ 4. **混淆规则文件**:`hmrouter_obfuscation_rules.txt`,包含需要保留的类名
414
+
415
+ 在`hmrouter_config.json`中设置`saveGeneratedFile: true`可保留所有编译产物
416
+
417
+ ## 系统版本要求
418
+
419
+ 最低版本:
241
420
 
242
421
  ```text
243
422
  DevEco Studio 5.0.3.700及以上
244
423
  hvigor 5.5.1及以上
245
424
  ```
246
425
 
247
- **推荐使用**
426
+ **推荐使用**:
248
427
 
249
428
  ```text
250
429
  DevEco Studio 5.0.3.800及以上
@@ -257,6 +436,68 @@ hvigor 5.7.3及以上
257
436
  > [HMRouterPlugin] Your DevEco Studio version less than 5.0.3.800, may cause remote hsp dependencies get failed
258
437
  > ```
259
438
 
439
+ ## 更新日志
440
+
441
+ ### 1.2.0-beta.0 (2025.06.17)
442
+
443
+ - 新增插件扩展能力
444
+ - 修复了路由表生成与hvigor编译流程冲突的问题
445
+
446
+ ### 1.1.0-beta.0 (2025.04.11)
447
+
448
+ - 更新了自定义模版,支持原生组件接入
449
+
450
+ ### 1.0.0-rc.9 (2024.10.09)
451
+
452
+ - 支持自定义页面模版配置`customPageTemplate`
453
+
454
+ ### 1.0.0-rc.6 (2024.09.27)
455
+
456
+ - 新增自动混淆配置参数`autoObfuscation`,开启可以自动配置HMRouter混淆白名单
457
+
458
+ ## 常见问题
459
+
460
+ ### 编译失败,提示重复的routerMap对象名称
461
+
462
+ ```
463
+ hvigorERROR: Duplicate 'routerMap' object names detected.
464
+
465
+ * Try the following:
466
+ > 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.
467
+ {"home":["BootView"]}
468
+ ```
469
+
470
+ 解决方案:
471
+ 1. 检查是否有多个页面使用了相同的pageUrl
472
+ 2. 升级DevEco Studio版本,已知Build Version: 5.0.3.600版本会偶现该问题
473
+ 3. 清理项目并重新编译
474
+
475
+ ### 编译成功但找不到生成的文件
476
+
477
+ 解决方案:
478
+ 1. 在`hmrouter_config.json`中设置`saveGeneratedFile: true`,保留生成文件
479
+ 2. 检查生成的文件是否正确,通常位于`src/main/ets/generated`目录
480
+ 3. 确认编译插件配置正确,每个使用HMRouter的模块都需要配置对应的插件
481
+
482
+ ### 多target问题
483
+
484
+ 出现编译报错`The "to" argument must be of type string. Received undefined`
485
+
486
+ 解决方案:
487
+ 1. 确保一个`target`对应一个`product`,不要出现多余的`target`定义
488
+ 2. 不要有一个模块中多个`target`给一个`product`的情况
489
+ 3. 遵循官方说明:同一个module的不同target不能打包到同一个product中
490
+
491
+ ## 使用建议
492
+
493
+ 1. **指定扫描目录**:配置`scanDir`,减少编译时间
494
+ 2. **开发阶段保留生成文件**:设置`saveGeneratedFile: true`,方便排查问题
495
+ 3. **启用自动混淆配置**:设置`autoObfuscation: true`,自动处理混淆规则
496
+ 4. **使用自定义模板**:根据项目需求,自定义页面模板
497
+ 5. **模块类型与插件类型匹配**:确保使用正确的插件类型
498
+ 6. **定期清理生成文件**:在发布前,设置`saveGeneratedFile: false`,清理生成文件
499
+ 7. **注意版本兼容性**:确保自定义模板与HMRouter版本匹配
500
+
260
501
  ## HMRouter路由框架使用
261
502
 
262
503
  详见[@hadss/hmrouter](https://ohpm.openharmony.cn/#/cn/detail/@hadss%2Fhmrouter)
@@ -0,0 +1,11 @@
1
+ import { HvigorNode } from '@ohos/hvigor';
2
+ import { PluginParam } from './Index';
3
+ export declare class HMRouterPluginManager {
4
+ private static instance;
5
+ private controllerMap;
6
+ private moduleNodes;
7
+ private constructor();
8
+ static getInstance(): HMRouterPluginManager;
9
+ registerPlugin(node: HvigorNode, pluginId: string, options: PluginParam): void;
10
+ addModuleNode(node: HvigorNode): void;
11
+ }
@@ -0,0 +1,66 @@
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.controllerMap = new Map();
11
+ this.moduleNodes = [];
12
+ framework_1.Logger.info('', 'HMRouterPlugin starting...');
13
+ (0, framework_1.registerPluginExtension)(new HMRouterExtension_1.HMRouterDefaultExtension());
14
+ hvigor_1.hvigor.nodesEvaluated(() => {
15
+ this.moduleNodes.forEach((node) => {
16
+ if (this.controllerMap.has(node.getNodeName()))
17
+ return;
18
+ node.getAllPluginIds().forEach((pluginId) => {
19
+ this.registerPlugin(node, pluginId, {});
20
+ });
21
+ });
22
+ for (const [_key, controller] of this.controllerMap) {
23
+ controller.start();
24
+ }
25
+ });
26
+ hvigor_1.hvigor.buildFinished(() => {
27
+ this.controllerMap.forEach((controller) => {
28
+ controller.complete();
29
+ });
30
+ HMRouterPluginManager.instance = null;
31
+ framework_1.PluginStore.destroy();
32
+ framework_1.ExtensionManager.destroy();
33
+ this.controllerMap.clear();
34
+ framework_1.TsAstUtil.clearProject();
35
+ });
36
+ }
37
+ static getInstance() {
38
+ if (!HMRouterPluginManager.instance) {
39
+ HMRouterPluginManager.instance = new HMRouterPluginManager();
40
+ }
41
+ return HMRouterPluginManager.instance;
42
+ }
43
+ registerPlugin(node, pluginId, options) {
44
+ const moduleContext = node.getContext(pluginId);
45
+ if (!moduleContext) {
46
+ throw PluginError_1.PluginError.create(PluginError_1.ErrorCode.ERROR_CONFIG, node.getNodeName(), node.getNodePath());
47
+ }
48
+ const controller = new framework_1.PluginExecutionController(node, moduleContext, options.extensions);
49
+ this.controllerMap.set(node.getNodeName(), controller);
50
+ if ((0, framework_1.isHspModule)(node)) {
51
+ let packageJsonPath = framework_1.PluginFileUtil.pathResolve(node.getNodePath(), 'oh-package.json5');
52
+ let packageJsonContent = framework_1.PluginFileUtil.readFileSync(packageJsonPath);
53
+ let packageJson = JSON.parse(packageJsonContent.toString());
54
+ let hspNameArr = framework_1.PluginStore.getInstance().get('hspModuleNames');
55
+ if (!hspNameArr) {
56
+ framework_1.PluginStore.getInstance().set('hspModuleNames', []);
57
+ }
58
+ framework_1.PluginStore.getInstance().get('hspModuleNames')?.push(packageJson.name);
59
+ }
60
+ }
61
+ addModuleNode(node) {
62
+ this.moduleNodes.push(node);
63
+ }
64
+ }
65
+ exports.HMRouterPluginManager = HMRouterPluginManager;
66
+ HMRouterPluginManager.instance = null;
package/dist/Index.d.ts CHANGED
@@ -1,4 +1,15 @@
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 interface AppPluginParam {
9
+ ignoreModuleNames: string[];
10
+ }
11
+ export declare function hapPlugin(options?: PluginParam): HvigorPlugin;
12
+ export declare function hspPlugin(options?: PluginParam): HvigorPlugin;
13
+ export declare function harPlugin(options?: PluginParam): HvigorPlugin;
14
+ export declare function modulePlugin(options?: PluginParam): HvigorPlugin;
15
+ export declare function appPlugin(options: AppPluginParam): HvigorPlugin;