1/* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15import { 16 ESMODULE, 17 THIS 18} from '../../../lib/pre_define'; 19 20import { 21 SDK_VERSION, 22 SDK_VERSION_STAGE, 23 BUNDLE_NAME_DEFAULT, 24 ETS_LOADER_VERSION, 25 OHPM, 26 RELEASE, 27 DEBUG, 28 ENTRY_MODULE_NAME_DEFAULT, 29 ENTRY_PACKAGE_NAME_DEFAULT, 30 ENTRY_MODULE_VERSION_DEFAULT, 31 MODULE_NAME_HASH_DEFAULT, 32 RESOURCE_TABLE_HASH_DEFAULT, 33 RUNTIME_OS_OPENHARMONY, 34 DEVICE_TYPE, 35 NODE_JS_PATH, 36 PORT_DEFAULT, 37 PACKAGE_DIR_DEFAULT, 38 PACKAGE_JSON_DEFAULT, 39 PAGES_JSON_FILE_NAME_DEFAULT, 40 PANDA_MODE_DEFAULT, 41 MIN_API_VERSION_DEAFAULT, 42 STRICT_MODE_DEFAULT, 43 DEVICE_TYPES_DEFAULT, 44 PKG_JSON_FILE_HASH_DEFAULT, 45 BUILD_ID_DEFAULT, 46} from './common'; 47import { 48 ACE_PROFILE_PATH, 49 CACHE_PATH, 50 LOADER_PATH, 51 MAIN_PATH, 52 PREVIEW_CACHE_PATH, 53 PREVIEW_MAIN_PATH, 54 PROJECT_PATH_HASH_DEFAULT, 55 RES_PATH 56} from './pathConfig'; 57 58interface ConfigPermission { 59 requestPermissions?: Array<{ name: string }>; 60 definePermissions?: Array<{ name: string }>; 61} 62 63class ProjectConfig { 64 deviceTypesMessage?: string; 65 deviceTypes?: string[]; 66 externalApiPaths?: string[]; 67 syscapIntersectionSet?: Set<string>; 68 syscapUnionSet?: Set<string>; 69 permission?: ConfigPermission; 70 requestPermissions?: string[]; 71 definePermissions?: string[]; 72 integratedHsp: boolean; 73 resetBundleName: boolean; 74 isCrossplatform: boolean; 75 tsImportSendable?: boolean; 76 xtsMode: boolean; 77 hotReload?: object; 78 testFrameworkPar?: object; 79 targetName?: string; 80 isOhosTest?: object; 81 packageDir: string = PACKAGE_DIR_DEFAULT; 82 packageJson: string = PACKAGE_JSON_DEFAULT; 83 modulePathMap: object; 84 enableDebugLine: boolean; 85 byteCodeHar?: boolean; 86 moduleName: string = ENTRY_MODULE_NAME_DEFAULT; 87 pagesJsonFileName: string = PAGES_JSON_FILE_NAME_DEFAULT; 88 ignoreWarning?: boolean; 89 shouldEmitJs?: boolean; 90 useArkoala: boolean; 91 entryArrayForObf?: string[]; 92 cardObj?: object; 93 manifestFilePath: string; 94 hashProjectPath: string = PROJECT_PATH_HASH_DEFAULT; 95 compilerTypes: string[]; 96 splitCommon: boolean; 97 bundleType: string; 98 optLazyForEach: boolean; 99 hspResourcesMap: boolean; 100 useTsHar: boolean; 101 optTryCatchFunc: boolean; 102 otherCompileFiles: object; 103 intentEntry: string[]; 104 es2abcCompileTsInAotMode: boolean; 105 es2abcCompileTsInNonAotMode: boolean; 106 processTs: boolean; 107 pandaMode: string = PANDA_MODE_DEFAULT; 108 minAPIVersion: number = MIN_API_VERSION_DEAFAULT; 109 anBuildOutPut?: string; 110 anBuildMode?: object; 111 nodeModulesPath?: object; 112 minPlatformVersion?: number; 113 changedFileList?: object; 114 moduleType?: string; 115 perf?: number; 116 debugLine?: boolean; 117 aotCompileMode?: string; 118 buildGeneratedProfilePath?: string; 119 apiVersion: number = SDK_VERSION; 120 needCompleteSourcesMap: boolean = false; 121 isFaMode: boolean = false; 122 strictMode?: object; 123 buildDir?: string; 124 ohPackagePathMap?: object; 125 branchElimination?: boolean; 126 caseSensitiveCheck?: boolean; 127 depName2RootPath?: Map<string, string>; 128 useNativeResolver?: boolean; 129 resolveComflictMode?: boolean; 130 rawFileResource?: string; 131 pkgNameToPkgBriefInfo?: object; 132 projectModel?: object; 133 pkgJsonFileHash: string = PKG_JSON_FILE_HASH_DEFAULT; 134 allModulePaths?: string[]; 135 routerMap?: object; 136 compileBlockPkg?: string[]; 137 overallProjectPaths?: string[]; 138 level?: object; 139 buildId?: string; 140 globalModulePaths?: string[]; 141 compileEnv?: string; 142 143 // copied from ark_compiler_ut 144 compileMode: string = ESMODULE; 145 packageManagerType: string = OHPM; 146 compileSdkVersion: number = SDK_VERSION; 147 compatibleSdkVersion: number = SDK_VERSION; 148 compatibleSdkVersionStage: string = SDK_VERSION_STAGE; 149 bundleName: string = BUNDLE_NAME_DEFAULT; 150 etsLoaderVersion: string = ETS_LOADER_VERSION; 151 etsLoaderReleaseType: string = RELEASE; 152 entryModuleName: string = ENTRY_MODULE_NAME_DEFAULT; 153 entryPackageName: string = ENTRY_PACKAGE_NAME_DEFAULT; 154 entryModuleVersion: string = ENTRY_MODULE_VERSION_DEFAULT; 155 allModuleNameHash: string = MODULE_NAME_HASH_DEFAULT; 156 resourceTableHash: string = RESOURCE_TABLE_HASH_DEFAULT; 157 runtimeOS: string = RUNTIME_OS_OPENHARMONY; 158 sdkInfo: string = `false:${SDK_VERSION}:${ETS_LOADER_VERSION}:${RELEASE}`; 159 160 watchMode: string; 161 isPreview: boolean; 162 isLocalTest: boolean; 163 buildMode: string = DEBUG; 164 localPropertiesPath?: string; 165 aceProfilePath: string; 166 etsLoaderPath?: string; 167 modulePath?: string; 168 needCoverageInsert: boolean; 169 projectTopDir?: string; 170 apPath?: string; 171 aceModuleJsonPath: string; 172 appResource?: string; 173 aceModuleRoot?: string; 174 aceSuperVisualPath: string; 175 aceBuildJson: string; 176 cachePath: string; 177 aceModuleBuild: string; 178 patchAbcPath?: string; 179 DynamicImportpath?: string; 180 supportChunks: boolean; 181 projectPath: string; 182 resolveModulePaths?: Array<string>; 183 compileHar: boolean; 184 compileShared: boolean; 185 moduleRootPath?: object; 186 buildPath: string; 187 rootPathSet?: Object; 188 189 deviceType?: string; 190 checkEntry?: string; 191 Path?: string; 192 note?: string; 193 hapMode?: string; 194 img2bin?: string; 195 projectProfilePath?: string; 196 logLevel?: string; 197 stageRouterConfig?: object; 198 port?: string; 199 aceSoPath?: string; 200 mockParams?: object; 201 projectRootPath: string; 202 pkgContextInfo?: object; 203 useNormalizedOHMUrl: boolean = false; 204 dependencyAliasMap?: Map<string, string>; 205 byteCodeHarInfo?: object; 206 entryObj?: object; 207 harNameOhmMap: object; 208 hspNameOhmMap: object; 209 cardEntryObj?: object; 210 widgetCompile?: boolean; 211 arkRouterMap: Array<object>; 212 declarationEntry: Array<string>; 213 214 constructor() { 215 this.watchMode = 'false'; 216 this.isPreview = false; 217 this.isLocalTest = false; 218 this.needCoverageInsert = false; 219 this.supportChunks = true; 220 this.compileHar = false; 221 this.compileShared = false; 222 this.xtsMode = false; 223 this.splitCommon = false; 224 this.isCrossplatform = false; 225 this.enableDebugLine = false; 226 this.bundleType = ''; 227 this.optLazyForEach = false; 228 this.hspResourcesMap = true; 229 this.useArkoala = false; 230 this.resetBundleName = false; 231 this.integratedHsp = false; 232 this.useTsHar = false; 233 this.optTryCatchFunc = true; 234 this.es2abcCompileTsInAotMode = true; 235 this.es2abcCompileTsInNonAotMode = false; 236 this.processTs = true; 237 238 this.harNameOhmMap = {}; 239 this.hspNameOhmMap = {}; 240 this.arkRouterMap = []; 241 this.declarationEntry = []; 242 this.compilerTypes = []; 243 this.otherCompileFiles = {} 244 this.intentEntry = []; 245 } 246 247 public setIgnoreWarning(ignoreWarning: boolean) { 248 this.ignoreWarning = ignoreWarning; 249 } 250 251 public setPreview(isPreview: boolean) { 252 this.isPreview = isPreview; 253 this.watchMode = String(isPreview); 254 } 255 256 public setBuildMode(buildMode: string) { 257 this.buildMode = buildMode; 258 } 259 260 public setCompilerVersion(version: number) { 261 this.compileSdkVersion = version; 262 this.compatibleSdkVersion = version; 263 } 264 265 public scan(projectRoot: string, testcase: string, pagePaths: string[] = []) { 266 this.initPath(`${projectRoot}/${testcase}`, pagePaths); 267 } 268 269 public concat(other: typeof ProjectConfig | ArkProjectConfig) { 270 const filteredEntries = Object.entries(other).reduce((acc, [key, value]) => { 271 if (value !== undefined || this[key] === undefined) { 272 acc.push([key, value]); 273 } 274 return acc; 275 }, []); 276 277 Object.assign(this, Object.fromEntries(filteredEntries)); 278 279 } 280 281 private initPath(projectPath: string, pagePaths: string[] = []) { 282 let mode = this.isPreview ? '.preview' : this.isLocalTest ? '.test' : 'build'; 283 284 this.projectPath = `${projectPath}/${this.entryModuleName}/src/main/ets`; 285 this.buildPath = this.isPreview 286 ? `${projectPath}/${this.entryModuleName}/${mode}/${PREVIEW_MAIN_PATH}` 287 : `${projectPath}/${this.entryModuleName}/${mode}/${MAIN_PATH}`; 288 this.aceModuleBuild = this.isPreview 289 ? `${projectPath}/${this.entryModuleName}/${mode}/${PREVIEW_MAIN_PATH}` 290 : `${projectPath}/${this.entryModuleName}/${mode}/${MAIN_PATH}`; 291 this.manifestFilePath = `${projectPath}/${this.entryModuleName}/src/main/ets/manifest.json`; 292 this.aceProfilePath = `${projectPath}/${this.entryModuleName}/${mode}/${ACE_PROFILE_PATH}`; 293 this.aceModuleJsonPath = `${projectPath}/${this.entryModuleName}/${mode}/${RES_PATH}/module.json`; 294 this.aceSuperVisualPath = `${projectPath}/${this.entryModuleName}/src/main/supervisual`; 295 this.aceBuildJson = `${projectPath}/${this.entryModuleName}/${mode}/${LOADER_PATH}/loader.json`; 296 this.cachePath = this.isPreview 297 ? `${projectPath}/${this.entryModuleName}/${mode}/${PREVIEW_CACHE_PATH}` 298 : `${projectPath}/${this.entryModuleName}/${mode}/${CACHE_PATH}`; 299 this.projectRootPath = `${projectPath}`; 300 301 if (this.isPreview) { 302 this.previewUniqueConfig(pagePaths); 303 } 304 } 305 306 private previewUniqueConfig(pagePaths: string[] = []) { 307 this.deviceType = DEVICE_TYPE; 308 this.checkEntry = 'true'; 309 this.Path = NODE_JS_PATH; 310 this.note = 'false'; 311 this.hapMode = 'false'; 312 this.img2bin = 'true'; 313 this.projectProfilePath = `${this.projectRootPath}/build-profile.json5`; 314 this.logLevel = '3'; 315 this.stageRouterConfig = { 316 'contents': [ 317 "{\"module\":{\"pages\":\"$profile:main_pages\",\"name\":\"entry\"}}", 318 JSON.stringify({ 'src': pagePaths }) 319 ], 320 'paths': [ 321 `${this.projectRootPath}/${this.entryModuleName}/.preview/${RES_PATH}/module.json`, 322 `${this.projectRootPath}/${this.entryModuleName}/.preview/${RES_PATH}/resources/base/profile/main_pages.json` 323 ] 324 }; 325 this.port = PORT_DEFAULT; 326 this.aceSoPath = `${this.projectRootPath}/${this.entryModuleName}/.preview/cache/nativeDependencies.txt`; 327 } 328 329 public mockCompileContextInfo(projectRoot: string, pagePaths: string[] = []) { 330 this.entryObj = { 331 'entryAbility/EntryAbility': `${projectRoot}/entry/src/main/ets/entryability/EntryAbility.ets`, 332 'entryformability/EntryFormAbility': `${projectRoot}/entry/src/main/ets/entryformability/EntryFormAbility.ets`, 333 }; 334 335 this.entryArrayForObf = [ 336 "./entryability/EntryAbility", 337 "./entryformability/EntryFormAbility", 338 ]; 339 340 pagePaths.forEach((mainPage: string) => { 341 this.entryObj[mainPage] = `${projectRoot}/entry/src/main/ets/${mainPage}.ets`; 342 this.entryArrayForObf.push(mainPage); 343 }) 344 345 this.cardObj = {}; 346 this.cardEntryObj = {}; 347 this.projectRootPath = projectRoot; 348 } 349 350 public mockRollupShareBuild(projectRoot: string) { 351 let mode = this.isPreview ? '.preview' : this.isLocalTest ? '.test' : 'build'; 352 353 this.moduleType = "entry"; 354 this.perf = 0; 355 this.targetName = ".default"; 356 this.localPropertiesPath = `${projectRoot}/local.properties`; 357 this.isOhosTest = false; 358 this.etsLoaderPath = `/${this.runtimeOS}/Sdk/${this.compileSdkVersion}/openharmony/ets/build-tools/ets-loader`; 359 this.modulePath = `${projectRoot}/${this.entryModuleName}`; 360 this.testFrameworkPar = {}; 361 this.debugLine = false; 362 this.projectTopDir = `${projectRoot}`; 363 this.aotCompileMode = 'type'; 364 this.apPath = `${projectRoot}/${this.entryModuleName}/modules.ap`; 365 this.externalApiPaths = [`/${this.runtimeOS}/Sdk/${this.compileSdkVersion}/hms/ets`] 366 this.buildGeneratedProfilePath = `${projectRoot}/${this.entryModuleName}/${mode}/default/generated/profile/default`; 367 this.bundleType = 'app'; 368 this.strictMode = STRICT_MODE_DEFAULT; 369 this.buildDir = `${projectRoot}/${this.entryModuleName}/${mode}`; 370 this.deviceTypes = DEVICE_TYPES_DEFAULT; 371 this.useNormalizedOHMUrl = true; 372 this.pkgContextInfo = { 373 '@ohos/hypium': { 374 'packageName': '@ohos/hypium', 375 'bundleName': '', 376 'moduleName': '', 377 'version': '1.0.19', 378 'entryPath': 'index.js', 379 'isSO': false, 380 'dependencyAlias': '' 381 }, 382 '@ohos/hamock': { 383 'packageName': '@ohos/hamock', 384 'bundleName': '', 385 'moduleName': '', 386 'version': '1.0.1-rc2', 387 'entryPath': 'index.ets', 388 'isSO': false, 389 'dependencyAlias': '' 390 }, 391 'entry': { 392 'packageName': 'entry', 393 'bundleName': '', 394 'moduleName': '', 395 'version': '', 396 'entryPath': 'src/main', 397 'isSO': false, 398 'dependencyAlias': '' 399 } 400 }; 401 this.ohPackagePathMap = {}; 402 this.dependencyAliasMap = new Map(); 403 this.permission = {}; 404 this.integratedHsp = false; 405 this.branchElimination = false; 406 this.caseSensitiveCheck = true; 407 this.tsImportSendable = false; 408 this.resolveComflictMode = true; 409 this.depName2RootPath = new Map(); 410 this.rootPathSet = new Set([`${projectRoot}`]); 411 this.useNativeResolver = true; 412 this.shouldEmitJs = true; 413 this.appResource = `${projectRoot}/${this.entryModuleName}/${mode}/${RES_PATH}/ResourceTable.txt`; 414 this.rawFileResource = `${projectRoot}/${this.entryModuleName}/${mode}/${RES_PATH}/resources/rawfile`; 415 this.aceModuleRoot = `${projectRoot}/${this.entryModuleName}/src/main/ets`; 416 this.pkgNameToPkgBriefInfo = { 417 'entry': { 418 'pkgRoot': `${projectRoot}/${this.entryModuleName}`, 419 'sourceRoots': [ 420 'src/main', 421 `${mode}/default/generated/profile/default` 422 ], 423 'pkgName': 'entry' 424 } 425 }; 426 this.projectModel = { 427 [`${projectRoot}/${this.entryModuleName}/src/main`]: { 428 'moduleName': this.entryModuleName, 429 'modulePkgPath': `${projectRoot}/${this.entryModuleName}`, 430 'belongProjectPath': projectRoot 431 }, 432 [`${projectRoot}/${this.entryModuleName}/${mode}/default/intermediates/src/ohosTest/ets`]: { 433 'moduleName': 'entry_test', 434 'modulePkgPath': `${projectRoot}/${this.entryModuleName}`, 435 'belongProjectPath': projectRoot 436 }, 437 [`${projectRoot}/${this.entryModuleName}/src/ohosTest`]: { 438 'moduleName': 'entry_test', 439 'modulePkgPath': `${projectRoot}/${this.entryModuleName}`, 440 'belongProjectPath': projectRoot 441 }, 442 [`${projectRoot}/${this.entryModuleName}`]: { 443 'moduleName': this.entryModuleName, 444 'modulePkgPath': `${projectRoot}/${this.entryModuleName}`, 445 'belongProjectPath': projectRoot 446 }, 447 [projectRoot]: { 448 'moduleName': 'application', 449 'modulePkgPath': projectRoot, 450 'belongProjectPath': projectRoot 451 } 452 }; 453 this.allModulePaths = [`${projectRoot}/${this.entryModuleName}`]; 454 this.routerMap = {}; 455 this.compileBlockPkg = []; 456 this.overallProjectPaths = [projectRoot]; 457 this.mockParams = { 458 'decorator': '@MockSetup', 459 'packageName': '@ohos/hamock', 460 'etsSourceRootPath': 'ets/main/ets', 461 'mockConfigPath': `${projectRoot}/${this.entryModuleName}/src/mock/mock-config.json5`, 462 'mockConfigKey2ModuleInfo': {}, 463 'isMockConfigChanged': this.isPreview 464 }; 465 this.level = { 466 'level': 20000, 467 'levelStr': 'INFO', 468 'colour': 'green' 469 }; 470 this.globalModulePaths = [ 471 `/${this.runtimeOS}/Sdk/${this.compileSdkVersion}/openharmony/ets/api`, 472 `/${this.runtimeOS}/Sdk/${this.compileSdkVersion}/openharmony/ets/arkts`, 473 `/${this.runtimeOS}/Sdk/${this.compileSdkVersion}/hms/ets/api`, 474 `/${this.runtimeOS}/Sdk/${this.compileSdkVersion}/hms/ets/kits`, 475 `/${this.runtimeOS}/Sdk/${this.compileSdkVersion}/openharmony/ets/api` 476 ] 477 this.compileEnv = [ 478 this.compileSdkVersion, 479 this.compatibleSdkVersion, 480 this.etsLoaderPath, 481 this.isLocalTest, 482 this.entryModuleName, 483 `${this.cachePath}/compiler.cache` 484 ].map((c) => c.toString()).join('#') 485 this.resolveModulePaths = [ 486 `${projectRoot}/${this.entryModuleName}/src/main`, 487 `${projectRoot}/${this.entryModuleName}/${mode}/default/generated/profile/default`, 488 `${projectRoot}/${this.entryModuleName}/oh_modules`, 489 `${projectRoot}/oh_modules`, 490 ] 491 492 if (!this.isPreview) { 493 this.buildId = BUILD_ID_DEFAULT 494 } 495 } 496} 497 498class PartialUpdateConfig { 499 partialUpdateMode: boolean = false; 500 builderCheck: boolean = true; 501 executeArkTSLinter: boolean = true; 502 standardArkTSLinter: boolean = true; 503 optimizeComponent: boolean = true; 504 arkTSVersion: string = undefined; 505 skipTscOhModuleCheck: boolean = false; 506 skipArkTSStaticBlocksCheck: boolean = false; 507 508 constructor() { 509 this.partialUpdateMode = false; 510 this.builderCheck = true; 511 this.executeArkTSLinter = true; 512 this.standardArkTSLinter = true; 513 this.optimizeComponent = true; 514 this.arkTSVersion = undefined; 515 this.skipTscOhModuleCheck = false; 516 this.skipArkTSStaticBlocksCheck = false; 517 } 518 519 public setPartialUpdateMode(newVal: boolean) { 520 this.partialUpdateMode = newVal; 521 } 522 523 public mockDisableArkTSLinter() { 524 this.executeArkTSLinter = false; 525 this.standardArkTSLinter = false; 526 } 527} 528 529interface ArkProjectConfig { 530 projectRootPath: string, 531 modulePathMap: object, 532 isOhosTest: object, 533 oldMapFilePath?: object, 534 processTs: boolean, 535 pandaMode: string, 536 anBuildOutPut?: string, 537 anBuildMode?: object, 538 apPath?: object, 539 nodeModulesPath?: object, 540 harNameOhmMap: object, 541 minPlatformVersion: number, 542 moduleName: string, 543 bundleName: string, 544 hotReload: object, 545 patchAbcPath: object, 546 changedFileList: object, 547 compileMode: string, 548 es2abcCompileTsInAotMode: boolean, 549 es2abcCompileTsInNonAotMode: boolean, 550 minAPIVersion: number, 551 pagesJsonFileName: string, 552} 553 554export { 555 ArkProjectConfig, 556 ProjectConfig, 557 PartialUpdateConfig 558}