@hadss/hmrouter-plugin 1.2.0-beta.2 → 1.2.0-rc.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.
package/README.md CHANGED
@@ -21,7 +21,7 @@ HMRouter编译插件是一个基于hvigor的编译插件,用于:
21
21
  ```json5
22
22
  {
23
23
  "dependencies": {
24
- "@hadss/hmrouter-plugin": "^1.2.0-beta.0" // 使用最新版本
24
+ "@hadss/hmrouter-plugin": "^1.2.0-rc.0" // 使用最新版本
25
25
  },
26
26
  // ...其余配置
27
27
  }
@@ -438,6 +438,10 @@ hvigor 5.7.3及以上
438
438
 
439
439
  ## 更新日志
440
440
 
441
+ ### 1.2.0-rc.0 (2025.09.02)
442
+
443
+ - [修复多target工程下编译app的问题](https://gitcode.com/openharmony-sig/ohrouter/issues/131)
444
+
441
445
  ### 1.2.0-beta.1 (2025.07.29)
442
446
 
443
447
  - 修复相同组件名称冲突的问题
@@ -5,4 +5,5 @@ export declare class TaskConstants {
5
5
  static readonly PLUGIN_TASK = "@HMRouterPluginTask";
6
6
  static readonly COPY_ROUTER_MAP_TASK = "@HMRouterCopyRouterMapToRawFileTask";
7
7
  static readonly GENERATE_OBFUSCATION_TASK = "@HMRouterGenerateObfuscationFileTask";
8
+ static readonly DEFAULT_SOURCE_ROOT = "./src/main";
8
9
  }
@@ -10,3 +10,4 @@ TaskConstants.PROCESS_RESOURCE = '@ProcessResource';
10
10
  TaskConstants.PLUGIN_TASK = '@HMRouterPluginTask';
11
11
  TaskConstants.COPY_ROUTER_MAP_TASK = '@HMRouterCopyRouterMapToRawFileTask';
12
12
  TaskConstants.GENERATE_OBFUSCATION_TASK = '@HMRouterGenerateObfuscationFileTask';
13
+ TaskConstants.DEFAULT_SOURCE_ROOT = './src/main';
@@ -6,7 +6,6 @@ export declare class PluginExecutionController {
6
6
  private readonly moduleContext;
7
7
  private readonly taskManager;
8
8
  private readonly baseContext;
9
- private extensionContextCache;
10
9
  private originalBuilderDir?;
11
10
  constructor(node: HvigorNode, moduleContext: OhosModuleContext, moduleExtensions?: PluginExtension[]);
12
11
  start(): void;
@@ -14,11 +13,7 @@ export declare class PluginExecutionController {
14
13
  private initializeContext;
15
14
  private registerHvigorTasks;
16
15
  private filterScanFilesForTarget;
17
- private backupScanFiles;
18
- private restoreScanFiles;
19
- private backupConfig;
20
- private restoreConfig;
21
- private adjustConfigForTarget;
16
+ private getAbsoluteSourceRoots;
22
17
  private registerMainPluginTask;
23
18
  private registerObfuscationTask;
24
19
  private registerCopyRouterMapTask;
@@ -8,7 +8,6 @@ const extension_1 = require("../extension");
8
8
  const utils_1 = require("../utils");
9
9
  class PluginExecutionController {
10
10
  constructor(node, moduleContext, moduleExtensions) {
11
- this.extensionContextCache = new Map();
12
11
  this.node = node;
13
12
  this.moduleContext = moduleContext;
14
13
  this.baseContext = this.initializeContext();
@@ -29,94 +28,54 @@ class PluginExecutionController {
29
28
  registerHvigorTasks() {
30
29
  this.moduleContext.targets((target) => {
31
30
  const targetName = target.getTargetName();
32
- this.taskManager.context.currentTarget = target;
33
- this.registerMainPluginTask(targetName);
31
+ this.registerMainPluginTask(target);
34
32
  this.registerObfuscationTask(targetName);
35
33
  if ((0, utils_1.isHapModule)(this.node) || (0, utils_1.isHspModule)(this.node)) {
36
- this.registerCopyRouterMapTask(targetName);
34
+ this.registerCopyRouterMapTask(target);
37
35
  }
38
36
  });
39
37
  }
40
- filterScanFilesForTarget() {
41
- const currentTargetName = this.taskManager.context.currentTarget?.getTargetName();
42
- if (!currentTargetName) {
43
- return;
44
- }
38
+ filterScanFilesForTarget(currentTargetName) {
45
39
  const buildProfileTargets = this.moduleContext.getBuildProfileOpt().targets || [];
46
40
  const targetOpt = buildProfileTargets.find((target) => target.name === currentTargetName);
41
+ let absoluteSourceRoots;
47
42
  if (!targetOpt?.source?.sourceRoots || targetOpt.source.sourceRoots.length === 0) {
48
- return;
43
+ absoluteSourceRoots = this.getAbsoluteSourceRoots([constants_1.TaskConstants.DEFAULT_SOURCE_ROOT]);
49
44
  }
50
- const moduleRoot = this.moduleContext.getModulePath();
51
- const absoluteSourceRoots = targetOpt.source.sourceRoots.map((sourceRoot) => {
52
- const absolutePath = utils_1.PluginFileUtil.resolve(moduleRoot, sourceRoot);
53
- const normalizedPath = utils_1.PluginFileUtil.normalize(absolutePath);
54
- return normalizedPath.endsWith(utils_1.PluginFileUtil.sep) ? normalizedPath : normalizedPath + utils_1.PluginFileUtil.sep;
55
- });
56
- this.baseContext.scanFiles = this.baseContext.scanFiles.filter((filePath) => {
45
+ else {
46
+ if (!targetOpt.source.sourceRoots.find((value) => value === constants_1.TaskConstants.DEFAULT_SOURCE_ROOT)) {
47
+ targetOpt.source.sourceRoots.push(constants_1.TaskConstants.DEFAULT_SOURCE_ROOT);
48
+ }
49
+ absoluteSourceRoots = this.getAbsoluteSourceRoots(targetOpt.source.sourceRoots);
50
+ }
51
+ const masterFiles = this.baseContext.getModuleScanFiles();
52
+ return masterFiles.filter((filePath) => {
57
53
  const normalizedFilePath = utils_1.PluginFileUtil.normalize(filePath);
58
54
  return absoluteSourceRoots.some((sourceRootPath) => {
59
55
  return normalizedFilePath.startsWith(sourceRootPath);
60
56
  });
61
57
  });
62
58
  }
63
- backupScanFiles() {
64
- return [...this.baseContext.scanFiles];
65
- }
66
- restoreScanFiles(backup) {
67
- this.baseContext.scanFiles = backup;
68
- }
69
- backupConfig() {
70
- const hmrouterContext = this.taskManager.context;
71
- if (hmrouterContext.config && hmrouterContext.config.builderDir) {
72
- this.originalBuilderDir = hmrouterContext.config.builderDir;
73
- }
74
- }
75
- restoreConfig() {
76
- if (this.originalBuilderDir) {
77
- const hmrouterContext = this.taskManager.context;
78
- if (hmrouterContext.config) {
79
- hmrouterContext.config.builderDir = this.originalBuilderDir;
80
- }
81
- this.originalBuilderDir = undefined;
82
- }
83
- }
84
- adjustConfigForTarget() {
85
- const currentTargetName = this.taskManager.context.currentTarget?.getTargetName();
86
- if (!currentTargetName) {
87
- return;
88
- }
89
- const buildProfileTargets = this.moduleContext.getBuildProfileOpt().targets || [];
90
- const targetOpt = buildProfileTargets.find((target) => target.name === currentTargetName);
91
- if (!targetOpt?.source?.sourceRoots || targetOpt.source.sourceRoots.length === 0) {
92
- return;
93
- }
94
- const hmrouterContext = this.taskManager.context;
95
- if (!hmrouterContext.config || !hmrouterContext.config.builderDir) {
96
- return;
97
- }
98
- const firstSourceRoot = targetOpt.source.sourceRoots[0];
99
- const cleanSourceRoot = firstSourceRoot.replace(/^\.\//, '');
100
- hmrouterContext.config.builderDir = `${cleanSourceRoot}/ets/generated`;
59
+ getAbsoluteSourceRoots(sourceRoots) {
60
+ const moduleRoot = this.moduleContext.getModulePath();
61
+ return sourceRoots.map((sourceRoot) => {
62
+ const absolutePath = utils_1.PluginFileUtil.resolve(moduleRoot, sourceRoot);
63
+ const normalizedPath = utils_1.PluginFileUtil.normalize(absolutePath);
64
+ return normalizedPath.endsWith(utils_1.PluginFileUtil.sep) ? normalizedPath : normalizedPath + utils_1.PluginFileUtil.sep;
65
+ });
101
66
  }
102
- registerMainPluginTask(targetName) {
67
+ registerMainPluginTask(target) {
68
+ const targetName = target.getTargetName();
103
69
  this.node.registerTask({
104
70
  name: this.getTaskName(targetName, constants_1.TaskConstants.PLUGIN_TASK),
105
71
  run: () => {
106
- const scanFilesBackup = this.backupScanFiles();
107
- try {
108
- this.backupConfig();
109
- this.filterScanFilesForTarget();
110
- this.adjustConfigForTarget();
111
- this.taskManager.executeStage(TaskStage_1.TaskStage.AFTER_ANNOTATION_ANALYSIS);
112
- this.taskManager.executeStage(TaskStage_1.TaskStage.AFTER_CODE_GENERATION);
113
- this.taskManager.executeStage(TaskStage_1.TaskStage.AFTER_ROUTER_MAP_BUILDING);
114
- this.taskManager.executeStage(TaskStage_1.TaskStage.AFTER_CONFIG_UPDATE);
115
- }
116
- finally {
117
- this.restoreConfig();
118
- this.restoreScanFiles(scanFilesBackup);
119
- }
72
+ this.taskManager.context.currentTarget = target;
73
+ const filtered = this.filterScanFilesForTarget(targetName);
74
+ this.baseContext.scanFiles = filtered;
75
+ this.taskManager.executeStage(TaskStage_1.TaskStage.AFTER_ANNOTATION_ANALYSIS);
76
+ this.taskManager.executeStage(TaskStage_1.TaskStage.AFTER_CODE_GENERATION);
77
+ this.taskManager.executeStage(TaskStage_1.TaskStage.AFTER_ROUTER_MAP_BUILDING);
78
+ this.taskManager.executeStage(TaskStage_1.TaskStage.AFTER_CONFIG_UPDATE);
120
79
  },
121
80
  dependencies: [],
122
81
  postDependencies: [this.getTaskName(targetName, constants_1.TaskConstants.PRE_BUILD)],
@@ -126,37 +85,19 @@ class PluginExecutionController {
126
85
  this.node.registerTask({
127
86
  name: this.getTaskName(targetName, constants_1.TaskConstants.GENERATE_OBFUSCATION_TASK),
128
87
  run: () => {
129
- const scanFilesBackup = this.backupScanFiles();
130
- try {
131
- this.backupConfig();
132
- this.filterScanFilesForTarget();
133
- this.adjustConfigForTarget();
134
- this.taskManager.executeStage(TaskStage_1.TaskStage.AFTER_OBFUSCATION_PROCESS);
135
- }
136
- finally {
137
- this.restoreConfig();
138
- this.restoreScanFiles(scanFilesBackup);
139
- }
88
+ this.taskManager.executeStage(TaskStage_1.TaskStage.AFTER_OBFUSCATION_PROCESS);
140
89
  },
141
90
  dependencies: [this.getTaskName(targetName, constants_1.TaskConstants.PLUGIN_TASK)],
142
91
  postDependencies: [this.getTaskName(targetName, constants_1.TaskConstants.PRE_BUILD)],
143
92
  });
144
93
  }
145
- registerCopyRouterMapTask(targetName) {
94
+ registerCopyRouterMapTask(target) {
95
+ const targetName = target.getTargetName();
146
96
  this.node.registerTask({
147
97
  name: this.getTaskName(targetName, constants_1.TaskConstants.COPY_ROUTER_MAP_TASK),
148
98
  run: () => {
149
- const scanFilesBackup = this.backupScanFiles();
150
- try {
151
- this.backupConfig();
152
- this.filterScanFilesForTarget();
153
- this.adjustConfigForTarget();
154
- this.taskManager.executeStage(TaskStage_1.TaskStage.AFTER_RESOURCE_PROCESS);
155
- }
156
- finally {
157
- this.restoreConfig();
158
- this.restoreScanFiles(scanFilesBackup);
159
- }
99
+ this.taskManager.context.currentTarget = target;
100
+ this.taskManager.executeStage(TaskStage_1.TaskStage.AFTER_RESOURCE_PROCESS);
160
101
  },
161
102
  dependencies: [this.getTaskName(targetName, constants_1.TaskConstants.PROCESS_ROUTER_MAP)],
162
103
  postDependencies: [this.getTaskName(targetName, constants_1.TaskConstants.PROCESS_RESOURCE)],
@@ -5,12 +5,13 @@ export declare class ExtensionContextImpl implements ExtensionContext {
5
5
  node: HvigorNode;
6
6
  moduleContext: OhosModuleContext;
7
7
  currentTarget?: Target;
8
- scanFiles: string[];
8
+ private moduleScanFilesMaster;
9
9
  private templateMetadata;
10
- private analyzeResults;
11
- private _currentView;
12
- private _currentFilePath?;
10
+ private perTargetState;
13
11
  constructor(node: HvigorNode, moduleContext: OhosModuleContext);
12
+ private getActiveTargetKey;
13
+ private getOrCreateState;
14
+ getModuleScanFiles(): string[];
14
15
  get currentView(): ReadonlyArray<BaseAnalyzeResult>;
15
16
  addAnalyzeResults(results: BaseAnalyzeResult | BaseAnalyzeResult[]): void;
16
17
  getAnalyzeResults<T extends BaseAnalyzeResult = BaseAnalyzeResult>(): Set<T>;
@@ -20,4 +21,16 @@ export declare class ExtensionContextImpl implements ExtensionContext {
20
21
  private validateAnalyzeResult;
21
22
  private syncToCurrentView;
22
23
  setCurrentFilePath(filePath: string): void;
24
+ get scanFiles(): string[];
25
+ set scanFiles(files: string[]);
26
+ get analyzeResults(): Set<BaseAnalyzeResult>;
27
+ set analyzeResults(v: Set<BaseAnalyzeResult>);
28
+ get _currentView(): BaseAnalyzeResult[];
29
+ set _currentView(v: BaseAnalyzeResult[]);
30
+ get _currentFilePath(): string | undefined;
31
+ set _currentFilePath(v: string | undefined);
32
+ get routerMap(): any[];
33
+ set routerMap(v: any[]);
34
+ get generatedPaths(): Map<string, string>;
35
+ set generatedPaths(v: Map<string, string>);
23
36
  }
@@ -5,24 +5,47 @@ const TemplateMetadataImpl_1 = require("./TemplateMetadataImpl");
5
5
  const utils_1 = require("../../utils");
6
6
  class ExtensionContextImpl {
7
7
  constructor(node, moduleContext) {
8
- this.scanFiles = [];
9
- this.analyzeResults = new Set();
10
- this._currentView = [];
8
+ this.moduleScanFilesMaster = [];
9
+ this.perTargetState = new Map();
11
10
  this.node = node;
12
11
  this.moduleContext = moduleContext;
13
12
  this.templateMetadata = new TemplateMetadataImpl_1.TemplateMetadataImpl();
14
13
  }
14
+ getActiveTargetKey() {
15
+ return this.currentTarget?.getTargetName() ?? '__global__';
16
+ }
17
+ getOrCreateState(key) {
18
+ let state = this.perTargetState.get(key);
19
+ if (!state) {
20
+ state = {
21
+ scanFiles: [],
22
+ analyzeResults: new Set(),
23
+ currentView: [],
24
+ currentFilePath: undefined,
25
+ routerMap: [],
26
+ generatedPaths: new Map(),
27
+ };
28
+ this.perTargetState.set(key, state);
29
+ }
30
+ return state;
31
+ }
32
+ getModuleScanFiles() {
33
+ return this.moduleScanFilesMaster;
34
+ }
15
35
  get currentView() {
16
- return this._currentView;
36
+ const key = this.getActiveTargetKey();
37
+ return this.getOrCreateState(key).currentView;
17
38
  }
18
39
  addAnalyzeResults(results) {
40
+ const key = this.getActiveTargetKey();
41
+ const state = this.getOrCreateState(key);
19
42
  if (Array.isArray(results)) {
20
43
  results.forEach((result) => {
21
44
  if (!this.validateAnalyzeResult(result)) {
22
45
  utils_1.Logger.warn(this.node.getNodeName(), `Invalid analyze result: ${JSON.stringify(result)}`);
23
46
  return;
24
47
  }
25
- this.analyzeResults.add(result);
48
+ state.analyzeResults.add(result);
26
49
  });
27
50
  }
28
51
  else {
@@ -30,12 +53,13 @@ class ExtensionContextImpl {
30
53
  utils_1.Logger.warn(this.node.getNodeName(), `Invalid analyze result: ${JSON.stringify(results)}`);
31
54
  return;
32
55
  }
33
- this.analyzeResults.add(results);
56
+ state.analyzeResults.add(results);
34
57
  }
35
58
  this.syncToCurrentView(results);
36
59
  }
37
60
  getAnalyzeResults() {
38
- return this.analyzeResults;
61
+ const key = this.getActiveTargetKey();
62
+ return this.getOrCreateState(key).analyzeResults;
39
63
  }
40
64
  getTemplateData(componentName) {
41
65
  return this.templateMetadata.getTemplateData(componentName);
@@ -50,15 +74,75 @@ class ExtensionContextImpl {
50
74
  return result.name !== undefined && result.sourceFilePath !== undefined && result.annotation !== undefined;
51
75
  }
52
76
  syncToCurrentView(results) {
53
- if (!this._currentFilePath)
77
+ const key = this.getActiveTargetKey();
78
+ const state = this.getOrCreateState(key);
79
+ if (!state.currentFilePath)
54
80
  return;
55
81
  const resultsArray = Array.isArray(results) ? results : [results];
56
- const currentFileResults = resultsArray.filter(result => result.sourceFilePath === this._currentFilePath);
57
- this._currentView.push(...currentFileResults);
82
+ const currentFileResults = resultsArray.filter(result => result.sourceFilePath === state.currentFilePath);
83
+ state.currentView.push(...currentFileResults);
58
84
  }
59
85
  setCurrentFilePath(filePath) {
60
- this._currentFilePath = filePath;
61
- this._currentView = [];
86
+ const key = this.getActiveTargetKey();
87
+ const state = this.getOrCreateState(key);
88
+ state.currentFilePath = filePath;
89
+ state.currentView = [];
90
+ }
91
+ get scanFiles() {
92
+ const key = this.getActiveTargetKey();
93
+ if (key === '__global__') {
94
+ return this.moduleScanFilesMaster;
95
+ }
96
+ return this.getOrCreateState(key).scanFiles;
97
+ }
98
+ set scanFiles(files) {
99
+ const key = this.getActiveTargetKey();
100
+ if (key === '__global__') {
101
+ this.moduleScanFilesMaster = files;
102
+ }
103
+ else {
104
+ this.getOrCreateState(key).scanFiles = files;
105
+ }
106
+ }
107
+ get analyzeResults() {
108
+ const key = this.getActiveTargetKey();
109
+ return this.getOrCreateState(key).analyzeResults;
110
+ }
111
+ set analyzeResults(v) {
112
+ const key = this.getActiveTargetKey();
113
+ this.getOrCreateState(key).analyzeResults = v;
114
+ }
115
+ get _currentView() {
116
+ const key = this.getActiveTargetKey();
117
+ return this.getOrCreateState(key).currentView;
118
+ }
119
+ set _currentView(v) {
120
+ const key = this.getActiveTargetKey();
121
+ this.getOrCreateState(key).currentView = v;
122
+ }
123
+ get _currentFilePath() {
124
+ const key = this.getActiveTargetKey();
125
+ return this.getOrCreateState(key).currentFilePath;
126
+ }
127
+ set _currentFilePath(v) {
128
+ const key = this.getActiveTargetKey();
129
+ this.getOrCreateState(key).currentFilePath = v;
130
+ }
131
+ get routerMap() {
132
+ const key = this.getActiveTargetKey();
133
+ return this.getOrCreateState(key).routerMap;
134
+ }
135
+ set routerMap(v) {
136
+ const key = this.getActiveTargetKey();
137
+ this.getOrCreateState(key).routerMap = v;
138
+ }
139
+ get generatedPaths() {
140
+ const key = this.getActiveTargetKey();
141
+ return this.getOrCreateState(key).generatedPaths;
142
+ }
143
+ set generatedPaths(v) {
144
+ const key = this.getActiveTargetKey();
145
+ this.getOrCreateState(key).generatedPaths = v;
62
146
  }
63
147
  }
64
148
  exports.ExtensionContextImpl = ExtensionContextImpl;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hadss/hmrouter-plugin",
3
- "version": "1.2.0-beta.2",
3
+ "version": "1.2.0-rc.0",
4
4
  "description": "HMRouter Compiler Plugin",
5
5
  "main": "dist/Index.js",
6
6
  "scripts": {
@@ -8,7 +8,7 @@
8
8
  "test": "cross-env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha",
9
9
  "dev": "tsc && node dist/index.js",
10
10
  "build": "tsc",
11
- "release": "tsc && npm publish --access public"
11
+ "release": "rm -rf ./dist && tsc && npm publish --access public"
12
12
  },
13
13
  "keywords": [
14
14
  "hmrouter",