From 98b4aa6561ae99635d011f2a3d715258348fbae4 Mon Sep 17 00:00:00 2001 From: Matt Lewis Date: Wed, 12 Apr 2023 02:06:36 +0100 Subject: [PATCH] fix(core): correctly pass resolved compilerOptions to ts-node (#16240) --- packages/nx/src/utils/register.spec.ts | 18 +++++++++++++++++- packages/nx/src/utils/register.ts | 11 ++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/packages/nx/src/utils/register.spec.ts b/packages/nx/src/utils/register.spec.ts index 7ba3b637b2..3cdba50c48 100644 --- a/packages/nx/src/utils/register.spec.ts +++ b/packages/nx/src/utils/register.spec.ts @@ -1,4 +1,4 @@ -import { ModuleKind, ScriptTarget } from 'typescript'; +import { JsxEmit, ModuleKind, ScriptTarget } from 'typescript'; import { getTsNodeCompilerOptions } from './register'; describe('getTsNodeCompilerOptions', () => { @@ -17,4 +17,20 @@ describe('getTsNodeCompilerOptions', () => { }).target ).toEqual('ES2020'); }); + + it('should remove jsx option', () => { + expect( + getTsNodeCompilerOptions({ + jsx: JsxEmit.ReactJSX, + }).jsx + ).toBeUndefined(); + }); + + it('should use correct lib value', () => { + expect( + getTsNodeCompilerOptions({ + lib: ['lib.es2022.d.ts'], + }).lib + ).toEqual(['es2022']); + }); }); diff --git a/packages/nx/src/utils/register.ts b/packages/nx/src/utils/register.ts index b0b39089f5..c727339f76 100644 --- a/packages/nx/src/utils/register.ts +++ b/packages/nx/src/utils/register.ts @@ -130,7 +130,7 @@ function readCompilerOptionsWithTypescript(tsConfigPath) { const { readConfigFile, parseJsonConfigFileContent, sys } = ts; const jsonContent = readConfigFile(tsConfigPath, sys.readFile); const { options } = parseJsonConfigFileContent( - jsonContent, + jsonContent.config, sys, dirname(tsConfigPath) ); @@ -203,6 +203,15 @@ export function getTsNodeCompilerOptions(compilerOptions: CompilerOptions) { delete result.pathsBasePath; delete result.configFilePath; + + // instead of mapping to enum value we just remove it as it shouldn't ever need to be set for ts-node + delete result.jsx; + + // lib option is in the format `lib.es2022.d.ts`, so we need to remove the leading `lib.` and trailing `.d.ts` to make it valid + result.lib = result.lib?.map((value) => { + return value.replace(/^lib\./, '').replace(/\.d\.ts$/, ''); + }); + if (result.moduleResolution) { result.moduleResolution = result.moduleResolution === 'NodeJs'