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 fs from 'fs'; 17import path from 'path'; 18import { 19 ARK_COMPILER_META_INFO, 20 ESMODULE, 21 IS_CACHE_INVALID 22} from './common/ark_define'; 23import { 24 TRANSFORMED_MOCK_CONFIG, 25 USER_DEFINE_MOCK_CONFIG 26} from '../../pre_define'; 27 28let disableCache: boolean = false; 29export function checkArkCompilerCacheInfo(rollupObject: Object): void { 30 disableCache = false; 31 const metaInfo: string = getMetaInfo(rollupObject.share.projectConfig); 32 const lastMetaInfo: string = rollupObject.cache.get(ARK_COMPILER_META_INFO); 33 if (!lastMetaInfo || metaInfo !== lastMetaInfo || isMockConfigModified(rollupObject)) { 34 rollupObject.cache.set(IS_CACHE_INVALID, true); 35 disableCache = true; 36 } 37 rollupObject.cache.set(ARK_COMPILER_META_INFO, metaInfo); 38} 39 40function getMetaInfo(projectConfig: Object): string { 41 let metaInfoArr: string[] = []; 42 // user selects the compiled API version information 43 const compileSdkVersion: string = projectConfig.compileSdkVersion ? 44 projectConfig.compileSdkVersion : 'null_compileSdkVersion'; 45 // user selects the compatible API version information 46 const compatibleSdkVersion: string = projectConfig.compatibleSdkVersion ? 47 projectConfig.compatibleSdkVersion : 'null_compatibleSdkVersion'; 48 const runtimeOS: string = projectConfig.runtimeOS ? projectConfig.runtimeOS : 'null_runtimeOS'; 49 const sdkPath: string = projectConfig.etsLoaderPath ? 50 projectConfig.etsLoaderPath : 'null_sdkPath'; 51 // version information for loading SDKs in the IDE 52 const sdkVersion: string = projectConfig.etsLoaderVersion ? 53 projectConfig.etsLoaderVersion : 'null_sdkVersion'; 54 const sdkReleaseType: string = projectConfig.etsLoaderReleaseType ? 55 projectConfig.etsLoaderReleaseType : 'null_sdkReleaseType'; 56 metaInfoArr.push(compileSdkVersion, compatibleSdkVersion, runtimeOS, sdkPath, sdkVersion, sdkReleaseType); 57 58 if (projectConfig.compileMode === ESMODULE) { 59 const bundleName: string = projectConfig.bundleName ? projectConfig.bundleName : 'null_bundleName'; 60 const allModuleNameHash: string = projectConfig.allModuleNameHash ? projectConfig.allModuleNameHash : 61 'null_allModuleNameHash'; 62 const aotCompileMode: string = projectConfig.aotCompileMode ? projectConfig.aotCompileMode : 'null_aotCompileMode'; 63 const apPath: string = projectConfig.apPath ? projectConfig.apPath : 'null_apPath'; 64 metaInfoArr.push(bundleName, allModuleNameHash, aotCompileMode, apPath); 65 } 66 67 return metaInfoArr.join(':'); 68} 69 70function isMockConfigModified(rollupObject): boolean { 71 // mock is only enabled in the following two modes 72 if (!rollupObject.share.projectConfig.isPreview && !rollupObject.share.projectConfig.isOhosTest) { 73 return false; 74 } 75 76 if (!rollupObject.share.projectConfig.mockParams || !rollupObject.share.projectConfig.mockParams.mockConfigPath) { 77 return false; 78 } 79 80 const userDefinedMockConfigCache: string = 81 path.resolve(rollupObject.share.projectConfig.cachePath, `./${USER_DEFINE_MOCK_CONFIG}`); 82 const transformedMockConfigCache: string = 83 path.resolve(rollupObject.share.projectConfig.cachePath, `./${TRANSFORMED_MOCK_CONFIG}`); 84 if (!fs.existsSync(userDefinedMockConfigCache) || !fs.existsSync(transformedMockConfigCache)) { 85 return true; 86 } 87 88 const mockConfigInfo: Object = 89 require('json5').parse(fs.readFileSync(rollupObject.share.projectConfig.mockParams.mockConfigPath, 'utf-8')); 90 const cachedMockConfigInfo: Object = 91 require('json5').parse(fs.readFileSync(userDefinedMockConfigCache, 'utf-8')); 92 return JSON.stringify(mockConfigInfo) !== JSON.stringify(cachedMockConfigInfo); 93} 94 95/** 96 * rollup shouldInvalidCache hook 97 * @param {rollup OutputOptions} options 98 */ 99export function shouldInvalidCache(): boolean { 100 return disableCache; 101} 102export const utCache = { 103 getMetaInfo 104}; 105