Skip to content

Commit

Permalink
fix(core): register swc transpiler once per compilerOptions (#26807)
Browse files Browse the repository at this point in the history
Apply the same patch for `@swc-node/register` that we did for `ts-node`.
This prevent errors during graph construction.

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #26798

(cherry picked from commit 8cf96cc)
  • Loading branch information
jaysoo authored and FrozenPandaz committed Jul 5, 2024
1 parent 9f118e8 commit 314c2fd
Showing 1 changed file with 36 additions and 17 deletions.
53 changes: 36 additions & 17 deletions packages/nx/src/plugins/js/utils/register.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { dirname, join, sep } from 'path';
import type { TsConfigOptions } from 'ts-node';
import type { CompilerOptions } from 'typescript';
import { NX_PREFIX, logger, stripIndent } from '../../../utils/logger';
import { logger, NX_PREFIX, stripIndent } from '../../../utils/logger';

const swcNodeInstalled = packageIsInstalled('@swc-node/register');
const tsNodeInstalled = packageIsInstalled('ts-node/register');
Expand Down Expand Up @@ -120,17 +120,9 @@ export function getSwcTranspiler(
return typeof cleanupFn === 'function' ? cleanupFn : () => {};
}

const registered = new Set<string>();

export function getTsNodeTranspiler(
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 @@ -139,7 +131,6 @@ 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 Down Expand Up @@ -227,6 +218,11 @@ function filterRecognizedTsConfigTsNodeOptions(jsonObject: any): {
return { recognized: filteredTsConfigOptions, unrecognized };
}

const registered = new Map<
string,
{ refCount: number; cleanup: () => (...args: unknown[]) => unknown }
>();

export function getTranspiler(
compilerOptions: CompilerOptions,
tsConfigRaw?: unknown
Expand All @@ -246,15 +242,38 @@ export function getTranspiler(
compilerOptions.inlineSourceMap = true;
compilerOptions.skipLibCheck = true;

if (swcNodeInstalled && !preferTsNode) {
return () => getSwcTranspiler(compilerOptions);
// Just return if transpiler was already registered before.
const registrationKey = JSON.stringify(compilerOptions);
const registrationEntry = registered.get(registrationKey);
if (registered.has(registrationKey)) {
registrationEntry.refCount++;
return registrationEntry.cleanup;
}

// We can fall back on ts-node if it's available
if (tsNodeInstalled) {
const tsNodeOptions =
filterRecognizedTsConfigTsNodeOptions(tsConfigRaw).recognized;
return () => getTsNodeTranspiler(compilerOptions);
const _getTranspiler =
swcNodeInstalled && !preferTsNode
? getSwcTranspiler
: tsNodeInstalled
? // We can fall back on ts-node if it's available
getTsNodeTranspiler
: undefined;

if (_getTranspiler) {
const transpilerCleanup = _getTranspiler(compilerOptions);
const currRegistrationEntry = {
refCount: 1,
cleanup: () => {
return () => {
currRegistrationEntry.refCount--;
if (currRegistrationEntry.refCount === 0) {
registered.delete(registrationKey);
transpilerCleanup();
}
};
},
};
registered.set(registrationKey, currRegistrationEntry);
return currRegistrationEntry.cleanup;
}
}

Expand Down

0 comments on commit 314c2fd

Please sign in to comment.