UNPKG

@hadss/hmrouter-plugin

Version:

HMRouter Compiler Plugin

87 lines (86 loc) 4.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ResourceProcessProcessor = void 0; const framework_1 = require("../../framework"); const constants_1 = require("../constants"); class ResourceProcessProcessor { constructor(context) { this.rawFileContent = ''; this.context = context; } execute() { framework_1.Logger.debug(this.context.node.getNodeName(), `Start to copy router map to raw file...`); const isHar = (0, framework_1.isHarModule)(this.context.node); if ((isHar && !this.context.routerMap) || (isHar && this.context.routerMap.length === 0)) { framework_1.Logger.info(this.context.node.getNodeName(), 'Skip copy router map'); return; } this.copyRouterMapToRawFile(); this.processHspModuleName(); } copyRouterMapToRawFile() { let routerMapFilePath = framework_1.PluginFileUtil.pathResolve(this.context.currentTarget?.getBuildTargetOutputPath(), constants_1.FilePathConstants.TEMP_ROUTER_MAP_PATH, this.context.currentTarget?.getTargetName(), constants_1.RouterMapConstants.ROUTER_MAP_NAME); let rawFilePath = this.context.config.getRawFilePath(); framework_1.PluginFileUtil.ensureFileSync(rawFilePath); if (framework_1.PluginFileUtil.exist(routerMapFilePath)) { this.rawFileContent = framework_1.PluginFileUtil.readFileSync(routerMapFilePath).toString(); } } processHspModuleName() { let rawFilePath = this.context.config.getRawFilePath(); const oldcontent = framework_1.PluginFileUtil.readFileSync(rawFilePath).toString(); let content = this.rawFileContent; if (!content) { framework_1.Logger.warn(this.context.node.getNodeName(), `Raw file is empty, filePath: ${rawFilePath}`); return; } if ((0, framework_1.isHapModule)(this.context.node)) { let rawFileRouterMap = JSON.parse(content); rawFileRouterMap.hspModuleNames = [...new Set(framework_1.PluginStore.getInstance().get('hspModuleNames'))]; rawFileRouterMap.hspModuleNames.push(...this.getRemoteHspModuleNames(this.context.node.getAllPluginIds())); let hspModulesInfo = framework_1.PluginStore.getInstance().get('hspModulesInfo'); if (hspModulesInfo && hspModulesInfo.length > 0) { rawFileRouterMap.hspModulesInfo = hspModulesInfo; } else { rawFileRouterMap.hspModulesInfo = []; } let remoteHspModuleNames = this.getRemoteHspModuleNames(this.context.node.getAllPluginIds()); for (const remoteName of remoteHspModuleNames) { rawFileRouterMap.hspModulesInfo.push({ moduleName: remoteName, packageName: remoteName, }); } content = JSON.stringify(rawFileRouterMap); } if (oldcontent !== content) { framework_1.Logger.debug(this.context.node.getNodeName(), `Raw file content has been updated, filePath: ${rawFilePath}`); framework_1.PluginFileUtil.writeFileSync(rawFilePath, content); } else { framework_1.Logger.debug(this.context.node.getNodeName(), `Raw file is not changed, filePath: ${rawFilePath}`); } } getRemoteHspModuleNames(pluginIds) { let remoteHspModuleNames = []; try { pluginIds.forEach((id) => { let context = this.context.node.getContext(id); let signedHspObj = context.getOhpmRemoteHspDependencyInfo(true); let remoteHspObj = context.getOhpmRemoteHspDependencyInfo(false); for (const key in signedHspObj) { remoteHspModuleNames.push(signedHspObj[key].name); } for (const key in remoteHspObj) { remoteHspModuleNames.push(remoteHspObj[key].name); } }); } catch (error) { framework_1.Logger.warn(this.context.node.getNodeName(), 'Your DevEco Studio version less than 5.0.3.800, may cause remote hsp dependencies get failed'); } return remoteHspModuleNames; } } exports.ResourceProcessProcessor = ResourceProcessProcessor;