UNPKG

@hadss/hmrouter-plugin

Version:

HMRouter Compiler Plugin

134 lines (133 loc) 6.83 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.HMRouterPluginConfig = void 0; const path_1 = __importDefault(require("path")); const framework_1 = require("../../framework"); const constants_1 = require("../constants"); const hvigor_1 = require("@ohos/hvigor"); class HMRouterPluginConfig { constructor(moduleName, modulePath, configDir, param) { this.moduleName = moduleName; this.modulePath = modulePath; this.configDir = configDir; this.scanDir = param.scanDir ? [...new Set(param.scanDir)] : [constants_1.FilePathConstants.DEFAULT_SCAN_DIR]; const scanFullDir = this.scanDir.map(item => framework_1.PluginFileUtil.pathResolve(modulePath, item)); this.commonScanFullDir = framework_1.StringUtil.longestCommonPrefixOpt(scanFullDir); this.routerMapDir = param.routerMapDir ?? constants_1.FilePathConstants.DEFAULT_ROUTER_MAP_DIR; this.builderDir = param.builderDir ?? constants_1.FilePathConstants.DEFAULT_BUILD_DIR; this.defaultPageTemplate = param.defaultPageTemplate ?? constants_1.TemplateConstants.VIEW_BUILDER_TEMPLATE; this.customPageTemplate = param.customPageTemplate ?? []; this.saveGeneratedFile = param.saveGeneratedFile ?? false; this.autoObfuscation = param.autoObfuscation ?? false; const isSupportIncremental = hvigor_1.hvigor.getHvigorVersion?.() !== undefined; if (param.incremental === undefined) { this.incremental = isSupportIncremental && param.builderDir === undefined && this.customPageTemplate.length === 0 && this.defaultPageTemplate === constants_1.TemplateConstants.VIEW_BUILDER_TEMPLATE; } else if (param.incremental === false) { this.incremental = false; } else { this.incremental = isSupportIncremental; } } getScanPath(dir) { return framework_1.PluginFileUtil.pathResolve(this.modulePath, dir); } getRelativeSourcePath(filePath) { return path_1.default.relative(this.modulePath, filePath); } getRelativeShortSourcePath(analyzeResult) { if (this.incremental) { const relativeShortPath = path_1.default.relative(this.commonScanFullDir, analyzeResult.sourceFilePath); const ext = path_1.default.extname(relativeShortPath); return relativeShortPath.replace(ext, `${framework_1.StringUtil.stringToHashCode(analyzeResult.name)}${ext}`); } else { return constants_1.PrefixConstants.VIEW_NAME_PREFIX + analyzeResult.name + framework_1.StringUtil.stringToHashCode(analyzeResult.name) + constants_1.FilePathConstants.ETS_SUFFIX; } } getRelativeBuilderPath(filePath, relativeShortPath) { if (this.incremental) { const absoluteFilePath = framework_1.PluginFileUtil.pathResolve(this.modulePath, filePath); const isolatedEtsPath = this.getGeneratedFilePath(relativeShortPath); const relativeBuilderPath = path_1.default.relative(path_1.default.dirname(isolatedEtsPath), absoluteFilePath); return relativeBuilderPath; } else { return path_1.default.relative(this.builderDir, filePath); } } getGeneratedFilePath(relativeShortPath) { if (this.incremental) { return framework_1.PluginFileUtil.pathResolve(this.modulePath, constants_1.FilePathConstants.ISOLATED_ETS_DIR, relativeShortPath); } else { return framework_1.PluginFileUtil.pathResolve(this.modulePath, this.builderDir, relativeShortPath); } } getBuilderDir(isDetele) { if (this.incremental && !isDetele) { return framework_1.PluginFileUtil.pathResolve(this.modulePath, constants_1.FilePathConstants.ISOLATED_ETS_DIR); } else { return framework_1.PluginFileUtil.pathResolve(this.modulePath, this.builderDir); } } getBuilderFilePath(relativeShortPath) { if (this.incremental) { return constants_1.FilePathConstants.ISOLATED_PAGE_SOURCE_PREFIX + constants_1.FilePathConstants.DELIMITER + relativeShortPath; } else { return path_1.default.join(this.builderDir, relativeShortPath); } } getRouterMapDir(isDetele) { if (this.incremental && !isDetele) { return framework_1.PluginFileUtil.pathResolve(this.modulePath, constants_1.FilePathConstants.ISOLATED_PROFILE_DIR, constants_1.RouterMapConstants.ROUTER_MAP_NAME); } else { return framework_1.PluginFileUtil.pathResolve(this.modulePath, this.routerMapDir, constants_1.RouterMapConstants.ROUTER_MAP_NAME); } } getDefaultRouterMapDir() { return framework_1.PluginFileUtil.pathResolve(this.modulePath, this.routerMapDir); } getModuleRouterMapFilePath(routerMapFileName) { return framework_1.PluginFileUtil.pathResolve(this.modulePath, this.routerMapDir, routerMapFileName + constants_1.FilePathConstants.JSON_SUFFIX); } getRawFilePath(isDetele) { if (this.incremental && !isDetele) { return framework_1.PluginFileUtil.pathResolve(this.modulePath, constants_1.FilePathConstants.ISOLATED_RAWFILE_DIR); } else { return framework_1.PluginFileUtil.pathResolve(this.modulePath, constants_1.FilePathConstants.RAWFILE_DIR); } } getDefaultTplFilePath() { if (path_1.default.isAbsolute(this.defaultPageTemplate) && framework_1.PluginFileUtil.exist(this.defaultPageTemplate)) { return this.defaultPageTemplate; } return framework_1.PluginFileUtil.pathResolve(__dirname, constants_1.FilePathConstants.PARENT_DELIMITER.repeat(3) + constants_1.TemplateConstants.VIEW_BUILDER_TEMPLATE); } getObfuscationFilePath(isDetele) { if (this.incremental && !isDetele) { return framework_1.PluginFileUtil.pathResolve(this.modulePath, constants_1.FilePathConstants.ISOLATED_OBFUSCATION_FILE_NAME); } else { return framework_1.PluginFileUtil.pathResolve(this.modulePath, constants_1.FilePathConstants.OBFUSCATION_FILE_NAME); } } getConsumerRulesFilePath(isDetele) { if (this.incremental && !isDetele) { return framework_1.PluginFileUtil.pathResolve(this.modulePath, constants_1.FilePathConstants.ISOLATED_CONSUMER_FILE_NAME); } else { return framework_1.PluginFileUtil.pathResolve(this.modulePath, constants_1.FilePathConstants.CONSUMER_FILE_NAME); } } } exports.HMRouterPluginConfig = HMRouterPluginConfig;