From b850123d168ed6f975a4619f610c3fe9d71598a4 Mon Sep 17 00:00:00 2001 From: zhangrengao Date: Tue, 21 Jun 2022 10:42:47 +0800 Subject: [PATCH] adapter es2abc Change-Id: Ib2b35215b55963c1802d29fdbff9328db8ecc611 Signed-off-by: zhangrengao --- compiler/main.js | 1 + compiler/src/gen_abc.ts | 31 +++++++++++++++++++++++++-- compiler/src/gen_abc_plugin.ts | 39 +++++++++++++++++++++++++--------- compiler/src/pre_define.ts | 3 +++ compiler/webpack.config.js | 1 + 5 files changed, 63 insertions(+), 12 deletions(-) diff --git a/compiler/main.js b/compiler/main.js index edd1013..436a0bf 100644 --- a/compiler/main.js +++ b/compiler/main.js @@ -375,6 +375,7 @@ function loadModuleInfo(projectConfig, envArgs) { if (buildJsonInfo.compileMode === 'esmodule') { projectConfig.nodeModulesPath = buildJsonInfo.nodeModulesPath; } + projectConfig.pandaMode = buildJsonInfo.pandaMode; } } diff --git a/compiler/src/gen_abc.ts b/compiler/src/gen_abc.ts index 5043681..d39de1a 100644 --- a/compiler/src/gen_abc.ts +++ b/compiler/src/gen_abc.ts @@ -20,7 +20,9 @@ import cluster from 'cluster'; import { logger } from './compile_info'; import { SUCCESS, - FAIL + FAIL, + TS2ABC, + ES2ABC } from './pre_define'; const red: string = '\u001b[31m'; @@ -53,10 +55,35 @@ function js2abcByWorkers(jsonInput: string, cmd: string): Promise { return; } +function es2abcByWorkers(jsonInput: string, cmd: string): Promise { + const inputPaths: any = JSON.parse(jsonInput); + for (let i = 0; i < inputPaths.length; ++i) { + const input: string = inputPaths[i].path; + const abcFile: string = input.replace(/_.js$/, '.abc'); + const singleCmd: any = `${cmd} "${input}" --output "${abcFile}"`; + logger.debug('gen abc cmd is: ', singleCmd, ' ,file size is:', inputPaths[i].size, ' byte'); + try { + childProcess.execSync(singleCmd); + } catch (e) { + logger.error(red, `ETS:ERROR Failed to convert file ${input} to abc `, reset); + process.exit(FAIL); + } + } + + return; +} + logger.debug('worker data is: ', JSON.stringify(process.env)); logger.debug('gen_abc isWorker is: ', cluster.isWorker); if (cluster.isWorker && process.env['inputs'] !== undefined && process.env['cmd'] !== undefined) { logger.debug('==>worker #', cluster.worker.id, 'started!'); - js2abcByWorkers(process.env['inputs'], process.env['cmd']); + if (process.env.panda === TS2ABC) { + js2abcByWorkers(process.env['inputs'], process.env['cmd']); + } else if (process.env.panda === ES2ABC || process.env.panda === 'undefined' || process.env.panda === undefined) { + es2abcByWorkers(process.env['inputs'], process.env['cmd']); + } else { + logger.error(red, `ETS:ERROR please set panda module`, reset); + process.exit(FAIL); + } process.exit(SUCCESS); } diff --git a/compiler/src/gen_abc_plugin.ts b/compiler/src/gen_abc_plugin.ts index e864817..c0e3355 100644 --- a/compiler/src/gen_abc_plugin.ts +++ b/compiler/src/gen_abc_plugin.ts @@ -43,7 +43,9 @@ import { EXTNAME_D_TS, EXTNAME_ABC, FAIL, - EXTNAME_JS_MAP + EXTNAME_JS_MAP, + TS2ABC, + ES2ABC } from './pre_define'; const firstFileEXT: string = '_.js'; @@ -556,22 +558,39 @@ function judgeModuleWorkersToGenAbc(callback): void { function invokeWorkersToGenAbc(): void { let param: string = ''; - if (isDebug) { - param += ' --debug'; - } + let cmdPrefix: string = ''; - let js2abc: string = path.join(arkDir, 'build', 'src', 'index.js'); - if (isWin) { - js2abc = path.join(arkDir, 'build-win', 'src', 'index.js'); - } else if (isMac) { - js2abc = path.join(arkDir, 'build-mac', 'src', 'index.js'); + if (process.env.panda === TS2ABC) { + if (isDebug) { + param += ' --debug'; + } + + let js2abc: string = path.join(arkDir, 'build', 'src', 'index.js'); + if (isWin) { + js2abc = path.join(arkDir, 'build-win', 'src', 'index.js'); + } else if (isMac) { + js2abc = path.join(arkDir, 'build-mac', 'src', 'index.js'); + } + cmdPrefix = `${nodeJs} --expose-gc "${js2abc}" ${param} `; + } else if (process.env.panda === ES2ABC || process.env.panda === 'undefined' || process.env.panda === undefined) { + if (isDebug) { + param += ' --debug-info'; + } + let es2abc: string = path.join(arkDir, 'build', 'bin', 'es2abc'); + if (isWin) { + es2abc = path.join(arkDir, 'build-win', 'bin', 'es2abc.exe'); + } else if (isMac) { + es2abc = path.join(arkDir, 'build-mac', 'bin', 'es2abc'); + } + cmdPrefix = `"${es2abc}" ${param}`; + } else { + logger.error(red, `ETS:ERROR please set panda module`, reset); } filterIntermediateJsBundleByHashJson(buildPathInfo, intermediateJsBundle); const maxWorkerNumber: number = 3; const splitedBundles: any[] = splitJsBundlesBySize(fileterIntermediateJsBundle, maxWorkerNumber); const workerNumber: number = maxWorkerNumber < splitedBundles.length ? maxWorkerNumber : splitedBundles.length; - const cmdPrefix: string = `${nodeJs} --expose-gc "${js2abc}" ${param} `; const clusterNewApiVersion: number = 16; const currentNodeVersion: number = parseInt(process.version.split('.')[0]); diff --git a/compiler/src/pre_define.ts b/compiler/src/pre_define.ts index 73f35ad..54ce5cc 100644 --- a/compiler/src/pre_define.ts +++ b/compiler/src/pre_define.ts @@ -263,3 +263,6 @@ export const EXTNAME_ABC: string = '.abc'; export const SUCCESS: number = 0; export const FAIL: number = 1; + +export const TS2ABC: string = 'ts2abc'; +export const ES2ABC: string = 'es2abc'; \ No newline at end of file diff --git a/compiler/webpack.config.js b/compiler/webpack.config.js index bb91ff4..5150853 100644 --- a/compiler/webpack.config.js +++ b/compiler/webpack.config.js @@ -312,6 +312,7 @@ function setTsConfigFile() { function setGenAbcPlugin(env, config) { process.env.compilerType = 'ark'; + process.env.panda = projectConfig.pandaMode; let arkDir = path.join(__dirname, 'bin', 'ark'); if (env.arkFrontendDir) { arkDir = env.arkFrontendDir;