From 050a6ba38eefe8803d21a8579f00a305480ea54b Mon Sep 17 00:00:00 2001 From: zhangrengao Date: Mon, 14 Mar 2022 15:04:57 +0800 Subject: [PATCH 1/3] incre compiler abc Signed-off-by: zhangrengao Change-Id: Ieeeffa9f63988167726789faeee7528b2ffce0ba --- compiler/src/gen_abc.ts | 6 +-- compiler/src/gen_abc_plugin.ts | 95 ++++++++++++++++++++++++++++++++-- compiler/src/utils.ts | 9 ++++ 3 files changed, 102 insertions(+), 8 deletions(-) diff --git a/compiler/src/gen_abc.ts b/compiler/src/gen_abc.ts index 8d20e07a5..dde95b102 100644 --- a/compiler/src/gen_abc.ts +++ b/compiler/src/gen_abc.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -35,10 +35,6 @@ function js2abcByWorkers(jsonInput: string, cmd: string): Promise { return; } - if (fs.existsSync(input)) { - fs.unlinkSync(input); - } - const abcFile: string = input.replace(/\.js$/, '.abc'); if (fs.existsSync(abcFile)) { const abcFileNew: string = abcFile.replace(/_.abc$/, '.abc'); diff --git a/compiler/src/gen_abc_plugin.ts b/compiler/src/gen_abc_plugin.ts index eac73f7de..f0b840dd9 100644 --- a/compiler/src/gen_abc_plugin.ts +++ b/compiler/src/gen_abc_plugin.ts @@ -16,9 +16,11 @@ import * as fs from 'fs'; import * as path from 'path'; import cluster from 'cluster'; -import * as process from 'process'; +import process from 'process'; import Compiler from 'webpack/lib/Compiler'; import { logger } from './compile_info'; +import { toUnixPath } from './utils'; +import { createHash } from 'crypto'; const firstFileEXT: string = '_.js'; const genAbcScript = 'gen_abc.js'; @@ -33,10 +35,15 @@ interface File { path: string, size: number } -const intermediateJsBundle: Array = []; +let intermediateJsBundle: Array = []; +let fileterIntermediateJsBundle: Array = []; +let hashJsonObject = {}; +let buildPathInfo = ""; const red: string = '\u001b[31m'; const reset: string = '\u001b[39m'; +const hashFile = 'gen_hash.json'; +const ARK = '/ark/'; export class GenAbcPlugin { constructor(output_, arkDir_, nodeJs_, isDebug_) { @@ -71,6 +78,7 @@ export class GenAbcPlugin { }); compiler.hooks.afterEmit.tap('GenAbcPluginMultiThread', () => { + buildPathInfo = output; invokeWorkersToGenAbc(); }); } @@ -146,8 +154,9 @@ function invokeWorkersToGenAbc() { js2abc = path.join(arkDir, 'build-mac', 'src', 'index.js'); } + filterIntermediateJsBundleByHashJson(buildPathInfo, intermediateJsBundle); const maxWorkerNumber = 3; - const splitedBundles = splitJsBundlesBySize(intermediateJsBundle, maxWorkerNumber); + const splitedBundles = splitJsBundlesBySize(fileterIntermediateJsBundle, maxWorkerNumber); const workerNumber = maxWorkerNumber < splitedBundles.length ? maxWorkerNumber : splitedBundles.length; const cmdPrefix: string = `${nodeJs} --expose-gc "${js2abc}" ${param} `; @@ -177,5 +186,85 @@ function invokeWorkersToGenAbc() { cluster.on('exit', (worker, code, signal) => { logger.debug(`worker ${worker.process.pid} finished`); }); + + process.on('exit', (code) => { + if (buildPathInfo.indexOf(ARK)) { + const hashPath = genHashJsonPath(buildPathInfo); + const hashFilePath = path.join(hashPath, hashFile); + const parent: string = path.join(hashFilePath, '..'); + if (!(fs.existsSync(parent) && !fs.statSync(parent).isFile())) { + mkDir(parent); + } + for (let i = 0; i < fileterIntermediateJsBundle.length; ++i) { + let input = fileterIntermediateJsBundle[i].path; + let abcPath = input.replace(/_.js$/, '.abc'); + if (fs.existsSync(input) && fs.existsSync(abcPath)) { + const inputContent = fs.readFileSync(input); + const hashInput = createHash('sha256'); + hashInput.update(inputContent); + const hashInputContentData = hashInput.digest('hex'); + const abcContent = fs.readFileSync(abcPath); + const hashAbc = createHash('sha256'); + hashAbc.update(abcContent); + const hashAbcContentData = hashAbc.digest('hex'); + hashJsonObject[input] = hashInputContentData; + hashJsonObject[abcPath] = hashAbcContentData; + } + if (fs.existsSync(input)) { + fs.unlinkSync(input); + } + } + fs.writeFileSync(hashFilePath, JSON.stringify(hashJsonObject)); + } + }) + } +} + +function filterIntermediateJsBundleByHashJson(buildPath: string, inputPaths: File[]) { + for (let i = 0; i < inputPaths.length; ++i) { + fileterIntermediateJsBundle.push(inputPaths[i]); } + let updateJsonObject = {}; + if (buildPath.indexOf(ARK)) { + const hashPath = genHashJsonPath(buildPath); + const hashFilePath = path.join(hashPath, hashFile); + let jsonObject = {}; + let jsonFile = ""; + if (fs.existsSync(hashFilePath)) { + jsonFile = fs.readFileSync(hashFilePath).toString(); + jsonObject = JSON.parse(jsonFile); + fileterIntermediateJsBundle = []; + for (let i = 0; i < inputPaths.length; ++i) { + let input = inputPaths[i].path; + let abcPath = input.replace(/_.js$/, '.abc'); + if (fs.existsSync(input) && fs.existsSync(abcPath)) { + const inputContent = fs.readFileSync(input); + const hashInput = createHash('sha256'); + hashInput.update(inputContent); + const hashInputContentData = hashInput.digest('hex'); + const abcContent = fs.readFileSync(abcPath); + const hashAbc = createHash('sha256'); + hashAbc.update(abcContent); + const hashAbcContentData = hashAbc.digest('hex'); + if (jsonObject[input] === hashInputContentData && jsonObject[abcPath] === hashAbcContentData) { + updateJsonObject[input] = hashInputContentData; + updateJsonObject[abcPath] = hashAbcContentData; + fs.unlinkSync(input); + } else { + fileterIntermediateJsBundle.push(inputPaths[i]); + } + } else { + fileterIntermediateJsBundle.push(inputPaths[i]); + } + } + } + } + + hashJsonObject = updateJsonObject; +} + +function genHashJsonPath(buildPath: string) { + buildPath = toUnixPath(buildPath); + const dataTmps = buildPath.split(ARK); + return path.join(dataTmps[0], ARK); } diff --git a/compiler/src/utils.ts b/compiler/src/utils.ts index 59e5b9b66..d491a4148 100644 --- a/compiler/src/utils.ts +++ b/compiler/src/utils.ts @@ -206,3 +206,12 @@ export function mkDir(path_: string): void { } fs.mkdirSync(path_); } + +export function toUnixPath(data: string): string { + if (/^win/.test(require('os').platform())) { + const fileTmps: string[] = data.split(path.sep); + const newData: string = path.posix.join(...fileTmps); + return newData; + } + return data; +} \ No newline at end of file -- Gitee From 9b93c162c3d0ee3d61c8b52b634b3848c5615b63 Mon Sep 17 00:00:00 2001 From: zhangrengao Date: Tue, 15 Mar 2022 10:49:58 +0800 Subject: [PATCH 2/3] Refactoring code Signed-off-by: zhangrengao Change-Id: I568f4e6555be8898b40282c2ad4a09a90ded9dc6 --- compiler/src/gen_abc.ts | 12 ++-- compiler/src/gen_abc_plugin.ts | 108 ++++++++++++++++----------------- compiler/src/utils.ts | 10 ++- 3 files changed, 69 insertions(+), 61 deletions(-) diff --git a/compiler/src/gen_abc.ts b/compiler/src/gen_abc.ts index dde95b102..2c4be9d56 100644 --- a/compiler/src/gen_abc.ts +++ b/compiler/src/gen_abc.ts @@ -27,7 +27,7 @@ function js2abcByWorkers(jsonInput: string, cmd: string): Promise { for (let i = 0; i < inputPaths.length; ++i) { const input = inputPaths[i].path; const singleCmd = `${cmd} "${input}"`; - logger.debug("gen abc cmd is: ", singleCmd, " ,file size is:", inputPaths[i].size, " byte"); + logger.debug('gen abc cmd is: ', singleCmd, ' ,file size is:', inputPaths[i].size, ' byte'); try { childProcess.execSync(singleCmd); } catch (e) { @@ -45,10 +45,10 @@ function js2abcByWorkers(jsonInput: string, cmd: string): Promise { } } -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"]); +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']); process.exit(); } diff --git a/compiler/src/gen_abc_plugin.ts b/compiler/src/gen_abc_plugin.ts index f0b840dd9..57628d0f3 100644 --- a/compiler/src/gen_abc_plugin.ts +++ b/compiler/src/gen_abc_plugin.ts @@ -19,8 +19,7 @@ import cluster from 'cluster'; import process from 'process'; import Compiler from 'webpack/lib/Compiler'; import { logger } from './compile_info'; -import { toUnixPath } from './utils'; -import { createHash } from 'crypto'; +import { toUnixPath, toHashData } from './utils'; const firstFileEXT: string = '_.js'; const genAbcScript = 'gen_abc.js'; @@ -35,10 +34,10 @@ interface File { path: string, size: number } -let intermediateJsBundle: Array = []; +const intermediateJsBundle: Array = []; let fileterIntermediateJsBundle: Array = []; let hashJsonObject = {}; -let buildPathInfo = ""; +let buildPathInfo = ''; const red: string = '\u001b[31m'; const reset: string = '\u001b[39m'; @@ -115,7 +114,7 @@ function getSmallestSizeGroup(groupSize: Map) { } function splitJsBundlesBySize(bundleArray: Array, groupNumber: number) { - let result = []; + const result = []; if (bundleArray.length < groupNumber) { result.push(bundleArray); return result; @@ -162,9 +161,9 @@ function invokeWorkersToGenAbc() { const clusterNewApiVersion = 16; const currentNodeVersion = parseInt(process.version.split('.')[0]); - const useNewApi = currentNodeVersion >= clusterNewApiVersion ? true : false; + const useNewApi = currentNodeVersion >= clusterNewApiVersion; - if ((useNewApi && cluster.isPrimary) || (!useNewApi && cluster.isMaster)) { + if (useNewApi && cluster.isPrimary || !useNewApi && cluster.isMaster) { if (useNewApi) { cluster.setupPrimary({ exec: path.resolve(__dirname, genAbcScript) @@ -176,10 +175,10 @@ function invokeWorkersToGenAbc() { } for (let i = 0; i < workerNumber; ++i) { - let workerData = { - "inputs": JSON.stringify(splitedBundles[i]), - "cmd": cmdPrefix - } + const workerData = { + 'inputs': JSON.stringify(splitedBundles[i]), + 'cmd': cmdPrefix + }; cluster.fork(workerData); } @@ -189,34 +188,9 @@ function invokeWorkersToGenAbc() { process.on('exit', (code) => { if (buildPathInfo.indexOf(ARK)) { - const hashPath = genHashJsonPath(buildPathInfo); - const hashFilePath = path.join(hashPath, hashFile); - const parent: string = path.join(hashFilePath, '..'); - if (!(fs.existsSync(parent) && !fs.statSync(parent).isFile())) { - mkDir(parent); - } - for (let i = 0; i < fileterIntermediateJsBundle.length; ++i) { - let input = fileterIntermediateJsBundle[i].path; - let abcPath = input.replace(/_.js$/, '.abc'); - if (fs.existsSync(input) && fs.existsSync(abcPath)) { - const inputContent = fs.readFileSync(input); - const hashInput = createHash('sha256'); - hashInput.update(inputContent); - const hashInputContentData = hashInput.digest('hex'); - const abcContent = fs.readFileSync(abcPath); - const hashAbc = createHash('sha256'); - hashAbc.update(abcContent); - const hashAbcContentData = hashAbc.digest('hex'); - hashJsonObject[input] = hashInputContentData; - hashJsonObject[abcPath] = hashAbcContentData; - } - if (fs.existsSync(input)) { - fs.unlinkSync(input); - } - } - fs.writeFileSync(hashFilePath, JSON.stringify(hashJsonObject)); + writeHashJson(); } - }) + }); } } @@ -224,28 +198,28 @@ function filterIntermediateJsBundleByHashJson(buildPath: string, inputPaths: Fil for (let i = 0; i < inputPaths.length; ++i) { fileterIntermediateJsBundle.push(inputPaths[i]); } - let updateJsonObject = {}; + const hashFilePath = genHashJsonPath(buildPath); + if (hashFilePath.length === 0) { + return; + } + const updateJsonObject = {}; if (buildPath.indexOf(ARK)) { - const hashPath = genHashJsonPath(buildPath); - const hashFilePath = path.join(hashPath, hashFile); let jsonObject = {}; - let jsonFile = ""; + let jsonFile = ''; if (fs.existsSync(hashFilePath)) { jsonFile = fs.readFileSync(hashFilePath).toString(); jsonObject = JSON.parse(jsonFile); fileterIntermediateJsBundle = []; for (let i = 0; i < inputPaths.length; ++i) { - let input = inputPaths[i].path; - let abcPath = input.replace(/_.js$/, '.abc'); - if (fs.existsSync(input) && fs.existsSync(abcPath)) { - const inputContent = fs.readFileSync(input); - const hashInput = createHash('sha256'); - hashInput.update(inputContent); - const hashInputContentData = hashInput.digest('hex'); - const abcContent = fs.readFileSync(abcPath); - const hashAbc = createHash('sha256'); - hashAbc.update(abcContent); - const hashAbcContentData = hashAbc.digest('hex'); + const input = inputPaths[i].path; + const abcPath = input.replace(/_.js$/, '.abc'); + if (!fs.existsSync(input)) { + logger.error(red, `ETS:ERROR ${input} is lost`, reset); + continue; + } + if (fs.existsSync(abcPath)) { + const hashInputContentData = toHashData(input); + const hashAbcContentData = toHashData(abcPath); if (jsonObject[input] === hashInputContentData && jsonObject[abcPath] === hashAbcContentData) { updateJsonObject[input] = hashInputContentData; updateJsonObject[abcPath] = hashAbcContentData; @@ -263,8 +237,34 @@ function filterIntermediateJsBundleByHashJson(buildPath: string, inputPaths: Fil hashJsonObject = updateJsonObject; } +function writeHashJson() { + const hashFilePath = genHashJsonPath(buildPathInfo); + if (hashFilePath.length === 0) { + return; + } + for (let i = 0; i < fileterIntermediateJsBundle.length; ++i) { + const input = fileterIntermediateJsBundle[i].path; + const abcPath = input.replace(/_.js$/, '.abc'); + if (!fs.existsSync(input) || !fs.existsSync(abcPath)) { + logger.error(red, `ETS:ERROR ${input} is lost`, reset); + continue; + } + const hashInputContentData = toHashData(input); + const hashAbcContentData = toHashData(abcPath); + hashJsonObject[input] = hashInputContentData; + hashJsonObject[abcPath] = hashAbcContentData; + fs.unlinkSync(input); + } + fs.writeFileSync(hashFilePath, JSON.stringify(hashJsonObject)); +} + function genHashJsonPath(buildPath: string) { buildPath = toUnixPath(buildPath); const dataTmps = buildPath.split(ARK); - return path.join(dataTmps[0], ARK); + const hashPath = path.join(dataTmps[0], ARK); + if (!fs.existsSync(hashPath) || !fs.statSync(hashPath).isDirectory()) { + logger.error(red, `ETS:ERROR hash path does not exist`, reset); + return ''; + } + return path.join(hashPath, hashFile); } diff --git a/compiler/src/utils.ts b/compiler/src/utils.ts index d491a4148..7af185cc9 100644 --- a/compiler/src/utils.ts +++ b/compiler/src/utils.ts @@ -16,6 +16,7 @@ import ts from 'typescript'; import path from 'path'; import fs from 'fs'; +import { createHash } from 'crypto'; export enum LogType { ERROR = 'ERROR', @@ -214,4 +215,11 @@ export function toUnixPath(data: string): string { return newData; } return data; -} \ No newline at end of file +} + +export function toHashData(path: string) { + const content = fs.readFileSync(path); + const hash = createHash('sha256'); + hash.update(content); + return hash.digest('hex'); +} -- Gitee From 2d2be0a85047ae0631b555bb859bc60849906666 Mon Sep 17 00:00:00 2001 From: zhangrengao Date: Tue, 15 Mar 2022 21:40:18 +0800 Subject: [PATCH 3/3] fix random of projectPath Signed-off-by: zhangrengao Change-Id: I9ffcea19033b4b6b50b3473e84f4ad9b20c319c1 --- compiler/main.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/compiler/main.js b/compiler/main.js index 9831b280d..b1f74739a 100644 --- a/compiler/main.js +++ b/compiler/main.js @@ -315,13 +315,7 @@ function processResourceArr(resourceArr, resourceMap, filePath) { } function hashProjectPath(projectPath) { - const ASSCIIStart = 65; - const ASSCIIEnd = 90; - const deviation = 1; - process.env.hashProjectPath = - String.fromCharCode(Math.floor(Math.random() * (ASSCIIEnd - ASSCIIStart + deviation) + ASSCIIStart)) + - md5(projectPath).substring(9, 16) + - String.fromCharCode(Math.floor(Math.random() * (ASSCIIEnd - ASSCIIStart + deviation) + ASSCIIStart)); + process.env.hashProjectPath = "_" + md5(projectPath); return process.env.hashProjectPath; } -- Gitee