UNPKG

@hadss/hmrouter-plugin

Version:

HMRouter Compiler Plugin

46 lines (45 loc) 2.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RouterMapBuildingProcessor = void 0; const framework_1 = require("../../framework"); const PluginError_1 = require("../error/PluginError"); class RouterMapBuildingProcessor { constructor(context) { this.context = context; } execute() { framework_1.Logger.debug(this.context.node.getNodeName(), `Start to build router map...`); 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 generating routerMap`); return; } this.generateRouterMapFile(); } generateRouterMapFile() { let set = new Set(); this.context.routerMap.forEach((item) => { if (set.has(item.name)) { throw PluginError_1.PluginError.create(PluginError_1.ErrorCode.DUPLICATE_NAME, this.context.config.moduleName, item.name); } set.add(item.name); }); let routerMap = { routerMap: this.context.routerMap.map((item) => { if (item.customData && item.customData.annotation) { delete item.customData.annotation; delete item.customData.sourceFilePath; delete item.customData.isDefaultExport; delete item.customData.templateData; } return item; }), }; const routerMapJsonStr = JSON.stringify(routerMap, null, 2); const routerMapFilePath = this.context.config.getRouterMapDir(); framework_1.PluginFileUtil.ensureFileSync(routerMapFilePath); framework_1.PluginFileUtil.writeFileSync(routerMapFilePath, routerMapJsonStr); framework_1.Logger.info(this.context.node.getNodeName(), `hm_router_map.json has been generated in ${routerMapFilePath}`); } } exports.RouterMapBuildingProcessor = RouterMapBuildingProcessor;