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 { initArkProjectConfig } from './common/process_ark_config';
17import { generateBundleAbc } from './generate_bundle_abc';
18import { generateModuleAbc, cleanModuleMode } from './generate_module_abc';
19import { transformForModule } from './transform';
20import { checkArkCompilerCacheInfo, shouldInvalidCache } from './cache';
21import { checkIfJsImportingArkts } from './check_import_module';
22import { cleanSharedModuleSet } from './check_shared_module';
23import { compilerOptions } from '../../ets_checker';
24import { ModuleSourceFile } from './module/module_source_file';
25import { SourceMapGenerator } from './generate_sourcemap';
26import { cleanUpUtilsObjects } from '../../ark_utils';
27import { cleanUpKitImportObjects } from '../../process_kit_import';
28import { cleanUpFilesList } from './utils';
29
30export function genAbc() {
31  return {
32    name: 'genAbc',
33    buildStart() {
34      this.share.arkProjectConfig = initArkProjectConfig(this.share);
35      checkArkCompilerCacheInfo(this);
36      //Because calling the method of SourceMapGenerator may not retrieve the rollupObject
37      //it is necessary to assign the rollupObject to SourceMapGenerator in the early stages of build
38      SourceMapGenerator.init(this);
39    },
40    shouldInvalidCache: shouldInvalidCache,
41    transform: transformForModule,
42    beforeBuildEnd: {
43      // [pre] means this handler running in first at the stage of beforeBuildEnd.
44      order: 'pre',
45      handler() {
46        if (compilerOptions.needDoArkTsLinter) {
47          checkIfJsImportingArkts(this);
48        }
49        if (this.share.projectConfig.needCoverageInsert) {
50          this.share.ModuleSourceFile = ModuleSourceFile.getSourceFiles();
51        }
52      }
53    },
54    buildEnd: generateModuleAbc,
55    generateBundle: generateBundleAbc,
56    cleanUp: () => {
57      SourceMapGenerator.cleanSourceMapObject();
58      cleanUpUtilsObjects();
59      cleanUpKitImportObjects();
60      cleanUpFilesList();
61      cleanModuleMode();
62      ModuleSourceFile.cleanUpObjects();
63      cleanSharedModuleSet();
64    }
65  };
66}
67