Skip to content

Commit 4156895

Browse files
committed
prevent handling of compile duplicate requests
1 parent b5932d7 commit 4156895

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/compiler.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ export const makeCompiler = (
4444
const tsConfigPath =
4545
resolveSync(cwd, typeof project === 'string' ? project : undefined) || ''
4646

47+
const compiledPathsHash: Record<string, true> = {}
48+
4749
const tmpDir = options['cache-directory']
4850
? path.resolve(options['cache-directory'])
4951
: fs.mkdtempSync(path.join(os.tmpdir(), '.ts-node'))
@@ -170,6 +172,9 @@ export const makeCompiler = (
170172
clearTimeout(_errorCompileTimeout)
171173
}
172174
const registerTsNode = () => {
175+
Object.keys(compiledPathsHash).forEach((key) => {
176+
delete compiledPathsHash[key]
177+
})
173178
// revert back original handler extensions
174179
// in case of re-registering
175180
;['.js', '.jsx', '.ts', '.tsx'].forEach(function (ext) {
@@ -232,9 +237,20 @@ export const makeCompiler = (
232237
const fileName = params.compile
233238
const code = fs.readFileSync(fileName, 'utf-8')
234239
const compiledPath = params.compiledPath
240+
241+
// Prevent occasional duplicate compilation requests
242+
if (compiledPathsHash[compiledPath]) {
243+
return
244+
}
245+
compiledPathsHash[compiledPath] = true
246+
235247
function writeCompiled(code: string, fileName?: string) {
236-
fs.writeFileSync(compiledPath, code)
237-
fs.writeFileSync(compiledPath + '.done', '')
248+
fs.writeFile(compiledPath, code, (err) => {
249+
err && log.error(err)
250+
fs.writeFile(compiledPath + '.done', '', (err) => {
251+
err && log.error(err)
252+
})
253+
})
238254
}
239255
if (fs.existsSync(compiledPath)) {
240256
return

0 commit comments

Comments
 (0)