From 83e74b541c7bdb2f3029fdfabcdd7e74c3f63635 Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Fri, 15 Sep 2023 09:13:52 -0400 Subject: [PATCH 1/2] fix(core): unregister in-process ts transpilers when projectGraph is created --- package.json | 2 +- packages/js/migrations.json | 9 ++++++ packages/js/src/utils/versions.ts | 2 +- packages/nx/package.json | 2 +- packages/nx/src/plugins/js/utils/register.ts | 4 ++- .../nx/src/project-graph/project-graph.ts | 7 ++++- .../utils/retrieve-workspace-files.ts | 1 + packages/nx/src/utils/nx-plugin.ts | 28 ++++++++++++++----- 8 files changed, 43 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 17b7f3f5d22f1..f2651d348e37a 100644 --- a/package.json +++ b/package.json @@ -101,7 +101,7 @@ "@supabase/supabase-js": "^2.26.0", "@svgr/rollup": "^8.0.1", "@svgr/webpack": "^8.0.1", - "@swc-node/register": "^1.4.2", + "@swc-node/register": "1.6.7", "@swc/cli": "0.1.62", "@swc/core": "^1.3.51", "@swc/jest": "^0.2.20", diff --git a/packages/js/migrations.json b/packages/js/migrations.json index 9f0ced6950a75..eee0dc90241aa 100644 --- a/packages/js/migrations.json +++ b/packages/js/migrations.json @@ -98,6 +98,15 @@ "version": "~5.1.3" } } + }, + "16.9.0": { + "version": "16.9.0-beta.1", + "packages": { + "@swc/register": { + "version": "1.6.7", + "alwaysAddToPackageJson": false + } + } } } } diff --git a/packages/js/src/utils/versions.ts b/packages/js/src/utils/versions.ts index 6a8510449504b..987ac3f6dd187 100644 --- a/packages/js/src/utils/versions.ts +++ b/packages/js/src/utils/versions.ts @@ -5,7 +5,7 @@ export const prettierVersion = '^2.6.2'; export const swcCliVersion = '~0.1.62'; export const swcCoreVersion = '~1.3.51'; export const swcHelpersVersion = '~0.5.0'; -export const swcNodeVersion = '~1.4.2'; +export const swcNodeVersion = '1.6.7'; export const tsLibVersion = '^2.3.0'; export const typesNodeVersion = '18.7.1'; export const verdaccioVersion = '^5.0.4'; diff --git a/packages/nx/package.json b/packages/nx/package.json index b61459fea83d0..12fe5dcbc948c 100644 --- a/packages/nx/package.json +++ b/packages/nx/package.json @@ -69,7 +69,7 @@ "node-machine-id": "1.1.12" }, "peerDependencies": { - "@swc-node/register": "^1.4.2", + "@swc-node/register": "1.6.7", "@swc/core": "^1.2.173" }, "peerDependenciesMeta": { diff --git a/packages/nx/src/plugins/js/utils/register.ts b/packages/nx/src/plugins/js/utils/register.ts index f60d7bbde2bf1..60f57e67f86c5 100644 --- a/packages/nx/src/plugins/js/utils/register.ts +++ b/packages/nx/src/plugins/js/utils/register.ts @@ -73,7 +73,9 @@ export function getTsNodeTranspiler( warnTsNodeUsage(); } - return () => {}; + return () => { + service.enabled(false); + }; } export function getTranspiler(compilerOptions: CompilerOptions) { diff --git a/packages/nx/src/project-graph/project-graph.ts b/packages/nx/src/project-graph/project-graph.ts index d0ba52be6c33a..493a9d95f6b48 100644 --- a/packages/nx/src/project-graph/project-graph.ts +++ b/packages/nx/src/project-graph/project-graph.ts @@ -14,6 +14,7 @@ import { workspaceRoot } from '../utils/workspace-root'; import { performance } from 'perf_hooks'; import { retrieveWorkspaceFiles } from './utils/retrieve-workspace-files'; import { readNxJson } from '../config/nx-json'; +import { unregisterPluginTSTranspiler } from '../utils/nx-plugin'; /** * Synchronously reads the latest cached copy of the workspace's ProjectGraph. @@ -75,7 +76,7 @@ export async function buildProjectGraphWithoutDaemon() { await retrieveWorkspaceFiles(workspaceRoot, nxJson); const cacheEnabled = process.env.NX_CACHE_PROJECT_GRAPH !== 'false'; - return ( + const projectGraph = ( await buildProjectGraphUsingProjectFileMap( projectConfigurations.projects, externalNodes, @@ -85,6 +86,10 @@ export async function buildProjectGraphWithoutDaemon() { cacheEnabled ) ).projectGraph; + + unregisterPluginTSTranspiler(); + + return projectGraph; } function handleProjectGraphError(opts: { exitOnError: boolean }, e) { diff --git a/packages/nx/src/project-graph/utils/retrieve-workspace-files.ts b/packages/nx/src/project-graph/utils/retrieve-workspace-files.ts index 9ab713def47d8..7461fb70180ed 100644 --- a/packages/nx/src/project-graph/utils/retrieve-workspace-files.ts +++ b/packages/nx/src/project-graph/utils/retrieve-workspace-files.ts @@ -25,6 +25,7 @@ import { loadNxPlugins, loadNxPluginsSync, NxPluginV2, + unregisterPluginTSTranspiler, } from '../../utils/nx-plugin'; import { CreateProjectJsonProjectsPlugin } from '../../plugins/project-json/build-nodes/project-json'; import { diff --git a/packages/nx/src/utils/nx-plugin.ts b/packages/nx/src/utils/nx-plugin.ts index 7c93cc91c9ae4..4fde168d9874d 100644 --- a/packages/nx/src/utils/nx-plugin.ts +++ b/packages/nx/src/utils/nx-plugin.ts @@ -178,7 +178,7 @@ function getPluginPathAndName( // Register the ts-transpiler if we are pointing to a // plain ts file that's not part of a plugin project - if (extension === '.ts' && !tsNodeAndPathsRegistered) { + if (extension === '.ts' && !tsNodeAndPathsUnregisterCallback) { registerPluginTSTranspiler(); } @@ -369,14 +369,14 @@ export function resolveLocalNxPlugin( return localPluginCache[importPath]; } -let tsNodeAndPathsRegistered = false; +let tsNodeAndPathsUnregisterCallback = undefined; /** * Register swc-node or ts-node if they are not currently registered * with some default settings which work well for Nx plugins. */ export function registerPluginTSTranspiler() { - if (!tsNodeAndPathsRegistered) { + if (!tsNodeAndPathsUnregisterCallback) { // nx-ignore-next-line const ts: typeof import('typescript') = require('typescript'); @@ -390,8 +390,8 @@ export function registerPluginTSTranspiler() { ? readTsConfig(tsConfigName) : {}; - registerTsConfigPaths(tsConfigName); - registerTranspiler({ + const unregisterTsConfigPaths = registerTsConfigPaths(tsConfigName); + const unregisterTranspiler = registerTranspiler({ experimentalDecorators: true, emitDecoratorMetadata: true, ...tsConfig.options, @@ -401,8 +401,21 @@ export function registerPluginTSTranspiler() { inlineSourceMap: true, skipLibCheck: true, }); + tsNodeAndPathsUnregisterCallback = () => { + unregisterTsConfigPaths(); + unregisterTranspiler(); + }; + } +} + +/** + * Unregister the ts-node transpiler if it is registered + */ +export function unregisterPluginTSTranspiler() { + if (tsNodeAndPathsUnregisterCallback) { + tsNodeAndPathsUnregisterCallback(); + tsNodeAndPathsUnregisterCallback = undefined; } - tsNodeAndPathsRegistered = true; } function lookupLocalPlugin(importPath: string, root = workspaceRoot) { @@ -412,7 +425,7 @@ function lookupLocalPlugin(importPath: string, root = workspaceRoot) { return null; } - if (!tsNodeAndPathsRegistered) { + if (!tsNodeAndPathsUnregisterCallback) { registerPluginTSTranspiler(); } @@ -452,6 +465,7 @@ function findNxProjectForImportPath( } let tsconfigPaths: Record; + function readTsConfigPaths(root: string = workspaceRoot) { if (!tsconfigPaths) { const tsconfigPath: string | null = ['tsconfig.base.json', 'tsconfig.json'] From df0ae47fff8942a78bae4fe66e637df1c90921dd Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Wed, 20 Sep 2023 17:21:44 -0400 Subject: [PATCH 2/2] fix(core): revert package updates --- package.json | 2 +- packages/js/migrations.json | 9 --------- packages/js/src/utils/versions.ts | 2 +- packages/nx/package.json | 2 +- 4 files changed, 3 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index f2651d348e37a..17b7f3f5d22f1 100644 --- a/package.json +++ b/package.json @@ -101,7 +101,7 @@ "@supabase/supabase-js": "^2.26.0", "@svgr/rollup": "^8.0.1", "@svgr/webpack": "^8.0.1", - "@swc-node/register": "1.6.7", + "@swc-node/register": "^1.4.2", "@swc/cli": "0.1.62", "@swc/core": "^1.3.51", "@swc/jest": "^0.2.20", diff --git a/packages/js/migrations.json b/packages/js/migrations.json index eee0dc90241aa..9f0ced6950a75 100644 --- a/packages/js/migrations.json +++ b/packages/js/migrations.json @@ -98,15 +98,6 @@ "version": "~5.1.3" } } - }, - "16.9.0": { - "version": "16.9.0-beta.1", - "packages": { - "@swc/register": { - "version": "1.6.7", - "alwaysAddToPackageJson": false - } - } } } } diff --git a/packages/js/src/utils/versions.ts b/packages/js/src/utils/versions.ts index 987ac3f6dd187..6a8510449504b 100644 --- a/packages/js/src/utils/versions.ts +++ b/packages/js/src/utils/versions.ts @@ -5,7 +5,7 @@ export const prettierVersion = '^2.6.2'; export const swcCliVersion = '~0.1.62'; export const swcCoreVersion = '~1.3.51'; export const swcHelpersVersion = '~0.5.0'; -export const swcNodeVersion = '1.6.7'; +export const swcNodeVersion = '~1.4.2'; export const tsLibVersion = '^2.3.0'; export const typesNodeVersion = '18.7.1'; export const verdaccioVersion = '^5.0.4'; diff --git a/packages/nx/package.json b/packages/nx/package.json index 12fe5dcbc948c..b61459fea83d0 100644 --- a/packages/nx/package.json +++ b/packages/nx/package.json @@ -69,7 +69,7 @@ "node-machine-id": "1.1.12" }, "peerDependencies": { - "@swc-node/register": "1.6.7", + "@swc-node/register": "^1.4.2", "@swc/core": "^1.2.173" }, "peerDependenciesMeta": {