1/* 2 * Copyright (c) 2023 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 */ 15 16import path from 'path'; 17 18import { ModuleMode } from './module_mode'; 19import { PATCH_SYMBOL_TABLE } from '../common/ark_define'; 20import { getEs2abcFileThreadNumber } from '../utils'; 21import { SourceMapGenerator } from '../generate_sourcemap'; 22 23export class ModuleHotfixMode extends ModuleMode { 24 patch: boolean; 25 inOldSymbolTablePath: string; 26 enableMap: boolean; 27 28 constructor(rollupObject: Object) { 29 super(rollupObject); 30 this.patch = false; 31 this.inOldSymbolTablePath = ''; 32 this.enableMap = false; 33 } 34 35 generateAbc(rollupObject: Object, parentEvent: Object): void { 36 this.patch = this.projectConfig.patch || false; 37 this.inOldSymbolTablePath = this.projectConfig.inOldSymbolTablePath || this.projectConfig.projectRootPath; 38 this.enableMap = this.projectConfig.enableMap || false; 39 this.prepareForCompilation(rollupObject, parentEvent); 40 SourceMapGenerator.getInstance().buildModuleSourceMapInfo(parentEvent); 41 42 this.generateEs2AbcCmdForHotfix(); 43 this.genDescriptionsForMergedEs2abc(!!this.projectConfig.byteCodeHarInfo); 44 this.generateMergedAbcOfEs2Abc(parentEvent); 45 } 46 47 private generateEs2AbcCmdForHotfix() { 48 const fileThreads = getEs2abcFileThreadNumber(); 49 this.cmdArgs.push(`"@${this.filesInfoPath}"`); 50 this.cmdArgs.push('--npm-module-entry-list'); 51 this.cmdArgs.push(`"${this.npmEntriesInfoPath}"`); 52 this.cmdArgs.push('--output'); 53 this.cmdArgs.push(`"${this.moduleAbcPath}"`); 54 this.cmdArgs.push('--file-threads'); 55 this.cmdArgs.push(`"${fileThreads}"`); 56 this.cmdArgs.push(`"--target-api-version=${this.projectConfig.compatibleSdkVersion}"`); 57 58 if (this.projectConfig.patch) { 59 const oldHapSymbolTable: string = path.join(this.projectConfig.inOldSymbolTablePath, PATCH_SYMBOL_TABLE); 60 this.cmdArgs.push('--input-symbol-table'); 61 this.cmdArgs.push(`"${oldHapSymbolTable}"`); 62 this.cmdArgs.push('--generate-patch'); 63 } 64 65 if (this.projectConfig.enableMap) { 66 // when generating map, cache is forbiden to avoid uncomplete symbol table 67 const oldHapSymbolTable: string = path.join(this.projectConfig.inOldSymbolTablePath, PATCH_SYMBOL_TABLE); 68 this.cmdArgs.push('--dump-symbol-table'); 69 this.cmdArgs.push(`"${oldHapSymbolTable}"`); 70 } 71 72 this.cmdArgs.push('--merge-abc'); 73 } 74} 75