• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 { ESMODULE } from './common/ark_define';
17import { ModuleBuildMode } from './module/module_build_mode';
18import { ModuleHotfixMode } from './module/module_hotfix_mode';
19import { ModuleHotreloadMode } from './module/module_hotreload_mode';
20import { ModulePreviewMode } from './module/module_preview_mode';
21import { ModuleSourceFile } from './module/module_source_file';
22import {
23  getHookEventFactory,
24  createAndStartEvent,
25  stopEvent
26} from '../../ark_utils';
27import type { ModuleMode } from './module/module_mode';
28
29let moduleMode: ModuleMode = null;
30
31export async function generateModuleAbc(error) {
32  const hookEventFactory = getHookEventFactory(this.share, 'genAbc', 'buildEnd');
33  if (error) {
34    // When error thrown in previous plugins, rollup will catch and call buildEnd plugin.
35    // Stop generate abc if error exists
36    return;
37  }
38  if (this.share.projectConfig.compileMode === ESMODULE) {
39    await ModuleSourceFile.processModuleSourceFiles(this, hookEventFactory);
40    if (this.share.projectConfig.compileHar) {
41      // compileHar: compile closed source har of project, which convert .ets to .d.ts and js, doesn't emit abc.
42      return;
43    }
44    generateAbc(this, hookEventFactory);
45  }
46}
47
48function generateAbc(rollupObject: Object, parentEvent: Object): void {
49  const eventGenerateAbc = createAndStartEvent(parentEvent, 'generate abc');
50  if (rollupObject.share.projectConfig.watchMode !== 'true') {
51    const moduleBuildMode: ModuleBuildMode = new ModuleBuildMode(rollupObject);
52    moduleBuildMode.generateAbc(rollupObject, eventGenerateAbc);
53    moduleMode = moduleBuildMode;
54  } else if (rollupObject.share.arkProjectConfig.hotReload) {
55    const moduleHotreloadMode: ModuleHotreloadMode = new ModuleHotreloadMode(rollupObject);
56    moduleHotreloadMode.generateAbc(rollupObject, eventGenerateAbc);
57    moduleMode = moduleHotreloadMode;
58  } else if (rollupObject.share.arkProjectConfig.hotFix) {
59    const moduleHotfixMode: ModuleHotfixMode = new ModuleHotfixMode(rollupObject);
60    moduleHotfixMode.generateAbc(rollupObject, eventGenerateAbc);
61    moduleMode = moduleHotfixMode;
62  } else {
63    const modulePreviewMode: ModulePreviewMode = new ModulePreviewMode(rollupObject);
64    modulePreviewMode.generateAbc(rollupObject, eventGenerateAbc);
65    moduleMode = modulePreviewMode;
66  }
67  stopEvent(eventGenerateAbc);
68}
69
70export function cleanModuleMode():void {
71  if (moduleMode) {
72    moduleMode.triggerAsync = null;
73    moduleMode.triggerEndSignal = null;
74    moduleMode = null;
75  }
76}