From f5c56fa4234661c58ec9dbfef05dfac41c4ac247 Mon Sep 17 00:00:00 2001 From: zhangrengao Date: Sun, 28 Aug 2022 15:14:21 +0800 Subject: [PATCH 1/4] Fix clean webpack of ark incre compile Signed-off-by: zhangrengao Change-Id: I89010a147f17951510f0f821e13bfe7c4e831b3f --- compiler/src/gen_abc.ts | 12 ++--- compiler/src/gen_abc_plugin.ts | 82 ++++++++++++++++++++++++---------- 2 files changed, 62 insertions(+), 32 deletions(-) diff --git a/compiler/src/gen_abc.ts b/compiler/src/gen_abc.ts index 2c4be9d..b4df1ce 100644 --- a/compiler/src/gen_abc.ts +++ b/compiler/src/gen_abc.ts @@ -26,7 +26,9 @@ function js2abcByWorkers(jsonInput: string, cmd: string): Promise { const inputPaths = JSON.parse(jsonInput); for (let i = 0; i < inputPaths.length; ++i) { const input = inputPaths[i].path; - const singleCmd = `${cmd} "${input}"`; + const cacheOutputPath: string = inputPaths[i].cacheOutputPath; + const cacheAbcFilePath: string = cacheOutputPath.replace(/\_.js$/, ".abc"); + const singleCmd: any = `${cmd} "${cacheOutputPath}" -o "${cacheAbcFilePath}" --source-file "${input}"`; logger.debug('gen abc cmd is: ', singleCmd, ' ,file size is:', inputPaths[i].size, ' byte'); try { childProcess.execSync(singleCmd); @@ -34,14 +36,6 @@ function js2abcByWorkers(jsonInput: string, cmd: string): Promise { logger.error(red, `ETS:ERROR Failed to convert file ${input} to abc `, reset); return; } - - const abcFile: string = input.replace(/\.js$/, '.abc'); - if (fs.existsSync(abcFile)) { - const abcFileNew: string = abcFile.replace(/_.abc$/, '.abc'); - fs.renameSync(abcFile, abcFileNew); - } else { - logger.error(red, `ETS:ERROR ${abcFile} is lost`, reset); - } } } diff --git a/compiler/src/gen_abc_plugin.ts b/compiler/src/gen_abc_plugin.ts index 26bc3e3..0326db2 100644 --- a/compiler/src/gen_abc_plugin.ts +++ b/compiler/src/gen_abc_plugin.ts @@ -32,7 +32,8 @@ let nodeJs: string; interface File { path: string, - size: number + size: number, + cacheOutputPath: string } const intermediateJsBundle: Array = []; let fileterIntermediateJsBundle: Array = []; @@ -71,7 +72,7 @@ export class GenAbcPlugin { if (output && path.extname(key) === '.js') { const newContent: string = compilation.assets[key].source(); const keyPath: string = key.replace(/\.js$/, firstFileEXT); - writeFileSync(newContent, path.resolve(output, keyPath), key); + writeFileSync(newContent, output, keyPath, key); } }); }); @@ -83,15 +84,26 @@ export class GenAbcPlugin { } } -function writeFileSync(inputString: string, output: string, jsBundleFile: string): void { - const parent: string = path.join(output, '..'); +function writeFileSync(inputString: string, buildPath: string, keyPath: string, jsBundleFile: string): void { + let output = path.resolve(buildPath, keyPath); + let parent: string = path.join(output, '..'); if (!(fs.existsSync(parent) && fs.statSync(parent).isDirectory())) { mkDir(parent); } - fs.writeFileSync(output, inputString); - if (fs.existsSync(output)) { - const fileSize = fs.statSync(output).size; - intermediateJsBundle.push({path: output, size: fileSize}); + let cacheOutputPath: string = ""; + if (process.env.cachePath) { + cacheOutputPath = path.join(process.env.cachePath, "temporary", keyPath); + } else { + cacheOutputPath = output; + } + parent = path.join(cacheOutputPath, '..'); + if (!(fs.existsSync(parent) && fs.statSync(parent).isDirectory())) { + mkDir(parent); + } + fs.writeFileSync(cacheOutputPath, inputString); + if (fs.existsSync(cacheOutputPath)) { + const fileSize = fs.statSync(cacheOutputPath).size; + intermediateJsBundle.push({path: output, size: fileSize, cacheOutputPath: cacheOutputPath}); } else { logger.error(red, `ETS:ERROR Failed to convert file ${jsBundleFile} to bin. ${output} is lost`, reset); } @@ -210,17 +222,25 @@ function filterIntermediateJsBundleByHashJson(buildPath: string, inputPaths: Fil for (let i = 0; i < inputPaths.length; ++i) { const input = inputPaths[i].path; const abcPath = input.replace(/_.js$/, '.abc'); - if (!fs.existsSync(input)) { - logger.error(red, `ETS:ERROR ${input} is lost`, reset); + const cacheOutputPath: string = inputPaths[i].cacheOutputPath; + const cacheAbcFilePath: string = cacheOutputPath.replace(/\_.js$/, '.abc'); + if (!fs.existsSync(cacheOutputPath)) { + logger.error(red, `ETS:ERROR ${cacheOutputPath} 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; - fs.unlinkSync(input); + if (fs.existsSync(cacheAbcFilePath)) { + const hashInputContentData = toHashData(cacheOutputPath); + const hashAbcContentData = toHashData(cacheAbcFilePath); + if (jsonObject[cacheOutputPath] === hashInputContentData && jsonObject[cacheAbcFilePath] === hashAbcContentData) { + updateJsonObject[cacheOutputPath] = hashInputContentData; + updateJsonObject[cacheAbcFilePath] = hashAbcContentData; + let parent: string = path.join(abcPath, '..'); + if (!(fs.existsSync(parent) && fs.statSync(parent).isDirectory())) { + mkDir(parent); + } + if (!fs.existsSync(abcPath)) { + fs.copyFileSync(cacheAbcFilePath, abcPath); + } } else { fileterIntermediateJsBundle.push(inputPaths[i]); } @@ -237,15 +257,31 @@ function writeHashJson() { 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)) { + const cacheOutputPath: string = fileterIntermediateJsBundle[i].cacheOutputPath; + const cacheAbcFilePath: string = cacheOutputPath.replace(/\_.js$/, '.abc'); + if (!fs.existsSync(cacheOutputPath) || !fs.existsSync(cacheAbcFilePath)) { 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); + const hashInputContentData: any = toHashData(cacheOutputPath); + const hashAbcContentData: any = toHashData(cacheAbcFilePath); + hashJsonObject[cacheOutputPath] = hashInputContentData; + hashJsonObject[cacheAbcFilePath] = hashAbcContentData; + } + for (let i = 0; i < intermediateJsBundle.length; ++i) { + const abcFile: string = intermediateJsBundle[i].path.replace(/\_.js$/, ".abc"); + const cacheAbcFilePath: string = intermediateJsBundle[i].cacheOutputPath.replace(/\_.js$/, ".abc"); + if (!fs.existsSync(cacheAbcFilePath)) { + logger.error(red, `ETS:ERROR ${cacheAbcFilePath} is lost`, reset); + break; + } + let parent: string = path.join(abcFile, '..'); + if (!(fs.existsSync(parent) && fs.statSync(parent).isDirectory())) { + mkDir(parent); + } + if (!fs.existsSync(abcFile)) { + fs.copyFileSync(cacheAbcFilePath, abcFile); + } } const hashFilePath = genHashJsonPath(buildPathInfo); if (hashFilePath.length === 0) { From 1b5f2a545f86adfbae4c44353124137805da5478 Mon Sep 17 00:00:00 2001 From: zhangrengao Date: Sun, 28 Aug 2022 18:26:34 +0800 Subject: [PATCH 2/4] refactor code of copy file Signed-off-by: zhangrengao Change-Id: Ia9e4b64eb2a1b068410a0fe6f569f64699bd9afd --- compiler/src/gen_abc_plugin.ts | 45 +++++++++++++++++----------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/compiler/src/gen_abc_plugin.ts b/compiler/src/gen_abc_plugin.ts index 0326db2..48caaf8 100644 --- a/compiler/src/gen_abc_plugin.ts +++ b/compiler/src/gen_abc_plugin.ts @@ -200,6 +200,7 @@ function invokeWorkersToGenAbc() { process.on('exit', (code) => { writeHashJson(); + copyFileCachePathToBuildPath() }); } } @@ -234,13 +235,6 @@ function filterIntermediateJsBundleByHashJson(buildPath: string, inputPaths: Fil if (jsonObject[cacheOutputPath] === hashInputContentData && jsonObject[cacheAbcFilePath] === hashAbcContentData) { updateJsonObject[cacheOutputPath] = hashInputContentData; updateJsonObject[cacheAbcFilePath] = hashAbcContentData; - let parent: string = path.join(abcPath, '..'); - if (!(fs.existsSync(parent) && fs.statSync(parent).isDirectory())) { - mkDir(parent); - } - if (!fs.existsSync(abcPath)) { - fs.copyFileSync(cacheAbcFilePath, abcPath); - } } else { fileterIntermediateJsBundle.push(inputPaths[i]); } @@ -268,21 +262,6 @@ function writeHashJson() { hashJsonObject[cacheOutputPath] = hashInputContentData; hashJsonObject[cacheAbcFilePath] = hashAbcContentData; } - for (let i = 0; i < intermediateJsBundle.length; ++i) { - const abcFile: string = intermediateJsBundle[i].path.replace(/\_.js$/, ".abc"); - const cacheAbcFilePath: string = intermediateJsBundle[i].cacheOutputPath.replace(/\_.js$/, ".abc"); - if (!fs.existsSync(cacheAbcFilePath)) { - logger.error(red, `ETS:ERROR ${cacheAbcFilePath} is lost`, reset); - break; - } - let parent: string = path.join(abcFile, '..'); - if (!(fs.existsSync(parent) && fs.statSync(parent).isDirectory())) { - mkDir(parent); - } - if (!fs.existsSync(abcFile)) { - fs.copyFileSync(cacheAbcFilePath, abcFile); - } - } const hashFilePath = genHashJsonPath(buildPathInfo); if (hashFilePath.length === 0) { return; @@ -311,3 +290,25 @@ function genHashJsonPath(buildPath: string) { return ''; } } + +function copyFileCachePathToBuildPath() { + for (let i = 0; i < intermediateJsBundle.length; ++i) { + const inputPath: string = intermediateJsBundle[i].path; + const abcFile: string = intermediateJsBundle[i].path.replace(/\_.js$/, ".abc"); + const cacheAbcFilePath: string = intermediateJsBundle[i].cacheOutputPath.replace(/\_.js$/, ".abc"); + if (!fs.existsSync(cacheAbcFilePath)) { + logger.error(red, `ETS:ERROR ${cacheAbcFilePath} is lost`, reset); + break; + } + let parent: string = path.join(abcFile, '..'); + if (!(fs.existsSync(parent) && fs.statSync(parent).isDirectory())) { + mkDir(parent); + } + if (!fs.existsSync(abcFile)) { + fs.copyFileSync(cacheAbcFilePath, abcFile); + } + if (process.env.cachePath === undefined && fs.existsSync(inputPath)) { + fs.unlinkSync(inputPath); + } + } +} From a46624b6c2806b78dd192eccf8f4cdd3094b1db8 Mon Sep 17 00:00:00 2001 From: zhangrengao Date: Sun, 28 Aug 2022 19:37:05 +0800 Subject: [PATCH 3/4] Fix dup name of temp file Signed-off-by: zhangrengao Change-Id: Iabf21097688cf1ad8bb8c999ea01c249534df181 --- compiler/src/gen_abc.ts | 2 +- compiler/src/gen_abc_plugin.ts | 16 +++++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/compiler/src/gen_abc.ts b/compiler/src/gen_abc.ts index b4df1ce..55008ea 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 cacheOutputPath: string = inputPaths[i].cacheOutputPath; - const cacheAbcFilePath: string = cacheOutputPath.replace(/\_.js$/, ".abc"); + const cacheAbcFilePath: string = cacheOutputPath.replace(/\.temp\.js$/, ".abc"); const singleCmd: any = `${cmd} "${cacheOutputPath}" -o "${cacheAbcFilePath}" --source-file "${input}"`; logger.debug('gen abc cmd is: ', singleCmd, ' ,file size is:', inputPaths[i].size, ' byte'); try { diff --git a/compiler/src/gen_abc_plugin.ts b/compiler/src/gen_abc_plugin.ts index 48caaf8..00155c0 100644 --- a/compiler/src/gen_abc_plugin.ts +++ b/compiler/src/gen_abc_plugin.ts @@ -92,9 +92,10 @@ function writeFileSync(inputString: string, buildPath: string, keyPath: string, } let cacheOutputPath: string = ""; if (process.env.cachePath) { - cacheOutputPath = path.join(process.env.cachePath, "temporary", keyPath); + let temporaryKeyPath = keyPath.replace(/\_.js$/, '.temp.js'); + cacheOutputPath = path.join(process.env.cachePath, "temporary", temporaryKeyPath); } else { - cacheOutputPath = output; + cacheOutputPath = output.replace(/\_.js$/, '.temp.js'); } parent = path.join(cacheOutputPath, '..'); if (!(fs.existsSync(parent) && fs.statSync(parent).isDirectory())) { @@ -224,7 +225,7 @@ function filterIntermediateJsBundleByHashJson(buildPath: string, inputPaths: Fil const input = inputPaths[i].path; const abcPath = input.replace(/_.js$/, '.abc'); const cacheOutputPath: string = inputPaths[i].cacheOutputPath; - const cacheAbcFilePath: string = cacheOutputPath.replace(/\_.js$/, '.abc'); + const cacheAbcFilePath: string = cacheOutputPath.replace(/\.temp\.js$/, '.abc'); if (!fs.existsSync(cacheOutputPath)) { logger.error(red, `ETS:ERROR ${cacheOutputPath} is lost`, reset); continue; @@ -252,7 +253,7 @@ function writeHashJson() { const input = fileterIntermediateJsBundle[i].path; const abcPath = input.replace(/_.js$/, '.abc'); const cacheOutputPath: string = fileterIntermediateJsBundle[i].cacheOutputPath; - const cacheAbcFilePath: string = cacheOutputPath.replace(/\_.js$/, '.abc'); + const cacheAbcFilePath: string = cacheOutputPath.replace(/\.temp\.js$/, '.abc'); if (!fs.existsSync(cacheOutputPath) || !fs.existsSync(cacheAbcFilePath)) { logger.error(red, `ETS:ERROR ${input} is lost`, reset); continue; @@ -295,7 +296,8 @@ function copyFileCachePathToBuildPath() { for (let i = 0; i < intermediateJsBundle.length; ++i) { const inputPath: string = intermediateJsBundle[i].path; const abcFile: string = intermediateJsBundle[i].path.replace(/\_.js$/, ".abc"); - const cacheAbcFilePath: string = intermediateJsBundle[i].cacheOutputPath.replace(/\_.js$/, ".abc"); + const cacheOutputPath: string = intermediateJsBundle[i].cacheOutputPath; + const cacheAbcFilePath: string = intermediateJsBundle[i].cacheOutputPath.replace(/\.temp\.js$/, ".abc"); if (!fs.existsSync(cacheAbcFilePath)) { logger.error(red, `ETS:ERROR ${cacheAbcFilePath} is lost`, reset); break; @@ -307,8 +309,8 @@ function copyFileCachePathToBuildPath() { if (!fs.existsSync(abcFile)) { fs.copyFileSync(cacheAbcFilePath, abcFile); } - if (process.env.cachePath === undefined && fs.existsSync(inputPath)) { - fs.unlinkSync(inputPath); + if (process.env.cachePath === undefined && fs.existsSync(cacheOutputPath)) { + fs.unlinkSync(cacheOutputPath); } } } From 21ecfbed17e8da2bbdf1be16179f3e394aa7b206 Mon Sep 17 00:00:00 2001 From: zhangrengao Date: Sun, 28 Aug 2022 22:29:05 +0800 Subject: [PATCH 4/4] Rename of temp js Signed-off-by: zhangrengao Change-Id: If875b4771150d6c3d9029cead9df9ac93f751bd8 --- compiler/src/gen_abc.ts | 3 ++- compiler/src/gen_abc_plugin.ts | 17 +++++------------ 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/compiler/src/gen_abc.ts b/compiler/src/gen_abc.ts index 55008ea..998b650 100644 --- a/compiler/src/gen_abc.ts +++ b/compiler/src/gen_abc.ts @@ -25,7 +25,8 @@ const reset: string = '\u001b[39m'; function js2abcByWorkers(jsonInput: string, cmd: string): Promise { const inputPaths = JSON.parse(jsonInput); for (let i = 0; i < inputPaths.length; ++i) { - const input = inputPaths[i].path; + // for matching debug info mechanism + const input = inputPaths[i].path.replace(/\.temp\.js$/, "_.js"); const cacheOutputPath: string = inputPaths[i].cacheOutputPath; const cacheAbcFilePath: string = cacheOutputPath.replace(/\.temp\.js$/, ".abc"); const singleCmd: any = `${cmd} "${cacheOutputPath}" -o "${cacheAbcFilePath}" --source-file "${input}"`; diff --git a/compiler/src/gen_abc_plugin.ts b/compiler/src/gen_abc_plugin.ts index 00155c0..7186757 100644 --- a/compiler/src/gen_abc_plugin.ts +++ b/compiler/src/gen_abc_plugin.ts @@ -21,7 +21,6 @@ import Compiler from 'webpack/lib/Compiler'; import { logger } from './compile_info'; import { toUnixPath, toHashData } from './utils'; -const firstFileEXT: string = '_.js'; const genAbcScript = 'gen_abc.js'; let output: string; let isWin: boolean = false; @@ -71,7 +70,7 @@ export class GenAbcPlugin { // choose *.js if (output && path.extname(key) === '.js') { const newContent: string = compilation.assets[key].source(); - const keyPath: string = key.replace(/\.js$/, firstFileEXT); + const keyPath: string = key.replace(/\.js$/, ".temp.js"); writeFileSync(newContent, output, keyPath, key); } }); @@ -92,10 +91,9 @@ function writeFileSync(inputString: string, buildPath: string, keyPath: string, } let cacheOutputPath: string = ""; if (process.env.cachePath) { - let temporaryKeyPath = keyPath.replace(/\_.js$/, '.temp.js'); - cacheOutputPath = path.join(process.env.cachePath, "temporary", temporaryKeyPath); + cacheOutputPath = path.join(process.env.cachePath, "temporary", keyPath); } else { - cacheOutputPath = output.replace(/\_.js$/, '.temp.js'); + cacheOutputPath = output; } parent = path.join(cacheOutputPath, '..'); if (!(fs.existsSync(parent) && fs.statSync(parent).isDirectory())) { @@ -222,8 +220,6 @@ function filterIntermediateJsBundleByHashJson(buildPath: string, inputPaths: Fil jsonObject = JSON.parse(jsonFile); fileterIntermediateJsBundle = []; for (let i = 0; i < inputPaths.length; ++i) { - const input = inputPaths[i].path; - const abcPath = input.replace(/_.js$/, '.abc'); const cacheOutputPath: string = inputPaths[i].cacheOutputPath; const cacheAbcFilePath: string = cacheOutputPath.replace(/\.temp\.js$/, '.abc'); if (!fs.existsSync(cacheOutputPath)) { @@ -250,12 +246,10 @@ function filterIntermediateJsBundleByHashJson(buildPath: string, inputPaths: Fil function writeHashJson() { for (let i = 0; i < fileterIntermediateJsBundle.length; ++i) { - const input = fileterIntermediateJsBundle[i].path; - const abcPath = input.replace(/_.js$/, '.abc'); const cacheOutputPath: string = fileterIntermediateJsBundle[i].cacheOutputPath; const cacheAbcFilePath: string = cacheOutputPath.replace(/\.temp\.js$/, '.abc'); if (!fs.existsSync(cacheOutputPath) || !fs.existsSync(cacheAbcFilePath)) { - logger.error(red, `ETS:ERROR ${input} is lost`, reset); + logger.error(red, `ETS:ERROR ${cacheOutputPath} is lost`, reset); continue; } const hashInputContentData: any = toHashData(cacheOutputPath); @@ -294,8 +288,7 @@ function genHashJsonPath(buildPath: string) { function copyFileCachePathToBuildPath() { for (let i = 0; i < intermediateJsBundle.length; ++i) { - const inputPath: string = intermediateJsBundle[i].path; - const abcFile: string = intermediateJsBundle[i].path.replace(/\_.js$/, ".abc"); + const abcFile: string = intermediateJsBundle[i].path.replace(/\.temp\.js$/, ".abc"); const cacheOutputPath: string = intermediateJsBundle[i].cacheOutputPath; const cacheAbcFilePath: string = intermediateJsBundle[i].cacheOutputPath.replace(/\.temp\.js$/, ".abc"); if (!fs.existsSync(cacheAbcFilePath)) {