@hadss/hmrouter-plugin
Version:
HMRouter Compiler Plugin
89 lines (88 loc) • 4.45 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigUpdateProcessor = void 0;
const framework_1 = require("../../framework");
const constants_1 = require("../constants");
class ConfigUpdateProcessor {
constructor(context) {
this.context = context;
this.moduleContext = context.moduleContext;
this.routerMap = context.routerMap;
this.nodeName = context.node.getNodeName();
}
execute() {
framework_1.Logger.debug(this.nodeName, `Start to update module.json and build profile...`);
const isHar = (0, framework_1.isHarModule)(this.context.node);
if (this.context.moduleIgnored && isHar) {
framework_1.Logger.info(this.nodeName, `skip update module.json and build profile...`);
return;
}
if (this.routerMap.length > 0 || !isHar) {
this.updateModuleJson();
}
this.updateBuildProfile();
}
updateModuleJson() {
const moduleJsonOpt = this.moduleContext.getModuleJsonOpt();
moduleJsonOpt.module.routerMap = constants_1.RouterMapConstants.MODULE_ROUTER_MAP_NAME;
this.moduleContext.setModuleJsonOpt(moduleJsonOpt);
framework_1.Logger.info(this.nodeName, `Update module.json5 routerMap successfully, current routerMap: ${this.moduleContext.getModuleJsonOpt().module.routerMap}`);
}
updateBuildProfile() {
const buildProfileOpt = this.moduleContext.getBuildProfileOpt();
let sources = framework_1.ObjectUtils.ensureNestedObject(buildProfileOpt, [
'buildOption',
'arkOptions',
'runtimeOnly',
'sources',
]);
if (!Array.isArray(sources)) {
buildProfileOpt.buildOption.arkOptions.runtimeOnly.sources = [];
}
this.pushRouterInfo(buildProfileOpt, this.routerMap);
if (this.context.config.incremental) {
this.updateDirectories(buildProfileOpt);
}
this.moduleContext.setBuildProfileOpt(buildProfileOpt);
framework_1.Logger.debug(this.nodeName, `Update build profile successfully, current sources: ${this.moduleContext.getBuildProfileOpt()?.buildOption?.arkOptions?.runtimeOnly?.sources}`);
}
pushRouterInfo(buildProfileOpt, routerMap) {
let sources = buildProfileOpt.buildOption.arkOptions.runtimeOnly.sources ?? [];
routerMap.forEach((item) => {
const name = item.name;
if (name.includes(constants_1.PrefixConstants.LIFECYCLE_PREFIX) ||
name.includes(constants_1.PrefixConstants.INTERCEPTOR_PREFIX) ||
name.includes(constants_1.PrefixConstants.ANIMATOR_PREFIX) ||
name.includes(constants_1.PrefixConstants.SERVICE_PREFIX) ||
name.includes(constants_1.PrefixConstants.SERVICE_PROVIDE_PREFIX)) {
sources.push(constants_1.FilePathConstants.CURRENT_DELIMITER + item.pageSourceFile);
}
});
sources = [...new Set(sources)];
}
updateDirectories(buildProfileOpt) {
const targets = buildProfileOpt.targets;
if (!targets || !Array.isArray(targets)) {
framework_1.Logger.warn(this.nodeName, `No targets found in build-profile.json5, skip updating directories`);
return;
}
const isolatedResourcesDir = constants_1.FilePathConstants.CURRENT_DELIMITER + constants_1.FilePathConstants.ISOLATED_RESOURCES_DIR;
if (!framework_1.PluginFileUtil.exist(framework_1.PluginFileUtil.pathResolve(this.context.moduleContext.getModulePath(), isolatedResourcesDir))) {
framework_1.Logger.warn(this.nodeName, `${isolatedResourcesDir} is not exist, skip updating directories`);
return;
}
targets.forEach((target) => {
if (!target.resource) {
target.resource = {};
}
if (!target.resource.directories) {
target.resource.directories = [constants_1.FilePathConstants.DEFAULT_RESOURCES_DIR];
}
if (!target.resource.directories.includes(isolatedResourcesDir)) {
target.resource.directories.unshift(isolatedResourcesDir);
}
framework_1.Logger.debug(this.nodeName, `Update target '${target.name}' directories: ${JSON.stringify(target.resource.directories)}`);
});
}
}
exports.ConfigUpdateProcessor = ConfigUpdateProcessor;