• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023-2025 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, writeDeclarationFiles } from '../../ark_utils';
27import { cleanUpKitImportObjects } from '../../process_kit_import';
28import { cleanUpFilesList } from './utils';
29import { CommonLogger } from './logger';
30import {
31  getHookEventFactory,
32  cleanUpAsyncEvents,
33  CompileEvent
34} from '../../performance';
35import { BytecodeObfuscator } from './bytecode_obfuscator';
36import { PreloadFileModules } from './module/module_preload_file_utils';
37import {
38  FileManager,
39  isMixCompile
40} from './interop/interop_manager';
41import { cleanUpProcessArkTSEvolutionObj } from './interop/process_arkts_evolution';
42
43export function genAbc() {
44  return {
45    name: 'genAbc',
46    buildStart() {
47      this.share.arkProjectConfig = initArkProjectConfig(this.share);
48      checkArkCompilerCacheInfo(this);
49      //Because calling the method of SourceMapGenerator may not retrieve the rollupObject
50      //it is necessary to assign the rollupObject to SourceMapGenerator in the early stages of build
51      SourceMapGenerator.init(this);
52      // Check proload path exist or not, initialize preloadBack path
53      PreloadFileModules.initialize(this);
54    },
55    shouldInvalidCache: shouldInvalidCache,
56    transform: transformForModule,
57    moduleParsed(moduleInfo: moduleInfoType): void {
58      // Process single ModuleSourceFile
59      if (!this.share.projectConfig.singleFileEmit) {
60        return;
61      }
62      const hookEventFactory: CompileEvent | undefined = getHookEventFactory(this.share, 'genAbc', 'moduleParsed');
63      ModuleSourceFile.processSingleModuleSourceFile(this, moduleInfo.id, hookEventFactory);
64    },
65    beforeBuildEnd: {
66      // [pre] means this handler running in first at the stage of beforeBuildEnd.
67      order: 'pre',
68      handler() {
69        if (this.share.projectConfig.singleFileEmit) {
70          writeDeclarationFiles(this.share.arkProjectConfig.compileMode);
71          return;
72        }
73        if (compilerOptions.needDoArkTsLinter) {
74          checkIfJsImportingArkts(this);
75        }
76        if (this.share.projectConfig.needCoverageInsert) {
77          this.share.ModuleSourceFile = ModuleSourceFile.getSourceFiles();
78        }
79      }
80    },
81    buildEnd: generateModuleAbc,
82    generateBundle: generateBundleAbc,
83    cleanUp: () => {
84      SourceMapGenerator.cleanSourceMapObject();
85      cleanUpUtilsObjects();
86      cleanUpKitImportObjects();
87      cleanUpFilesList();
88      cleanModuleMode();
89      ModuleSourceFile.cleanUpObjects();
90      cleanSharedModuleSet();
91      CommonLogger.destroyInstance();
92      cleanUpAsyncEvents();
93      BytecodeObfuscator.cleanBcObfuscatorObject();
94      PreloadFileModules.cleanUpPreloadSoObjects();
95      isMixCompile() && cleanUpProcessArkTSEvolutionObj();
96      isMixCompile() && FileManager.cleanFileManagerObject();
97    }
98  };
99}
100
101interface moduleInfoType {
102  id: string;
103};