107ac75b1Sopenharmony_ci/*
207ac75b1Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
307ac75b1Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
407ac75b1Sopenharmony_ci * you may not use this file except in compliance with the License.
507ac75b1Sopenharmony_ci * You may obtain a copy of the License at
607ac75b1Sopenharmony_ci *
707ac75b1Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
807ac75b1Sopenharmony_ci *
907ac75b1Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1007ac75b1Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1107ac75b1Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1207ac75b1Sopenharmony_ci * See the License for the specific language governing permissions and
1307ac75b1Sopenharmony_ci * limitations under the License.
1407ac75b1Sopenharmony_ci */
1507ac75b1Sopenharmony_ci
1607ac75b1Sopenharmony_ciimport fs from 'fs';
1707ac75b1Sopenharmony_ciimport path from 'path';
1807ac75b1Sopenharmony_ciimport {
1907ac75b1Sopenharmony_ci  ARK_COMPILER_META_INFO,
2007ac75b1Sopenharmony_ci  ESMODULE,
2107ac75b1Sopenharmony_ci  IS_CACHE_INVALID
2207ac75b1Sopenharmony_ci} from './common/ark_define';
2307ac75b1Sopenharmony_ciimport {
2407ac75b1Sopenharmony_ci  TRANSFORMED_MOCK_CONFIG,
2507ac75b1Sopenharmony_ci  USER_DEFINE_MOCK_CONFIG
2607ac75b1Sopenharmony_ci} from '../../pre_define';
2707ac75b1Sopenharmony_ci
2807ac75b1Sopenharmony_cilet disableCache: boolean = false;
2907ac75b1Sopenharmony_ciexport function checkArkCompilerCacheInfo(rollupObject: Object): void {
3007ac75b1Sopenharmony_ci  disableCache = false;
3107ac75b1Sopenharmony_ci  const metaInfo: string = getMetaInfo(rollupObject.share.projectConfig, rollupObject.share.arkProjectConfig);
3207ac75b1Sopenharmony_ci  const lastMetaInfo: string = rollupObject.cache.get(ARK_COMPILER_META_INFO);
3307ac75b1Sopenharmony_ci  if (!lastMetaInfo || metaInfo !== lastMetaInfo || isMockConfigModified(rollupObject)) {
3407ac75b1Sopenharmony_ci    rollupObject.cache.set(IS_CACHE_INVALID, true);
3507ac75b1Sopenharmony_ci    disableCache = true;
3607ac75b1Sopenharmony_ci  }
3707ac75b1Sopenharmony_ci  rollupObject.cache.set(ARK_COMPILER_META_INFO, metaInfo);
3807ac75b1Sopenharmony_ci}
3907ac75b1Sopenharmony_ci
4007ac75b1Sopenharmony_cifunction getMetaInfo(projectConfig: Object, arkProjectConfig: Object): string {
4107ac75b1Sopenharmony_ci  let metaInfoArr: string[] = [];
4207ac75b1Sopenharmony_ci  // user selects the compiled API version information
4307ac75b1Sopenharmony_ci  const compileSdkVersion: string = projectConfig.compileSdkVersion ?
4407ac75b1Sopenharmony_ci    projectConfig.compileSdkVersion : 'null_compileSdkVersion';
4507ac75b1Sopenharmony_ci  // user selects the compatible API version information
4607ac75b1Sopenharmony_ci  const compatibleSdkVersion: string = projectConfig.compatibleSdkVersion ?
4707ac75b1Sopenharmony_ci    projectConfig.compatibleSdkVersion : 'null_compatibleSdkVersion';
4807ac75b1Sopenharmony_ci  const compatibleSdkVersionStage: string = projectConfig.compatibleSdkVersionStage ?
4907ac75b1Sopenharmony_ci    projectConfig.compatibleSdkVersionStage : 'null_compatibleSdkVersionStage';
5007ac75b1Sopenharmony_ci  const runtimeOS: string = projectConfig.runtimeOS ? projectConfig.runtimeOS : 'null_runtimeOS';
5107ac75b1Sopenharmony_ci  const sdkPath: string = projectConfig.etsLoaderPath ?
5207ac75b1Sopenharmony_ci    projectConfig.etsLoaderPath : 'null_sdkPath';
5307ac75b1Sopenharmony_ci  // version information for loading SDKs in the IDE
5407ac75b1Sopenharmony_ci  const sdkVersion: string = projectConfig.etsLoaderVersion ?
5507ac75b1Sopenharmony_ci    projectConfig.etsLoaderVersion : 'null_sdkVersion';
5607ac75b1Sopenharmony_ci  const sdkReleaseType: string = projectConfig.etsLoaderReleaseType ?
5707ac75b1Sopenharmony_ci    projectConfig.etsLoaderReleaseType : 'null_sdkReleaseType';
5807ac75b1Sopenharmony_ci  metaInfoArr.push(
5907ac75b1Sopenharmony_ci    compileSdkVersion, compatibleSdkVersion, compatibleSdkVersionStage, runtimeOS, sdkPath, sdkVersion, sdkReleaseType);
6007ac75b1Sopenharmony_ci
6107ac75b1Sopenharmony_ci  if (projectConfig.compileHar) {
6207ac75b1Sopenharmony_ci    const useTsHar: string = arkProjectConfig.useTsHar;
6307ac75b1Sopenharmony_ci    metaInfoArr.push(useTsHar);
6407ac75b1Sopenharmony_ci  }
6507ac75b1Sopenharmony_ci
6607ac75b1Sopenharmony_ci  if (projectConfig.compileMode === ESMODULE) {
6707ac75b1Sopenharmony_ci    const bundleName: string = projectConfig.bundleName ? projectConfig.bundleName : 'null_bundleName';
6807ac75b1Sopenharmony_ci    const allModuleNameHash: string = projectConfig.allModuleNameHash ? projectConfig.allModuleNameHash :
6907ac75b1Sopenharmony_ci      'null_allModuleNameHash';
7007ac75b1Sopenharmony_ci    const aotCompileMode: string = projectConfig.aotCompileMode ? projectConfig.aotCompileMode : 'null_aotCompileMode';
7107ac75b1Sopenharmony_ci    const apPath: string = projectConfig.apPath ? projectConfig.apPath : 'null_apPath';
7207ac75b1Sopenharmony_ci    metaInfoArr.push(bundleName, allModuleNameHash, aotCompileMode, apPath);
7307ac75b1Sopenharmony_ci  }
7407ac75b1Sopenharmony_ci
7507ac75b1Sopenharmony_ci  return metaInfoArr.join(':');
7607ac75b1Sopenharmony_ci}
7707ac75b1Sopenharmony_ci
7807ac75b1Sopenharmony_cifunction isMockConfigModified(rollupObject): boolean {
7907ac75b1Sopenharmony_ci  // mock is only enabled in the following two modes
8007ac75b1Sopenharmony_ci  if (!rollupObject.share.projectConfig.isPreview && !rollupObject.share.projectConfig.isOhosTest) {
8107ac75b1Sopenharmony_ci    return false;
8207ac75b1Sopenharmony_ci  }
8307ac75b1Sopenharmony_ci
8407ac75b1Sopenharmony_ci  if (!rollupObject.share.projectConfig.mockParams || !rollupObject.share.projectConfig.mockParams.mockConfigPath) {
8507ac75b1Sopenharmony_ci    return false;
8607ac75b1Sopenharmony_ci  }
8707ac75b1Sopenharmony_ci
8807ac75b1Sopenharmony_ci  const userDefinedMockConfigCache: string =
8907ac75b1Sopenharmony_ci    path.resolve(rollupObject.share.projectConfig.cachePath, `./${USER_DEFINE_MOCK_CONFIG}`);
9007ac75b1Sopenharmony_ci  const transformedMockConfigCache: string =
9107ac75b1Sopenharmony_ci    path.resolve(rollupObject.share.projectConfig.cachePath, `./${TRANSFORMED_MOCK_CONFIG}`);
9207ac75b1Sopenharmony_ci  if (!fs.existsSync(userDefinedMockConfigCache) || !fs.existsSync(transformedMockConfigCache)) {
9307ac75b1Sopenharmony_ci    return true;
9407ac75b1Sopenharmony_ci  }
9507ac75b1Sopenharmony_ci
9607ac75b1Sopenharmony_ci  const mockConfigInfo: Object =
9707ac75b1Sopenharmony_ci    require('json5').parse(fs.readFileSync(rollupObject.share.projectConfig.mockParams.mockConfigPath, 'utf-8'));
9807ac75b1Sopenharmony_ci  const cachedMockConfigInfo: Object =
9907ac75b1Sopenharmony_ci    require('json5').parse(fs.readFileSync(userDefinedMockConfigCache, 'utf-8'));
10007ac75b1Sopenharmony_ci  return JSON.stringify(mockConfigInfo) !== JSON.stringify(cachedMockConfigInfo);
10107ac75b1Sopenharmony_ci}
10207ac75b1Sopenharmony_ci
10307ac75b1Sopenharmony_ci/**
10407ac75b1Sopenharmony_ci * rollup shouldInvalidCache hook
10507ac75b1Sopenharmony_ci * @param {rollup OutputOptions} options
10607ac75b1Sopenharmony_ci */
10707ac75b1Sopenharmony_ciexport function shouldInvalidCache(): boolean {
10807ac75b1Sopenharmony_ci  return disableCache;
10907ac75b1Sopenharmony_ci}
11007ac75b1Sopenharmony_ciexport const utCache = {
11107ac75b1Sopenharmony_ci  getMetaInfo
11207ac75b1Sopenharmony_ci};
113