@hadss/hmrouter-plugin 1.0.0-rc.5 → 1.0.0-rc.9

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.
package/README.md CHANGED
@@ -1,40 +1,258 @@
1
1
  ## 编译插件配置
2
2
 
3
- 1. 修改项目的`hvigor/hvigor-config.json`文件,加入路由编译插件
3
+ 1.修改项目的`hvigor/hvigor-config.json`文件,加入路由编译插件
4
+
4
5
  ```json5
5
6
  {
6
7
  "dependencies": {
7
- "@hadss/hmrouter-plugin": "^1.0.0-rc.4" // 使用npm仓版本号
8
+ "@hadss/hmrouter-plugin": "^1.0.0-rc.9" // 使用npm仓版本号
8
9
  },
9
10
  // ...其他配置
10
11
  }
11
12
  ```
12
13
 
13
- 2. 在模块中引入路由编译插件,修改`hvigorfile.ts`
14
+ 2.在模块中引入路由编译插件,修改`hvigorfile.ts`
15
+
14
16
  ```typescript
15
- import { hapTasks } from '@ohos/hvigor-ohos-plugin';
16
- import { hapPlugin } from '@hadss/hmrouter-plugin';
17
+ import { hapTasks } from "@ohos/hvigor-ohos-plugin";
18
+ import { hapPlugin } from "@hadss/hmrouter-plugin";
17
19
 
18
20
  export default {
19
- system: hapTasks,
20
- plugins: [hapPlugin()] // 使用HMRouter框架中标签的模块均需要配置,与模块类型保持一致
21
- }
21
+ system: hapTasks,
22
+ plugins: [hapPlugin()], // 使用HMRouter框架中标签的模块均需要配置,与模块类型保持一致
23
+ };
22
24
  ```
23
25
 
24
- > 如果模块是Har则使用`harPlugin()`, 模块是Hsp则使用`hspPlugin()`
26
+ > 请使用与模块类型一致的插件方法,如果模块是 Har 则使用`harPlugin()`, 模块是 Hsp 则使用`hspPlugin()`
27
+
28
+ 3.项目根目录或者模块目录创建路由编译插件配置文件`hmrouter_config.json`(推荐)
25
29
 
26
- 3. 项目根目录或者模块目录创建路由编译插件配置文件`hmrouter_config.json`(推荐)
27
30
  ```json5
28
31
  {
29
32
  // 如果不配置则扫描src/main/ets目录,对代码进行全量扫描,如果配置则数组不能为空,建议配置指定目录可缩短编译耗时
30
- "scanDir": ["src/main/ets/components","src/main/ets/interceptors"],
31
- "saveGeneratedFile": false, // 默认为false,调试排除错误时可以改成true,不删除编译产物
33
+ "scanDir": [
34
+ "src/main/ets/components",
35
+ "src/main/ets/interceptors"
36
+ ],
37
+ // 默认为false,调试排除错误时可以改成true,不删除编译产物
38
+ "saveGeneratedFile": false,
39
+ // 默认为false,不自动配置混淆规则,只会生成hmrouter_obfuscation_rules.txt文件帮助开发者配置混淆文件;如果设置为true,会自动配置混淆规则,并删除hmrouter_obfuscation_rules.txt文件
40
+ "autoObfuscation": false,
41
+ // 默认模板文件,不配置时使用插件内置模板
42
+ "defaultPageTemplate": "./templates/defaultTemplate.ejs",
43
+ // 特殊页面模版文件,匹配原则支持文件通配符
44
+ "customPageTemplate": [
45
+ {
46
+ "srcPath": ["**/component/Home/**/*.ets"],
47
+ "templatePath": "./templates/home_shopping_template.ejs"
48
+ },
49
+ {
50
+ "srcPath": ["**/common/**/*.ets"],
51
+ "templatePath": "./templates/common_template.ejs"
52
+ },
53
+ {
54
+ "srcPath": ["**/live/**/*.ets"],
55
+ "templatePath": "./templates/live_template.ejs"
56
+ }
57
+ ]
32
58
  }
33
59
  ```
34
60
 
61
+ ### hmrouter_config.json 配置
62
+
63
+ `hmrouter_config.json`文件用于配置该插件在编译期的行为
64
+
35
65
  > 配置文件读取规则为 模块 > 工程 > 默认
36
66
  > 优先使用本模块内的配置,如果没有配置,则找模块目录的上级目录(最多找三层目录,找到则停止),若找不到则使用默认配置
37
67
 
