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 rollupObject 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 fs from 'fs'; 17import { 18 COMMONJS, 19 ESM, 20 EXTNAME_PROTO_BIN, 21 EXTNAME_JS, 22 EXTNAME_TS, 23 EXTNAME_ETS 24} from '../../../../lib/fast_build/ark_compiler/common/ark_define'; 25import { 26 ModuleMode, 27 PackageEntryInfo 28} from '../../../../lib/fast_build/ark_compiler/module/module_mode'; 29import { changeFileExtension } from '../../../../lib/fast_build/ark_compiler/utils'; 30import { META } from '../rollup_mock/common'; 31class ModuleModeMock extends ModuleMode { 32 collectModuleFileListMock(rollupObject: object) { 33 const fileList = Array.from(rollupObject.getModuleIds()); 34 this.collectModuleFileList(rollupObject, fileList); 35 } 36 37 addModuleInfoItemMock(rollupObject: object, isCommonJs: boolean, extName: string) { 38 const mockFileList = rollupObject.getModuleIds(); 39 for (const filePath of mockFileList) { 40 if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) { 41 const moduleInfo: object = rollupObject.getModuleInfo(filePath); 42 const metaInfo: object = moduleInfo[META]; 43 this.addModuleInfoItem(filePath, isCommonJs, extName, metaInfo, this.moduleInfos); 44 } 45 } 46 } 47 48 generateCompileFilesInfoMock() { 49 this.generateCompileFilesInfo(); 50 } 51 52 generateNpmEntriesInfoMock() { 53 this.generateNpmEntriesInfo(); 54 } 55 56 generateAbcCacheFilesInfoMock() { 57 this.generateAbcCacheFilesInfo(); 58 } 59 60 checkGenerateCompileFilesInfo(): boolean { 61 let mockfilesInfo: string = ''; 62 const filesInfo = fs.readFileSync(this.filesInfoPath, 'utf-8'); 63 this.moduleInfos.forEach((info) => { 64 const moduleType: string = info.isCommonJs ? COMMONJS : ESM; 65 mockfilesInfo += 66 `${info.cacheFilePath};${info.recordName};${moduleType};${info.sourceFile};${info.packageName}\n`; 67 }); 68 if (filesInfo === mockfilesInfo) { 69 return true; 70 } 71 return false; 72 } 73 74 checkGenerateNpmEntriesInfo(): boolean { 75 let mockentriesInfo: string = ''; 76 const filesInfo = fs.readFileSync(this.npmEntriesInfoPath, 'utf-8'); 77 for (const value of this.pkgEntryInfos.values()) { 78 mockentriesInfo += `${value.pkgEntryPath}:${value.pkgBuildPath}\n`; 79 } 80 if (filesInfo === mockentriesInfo) { 81 return true; 82 } 83 return false; 84 } 85 86 checkGenerateAbcCacheFilesInfo(): boolean { 87 let mockabcCacheFilesInfo: string = ''; 88 const filesInfo = fs.readFileSync(this.cacheFilePath, 'utf-8'); 89 this.moduleInfos.forEach((info) => { 90 const abcCacheFilePath: string = changeFileExtension(info.cacheFilePath, EXTNAME_PROTO_BIN); 91 mockabcCacheFilesInfo += `${info.cacheFilePath};${abcCacheFilePath}\n`; 92 }); 93 94 const npmEntriesCacheFilePath: string = changeFileExtension(this.npmEntriesInfoPath, EXTNAME_PROTO_BIN); 95 mockabcCacheFilesInfo += `${this.npmEntriesInfoPath};${npmEntriesCacheFilePath}\n`; 96 97 if (filesInfo === mockabcCacheFilesInfo) { 98 return true; 99 } 100 return false; 101 } 102 103 checkGetPackageEntryInfo(rollup: object) { 104 this.pkgEntryInfos = new Map<String, PackageEntryInfo>(); 105 const mockFileList = rollup.getModuleIds(); 106 for (const filePath of mockFileList) { 107 if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) { 108 const moduleInfos = rollup.getModuleInfo(filePath); 109 moduleInfos.setIsLocalDependency(false); 110 moduleInfos.setIsNodeEntryFile(true); 111 const metaInfo: object = moduleInfos[META]; 112 this.getPackageEntryInfo(filePath, metaInfo, this.pkgEntryInfos); 113 } 114 } 115 } 116 117 updateCachedSourceMapsMock() { 118 this.updateCachedSourceMaps(); 119 } 120 121 buildModuleSourceMapInfoMock() { 122 this.buildModuleSourceMapInfo(); 123 } 124 125 checkModuleSourceMapInfoMock(): boolean { 126 const readSourceMap = fs.readFileSync(this.sourceMapPath, 'utf-8'); 127 const readCacheSourceMap = fs.readFileSync(this.cacheSourceMapPath, 'utf-8'); 128 if (readSourceMap.length == 0 && readCacheSourceMap.length == 0) { 129 return true; 130 } else if (readSourceMap === readCacheSourceMap) { 131 return true; 132 } else { 133 return false; 134 } 135 } 136} 137 138export default ModuleModeMock;