UNPKG

@hadss/hmrouter-plugin

Version:

HMRouter Compiler Plugin

107 lines (106 loc) 5.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PluginExecutionController = void 0; const TaskManager_1 = require("./TaskManager"); const TaskStage_1 = require("./TaskStage"); const constants_1 = require("../constants"); const extension_1 = require("../extension"); const utils_1 = require("../utils"); class PluginExecutionController { constructor(node, moduleContext, moduleExtensions) { this.node = node; this.moduleContext = moduleContext; this.baseContext = this.initializeContext(); this.taskManager = new TaskManager_1.TaskManager(this.baseContext, moduleExtensions); } start() { this.taskManager.executeStage(TaskStage_1.TaskStage.AFTER_INITIALIZE); this.registerHvigorTasks(); } complete() { this.taskManager.executeStage(TaskStage_1.TaskStage.AFTER_COMPLETION); } initializeContext() { const context = new extension_1.ExtensionContextImpl(this.node, this.moduleContext); context.scanFiles = []; return context; } registerHvigorTasks() { this.moduleContext.targets((target) => { const targetName = target.getTargetName(); this.registerMainPluginTask(targetName); this.registerObfuscationTask(targetName); if ((0, utils_1.isHapModule)(this.node) || (0, utils_1.isHspModule)(this.node)) { this.registerCopyRouterMapTask(target); } }); } filterScanFilesForTarget(currentTargetName) { const buildProfileTargets = this.moduleContext.getBuildProfileOpt().targets || []; const targetOpt = buildProfileTargets.find((target) => target.name === currentTargetName); let absoluteSourceRoots; if (!targetOpt?.source?.sourceRoots || targetOpt.source.sourceRoots.length === 0) { absoluteSourceRoots = this.getAbsoluteSourceRoots([constants_1.TaskConstants.DEFAULT_SOURCE_ROOT]); } else { if (!targetOpt.source.sourceRoots.find((value) => value === constants_1.TaskConstants.DEFAULT_SOURCE_ROOT)) { targetOpt.source.sourceRoots.push(constants_1.TaskConstants.DEFAULT_SOURCE_ROOT); } absoluteSourceRoots = this.getAbsoluteSourceRoots(targetOpt.source.sourceRoots); } this.baseContext.scanFiles = this.baseContext.scanFiles.filter((filePath) => { const normalizedFilePath = utils_1.PluginFileUtil.normalize(filePath); return absoluteSourceRoots.some((sourceRootPath) => { return normalizedFilePath.startsWith(sourceRootPath); }); }); } getAbsoluteSourceRoots(sourceRoots) { const moduleRoot = this.moduleContext.getModulePath(); return sourceRoots.map((sourceRoot) => { const absolutePath = utils_1.PluginFileUtil.resolve(moduleRoot, sourceRoot); const normalizedPath = utils_1.PluginFileUtil.normalize(absolutePath); return normalizedPath.endsWith(utils_1.PluginFileUtil.sep) ? normalizedPath : normalizedPath + utils_1.PluginFileUtil.sep; }); } registerMainPluginTask(targetName) { this.node.registerTask({ name: this.getTaskName(targetName, constants_1.TaskConstants.PLUGIN_TASK), run: () => { this.filterScanFilesForTarget(targetName); this.taskManager.executeStage(TaskStage_1.TaskStage.AFTER_ANNOTATION_ANALYSIS); this.taskManager.executeStage(TaskStage_1.TaskStage.AFTER_CODE_GENERATION); this.taskManager.executeStage(TaskStage_1.TaskStage.AFTER_ROUTER_MAP_BUILDING); this.taskManager.executeStage(TaskStage_1.TaskStage.AFTER_CONFIG_UPDATE); }, dependencies: [], postDependencies: [this.getTaskName(targetName, constants_1.TaskConstants.PRE_BUILD)], }); } registerObfuscationTask(targetName) { this.node.registerTask({ name: this.getTaskName(targetName, constants_1.TaskConstants.GENERATE_OBFUSCATION_TASK), run: () => { this.taskManager.executeStage(TaskStage_1.TaskStage.AFTER_OBFUSCATION_PROCESS); }, dependencies: [this.getTaskName(targetName, constants_1.TaskConstants.PLUGIN_TASK)], postDependencies: [this.getTaskName(targetName, constants_1.TaskConstants.PRE_BUILD)], }); } registerCopyRouterMapTask(target) { const targetName = target.getTargetName(); this.node.registerTask({ name: this.getTaskName(targetName, constants_1.TaskConstants.COPY_ROUTER_MAP_TASK), run: () => { this.taskManager.context.currentTarget = target; this.taskManager.executeStage(TaskStage_1.TaskStage.AFTER_RESOURCE_PROCESS); }, dependencies: [this.getTaskName(targetName, constants_1.TaskConstants.PROCESS_ROUTER_MAP)], postDependencies: [this.getTaskName(targetName, constants_1.TaskConstants.PROCESS_RESOURCE)], }); } getTaskName(targetName, taskConstant) { return targetName + taskConstant; } } exports.PluginExecutionController = PluginExecutionController;