• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 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 { CompileFileInfo, ModuleInfo } from '../types';
17import * as fs from 'fs';
18import * as path from 'path';
19import { ensurePathExists } from '../utils';
20import { KOALA_WRAPPER_PATH_FROM_SDK } from '../pre_define';
21import { PluginDriver, PluginHook } from '../plugins/plugins_driver';
22import { BuildConfig } from '../types';
23import { BUILD_MODE } from '../pre_define';
24import { Logger } from '../logger';
25
26process.on('message', (message: {
27  taskList: CompileFileInfo[];
28  buildConfig: BuildConfig;
29  moduleInfos: Array<[string, ModuleInfo]>;
30}) => {
31  if (!process.send) {
32    throw new Error('process.send is undefined. This worker must be run as a forked process.');
33  }
34  const { taskList, buildConfig, moduleInfos } = message;
35  const isDebug = buildConfig.buildMode === BUILD_MODE.DEBUG;
36
37  Logger.getInstance(buildConfig);
38  PluginDriver.getInstance().initPlugins(buildConfig);
39  const koalaWrapperPath = path.resolve(buildConfig.buildSdkPath, KOALA_WRAPPER_PATH_FROM_SDK);
40  let { arkts, arktsGlobal } = require(koalaWrapperPath);
41
42  for (const fileInfo of taskList) {
43    let errorStatus = false;
44    try {
45      ensurePathExists(fileInfo.abcFilePath);
46      const source = fs.readFileSync(fileInfo.filePath).toString();
47
48      const ets2pandaCmd = [
49        '_', '--extension', 'ets',
50        '--arktsconfig', fileInfo.arktsConfigFile,
51        '--output', fileInfo.abcFilePath,
52      ];
53      if (isDebug) {
54        ets2pandaCmd.push('--debug-info');
55      }
56      ets2pandaCmd.push(fileInfo.filePath);
57
58      arktsGlobal.filePath = fileInfo.filePath;
59      arktsGlobal.config = arkts.Config.create(ets2pandaCmd).peer;
60      arktsGlobal.compilerContext = arkts.Context.createFromString(source);
61
62      PluginDriver.getInstance().getPluginContext().setArkTSProgram(arktsGlobal.compilerContext.program);
63
64      arkts.proceedToState(arkts.Es2pandaContextState.ES2PANDA_STATE_PARSED);
65      PluginDriver.getInstance().runPluginHook(PluginHook.PARSED);
66
67      arkts.proceedToState(arkts.Es2pandaContextState.ES2PANDA_STATE_CHECKED);
68      PluginDriver.getInstance().runPluginHook(PluginHook.CHECKED);
69
70      arkts.proceedToState(arkts.Es2pandaContextState.ES2PANDA_STATE_BIN_GENERATED);
71    } catch (error) {
72      errorStatus = true;
73      if (error instanceof Error) {
74        process.send({
75          success: false,
76          filePath: fileInfo.filePath,
77          error: 'Compile abc files failed.\n' + error.message
78        });
79      }
80    } finally {
81      if (!errorStatus) {
82        // when error occur,wrapper will destroy context.
83        arktsGlobal.es2panda._DestroyContext(arktsGlobal.compilerContext.peer);
84      }
85      PluginDriver.getInstance().runPluginHook(PluginHook.CLEAN);
86      arkts.destroyConfig(arktsGlobal.config);
87    }
88  }
89
90  process.exit(0);
91});