From 1590db82352a5116435e90c7edc5167c6af21edd Mon Sep 17 00:00:00 2001 From: zhangrengao Date: Sun, 15 May 2022 14:21:02 +0800 Subject: [PATCH] fix multi abc bug Signed-off-by: zhangrengao Change-Id: Ia125560323166d642e2979e7864d649bbb45e842 --- compiler/src/gen_abc_plugin.ts | 49 +++++++++++++++++----------------- compiler/src/gen_module_abc.ts | 18 +++---------- compiler/src/utils.ts | 25 +++++++++++++---- compiler/webpack.config.js | 2 ++ 4 files changed, 51 insertions(+), 43 deletions(-) diff --git a/compiler/src/gen_abc_plugin.ts b/compiler/src/gen_abc_plugin.ts index b224093..533cd65 100644 --- a/compiler/src/gen_abc_plugin.ts +++ b/compiler/src/gen_abc_plugin.ts @@ -26,7 +26,8 @@ import { genTemporaryPath, genBuilldPath, genAbcFileName, - mkdirsSync} from './utils'; + mkdirsSync, + genSourceMapFileName} from './utils'; import { Compilation } from 'webpack'; import { projectConfig } from '../main'; @@ -95,22 +96,29 @@ export class GenAbcPlugin { } compiler.hooks.compilation.tap("GenAbcPlugin", (compilation) => { + if (process.env.moduleAbc === 'false') { + return ; + } buildPathInfo = output; compilation.hooks.finishModules.tap("finishModules", handleFinishModules.bind(this)); }); compiler.hooks.compilation.tap("GenAbcPlugin", (compilation) => { compilation.hooks.afterOptimizeTree.tap("afterOptimizeModules", (chunks, modules) => { - console.error("==================afterOptimizeModules===================="); + if (process.env.moduleAbc === 'false') { + return ; + } modules.forEach(module => { if (module != undefined && module.resourceResolveData != undefined) { let filePath: string = module.resourceResolveData.path; - console.error(filePath); } }); }); compilation.hooks.processAssets.tap("processAssets", (assets) => { + if (process.env.moduleAbc === 'false') { + return ; + } Object.keys(compilation.assets).forEach(key => { delete assets[key]; }) @@ -119,6 +127,9 @@ export class GenAbcPlugin { }) compiler.hooks.emit.tap('GenAbcPlugin', (compilation) => { + if (process.env.moduleAbc === 'true') { + return ; + } Object.keys(compilation.assets).forEach(key => { // choose *.js if (output && path.extname(key) === '.js') { @@ -130,8 +141,11 @@ export class GenAbcPlugin { }); compiler.hooks.afterEmit.tap('GenAbcPluginMultiThread', () => { + if (process.env.moduleAbc === 'true') { + return ; + } buildPathInfo = output; - // invokeWorkersToGenAbc(); + invokeWorkersToGenAbc(); }); @@ -139,12 +153,10 @@ export class GenAbcPlugin { } function handleFinishModules(modules, callback) { - console.error("==================handleFinishModules===================="); let nodeModulesFile = new Array(); modules.forEach(module => { if (module != undefined && module.resourceResolveData != undefined) { let filePath: string = module.resourceResolveData.path; - console.error(filePath); let tempFilePath = genTemporaryPath(filePath, projectConfig.projectPath, process.env.cachePath); let buildFilePath = genBuilldPath(filePath, projectConfig.projectPath, projectConfig.buildPath); tempFilePath = toUnixPath(tempFilePath); @@ -187,15 +199,12 @@ function handleFinishModules(modules, callback) { moduleInfos.push(tempModuleInfo); } } else { - console.error("=========================handleFinishModules else==================="); - console.error(filePath); + console.error("ETS error: Cannot find resolve this path: ", filePath); } } }); invokeWorkersModuleToGenAbc(moduleInfos); - processToAbcFile(nodeModulesFile); - } function processToAbcFile(nodeModulesFile: Array) { @@ -383,7 +392,6 @@ function invokeWorkersToGenAbc() { }); } - var count_ = 0; for (let i = 0; i < workerNumber; ++i) { const workerData = { 'inputs': JSON.stringify(splitedBundles[i]), @@ -393,15 +401,9 @@ function invokeWorkersToGenAbc() { } cluster.on('exit', (worker, code, signal) => { - count_++; logger.debug(`worker ${worker.process.pid} finished`); }); - while(true) { - if (count_ === workerNumber) { - break; - } - } process.on('exit', (code) => { writeHashJson(); }); @@ -436,11 +438,12 @@ function filterIntermediateModuleByHashJson(buildPath: string, moduleInfos: Arra if (jsonObject[input] === hashInputContentData && jsonObject[abcPath] === hashAbcContentData) { updateJsonObject[input] = hashInputContentData; updateJsonObject[abcPath] = hashAbcContentData; - console.error("===============filterIntermediateModuleByHashJson copy==============") - console.error(moduleInfos[i].tempFilePath); mkdirsSync(path.dirname(moduleInfos[i].buildFilePath)); fs.copyFileSync(moduleInfos[i].tempFilePath, moduleInfos[i].buildFilePath); fs.copyFileSync(genAbcFileName(moduleInfos[i].tempFilePath), genAbcFileName(moduleInfos[i].buildFilePath)); + if (process.env.buildMode == "debug" && fs.existsSync(genSourceMapFileName(moduleInfos[i].tempFilePath))) { + fs.copyFileSync(genSourceMapFileName(moduleInfos[i].tempFilePath), genSourceMapFileName(moduleInfos[i].buildFilePath)); + } } else { filterModuleInfos.push(moduleInfos[i]); } @@ -454,8 +457,6 @@ function filterIntermediateModuleByHashJson(buildPath: string, moduleInfos: Arra } function writeModuleHashJson() { - console.error("===============writeModuleHashJson==============") - console.error(filterModuleInfos); for (let i = 0; i < filterModuleInfos.length; ++i) { const input = filterModuleInfos[i].tempFilePath; const abcPath = filterModuleInfos[i].abcFilePath; @@ -468,11 +469,11 @@ function writeModuleHashJson() { moduleHashJsonObject[input] = hashInputContentData; moduleHashJsonObject[abcPath] = hashAbcContentData; mkdirsSync(path.dirname(filterModuleInfos[i].buildFilePath)); - console.error("===============writeModuleHashJson copy==============") - console.error(filterModuleInfos[i].tempFilePath); fs.copyFileSync(filterModuleInfos[i].tempFilePath, filterModuleInfos[i].buildFilePath); fs.copyFileSync(genAbcFileName(filterModuleInfos[i].tempFilePath), genAbcFileName(filterModuleInfos[i].buildFilePath)); - // fs.unlinkSync(input); + if (process.env.buildMode == "debug" && fs.existsSync(genSourceMapFileName(moduleInfos[i].tempFilePath))) { + fs.copyFileSync(genSourceMapFileName(moduleInfos[i].tempFilePath), genSourceMapFileName(moduleInfos[i].buildFilePath)); + } } const hashFilePath = genHashJsonPath(buildPathInfo); if (hashFilePath.length === 0) { diff --git a/compiler/src/gen_module_abc.ts b/compiler/src/gen_module_abc.ts index ffe5323..601cca8 100644 --- a/compiler/src/gen_module_abc.ts +++ b/compiler/src/gen_module_abc.ts @@ -27,26 +27,16 @@ function js2abcByWorkers(jsonInput: string, cmd: string): Promise { let inputs = []; for (let i = 0; i < inputPaths.length; ++i) { const input = inputPaths[i].tempFilePath; - inputs.push(input); - // const abcFilePathPath = inputPaths[i].abcFilePath; - // const singleCmd = `${cmd} "${input}" -o "${abcFilePathPath}"`; - // logger.debug('gen abc cmd is: ', singleCmd, ' ,file size is:', inputPaths[i].size, ' byte'); - // try { - // console.error(singleCmd); - // childProcess.execSync(singleCmd); - // } catch (e) { - // logger.error(red, `ETS:ERROR Failed to convert file ${input} to abc `, reset); - // return; - // } + inputs.push('"' + input + '"'); } - let inputsStr = inputs.join(','); - const singleCmd = `${cmd} "${inputs}"`; + let inputsStr = inputs.join(' '); + const singleCmd = `${cmd} ${inputsStr}`; logger.debug('gen abc cmd is: ', singleCmd); try { console.error(singleCmd); childProcess.execSync(singleCmd); } catch (e) { - logger.error(red, `ETS:ERROR Failed to convert file ${inputs} to abc `, reset); + logger.error(red, `ETS:ERROR Failed to convert file ${inputsStr} to abc `, reset); return; } diff --git a/compiler/src/utils.ts b/compiler/src/utils.ts index d3f218e..f6a067e 100644 --- a/compiler/src/utils.ts +++ b/compiler/src/utils.ts @@ -309,6 +309,9 @@ export function mkdirsSync(dirname: string): boolean { } export function writeFileSyncByString(sourcePath: string, sourceCode: string, toTsFile: boolean) { + if (process.env.moduleAbc === 'false') { + return ; + } let temporaryFile: string = genTemporaryPath(sourcePath, projectConfig.projectPath, process.env.cachePath, toTsFile); if (temporaryFile.length === 0) { return ; @@ -318,6 +321,9 @@ export function writeFileSyncByString(sourcePath: string, sourceCode: string, to } export function writeFileSyncByNode(node: ts.SourceFile, toTsFile: boolean) { + if (process.env.moduleAbc === 'false') { + return ; + } if (toTsFile) { const newStatements = []; const tsIgnoreNode: ts.Node = ts.factory.createExpressionStatement(ts.factory.createIdentifier(TS_NOCHECK)); @@ -371,22 +377,21 @@ export function writeFileSyncByNode(node: ts.SourceFile, toTsFile: boolean) { } let temporarySourceMapFile: string = ""; if (temporaryFile.endsWith("ets")) { - temporarySourceMapFile = temporaryFile.replace(/\.ets$/, '.ets.sourcemap'); if (toTsFile) { temporaryFile = temporaryFile.replace(/\.ets$/, '.ets.ts'); } else { temporaryFile = temporaryFile.replace(/\.ets$/, '.ets.js'); - } + } + temporarySourceMapFile = genSourceMapFileName(temporaryFile); } else { if (!toTsFile) { - temporarySourceMapFile = temporaryFile.replace(/\.ts$/, '.ts.sourcemap'); temporaryFile = temporaryFile.replace(/\.ts$/, '.ts.js'); + temporarySourceMapFile = genSourceMapFileName(temporaryFile); } } mkdirsSync(path.dirname(temporaryFile)); fs.writeFileSync(temporaryFile, content); - if (temporarySourceMapFile.length > 0) { - + if (temporarySourceMapFile.length > 0 && process.env.buildMode === "debug") { fs.writeFileSync(temporarySourceMapFile, sourceMapContent); } } @@ -400,3 +405,13 @@ export function genAbcFileName(temporaryFile: string): string { } return abcFile; } + +export function genSourceMapFileName(temporaryFile: string): string { + let abcFile: string = temporaryFile; + if (temporaryFile.endsWith('ts')) { + abcFile = temporaryFile.replace(/\.ts$/, '.sourcemap'); + } else { + abcFile = temporaryFile.replace(/\.js$/, '.sourcemap'); + } + return abcFile; +} diff --git a/compiler/webpack.config.js b/compiler/webpack.config.js index 6cf9052..f1bd168 100644 --- a/compiler/webpack.config.js +++ b/compiler/webpack.config.js @@ -313,7 +313,9 @@ module.exports = (env, argv) => { setOptimizationConfig(config, workerFile); setCopyPluginConfig(config); + process.env.moduleAbc = false; process.env.processTs = false; + process.env.buildMode = env.buildMode; if (env.isPreview !== "true") { loadWorker(projectConfig, workerFile);