UNPKG

@hadss/hmrouter-plugin

Version:

HMRouter Compiler Plugin

75 lines (74 loc) 3.77 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.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)) { framework_1.PluginFileUtil.copyFileSync(routerMapFilePath, rawFilePath); } framework_1.Logger.info(this.context.node.getNodeName(), `Copy router map to raw file successfully, filePath: ${rawFilePath}`); } processHspModuleName() { if (!(0, framework_1.isHapModule)(this.context.node)) { return; } let rawFilePath = this.context.config.getRawFilePath(); let rawFileRouterMap = JSON.parse(framework_1.PluginFileUtil.readFileSync(rawFilePath).toString()); 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, }); } framework_1.PluginFileUtil.writeFileSync(rawFilePath, JSON.stringify(rawFileRouterMap)); } 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;