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 Cache from "./cache"; 17import Share from "./share"; 18import path from "path"; 19import fs from "fs"; 20import ModuleInfo from "./module_info"; 21import { 22 SDK_VERSION, 23 BUNDLE_NAME_DEFAULT, 24 ETS_LOADER_VERSION, 25 RUNTIME_OS_OPENHARMONY, 26 MODULE_NAME_HASH_DEFAULT 27} from "./common"; 28import { 29 DEFAULT_PROJECT, 30 MODULE_ID_ROLLUP_PLACEHOLDER, 31 NODE_MODULES_PATH 32} from "./path_config"; 33import { scanFiles } from "../../utils/utils"; 34import { IArkProjectConfig } from "./project_config"; 35import { 36 ESMODULE, 37 RELEASE, 38 DEBUG, 39 ARK_COMPILER_META_INFO, 40 IS_CACHE_INVALID 41} from "../../../../lib/fast_build/ark_compiler/common/ark_define"; 42class RollUpPluginMock { 43 cache: Cache; 44 meta: object = { rollupVersion: '3.10.0', watchMode: false }; 45 moduleIds: object; 46 share: Share; 47 moduleInfos: Array<ModuleInfo>; 48 49 private isPreview: boolean = false; 50 51 constructor() { 52 this.cache = new Cache(); 53 } 54 55 public preConfig(buildMode: string = DEBUG) { 56 this.share = new Share(buildMode); 57 } 58 59 public build(buildMode: string = DEBUG) { 60 this.isPreview = false; 61 this.share = new Share(buildMode); 62 63 this.share.projectConfig.setPreview(this.isPreview); 64 this.meta.watchMode = this.isPreview; 65 66 this.doBuild(DEFAULT_PROJECT); 67 } 68 69 public chooseTestData(testcase: string) { 70 if (!this.share) { 71 throw new Error('Call build API first.'); 72 } 73 this.doBuild(testcase); 74 } 75 76 public preview(testcase: string = DEFAULT_PROJECT) { 77 this.isPreview = true; 78 this.share = new Share(DEBUG); 79 80 this.share.projectConfig.setPreview(this.isPreview); 81 this.meta.watchMode = this.isPreview; 82 83 this.doBuild(testcase); 84 } 85 86 public hotReload(testcase: string = DEFAULT_PROJECT) { 87 this.isPreview = false; 88 this.share = new Share(DEBUG); 89 90 this.share.projectConfig.setPreview(this.isPreview); 91 this.meta.watchMode = this.isPreview; 92 93 this.doBuild(testcase); 94 } 95 96 private doBuild(testcase: string) { 97 this.share.scan(testcase); 98 this.load(); 99 100 // mock ets-loader build start 101 this.share.arkProjectConfig = this.mockArkProjectConfig(); 102 this.mockCheckArkCompilerCacheInfo(); 103 } 104 105 private mockArkProjectConfig(): IArkProjectConfig { 106 const mode = this.isPreview ? 'preview' : 'build'; 107 const projectRootDir = this.share.projectConfig.projectTopDir; 108 const entryName = this.share.projectConfig.entryModuleName; 109 110 return { 111 projectRootPath: projectRootDir, 112 modulePathMap: { entry: `${projectRootDir}/${entryName}` }, 113 isOhosTest: undefined, 114 processTs: false, 115 pandaMode: undefined, 116 nodeModulesPath: `${projectRootDir}/${entryName}/${mode}/${NODE_MODULES_PATH}`, 117 harNameOhmMap: {}, 118 minPlatformVersion: SDK_VERSION, 119 moduleName: `${entryName}`, 120 bundleName: BUNDLE_NAME_DEFAULT, 121 hotReload: undefined, 122 patchAbcPath: undefined, 123 changedFileList: undefined, 124 compileMode: ESMODULE 125 } 126 } 127 128 private mockCheckArkCompilerCacheInfo(): void { 129 const metaInfos = [ 130 SDK_VERSION, 131 SDK_VERSION, 132 RUNTIME_OS_OPENHARMONY, 133 `/OpenHarmony/Sdk/${SDK_VERSION}/ets/build-tools/app`, 134 ETS_LOADER_VERSION, 135 RELEASE, 136 BUNDLE_NAME_DEFAULT, 137 MODULE_NAME_HASH_DEFAULT, 138 'null_aotCompileMode', 139 'null_apPath' 140 ] 141 const metaInfo = metaInfos.join(':'); 142 this.cache.set(IS_CACHE_INVALID, true); 143 this.cache.set(ARK_COMPILER_META_INFO, metaInfo); 144 } 145 146 public addWatchFile() { } 147 148 public async(func: Function) { 149 if (func) { 150 func(); 151 } 152 } 153 154 public block() { } 155 156 public emitFile() { } 157 158 public error() { } 159 160 public getFileName() { } 161 162 public getModuleIds(): IterableIterator<string> { 163 return this.share.allFiles ? this.share.allFiles.values() : undefined; 164 } 165 166 public getModuleInfo(id: string) { 167 for (let i = 0; i < this.moduleInfos.length - 1; i++) { 168 return this.moduleInfos.find(item => item.id === id); 169 } 170 } 171 172 public getWatchFiles() { } 173 174 public load() { 175 // load project files list 176 this.share.allFiles = new Set<string>(); 177 if (fs.existsSync(this.share.projectConfig.projectPath)) { 178 scanFiles(this.share.projectConfig.projectPath, this.share.allFiles); 179 } else { 180 const tsFilePath = path.join(this.share.projectConfig.projectPath, '/entryability/EntryAbility.ts'); 181 const jsFilePath = path.join(this.share.projectConfig.projectPath, '/entryability/EntryAbility.js'); 182 const etsFilePath = path.join(this.share.projectConfig.projectPath, '/pages/Index.ets'); 183 this.share.allFiles.add(tsFilePath); 184 this.share.allFiles.add(jsFilePath); 185 this.share.allFiles.add(etsFilePath); 186 } 187 this.share.allFiles.add(MODULE_ID_ROLLUP_PLACEHOLDER); 188 189 // load all files module info 190 const allFiles = Array.from(this.share.allFiles); 191 this.moduleInfos = new Array<ModuleInfo>(); 192 for (let i = 0; i < allFiles.length - 1; i++) { 193 this.moduleInfos.push(new ModuleInfo(allFiles[i], 194 this.share.projectConfig.entryModuleName, 195 this.share.projectConfig.modulePath)); 196 } 197 } 198 199 public parse() { } 200 201 public resolve() { } 202 203 public setAssetSource() { } 204 205 public signal() { } 206 207 public warn() { } 208 209 public clearCache() { 210 this.cache.set(IS_CACHE_INVALID, false); 211 this.cache.set(ARK_COMPILER_META_INFO, undefined); 212 } 213 214 public mockProjectConfigMockParams() { 215 this.share.setMockParams(); 216 } 217} 218 219export default RollUpPluginMock; 220