From ba8190c67c4742f3993214793cc93d05a5dbfe97 Mon Sep 17 00:00:00 2001 From: Harry Chen Date: Thu, 7 Mar 2024 14:11:13 +0800 Subject: [PATCH] chore: optimization kill app speed --- lib/index.js | 36 ++++++++++++++++++++---------------- lib/process.js | 11 +++++++++-- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/lib/index.js b/lib/index.js index 06f7113..eb93da1 100644 --- a/lib/index.js +++ b/lib/index.js @@ -98,24 +98,28 @@ function run() { debug(`hasPaths: ${hasPaths}`); let runChild; - const restart = debounce(() => { + const restart = debounce(async () => { if (fileChangeList.length === 0) return; - if (hasPaths) { - // 这里使用全量替换,tsc 增量编译会把老文件修改回去 - replaceTscAliasPaths({ - configFile: tsconfigPath, - outDir, - }); - // 避免重复触发文件变化 - isCompileSuccess = false; + async function aliasReplace() { + if (hasPaths) { + // 这里使用全量替换,tsc 增量编译会把老文件修改回去 + await replaceTscAliasPaths({ + configFile: tsconfigPath, + outDir, + }); + // 避免重复触发文件变化 + isCompileSuccess = false; + } + output( + `${fileChangeList.length} ${colors.dim('Files has been changed.')}`, + true + ); + // 清空文件列表 + fileChangeList.length = 0; } - output( - `${fileChangeList.length} ${colors.dim('Files has been changed.')}`, - true - ); - // 清空文件列表 - fileChangeList.length = 0; - runChild && runChild.restart(); + + await Promise.all([runChild && runChild.kill(), aliasReplace()]); + runChild && runChild.forkChild(); }, 1000); function runAfterTsc() { diff --git a/lib/process.js b/lib/process.js index 2c2e5df..9a22070 100644 --- a/lib/process.js +++ b/lib/process.js @@ -79,7 +79,11 @@ const forkRun = (runCmdPath, runArgs = [], options = {}) => { try { if (data.title === 'server-ready') { onServerReadyCallback && - await onServerReadyCallback(data, isFirstFork, Date.now() - startTime); + (await onServerReadyCallback( + data, + isFirstFork, + Date.now() - startTime + )); runChild.removeListener('message', onServerReady); } } catch (err) { @@ -114,7 +118,10 @@ const forkRun = (runCmdPath, runArgs = [], options = {}) => { // 杀进程 await killRunningChild(); // 重新拉起来 - innerFork(); + innerFork(false); + }, + forkChild() { + innerFork(false); }, onServerReady(readyCallback) { onServerReadyCallback = readyCallback;