From 69cf4168364a0679e387aaf32a1a73ae26546ba7 Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Fri, 15 Sep 2023 09:13:52 -0400 Subject: [PATCH] fix(core): enable esModuleInterop --- 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 ccdea4a45e7955..68e5197529f3d9 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.5.8", "@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 9f0ced6950a751..e87a4469a3ad72 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.5.8", + "alwaysAddToPackageJson": false + } + } } } } diff --git a/packages/js/src/utils/versions.ts b/packages/js/src/utils/versions.ts index 6a8510449504b2..7db777a238cd0e 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.5.8'; 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 b61459fea83d00..76f10471f83f3d 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.5.8", "@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 47bd4dfcd65127..c69975ed9e6a01 100644 --- a/packages/nx/src/plugins/js/utils/register.ts +++ b/packages/nx/src/plugins/js/utils/register.ts @@ -66,7 +66,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 7004f15d023a45..e4c9589ba4a8f6 100644 --- a/packages/nx/src/project-graph/project-graph.ts +++ b/packages/nx/src/project-graph/project-graph.ts @@ -17,6 +17,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. @@ -82,7 +83,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, @@ -92,6 +93,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 775caadaa269ee..6520d9bf839b80 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 54ac18e378cc73..c95dd3ba5a1a25 100644 --- a/packages/nx/src/utils/nx-plugin.ts +++ b/packages/nx/src/utils/nx-plugin.ts @@ -177,7 +177,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(); } @@ -368,14 +368,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'); @@ -389,8 +389,8 @@ export function registerPluginTSTranspiler() { ? readTsConfig(tsConfigName) : {}; - registerTsConfigPaths(tsConfigName); - registerTranspiler({ + const unregisterTsConfigPaths = registerTsConfigPaths(tsConfigName); + const unregisterTranspiler = registerTranspiler({ experimentalDecorators: true, emitDecoratorMetadata: true, ...tsConfig.options, @@ -400,8 +400,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) { @@ -411,7 +424,7 @@ function lookupLocalPlugin(importPath: string, root = workspaceRoot) { return null; } - if (!tsNodeAndPathsRegistered) { + if (!tsNodeAndPathsUnregisterCallback) { registerPluginTSTranspiler(); } @@ -451,6 +464,7 @@ function findNxProjectForImportPath( } let tsconfigPaths: Record; + function readTsConfigPaths(root: string = workspaceRoot) { if (!tsconfigPaths) { const tsconfigPath: string | null = ['tsconfig.base.json', 'tsconfig.json']