diff --git a/compiler/src/gen_abc.ts b/compiler/src/gen_abc.ts index 6dbb3cf..83f79f8 100644 --- a/compiler/src/gen_abc.ts +++ b/compiler/src/gen_abc.ts @@ -31,9 +31,9 @@ const reset: string = '\u001b[39m'; function js2abcByWorkers(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 input: string = inputPaths[i].path.replace(/\.temp\.js$/, "_.js"); 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 { @@ -50,9 +50,9 @@ function js2abcByWorkers(jsonInput: string, cmd: string): Promise { 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 input: string = inputPaths[i].path.replace(/\.temp\.js$/, "_.js"); 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}" --output "${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 cf67942..196d489 100644 --- a/compiler/src/gen_abc_plugin.ts +++ b/compiler/src/gen_abc_plugin.ts @@ -49,10 +49,10 @@ import { EXTNAME_JS_MAP, TS2ABC, ES2ABC, - TEMPORARY + TEMPORARY, + SUCCESS } from './pre_define'; -const firstFileEXT: string = '_.js'; const genAbcScript: string = 'gen_abc.js'; const genModuleAbcScript: string = 'gen_module_abc.js'; let output: string; @@ -176,8 +176,8 @@ export class GenAbcPlugin { // choose *.js if (output && path.extname(key) === EXTNAME_JS) { const newContent: string = compilation.assets[key].source(); - const keyPath: string = key.replace(/\.js$/, firstFileEXT); - writeFileSync(newContent, path.resolve(output, keyPath), key); + const keyPath: string = key.replace(/\.js$/, ".temp.js"); + writeFileSync(newContent, output, keyPath, key); } }); }); @@ -356,16 +356,15 @@ function processEntryToGenAbc(entryInfos: Map): void { } } -function writeFileSync(inputString: string, output: string, jsBundleFile: string): void { +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); } - let buildParentPath: string = path.join(projectConfig.buildPath, '..'); - let sufStr: string = output.replace(buildParentPath, ''); let cacheOutputPath: string = ""; if (process.env.cachePath) { - cacheOutputPath = path.join(process.env.cachePath, TEMPORARY, sufStr); + cacheOutputPath = path.join(process.env.cachePath, TEMPORARY, keyPath); } else { cacheOutputPath = output; } @@ -474,6 +473,9 @@ function initAbcEnv() : string[] { } function invokeCluterModuleToAbc(): void { + if (projectConfig.isPreview) { + process.exitCode = SUCCESS; + } filterIntermediateModuleByHashJson(buildPathInfo, moduleInfos); filterModuleInfos.forEach(moduleInfo => { if (moduleInfo.isCommonJs) { @@ -599,6 +601,9 @@ function judgeModuleWorkersToGenAbc(callback): void { } function invokeWorkersToGenAbc(): void { + if (projectConfig.isPreview) { + process.exitCode = SUCCESS; + } let cmdPrefix: string = ''; let maxWorkerNumber: number = 3; @@ -647,9 +652,9 @@ function invokeWorkersToGenAbc(): void { } count_++; if (count_ === workerNumber) { - writeHashJson(); - clearGlobalInfo(); + // for preview of with incre compile if (projectConfig.isPreview) { + processExtraAssetForBundle(); console.info(red, 'COMPILE RESULT:SUCCESS ', reset); } } @@ -657,13 +662,14 @@ function invokeWorkersToGenAbc(): void { }); process.on('exit', (code) => { - intermediateJsBundle.forEach((item) => { - const input = item.path; - if (fs.existsSync(input)) { - fs.unlinkSync(input); - } - }); + // for build options + processExtraAssetForBundle(); }); + + // for preview of without incre compile + if (workerNumber === 0 && projectConfig.isPreview) { + processExtraAssetForBundle(); + } } } @@ -786,10 +792,8 @@ function filterIntermediateJsBundleByHashJson(buildPath: string, inputPaths: Fil jsonObject = JSON.parse(jsonFile); fileterIntermediateJsBundle = []; for (let i = 0; i < inputPaths.length; ++i) { - const input: string = inputPaths[i].path; - const abcPath: string = input.replace(/_.js$/, EXTNAME_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); process.exitCode = FAIL; @@ -801,10 +805,6 @@ function filterIntermediateJsBundleByHashJson(buildPath: string, inputPaths: Fil if (jsonObject[cacheOutputPath] === hashInputContentData && jsonObject[cacheAbcFilePath] === hashAbcContentData) { updateJsonObject[cacheOutputPath] = hashInputContentData; updateJsonObject[cacheAbcFilePath] = hashAbcContentData; - if (!fs.existsSync(abcPath)) { - mkdirsSync(path.dirname(abcPath)); - fs.copyFileSync(cacheAbcFilePath, abcPath); - } } else { fileterIntermediateJsBundle.push(inputPaths[i]); } @@ -819,10 +819,8 @@ function filterIntermediateJsBundleByHashJson(buildPath: string, inputPaths: Fil function writeHashJson(): void { for (let i = 0; i < fileterIntermediateJsBundle.length; ++i) { - const input:string = fileterIntermediateJsBundle[i].path; - const abcPath: string = input.replace(/_.js$/, EXTNAME_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 ${cacheOutputPath} is lost`, reset); process.exitCode = FAIL; @@ -833,22 +831,6 @@ function writeHashJson(): void { 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); - process.exitCode = FAIL; - break; - } - if (process.env.cachePath !== undefined) { - mkdirsSync(path.dirname(abcFile)); - fs.copyFileSync(cacheAbcFilePath, abcFile); - } - if (process.env.cachePath === undefined && fs.existsSync(intermediateJsBundle[i].path)) { - fs.unlinkSync(intermediateJsBundle[i].path); - } - } const hashFilePath: string = genHashJsonPath(buildPathInfo); if (hashFilePath.length === 0) { return; @@ -899,3 +881,32 @@ function checkNodeModules() { return true; } + +function copyFileCachePathToBuildPath() { + for (let i = 0; i < intermediateJsBundle.length; ++i) { + 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)) { + 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); + } + // for preview mode, cache path and old abc file both exist, should copy abc file for updating + if (process.env.cachePath !== undefined) { + fs.copyFileSync(cacheAbcFilePath, abcFile); + } + if (process.env.cachePath === undefined && fs.existsSync(cacheOutputPath)) { + fs.unlinkSync(cacheOutputPath); + } + } +} + +function processExtraAssetForBundle() { + writeHashJson(); + copyFileCachePathToBuildPath(); + clearGlobalInfo(); +} \ No newline at end of file