• 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';
37
38export function genAbc() {
39  return {
40    name: 'genAbc',
41    buildStart() {
42      this.share.arkProjectConfig = initArkProjectConfig(this.share);
43      checkArkCompilerCacheInfo(this);
44      //Because calling the method of SourceMapGenerator may not retrieve the rollupObject
45      //it is necessary to assign the rollupObject to SourceMapGenerator in the early stages of build
46      SourceMapGenerator.init(this);
47      // Check proload path exist or not, initialize preloadBack path
48      PreloadFileModules.initialize(this);
49    },
50    shouldInvalidCache: shouldInvalidCache,
51    transform: transformForModule,
52    moduleParsed(moduleInfo: moduleInfoType): void {
53      // Process single ModuleSourceFile
54      if (!this.share.projectConfig.singleFileEmit) {
55        return;
56      }
57      const hookEventFactory: CompileEvent | undefined = getHookEventFactory(this.share, 'genAbc', 'moduleParsed');
58      ModuleSourceFile.processSingleModuleSourceFile(this, moduleInfo.id, hookEventFactory);
59    },
60    beforeBuildEnd: {
61      // [pre] means this handler running in first at the stage of beforeBuildEnd.
62      order: 'pre',
63      handler() {
64        if (this.share.projectConfig.singleFileEmit) {
65          writeDeclarationFiles(this.share.arkProjectConfig.compileMode);
66          return;
67        }
68        if (compilerOptions.needDoArkTsLinter) {
69          checkIfJsImportingArkts(this);
70        }
71        if (this.share.projectConfig.needCoverageInsert) {
72          this.share.ModuleSourceFile = ModuleSourceFile.getSourceFiles();
73        }
74      }
75    },
76    buildEnd: generateModuleAbc,
77    generateBundle: generateBundleAbc,
78    cleanUp: () => {
79      SourceMapGenerator.cleanSourceMapObject();
80      cleanUpUtilsObjects();
81      cleanUpKitImportObjects();
82      cleanUpFilesList();
83      cleanModuleMode();
84      ModuleSourceFile.cleanUpObjects();
85      cleanSharedModuleSet();
86      CommonLogger.destroyInstance();
87      cleanUpAsyncEvents();
88      BytecodeObfuscator.cleanBcObfuscatorObject();
89      PreloadFileModules.cleanUpPreloadSoObjects();
90    }
91  };
92}
93
94interface moduleInfoType {
95  id: string;
96};