Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): do not re-register ts-node twice for the same compiler opt… #26758

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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