Skip to content

Commit

Permalink
fix(core): do not re-register ts-node twice for the same compiler opt…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
FrozenPandaz committed Jun 28, 2024
1 parent 22aa2d9 commit 7fbab35
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions packages/nx/src/plugins/js/utils/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,17 @@ export function getSwcTranspiler(
return typeof cleanupFn === 'function' ? cleanupFn : () => {};
}

const registered = new Set<string>();

export function getTsNodeTranspiler(
compilerOptions: CompilerOptions,
tsNodeOptions?: TsConfigOptions
compilerOptions: CompilerOptions
): (...args: unknown[]) => unknown {
// Just return if transpiler was already registered before.
const registrationKey = JSON.stringify(compilerOptions);
if (registered.has(registrationKey)) {
return () => {};
}

const { register } = require('ts-node') as typeof import('ts-node');
// ts-node doesn't provide a cleanup method
const service = register({
Expand All @@ -132,6 +139,7 @@ export function getTsNodeTranspiler(
// we already read and provide the compiler options, so prevent ts-node from reading them again
skipProject: true,
});
registered.add(registrationKey);

const { transpiler, swc } = service.options;

Expand All @@ -141,7 +149,7 @@ export function getTsNodeTranspiler(
}

return () => {
service.enabled(false);
// Do not cleanup ts-node service since other consumers may need it
};
}

Expand Down Expand Up @@ -246,7 +254,7 @@ export function getTranspiler(
if (tsNodeInstalled) {
const tsNodeOptions =
filterRecognizedTsConfigTsNodeOptions(tsConfigRaw).recognized;
return () => getTsNodeTranspiler(compilerOptions, tsNodeOptions);
return () => getTsNodeTranspiler(compilerOptions);
}
}

Expand Down

0 comments on commit 7fbab35

Please sign in to comment.