UNPKG

@hadss/hmrouter-plugin

Version:

HMRouter Compiler Plugin

119 lines (118 loc) 5.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ObfuscationProcessor = void 0; const framework_1 = require("../../framework"); const constants_1 = require("../constants"); class ObfuscationProcessor { constructor(context) { this.context = context; } execute() { framework_1.Logger.debug(this.context.node.getNodeName(), `Start to obfuscation...`); const buildProfileOpt = this.context.moduleContext?.getBuildProfileOpt(); if (!this.isEnableObfuscation(buildProfileOpt)) { framework_1.Logger.info(this.context.node.getNodeName(), 'This compilation does not turn on code obfuscation, skip hmrouter_obfuscation_rules.txt file generation'); return; } const obfuscationString = this.generateObfuscationRuleFile(); this.handleHarPackageLogic(obfuscationString); this.context.moduleContext?.setBuildProfileOpt(buildProfileOpt); } generateObfuscationRuleFile() { const obfuscationFilePath = framework_1.PluginFileUtil.pathResolve(this.context.config.modulePath, constants_1.FilePathConstants.OBFUSCATION_FILE_NAME); const routerMap = JSON.parse(framework_1.PluginFileUtil.readFileSync(this.context.config.getRouterMapDir()).toString()).routerMap; const obfuscationString = this.buildObfuscatedStrings(routerMap); framework_1.PluginFileUtil.ensureFileSync(obfuscationFilePath); framework_1.PluginFileUtil.writeFileSync(obfuscationFilePath, obfuscationString); framework_1.Logger.info(this.context.node.getNodeName(), `Generate obfuscation rule file successfully, filePath: ${obfuscationFilePath}`); return obfuscationString; } handleHarPackageLogic(obfuscationString) { if (this.context.moduleContext.getModuleType() === constants_1.PluginConstants.HAR_MODULE_NAME) { const consumerRulesPath = framework_1.PluginFileUtil.pathResolve(this.context.config.modulePath, constants_1.FilePathConstants.CONSUMER_FILE_NAME); framework_1.PluginFileUtil.ensureFileSync(consumerRulesPath); framework_1.PluginFileUtil.writeFileSync(consumerRulesPath, obfuscationString); } } isEnableObfuscation(buildProfileOpt) { const currentBuildMode = framework_1.PluginStore.getInstance().get('buildMode'); const buildOption = buildProfileOpt.buildOptionSet?.find((item) => item.name === currentBuildMode); if (!buildOption) { return false; } const arkOptions = framework_1.ObjectUtils.ensureNestedObject(buildOption, ['arkOptions']); const obfuscationOptions = framework_1.ObjectUtils.ensureNestedObject(arkOptions, ['obfuscation']); const ruleOptions = framework_1.ObjectUtils.ensureNestedObject(obfuscationOptions, ['ruleOptions']); const isObfuscationEnabled = !!ruleOptions.enable; if (!isObfuscationEnabled) { return false; } if (this.context.config?.autoObfuscation) { this.addHMRouterObfuscationRules(ruleOptions, obfuscationOptions); } return true; } addHMRouterObfuscationRules(ruleOptions, obfuscationOptions) { const obfuscationFilePath = constants_1.FilePathConstants.CURRENT_DELIMITER + constants_1.FilePathConstants.OBFUSCATION_FILE_NAME; ruleOptions.files = this.appendFilePathSafely(ruleOptions.files, obfuscationFilePath); if (this.context.moduleContext.getModuleType() === constants_1.PluginConstants.HAR_MODULE_NAME) { const consumerRulesPath = constants_1.FilePathConstants.CURRENT_DELIMITER + constants_1.FilePathConstants.CONSUMER_FILE_NAME; obfuscationOptions.consumerFiles = this.appendFilePathSafely(obfuscationOptions.consumerFiles, consumerRulesPath); } } appendFilePathSafely(target, filePath) { if (typeof target === 'string') { return target === filePath ? [target] : [target, filePath]; } else if (Array.isArray(target)) { if (!target.includes(filePath)) { target.push(filePath); } return target; } else { return [filePath]; } } buildObfuscatedStrings(routerMap) { let srcPathArr = [ ...new Set(routerMap.map((routerInfo) => { return constants_1.FilePathConstants.CURRENT_DELIMITER + routerInfo.pageSourceFile; })), ]; let classNameArr = [ ...new Set(routerMap .filter((routerInfo) => { return routerInfo.name.includes('__'); }) .map((routerInfo) => { return routerInfo.customData.name; })), ]; let functionName = [ ...new Set(routerMap .filter((routerInfo) => { return routerInfo.name.includes(constants_1.PrefixConstants.SERVICE_PREFIX); }) .map((routerInfo) => { return routerInfo.customData.functionName; })), ]; let watchFunctionName = [ ...new Set(routerMap .filter((routerInfo) => { return routerInfo.buildFunction; }) .map((routerInfo) => { return routerInfo.buildFunction + constants_1.TemplateConstants.WARP_BUILDER; })), ]; return (constants_1.ObfuscationConstants.KEEP_FILE_NAME + constants_1.FilePathConstants.LINE_BREAK + srcPathArr .concat(constants_1.ObfuscationConstants.KEEP_PROPERTY_NAME, functionName) .concat(constants_1.ObfuscationConstants.KEEP_GLOBAL_NAME, classNameArr, watchFunctionName) .join(constants_1.FilePathConstants.LINE_BREAK)); } } exports.ObfuscationProcessor = ObfuscationProcessor;