@hadss/hmrouter-plugin 1.0.0-rc.9 → 1.1.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.
@@ -4,7 +4,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.HMRouterPluginHandle = void 0;
7
- const hvigor_1 = require("@ohos/hvigor");
8
7
  const hvigor_ohos_plugin_1 = require("@ohos/hvigor-ohos-plugin");
9
8
  const HMRouterPluginConfig_1 = require("./HMRouterPluginConfig");
10
9
  const HMRouterHvigorPlugin_1 = require("./HMRouterHvigorPlugin");
@@ -13,12 +12,14 @@ const CommonConstants_1 = __importDefault(require("./constants/CommonConstants")
13
12
  const TaskConstants_1 = __importDefault(require("./constants/TaskConstants"));
14
13
  const FileUtil_1 = __importDefault(require("./utils/FileUtil"));
15
14
  const ObfuscationUtil_1 = __importDefault(require("./utils/ObfuscationUtil"));
15
+ const PluginStore_1 = __importDefault(require("./store/PluginStore"));
16
16
  class HMRouterPluginHandle {
17
17
  constructor(node, moduleContext) {
18
18
  this.node = node;
19
19
  this.moduleContext = moduleContext;
20
20
  this.config = this.readConfig();
21
21
  this.appContext = this.node.getParentNode()?.getContext(hvigor_ohos_plugin_1.OhosPluginId.OHOS_APP_PLUGIN);
22
+ PluginStore_1.default.getInstance().projectFilePath = this.appContext.getProjectPath();
22
23
  this.plugin = new HMRouterHvigorPlugin_1.HMRouterHvigorPlugin(this.config);
23
24
  }
24
25
  start() {
@@ -29,6 +30,8 @@ class HMRouterPluginHandle {
29
30
  name: targetName + TaskConstants_1.default.PLUGIN_TASK,
30
31
  run: () => {
31
32
  this.taskExec();
33
+ this.plugin.scanFiles = [];
34
+ this.plugin.routerMap = [];
32
35
  },
33
36
  dependencies: [targetName + TaskConstants_1.default.PRE_BUILD],
34
37
  postDependencies: [targetName + TaskConstants_1.default.MERGE_PROFILE]
@@ -58,8 +61,15 @@ class HMRouterPluginHandle {
58
61
  return;
59
62
  }
60
63
  let obfuscationFilePath = FileUtil_1.default.pathResolve(this.config.modulePath, CommonConstants_1.default.OBFUSCATION_FILE_NAME);
64
+ let routerMap = JSON.parse(FileUtil_1.default.readFileSync(this.config.getRouterMapDir()).toString()).routerMap;
65
+ let obfuscationString = ObfuscationUtil_1.default.buildObfuscatedStrings(routerMap);
61
66
  FileUtil_1.default.ensureFileSync(obfuscationFilePath);
62
- FileUtil_1.default.writeFileSync(obfuscationFilePath, ObfuscationUtil_1.default.buildObfuscatedStrings(this.plugin.routerMap));
67
+ FileUtil_1.default.writeFileSync(obfuscationFilePath, obfuscationString);
68
+ if (this.moduleContext.getModuleType() === CommonConstants_1.default.HAR_MODULE_NAME) {
69
+ let consumerRulesPath = FileUtil_1.default.pathResolve(this.config.modulePath, CommonConstants_1.default.CONSUMER_FILE_NAME);
70
+ FileUtil_1.default.ensureFileSync(consumerRulesPath);
71
+ FileUtil_1.default.writeFileSync(consumerRulesPath, obfuscationString);
72
+ }
63
73
  Logger_1.Logger.info('Generate obfuscation rule file successfully, filePath:', obfuscationFilePath);
64
74
  }
65
75
  isEnableObfuscation(buildProfileOpt) {
@@ -67,25 +77,40 @@ class HMRouterPluginHandle {
67
77
  let buildOption = buildProfileOpt.buildOptionSet?.find((item) => {
68
78
  return item.name == currentBuildMode;
69
79
  });
70
- if (buildOption) {
71
- let ruleOptions = this.ensureNestedObject(buildOption, ['arkOptions', 'obfuscation', 'ruleOptions']);
72
- if (this.config.autoObfuscation && ruleOptions.enable) {
73
- let files = this.ensureNestedObject(buildOption, ['arkOptions', 'obfuscation', 'ruleOptions', 'files']);
74
- let obfuscationFilePath = CommonConstants_1.default.CURRENT_DELIMITER +
75
- CommonConstants_1.default.OBFUSCATION_FILE_NAME;
76
- if (typeof files === 'string') {
77
- ruleOptions.files = [files, obfuscationFilePath];
80
+ if (!buildOption)
81
+ return false;
82
+ let ruleOptions = this.ensureNestedObject(buildOption, ['arkOptions', 'obfuscation', 'ruleOptions']);
83
+ if (this.config.autoObfuscation && ruleOptions.enable) {
84
+ let files = this.ensureNestedObject(buildOption, ['arkOptions', 'obfuscation', 'ruleOptions', 'files']);
85
+ let obfuscationFilePath = CommonConstants_1.default.CURRENT_DELIMITER +
86
+ CommonConstants_1.default.OBFUSCATION_FILE_NAME;
87
+ if (typeof files === 'string') {
88
+ ruleOptions.files = [files, obfuscationFilePath];
89
+ }
90
+ else if (Array.isArray(files)) {
91
+ files.push(obfuscationFilePath);
92
+ }
93
+ else {
94
+ ruleOptions.files = obfuscationFilePath;
95
+ }
96
+ if (this.moduleContext.getModuleType() === CommonConstants_1.default.HAR_MODULE_NAME) {
97
+ let consumerFiles = this.ensureNestedObject(buildOption, ['arkOptions', 'obfuscation', 'consumerFiles']);
98
+ let consumerRulesPath = CommonConstants_1.default.CURRENT_DELIMITER +
99
+ CommonConstants_1.default.CONSUMER_FILE_NAME;
100
+ if (typeof consumerFiles === 'string') {
101
+ this.ensureNestedObject(buildOption, ['arkOptions', 'obfuscation']).consumerFiles =
102
+ [consumerFiles, consumerRulesPath];
103
+ }
104
+ else if (Array.isArray(consumerFiles)) {
105
+ consumerFiles.push(consumerRulesPath);
78
106
  }
79
107
  else {
80
- files.push(obfuscationFilePath);
108
+ this.ensureNestedObject(buildOption, ['arkOptions', 'obfuscation']).consumerFiles = consumerRulesPath;
81
109
  }
82
- this.moduleContext.setBuildProfileOpt(buildProfileOpt);
83
110
  }
84
- return ruleOptions.enable;
85
- }
86
- else {
87
- return false;
111
+ this.moduleContext.setBuildProfileOpt(buildProfileOpt);
88
112
  }
113
+ return ruleOptions.enable;
89
114
  }
90
115
  copyRouterMapToRawFileTask(buildOutputPath, targetName) {
91
116
  let routerMapFilePath = FileUtil_1.default.pathResolve(buildOutputPath, CommonConstants_1.default.TEMP_ROUTER_MAP_PATH, targetName, CommonConstants_1.default.ROUTER_MAP_NAME);
@@ -94,36 +119,33 @@ class HMRouterPluginHandle {
94
119
  FileUtil_1.default.copyFileSync(routerMapFilePath, rawFilePath);
95
120
  }
96
121
  writeHspModuleName() {
97
- if (this.node.getAllPluginIds().includes(hvigor_ohos_plugin_1.OhosPluginId.OHOS_HAP_PLUGIN)) {
98
- let rawFilePath = this.config.getRawFilePath();
99
- let rawFileRouterMap = JSON.parse(FileUtil_1.default.readFileSync(rawFilePath).toString());
100
- rawFileRouterMap.hspModuleNames = [];
101
- hvigor_1.hvigor.getAllNodes().forEach((node) => {
102
- let pluginIds = node.getAllPluginIds();
103
- if (pluginIds.includes(hvigor_ohos_plugin_1.OhosPluginId.OHOS_HSP_PLUGIN)) {
104
- let packageJson = FileUtil_1.default.readJson5(FileUtil_1.default.pathResolve(node.getNodePath(), 'oh-package.json5'));
105
- rawFileRouterMap.hspModuleNames.push(packageJson.name);
106
- }
107
- try {
108
- pluginIds.forEach((id) => {
109
- let context = node.getContext(id);
110
- let signedHspObj = context.getOhpmRemoteHspDependencyInfo(true);
111
- let remoteHspObj = context.getOhpmRemoteHspDependencyInfo(false);
112
- for (const key in signedHspObj) {
113
- rawFileRouterMap.hspModuleNames.push(signedHspObj[key].name);
114
- }
115
- for (const key in remoteHspObj) {
116
- rawFileRouterMap.hspModuleNames.push(remoteHspObj[key].name);
117
- }
118
- });
122
+ if (!this.node.getAllPluginIds().includes(hvigor_ohos_plugin_1.OhosPluginId.OHOS_HAP_PLUGIN))
123
+ return;
124
+ let rawFilePath = this.config.getRawFilePath();
125
+ let rawFileRouterMap = JSON.parse(FileUtil_1.default.readFileSync(rawFilePath).toString());
126
+ rawFileRouterMap.hspModuleNames = [...new Set(PluginStore_1.default.getInstance().hspModuleNames)];
127
+ rawFileRouterMap.hspModuleNames.push(...this.getRemoteHspModuleNames(this.node, this.node.getAllPluginIds()));
128
+ FileUtil_1.default.writeFileSync(rawFilePath, JSON.stringify(rawFileRouterMap));
129
+ }
130
+ getRemoteHspModuleNames(node, pluginIds) {
131
+ let remoteHspModuleNames = [];
132
+ try {
133
+ pluginIds.forEach((id) => {
134
+ let context = node.getContext(id);
135
+ let signedHspObj = context.getOhpmRemoteHspDependencyInfo(true);
136
+ let remoteHspObj = context.getOhpmRemoteHspDependencyInfo(false);
137
+ for (const key in signedHspObj) {
138
+ remoteHspModuleNames.push(signedHspObj[key].name);
119
139
  }
120
- catch (error) {
121
- Logger_1.Logger.warn('Your DevEco Studio version less than 5.0.3.800, may cause remote hsp dependencies get failed');
140
+ for (const key in remoteHspObj) {
141
+ remoteHspModuleNames.push(remoteHspObj[key].name);
122
142
  }
123
143
  });
124
- rawFileRouterMap.hspModuleNames = [...new Set(rawFileRouterMap.hspModuleNames)];
125
- FileUtil_1.default.writeFileSync(rawFilePath, JSON.stringify(rawFileRouterMap));
126
144
  }
145
+ catch (error) {
146
+ Logger_1.Logger.warn('Your DevEco Studio version less than 5.0.3.800, may cause remote hsp dependencies get failed');
147
+ }
148
+ return remoteHspModuleNames;
127
149
  }
128
150
  taskExec() {
129
151
  let startTime = Date.now();
@@ -141,7 +163,9 @@ class HMRouterPluginHandle {
141
163
  let routerMapFileName = moduleJsonOpt.module.routerMap.split(':')[1];
142
164
  let routerMapFilePath = this.config.getModuleRouterMapFilePath(routerMapFileName);
143
165
  let routerMapObj = FileUtil_1.default.readJson5(routerMapFilePath);
144
- this.plugin.routerMap.unshift(...routerMapObj[CommonConstants_1.default.ROUTER_MAP_KEY]);
166
+ if (routerMapFileName !== 'hm_router_map') {
167
+ this.plugin.routerMap.unshift(...routerMapObj[CommonConstants_1.default.ROUTER_MAP_KEY]);
168
+ }
145
169
  }
146
170
  this.plugin.generateRouterMap();
147
171
  moduleJsonOpt.module.routerMap = CommonConstants_1.default.MODULE_ROUTER_MAP_NAME;
@@ -169,19 +193,21 @@ class HMRouterPluginHandle {
169
193
  });
170
194
  }
171
195
  readConfig() {
172
- let levels = 0;
173
- let configParam = {};
174
- let configFilePath = '';
175
- while (levels < 4) {
176
- configFilePath = FileUtil_1.default.pathResolve(this.node.getNodePath(), CommonConstants_1.default.PARENT_DELIMITER.repeat(levels) + CommonConstants_1.default.CONFIG_FILE_NAME);
177
- if (FileUtil_1.default.exist(configFilePath)) {
178
- configParam = FileUtil_1.default.readJson5(configFilePath);
179
- break;
180
- }
181
- levels++;
196
+ let configParam;
197
+ let configFilePath;
198
+ let configDir;
199
+ configFilePath = FileUtil_1.default.pathResolve(this.node.getNodePath(), CommonConstants_1.default.CONFIG_FILE_NAME);
200
+ if (!FileUtil_1.default.exist(configFilePath)) {
201
+ configFilePath =
202
+ FileUtil_1.default.pathResolve(PluginStore_1.default.getInstance().projectFilePath, CommonConstants_1.default.CONFIG_FILE_NAME);
203
+ }
204
+ if (FileUtil_1.default.exist(configFilePath)) {
205
+ configParam = FileUtil_1.default.readJson5(configFilePath);
206
+ }
207
+ else {
208
+ configParam = {};
182
209
  }
183
- let configDir = configFilePath.split(CommonConstants_1.default.FILE_SEPARATOR).slice(0, -1)
184
- .join(CommonConstants_1.default.FILE_SEPARATOR);
210
+ configDir = FileUtil_1.default.pathResolve(configFilePath, CommonConstants_1.default.PARENT_DELIMITER);
185
211
  return new HMRouterPluginConfig_1.HMRouterPluginConfig(this.node.getNodeName(), this.node.getNodePath(), configDir, configParam);
186
212
  }
187
213
  ensureNestedObject(obj, path) {
package/dist/Index.js CHANGED
@@ -3,9 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.hapPlugin = hapPlugin;
7
- exports.hspPlugin = hspPlugin;
8
- exports.harPlugin = harPlugin;
6
+ exports.harPlugin = exports.hspPlugin = exports.hapPlugin = void 0;
9
7
  const hvigor_1 = require("@ohos/hvigor");
10
8
  const hvigor_ohos_plugin_1 = require("@ohos/hvigor-ohos-plugin");
11
9
  const Logger_1 = require("./common/Logger");
@@ -13,6 +11,7 @@ const CommonConstants_1 = __importDefault(require("./constants/CommonConstants")
13
11
  const TsAstUtil_1 = require("./utils/TsAstUtil");
14
12
  const FileUtil_1 = __importDefault(require("./utils/FileUtil"));
15
13
  const HMRouterPluginHandle_1 = require("./HMRouterPluginHandle");
14
+ const PluginStore_1 = __importDefault(require("./store/PluginStore"));
16
15
  class HMRouterPluginMgr {
17
16
  constructor() {
18
17
  this.hmRouterPluginSet = new Set();
@@ -46,10 +45,13 @@ class HMRouterPluginMgr {
46
45
  throw new Error('moduleContext is null');
47
46
  }
48
47
  let pluginHandle = new HMRouterPluginHandle_1.HMRouterPluginHandle(node, moduleContext);
48
+ let packageJson = FileUtil_1.default.readJson5(FileUtil_1.default.pathResolve(node.getNodePath(), 'oh-package.json5'));
49
49
  if (pluginId === hvigor_ohos_plugin_1.OhosPluginId.OHOS_HAP_PLUGIN) {
50
- let packageJson = FileUtil_1.default.readJson5(FileUtil_1.default.pathResolve(node.getNodePath(), 'oh-package.json5'));
51
50
  pluginHandle.config.moduleName = packageJson.name;
52
51
  }
52
+ if (pluginId === hvigor_ohos_plugin_1.OhosPluginId.OHOS_HSP_PLUGIN) {
53
+ PluginStore_1.default.getInstance().hspModuleNames.push(packageJson.name);
54
+ }
53
55
  this.hmRouterPluginSet.add(pluginHandle);
54
56
  }
55
57
  deleteGeneratorFile(pluginHandle) {
@@ -79,7 +81,12 @@ class HMRouterPluginMgr {
79
81
  let obfuscationFilePath = pluginHandle.config.getObfuscationFilePath();
80
82
  if (FileUtil_1.default.exist(obfuscationFilePath)) {
81
83
  FileUtil_1.default.unlinkSync(obfuscationFilePath);
82
- Logger_1.Logger.info(pluginHandle.config.modulePath + ' delete obfuscation hmrouter_obfuscation_rules.txt');
84
+ Logger_1.Logger.info(pluginHandle.config.modulePath + ' delete obfuscation hmrouter-obfuscation-rules.txt');
85
+ }
86
+ let consumerRulesPath = pluginHandle.config.getConsumerRulesFilePath();
87
+ if (FileUtil_1.default.exist(consumerRulesPath)) {
88
+ FileUtil_1.default.unlinkSync(consumerRulesPath);
89
+ Logger_1.Logger.info(pluginHandle.config.modulePath + ' delete hmrouter-consumer-rules.txt');
83
90
  }
84
91
  }
85
92
  }
@@ -95,6 +102,7 @@ function hapPlugin() {
95
102
  }
96
103
  };
97
104
  }
105
+ exports.hapPlugin = hapPlugin;
98
106
  function hspPlugin() {
99
107
  return {
100
108
  pluginId: CommonConstants_1.default.HSP_PLUGIN_ID,
@@ -106,6 +114,7 @@ function hspPlugin() {
106
114
  }
107
115
  };
108
116
  }
117
+ exports.hspPlugin = hspPlugin;
109
118
  function harPlugin() {
110
119
  return {
111
120
  pluginId: CommonConstants_1.default.HAR_PLUGIN_ID,
@@ -117,3 +126,4 @@ function harPlugin() {
117
126
  }
118
127
  };
119
128
  }
129
+ exports.harPlugin = harPlugin;
@@ -27,7 +27,7 @@ var PluginError;
27
27
  PluginError["ERR_ERROR_CONFIG"] = "ERR_INIT_COMPONENT";
28
28
  PluginError["ERR_NOT_EMPTY_STRING"] = "ERR_INIT_NOT_READY";
29
29
  PluginError["ERR_INVALID_STRING_VALUE"] = "ERR_INVALID_STRING_VALUE";
30
- })(PluginError || (exports.PluginError = PluginError = {}));
30
+ })(PluginError = exports.PluginError || (exports.PluginError = {}));
31
31
  const DEFINED_ERROR = new Map();
32
32
  DEFINED_ERROR.set(PluginError.ERR_DUPLICATE_NAME, {
33
33
  errorCode: 40000001,
@@ -4,6 +4,7 @@ export interface BaseAnalyzeResult {
4
4
  module?: string;
5
5
  annotation?: string;
6
6
  pageSourceFile?: string;
7
+ isDefaultExport?: boolean;
7
8
  }
8
9
  export interface HMRouterResult extends BaseAnalyzeResult {
9
10
  pageUrl?: any;
@@ -12,6 +13,7 @@ export interface HMRouterResult extends BaseAnalyzeResult {
12
13
  interceptors?: string[];
13
14
  animator?: string;
14
15
  lifecycle?: string;
16
+ useNavDst?: boolean;
15
17
  }
16
18
  export interface HMAnimatorResult extends BaseAnalyzeResult {
17
19
  animatorName?: string;
@@ -37,7 +39,8 @@ export declare class TemplateModel {
37
39
  componentName: string;
38
40
  dialog: boolean;
39
41
  generatorViewName: string;
40
- constructor(pageUrl: string, importPath: string, componentName: string, dialog: boolean, generatorViewName: string);
42
+ isDefaultExport?: boolean;
43
+ constructor(pageUrl: string, importPath: string, componentName: string, dialog: boolean, generatorViewName: string, isDefaultExport?: boolean);
41
44
  }
42
45
  export declare class RouterInfo {
43
46
  name: string;
@@ -2,7 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RouterInfo = exports.TemplateModel = void 0;
4
4
  class TemplateModel {
5
- constructor(pageUrl, importPath, componentName, dialog, generatorViewName) {
5
+ constructor(pageUrl, importPath, componentName, dialog, generatorViewName, isDefaultExport) {
6
+ this.isDefaultExport = isDefaultExport;
6
7
  this.pageUrl = pageUrl;
7
8
  this.importPath = importPath;
8
9
  this.componentName = componentName;
@@ -3,10 +3,9 @@ export default class HMRouterPluginConstant {
3
3
  static readonly INTERCEPTOR_PREFIX = "__interceptor__";
4
4
  static readonly LIFECYCLE_PREFIX = "__lifecycle__";
5
5
  static readonly SERVICE_PREFIX = "__service__";
6
- static readonly PAGE_URL_PREFIX = "";
7
- static readonly OBFUSCATION_FILE_NAME = "hmrouter_obfuscation_rules.txt";
8
- static readonly VARIABLE_SEPARATOR = "__";
9
- static readonly PAGE_URL = "pageUrl";
6
+ static readonly SERVICE_PROVIDE_PREFIX = "__service_provider__";
7
+ static readonly OBFUSCATION_FILE_NAME = "hmrouter-obfuscation-rules.txt";
8
+ static readonly CONSUMER_FILE_NAME = "hmrouter-consumer-rules.txt";
10
9
  static readonly ROUTER_MAP_KEY = "routerMap";
11
10
  static readonly FILE_SEPARATOR: "\\" | "/";
12
11
  static readonly DELIMITER = "/";
@@ -15,20 +14,26 @@ export default class HMRouterPluginConstant {
15
14
  static readonly TEMP_ROUTER_MAP_PATH = "../../intermediates/router_map";
16
15
  static readonly RAWFILE_DIR = "src/main/resources/rawfile/hm_router_map.json";
17
16
  static readonly VIEW_NAME_PREFIX = "HM";
18
- static readonly VIEW_NAME_SUFFIX = ".ets";
17
+ static readonly ETS_SUFFIX = ".ets";
19
18
  static readonly JSON_SUFFIX = ".json";
20
19
  static readonly HAP_PLUGIN_ID = "HAP_HMROUTER_PLUGIN";
21
20
  static readonly HSP_PLUGIN_ID = "HSP_HMROUTER_PLUGIN";
22
21
  static readonly HAR_PLUGIN_ID = "HAR_HMROUTER_PLUGIN";
22
+ static readonly HAR_MODULE_NAME = "har";
23
23
  static readonly ROUTER_ANNOTATION = "HMRouter";
24
24
  static readonly ANIMATOR_ANNOTATION = "HMAnimator";
25
25
  static readonly INTERCEPTOR_ANNOTATION = "HMInterceptor";
26
26
  static readonly LIFECYCLE_ANNOTATION = "HMLifecycle";
27
27
  static readonly SERVICE_ANNOTATION = "HMService";
28
+ static readonly SERVICE_PROVIDE_ANNOTATION = "HMServiceProvider";
28
29
  static readonly CONFIG_FILE_NAME = "hmrouter_config.json";
29
30
  static readonly PARENT_DELIMITER = "../";
30
31
  static readonly CURRENT_DELIMITER = "./";
31
32
  static readonly LINE_BREAK = "\n";
32
33
  static readonly KEEP_FILE_NAME = "-keep-file-name";
33
34
  static readonly KEEP_PROPERTY_NAME = "-keep-property-name";
35
+ static readonly KEEP_GLOBAL_NAME = "-keep-global-name";
36
+ static readonly OH_MODULE_PATH = "oh_modules";
37
+ static readonly WARP_BUILDER = "WrapBuilder";
38
+ static readonly CUSTOM_BUILDER_TEMPLATE = "template/customBuilder.ejs";
34
39
  }
@@ -6,14 +6,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const path_1 = __importDefault(require("path"));
7
7
  class HMRouterPluginConstant {
8
8
  }
9
+ exports.default = HMRouterPluginConstant;
9
10
  HMRouterPluginConstant.ANIMATOR_PREFIX = '__animator__';
10
11
  HMRouterPluginConstant.INTERCEPTOR_PREFIX = '__interceptor__';
11
12
  HMRouterPluginConstant.LIFECYCLE_PREFIX = '__lifecycle__';
12
13
  HMRouterPluginConstant.SERVICE_PREFIX = '__service__';
13
- HMRouterPluginConstant.PAGE_URL_PREFIX = '';
14
- HMRouterPluginConstant.OBFUSCATION_FILE_NAME = 'hmrouter_obfuscation_rules.txt';
15
- HMRouterPluginConstant.VARIABLE_SEPARATOR = '__';
16
- HMRouterPluginConstant.PAGE_URL = 'pageUrl';
14
+ HMRouterPluginConstant.SERVICE_PROVIDE_PREFIX = '__service_provider__';
15
+ HMRouterPluginConstant.OBFUSCATION_FILE_NAME = 'hmrouter-obfuscation-rules.txt';
16
+ HMRouterPluginConstant.CONSUMER_FILE_NAME = 'hmrouter-consumer-rules.txt';
17
17
  HMRouterPluginConstant.ROUTER_MAP_KEY = 'routerMap';
18
18
  HMRouterPluginConstant.FILE_SEPARATOR = path_1.default.sep;
19
19
  HMRouterPluginConstant.DELIMITER = '/';
@@ -22,20 +22,25 @@ HMRouterPluginConstant.ROUTER_MAP_NAME = 'hm_router_map.json';
22
22
  HMRouterPluginConstant.TEMP_ROUTER_MAP_PATH = '../../intermediates/router_map';
23
23
  HMRouterPluginConstant.RAWFILE_DIR = 'src/main/resources/rawfile/hm_router_map.json';
24
24
  HMRouterPluginConstant.VIEW_NAME_PREFIX = 'HM';
25
- HMRouterPluginConstant.VIEW_NAME_SUFFIX = '.ets';
25
+ HMRouterPluginConstant.ETS_SUFFIX = '.ets';
26
26
  HMRouterPluginConstant.JSON_SUFFIX = '.json';
27
27
  HMRouterPluginConstant.HAP_PLUGIN_ID = 'HAP_HMROUTER_PLUGIN';
28
28
  HMRouterPluginConstant.HSP_PLUGIN_ID = 'HSP_HMROUTER_PLUGIN';
29
29
  HMRouterPluginConstant.HAR_PLUGIN_ID = 'HAR_HMROUTER_PLUGIN';
30
+ HMRouterPluginConstant.HAR_MODULE_NAME = 'har';
30
31
  HMRouterPluginConstant.ROUTER_ANNOTATION = 'HMRouter';
31
32
  HMRouterPluginConstant.ANIMATOR_ANNOTATION = 'HMAnimator';
32
33
  HMRouterPluginConstant.INTERCEPTOR_ANNOTATION = 'HMInterceptor';
33
34
  HMRouterPluginConstant.LIFECYCLE_ANNOTATION = 'HMLifecycle';
34
35
  HMRouterPluginConstant.SERVICE_ANNOTATION = 'HMService';
36
+ HMRouterPluginConstant.SERVICE_PROVIDE_ANNOTATION = 'HMServiceProvider';
35
37
  HMRouterPluginConstant.CONFIG_FILE_NAME = 'hmrouter_config.json';
36
38
  HMRouterPluginConstant.PARENT_DELIMITER = '../';
37
39
  HMRouterPluginConstant.CURRENT_DELIMITER = './';
38
40
  HMRouterPluginConstant.LINE_BREAK = '\n';
39
41
  HMRouterPluginConstant.KEEP_FILE_NAME = '-keep-file-name';
40
42
  HMRouterPluginConstant.KEEP_PROPERTY_NAME = '-keep-property-name';
41
- exports.default = HMRouterPluginConstant;
43
+ HMRouterPluginConstant.KEEP_GLOBAL_NAME = '-keep-global-name';
44
+ HMRouterPluginConstant.OH_MODULE_PATH = 'oh_modules';
45
+ HMRouterPluginConstant.WARP_BUILDER = 'WrapBuilder';
46
+ HMRouterPluginConstant.CUSTOM_BUILDER_TEMPLATE = 'template/customBuilder.ejs';
@@ -2,10 +2,11 @@ export default class ConfigConstants {
2
2
  static readonly DEFAULT_SCAN_DIR = "src/main/ets";
3
3
  static readonly DEFAULT_ROUTER_MAP_DIR = "src/main/resources/base/profile";
4
4
  static readonly DEFAULT_BUILD_DIR = "src/main/ets/generated";
5
- static readonly DEFAULT_BUILD_TPL = "viewBuilder.ejs";
5
+ static readonly DEFAULT_BUILD_TPL = "template/viewBuilder.ejs";
6
6
  static readonly ROUTER_ANNOTATION = "HMRouter";
7
7
  static readonly ANIMATOR_ANNOTATION = "HMAnimator";
8
8
  static readonly INTERCEPTOR_ANNOTATION = "HMInterceptor";
9
9
  static readonly LIFECYCLE_ANNOTATION = "HMLifecycle";
10
10
  static readonly SERVICE_ANNOTATION = "HMService";
11
+ static readonly SERVICE_PROVIDE_ANNOTATION = "HMServiceProvider";
11
12
  }
@@ -2,14 +2,15 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  class ConfigConstants {
4
4
  }
5
+ exports.default = ConfigConstants;
5
6
  ConfigConstants.DEFAULT_SCAN_DIR = 'src/main/ets';
6
7
  ConfigConstants.DEFAULT_ROUTER_MAP_DIR = 'src/main/resources/base/profile';
7
8
  ConfigConstants.DEFAULT_BUILD_DIR = 'src/main/ets/generated';
8
- ConfigConstants.DEFAULT_BUILD_TPL = 'viewBuilder.ejs';
9
+ ConfigConstants.DEFAULT_BUILD_TPL = 'template/viewBuilder.ejs';
9
10
  ConfigConstants.ROUTER_ANNOTATION = 'HMRouter';
10
11
  ConfigConstants.ANIMATOR_ANNOTATION = 'HMAnimator';
11
12
  ConfigConstants.INTERCEPTOR_ANNOTATION = 'HMInterceptor';
12
13
  ConfigConstants.LIFECYCLE_ANNOTATION = 'HMLifecycle';
13
14
  ConfigConstants.SERVICE_ANNOTATION = 'HMService';
14
- exports.default = ConfigConstants;
15
+ ConfigConstants.SERVICE_PROVIDE_ANNOTATION = 'HMServiceProvider';
15
16
  ;
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  class TaskConstants {
4
4
  }
5
+ exports.default = TaskConstants;
5
6
  TaskConstants.PRE_BUILD = '@PreBuild';
6
7
  TaskConstants.MERGE_PROFILE = '@MergeProfile';
7
8
  TaskConstants.PROCESS_ROUTER_MAP = '@ProcessRouterMap';
@@ -9,5 +10,4 @@ TaskConstants.PROCESS_RESOURCE = '@ProcessResource';
9
10
  TaskConstants.PLUGIN_TASK = '@HMRouterPluginTask';
10
11
  TaskConstants.COPY_ROUTER_MAP_TASK = '@HMRouterCopyRouterMapToRawFileTask';
11
12
  TaskConstants.GENERATE_OBFUSCATION_TASK = '@HMRouterGenerateObfuscationFileTask';
12
- exports.default = TaskConstants;
13
13
  ;
@@ -0,0 +1,14 @@
1
+ export default class PluginStore implements IPluginStore {
2
+ private static instance;
3
+ variableCache: Map<string, Map<string, string[]>>;
4
+ projectFilePath: string;
5
+ hspModuleNames: string[];
6
+ static clear(): void;
7
+ static getInstance(): IPluginStore;
8
+ }
9
+ interface IPluginStore {
10
+ variableCache: Map<string, Map<string, string[]>>;
11
+ projectFilePath: string;
12
+ hspModuleNames: string[];
13
+ }
14
+ export {};
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class PluginStore {
4
+ constructor() {
5
+ this.variableCache = new Map();
6
+ this.projectFilePath = '';
7
+ this.hspModuleNames = [];
8
+ }
9
+ static clear() {
10
+ this.instance = null;
11
+ }
12
+ static getInstance() {
13
+ if (!this.instance) {
14
+ this.instance = new PluginStore();
15
+ }
16
+ return this.instance;
17
+ }
18
+ }
19
+ exports.default = PluginStore;
20
+ PluginStore.instance = null;
@@ -1,3 +1,5 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
1
3
  import { FileUtil } from '@ohos/hvigor';
2
4
  import fs from 'fs';
3
5
  export default class HMFileUtil extends FileUtil {
@@ -19,8 +19,14 @@ class ObfuscationUtil {
19
19
  }).map((routerInfo) => {
20
20
  return routerInfo.customData.functionName;
21
21
  }))];
22
+ let watchFunctionName = [...new Set(routerMap.filter((routerInfo) => {
23
+ return routerInfo.buildFunction;
24
+ }).map((routerInfo) => {
25
+ return routerInfo.buildFunction + CommonConstants_1.default.WARP_BUILDER;
26
+ }))];
22
27
  return CommonConstants_1.default.KEEP_FILE_NAME + CommonConstants_1.default.LINE_BREAK +
23
- srcPathArr.concat(CommonConstants_1.default.KEEP_PROPERTY_NAME, classNameArr, functionName)
28
+ srcPathArr.concat(CommonConstants_1.default.KEEP_PROPERTY_NAME, functionName)
29
+ .concat(CommonConstants_1.default.KEEP_GLOBAL_NAME, classNameArr, watchFunctionName)
24
30
  .join(CommonConstants_1.default.LINE_BREAK);
25
31
  }
26
32
  }
@@ -1,8 +1,9 @@
1
1
  import { Project, SourceFile } from 'ts-morph';
2
2
  export declare let project: Project | null;
3
- export declare const importVariableCache: Map<string, string>;
4
3
  export declare class TsAstUtil {
5
- static getSourceFile(filePath: string): SourceFile | null;
4
+ static getSourceFile(filePath: string): SourceFile;
6
5
  static clearProject(): void;
7
6
  static parseConstantValue(sourceFile: SourceFile, variableName: string, propertyName?: string): string;
7
+ static parseCrossModuleVariable(scanDir: string): Map<string, string[]>;
8
+ private static getExportedVariables;
8
9
  }
@@ -1,15 +1,22 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TsAstUtil = exports.importVariableCache = exports.project = void 0;
3
+ exports.TsAstUtil = exports.project = void 0;
4
4
  const ts_morph_1 = require("ts-morph");
5
5
  const hvigor_1 = require("@ohos/hvigor");
6
6
  const Logger_1 = require("../common/Logger");
7
7
  exports.project = null;
8
- exports.importVariableCache = new Map();
9
8
  class TsAstUtil {
10
9
  static getSourceFile(filePath) {
11
10
  if (!hvigor_1.FileUtil.exist(filePath)) {
12
- return null;
11
+ Logger_1.Logger.warn(`File not found: ${filePath}`);
12
+ const normalizedPath = filePath.replace(/\.+\/+/g, './');
13
+ if (normalizedPath !== filePath && hvigor_1.FileUtil.exist(normalizedPath)) {
14
+ Logger_1.Logger.info(`Using normalized path instead: ${normalizedPath}`);
15
+ filePath = normalizedPath;
16
+ }
17
+ else {
18
+ throw new Error(`File not found: ${filePath}`);
19
+ }
13
20
  }
14
21
  if (!exports.project) {
15
22
  exports.project = new ts_morph_1.Project({
@@ -24,24 +31,24 @@ class TsAstUtil {
24
31
  static parseConstantValue(sourceFile, variableName, propertyName) {
25
32
  let result;
26
33
  if (propertyName) {
27
- let classInstance = sourceFile.getClasses().filter((classes) => {
34
+ let classInstance = sourceFile.getClasses().find((classes) => {
28
35
  return classes.getName() === variableName;
29
36
  });
30
- if (classInstance.length === 0) {
37
+ if (!classInstance) {
31
38
  throw new Error(`Unknown class '${variableName}'`);
32
39
  }
33
- let property = classInstance[0].getProperties().filter((properties) => {
40
+ let property = classInstance.getProperties().find((properties) => {
34
41
  return properties.getName() === propertyName;
35
42
  });
36
- if (property.length === 0) {
43
+ if (!property) {
37
44
  throw new Error(`Unknown property '${propertyName}'`);
38
45
  }
39
- result = property[0].getInitializer();
46
+ result = property.getInitializer();
40
47
  }
41
48
  else {
42
- let constant = sourceFile.getVariableDeclarations().filter(declaration => {
49
+ let constant = sourceFile.getVariableDeclarations().find(declaration => {
43
50
  return declaration.getName() === variableName;
44
- })[0];
51
+ });
45
52
  if (!constant) {
46
53
  throw new Error(`Unknown constant '${variableName}'`);
47
54
  }
@@ -49,9 +56,42 @@ class TsAstUtil {
49
56
  }
50
57
  if (result.getKind() !== ts_morph_1.SyntaxKind.StringLiteral) {
51
58
  Logger_1.Logger.error(Logger_1.PluginError.ERR_INVALID_STRING_VALUE, variableName);
52
- throw new Error('Invalid constant value');
59
+ throw new Error('Invalid constants value, only string literal is supported, variableName:' + variableName);
53
60
  }
54
61
  return result.asKind(ts_morph_1.SyntaxKind.StringLiteral)?.getLiteralValue();
55
62
  }
63
+ static parseCrossModuleVariable(scanDir) {
64
+ let sourceFiles = exports.project.addSourceFilesAtPaths(`${scanDir}/**/*.ets`);
65
+ const exportMap = new Map();
66
+ for (let sourceFile of sourceFiles) {
67
+ const exportedNames = this.getExportedVariables(sourceFile);
68
+ if (exportedNames.length > 0) {
69
+ exportMap.set(sourceFile.getFilePath(), exportedNames);
70
+ }
71
+ }
72
+ return exportMap;
73
+ }
74
+ static getExportedVariables(sourceFile) {
75
+ const exportSymbols = [];
76
+ let exportKeywordNodes = sourceFile.getDescendantsOfKind(ts_morph_1.SyntaxKind.ExportKeyword);
77
+ exportKeywordNodes.forEach((node) => {
78
+ let parentNodeKind = node.getParent()?.getKind();
79
+ switch (parentNodeKind) {
80
+ case ts_morph_1.SyntaxKind.VariableStatement:
81
+ let variableStatement = node.getParent()?.asKind(ts_morph_1.SyntaxKind.VariableStatement);
82
+ let variableNames = variableStatement.getDeclarationList().getDeclarations().map((declaration) => {
83
+ return declaration.getName();
84
+ });
85
+ exportSymbols.push(...variableNames);
86
+ break;
87
+ case ts_morph_1.SyntaxKind.ClassDeclaration:
88
+ let classDeclaration = node.getParent();
89
+ let className = classDeclaration?.asKind(ts_morph_1.SyntaxKind.ClassDeclaration)?.getName();
90
+ exportSymbols.push(className);
91
+ break;
92
+ }
93
+ });
94
+ return exportSymbols;
95
+ }
56
96
  }
57
97
  exports.TsAstUtil = TsAstUtil;