38
- ## HMRouter使用
68
+ > 1.0.0-rc.6 版本开始,支持混淆配置`autoObfuscation`
69
+ > 1.0.0-rc.9 版本开始,支持自定义模版配置`customPageTemplate`
70
+
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为模版路径,可以实现不同的代码使用不同的模版来生成 |
78
+
79
+ ## 自定义模板使用
80
+
81
+ 在 `HMRouterPlugin` 中,EJS模板用于生成动态页面或组件,你可以在模板文件中使用 EJS 的语法
82
+
83
+ > EJS语法可阅读参考[官网链接](https://github.com/mde/ejs)
84
+
85
+ **模板文件示例:**
86
+
87
+ ```ejs
88
+ import { <%= componentName %> } from '<%= importPath %>'
89
+
90
+ @Builder
91
+ export function <%= componentName %>Builder(name: string, param: Object) {
92
+ <%= componentName %>Generated()
93
+ }
94
+
95
+ @Component
96
+ export struct <%= componentName %>Generated {
97
+ private pageUrl: string = '<%= pageUrl %>'
98
+
99
+ build() {
100
+ NavDestination() {
101
+ <%= componentName %>()
102
+ }
103
+ <% if(dialog){ %>.mode(NavDestinationMode.DIALOG)<% } %>
104
+ .hideTitleBar(true)
105
+ }
106
+ }
107
+ ```
108
+
109
+ > 模板文件中至少需要包含`NavDestination`组件代码和相对应的`build`函数,缺少会导致编译失败或者页面白屏,插件中会内置一套默认模板,其中包含了**页面展示、生命周期注册、转场动画注册**
110
+
111
+ ### 默认模板介绍
112
+
113
+ 插件默认会根据如下模板来生成`NavDestination`页面代码,如有自定义模板的需求,建议先阅读内置模板的介绍在做更改,书写自定义模板时建议在内置模板基础上添加代码,删除内置模板相关代码可能会导致**编译失败、生命周期生效、转场动画失效等问题**。
114
+
115
+ **默认模版`viewBuilder.ejs`:**
116
+
117
+ ```ejs
118
+ import { <%= componentName %> } from '<%= importPath %>'
119
+ import { TemplateService, TranslateOption, ScaleOption, OpacityOption } from '@hadss/hmrouter'
120
+
121
+ @Builder
122
+ export function <%= componentName %>Builder(name: string, param: Object) {
123
+ <%= componentName %>Generated()
124
+ }
125
+
126
+ @Component
127
+ export struct <%= componentName %>Generated {
128
+ @State translateOption: TranslateOption = new TranslateOption()
129
+ @State scaleOption: ScaleOption = new ScaleOption()
130
+ @State opacityOption: OpacityOption = new OpacityOption()
131
+ private pageUrl: string = '<%= pageUrl %>'
132
+ private ndId: string = ''
133
+ private navigationId: string = ''
134
+
135
+ aboutToAppear(): void {
136
+ this.navigationId = this.queryNavigationInfo()!.navigationId;
137
+ TemplateService.aboutToAppear(this.navigationId, this.pageUrl, <%= dialog %>,
138
+ this.translateOption, this.scaleOption, this.opacityOption)
139
+ }
140
+
141
+ aboutToDisappear(): void {
142
+ TemplateService.aboutToDisappear(this.navigationId, this.pageUrl, this.ndId)
143
+ }
144
+
145
+ build() {
146
+ NavDestination() {
147
+ <%= componentName %>()
148
+ }
149
+ <% if(dialog){ %>.mode(NavDestinationMode.DIALOG)<% } %>
150
+ .hideTitleBar(true)
151
+ .gesture(PanGesture()
152
+ .onActionStart((event: GestureEvent) => {
153
+ TemplateService.interactiveStart(this.navigationId, this.ndId, event)
154
+ })
155
+ .onActionUpdate((event: GestureEvent) =>{
156
+ TemplateService.interactiveProgress(this.navigationId, this.ndId, event)
157
+ })
158
+ .onActionEnd((event: GestureEvent) =>{
159
+ TemplateService.interactiveFinish(this.navigationId, this.ndId, event)
160
+ })
161
+ )
162
+ .translate(this.translateOption)
163
+ .scale(this.scaleOption)
164
+ .opacity(this.opacityOption.opacity)
165
+ .onAppear(() => {
166
+ TemplateService.onAppear(this.navigationId, this.pageUrl, this.ndId)
167
+ })
168
+ .onDisAppear(() => {
169
+ TemplateService.onDisAppear(this.navigationId, this.pageUrl, this.ndId)
170
+ })
171
+ .onShown(() => {
172
+ TemplateService.onShown(this.navigationId, this.pageUrl, this.ndId)
173
+ })
174
+ .onHidden(() => {
175
+ TemplateService.onHidden(this.navigationId, this.pageUrl, this.ndId)
176
+ })
177
+ .onWillAppear(() => {
178
+ TemplateService.onWillAppear(this.navigationId, this.pageUrl)
179
+ })
180
+ .onWillDisappear(() => {
181
+ TemplateService.onWillDisappear(this.navigationId, this.pageUrl, this.ndId)
182
+ })
183
+ .onWillShow(() => {
184
+ TemplateService.onWillShow(this.navigationId, this.pageUrl, this.ndId)
185
+ })
186
+ .onWillHide(() => {
187
+ TemplateService.onWillHide(this.navigationId, this.pageUrl, this.ndId)
188
+ })
189
+ .onReady((navContext: NavDestinationContext) => {
190
+ this.ndId = navContext.navDestinationId!
191
+ TemplateService.onReady(this.navigationId, this.pageUrl, navContext)
192
+ })
193
+ .onBackPressed(() => {
194
+ return TemplateService.onBackPressed(this.navigationId, this.pageUrl, this.ndId)
195
+ })
196
+ }
197
+ }
198
+ ```
199
+
200
+ ### 模板变量
201
+
202
+ | 属性 | 描述 |
203
+ |-------------------|-----------------|
204
+ | pageUrl | 标签中配置的pageUrl的值 |
205
+ | importPath | 原组件的导入路径 |
206
+ | componentName | 原组件名 |
207
+ | 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
+ 最低版本
235
+
236
+ ```text
237
+ HarmonyOS NEXT Developer Beta5版本及以上
238
+ DevEco Studio 5.0.3.700及以上
239
+ hvigor 5.5.1及以上
240
+ ```
241
+
242
+ **推荐使用**
243
+
244
+ ```text
245
+ HarmonyOS NEXT Beta1版本及以上
246
+ DevEco Studio 5.0.3.800及以上
247
+ hvigor 5.7.3及以上
248
+ ```
249
+
250
+ > 低于DevEco Studio 5.0.3.800(hvigor 5.7.3)可能会导致远程的 HSP 中定义的 HMRouter 标签与路由表失效,会有如下 WARN 日志
251
+ >
252
+ > ```
253
+ > [HMRouterPlugin] Your DevEco Studio version less than 5.0.3.800, may cause remote hsp dependencies get failed
254
+ > ```
255
+
256
+ ## HMRouter 使用
39
257
 
40
- 详见[@hadss/hmrouter](https://ohpm.openharmony.cn/#/cn/detail/@hadss%2Fhmrouter)
258
+ 详见[@hadss/hmrouter](https://ohpm.openharmony.cn/#/cn/detail/@hadss%2Fhmrouter)
@@ -1,40 +1,8 @@
1
- import { HMRouterPluginConfig } from './HMRouterPluginConfig';
2
1
  import { AnalyzerResultLike } from './common/PluginModel';
3
- export declare class Analyzer {
4
- analyzeResultSet: Set<AnalyzerResultLike>;
5
- private readonly sourcePath;
6
- private readonly pluginConfig;
2
+ import { HMRouterPluginConfig } from './HMRouterPluginConfig';
3
+ export declare class AnalyzerController {
7
4
  private analyzeResult;
8
- private classMethodAnalyzeResult;
9
- private keywordPos;
10
- private importMap;
11
- constructor(sourcePath: string, pluginConfig: HMRouterPluginConfig);
12
- start(): void;
13
- private resolveNode;
14
- private resolveImportDeclaration;
15
- private resolveMissingDeclaration;
16
- private isDecoratedClass;
17
- private resolveClass;
18
- private resolveClassMethod;
19
- private resolveDecorator;
20
- private switchIdentifier;
21
- private resolveCallExpression;
22
- private resolveExpression;
23
- private resolveBlock;
24
- private parseAnnotation;
25
- private parseConfig;
26
- private resolvePropertyAccess;
27
- private resolveIdentifier;
28
- private resolveArrayLiteral;
29
- private resolveNumericLiteral;
30
- private resolveStringLiteral;
31
- private resolveTrueKeyword;
32
- private resolveFalseKeyword;
33
- private getSourceFile;
34
- private findConstantValueInSourceFile;
35
- private findObjectValueInSourceFile;
36
- private resolveConstantValue;
37
- private resolveObjectValue;
38
- private resolvePath;
39
- private parseVariableDeclaration;
5
+ analyzeFile(sourceFilePath: string, config: HMRouterPluginConfig): void;
6
+ parsePageUrl(): void;
7
+ getAnalyzeResultSet(): Set<AnalyzerResultLike>;
40
8
  }