From d1cab45c21508377d36d7bc22252bfedb4c81d8f Mon Sep 17 00:00:00 2001 From: A O Date: Thu, 10 Dec 2020 02:30:41 +0500 Subject: [PATCH] use either `process.send` or `writeFile` fallback --- src/child-require-hook.ts | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/child-require-hook.ts b/src/child-require-hook.ts index 86cd3cf..6ac5c28 100644 --- a/src/child-require-hook.ts +++ b/src/child-require-hook.ts @@ -38,15 +38,27 @@ const waitForFile = function (fileName: string) { } } -const compile = (code: string, fileName: string) => { - const compiledPath = getCompiledPath(code, fileName, compiledDir) - process.send && - process.send({ - compile: fileName, - compiledPath: compiledPath, - }) +const sendFsCompileRequest = (fileName: string, compiledPath: string) => { const compileRequestFile = [compiledDir, compilationId + '.req'].join(sep) fs.writeFileSync(compileRequestFile, [fileName, compiledPath].join('\n')) +} + +const compile = (code: string, fileName: string) => { + const compiledPath = getCompiledPath(code, fileName, compiledDir) + if (process.send) { + try { + process.send({ + compile: fileName, + compiledPath: compiledPath, + }) + } catch (e) { + console.warn('Error while sending compile request via process.send') + sendFsCompileRequest(fileName, compiledPath) + } + } else { + sendFsCompileRequest(fileName, compiledPath) + } + waitForFile(compiledPath + '.done') const compiled = fs.readFileSync(compiledPath, 'utf-8') return compiled