From c70fc77e5f0e43473b2a57ff9f09178d2c61d521 Mon Sep 17 00:00:00 2001 From: Craigory Coppola Date: Tue, 21 Mar 2023 18:35:55 -0400 Subject: [PATCH] fix(nx-plugin): allow some customization when running nx-plugins (#15438) (cherry picked from commit 6834b658f71d5415f3d53fe3cb4a2c90efb17232) --- packages/cypress/package.json | 2 +- packages/detox/package.json | 2 +- packages/devkit/package.json | 2 +- packages/esbuild/package.json | 2 +- packages/expo/package.json | 2 +- packages/express/package.json | 2 +- packages/jest/package.json | 2 +- packages/linter/package.json | 2 +- packages/nest/package.json | 2 +- packages/next/package.json | 2 +- packages/node/package.json | 2 +- packages/nx-plugin/package.json | 2 +- packages/nx/.eslintrc.json | 18 ++++++++++++++++-- packages/nx/src/utils/nx-plugin.ts | 17 ++++++++++++++--- packages/nx/src/utils/register.ts | 21 ++++++++++++++++----- packages/react-native/package.json | 2 +- packages/react/package.json | 2 +- packages/rollup/package.json | 2 +- packages/storybook/package.json | 2 +- packages/tao/package.json | 4 ++-- packages/vite/package.json | 2 +- packages/web/package.json | 2 +- packages/webpack/package.json | 2 +- packages/workspace/package.json | 2 +- 24 files changed, 68 insertions(+), 32 deletions(-) diff --git a/packages/cypress/package.json b/packages/cypress/package.json index 9e210cf0a9570..e26ee56be2fe9 100644 --- a/packages/cypress/package.json +++ b/packages/cypress/package.json @@ -19,7 +19,7 @@ "Cypress", "CLI" ], - "main": "./index.js", + "main": "./index", "typings": "./index.d.ts", "author": "Victor Savkin", "license": "MIT", diff --git a/packages/detox/package.json b/packages/detox/package.json index 4a146872139f1..dffe0875065bc 100644 --- a/packages/detox/package.json +++ b/packages/detox/package.json @@ -22,7 +22,7 @@ }, "license": "MIT", "author": "Victor Savkin", - "main": "index.js", + "main": "./index", "types": "index.d.ts", "dependencies": { "@nrwl/devkit": "file:../devkit", diff --git a/packages/devkit/package.json b/packages/devkit/package.json index 2059ea2bffaa0..1582771901d89 100644 --- a/packages/devkit/package.json +++ b/packages/devkit/package.json @@ -19,7 +19,7 @@ "Cypress", "CLI" ], - "main": "./index.js", + "main": "./index", "typings": "./index.d.ts", "author": "Victor Savkin", "license": "MIT", diff --git a/packages/esbuild/package.json b/packages/esbuild/package.json index d42066efe56ee..13649571cbb7d 100644 --- a/packages/esbuild/package.json +++ b/packages/esbuild/package.json @@ -14,7 +14,7 @@ "Web", "CLI" ], - "main": "./index.js", + "main": "./index", "typings": "./index.d.ts", "author": "Victor Savkin", "license": "MIT", diff --git a/packages/expo/package.json b/packages/expo/package.json index 102372ebd4435..87057d794c813 100644 --- a/packages/expo/package.json +++ b/packages/expo/package.json @@ -23,7 +23,7 @@ }, "license": "MIT", "author": "Victor Savkin", - "main": "index.js", + "main": "./index", "types": "index.d.ts", "dependencies": { "@nrwl/detox": "file:../detox", diff --git a/packages/express/package.json b/packages/express/package.json index 6b10225d8c46b..7b4bc15b456f0 100644 --- a/packages/express/package.json +++ b/packages/express/package.json @@ -16,7 +16,7 @@ "Cypress", "CLI" ], - "main": "./index.js", + "main": "./index", "typings": "./index.d.ts", "author": "Victor Savkin", "license": "MIT", diff --git a/packages/jest/package.json b/packages/jest/package.json index 1a907a464337a..fe17321aa70ae 100644 --- a/packages/jest/package.json +++ b/packages/jest/package.json @@ -19,7 +19,7 @@ "Unit Testing", "CLI" ], - "main": "./index.js", + "main": "./index", "typings": "./index.d.ts", "author": "Victor Savkin", "license": "MIT", diff --git a/packages/linter/package.json b/packages/linter/package.json index 6ee91db1058ec..531c044354a9c 100644 --- a/packages/linter/package.json +++ b/packages/linter/package.json @@ -15,7 +15,7 @@ "ESLint", "CLI" ], - "main": "./index.js", + "main": "./index", "typings": "./index.d.ts", "author": "Victor Savkin", "license": "MIT", diff --git a/packages/nest/package.json b/packages/nest/package.json index d3fbfdb2d98e9..b8648052c5643 100644 --- a/packages/nest/package.json +++ b/packages/nest/package.json @@ -16,7 +16,7 @@ "Cypress", "CLI" ], - "main": "./index.js", + "main": "./index", "typings": "./index.d.ts", "author": "Victor Savkin", "license": "MIT", diff --git a/packages/next/package.json b/packages/next/package.json index af5cb5c0fde2a..6e37247e68c9d 100644 --- a/packages/next/package.json +++ b/packages/next/package.json @@ -16,7 +16,7 @@ "Cypress", "CLI" ], - "main": "./index.js", + "main": "./index", "typings": "./index.d.ts", "author": "Victor Savkin", "license": "MIT", diff --git a/packages/node/package.json b/packages/node/package.json index 445db110b3648..65cd661b4fde0 100644 --- a/packages/node/package.json +++ b/packages/node/package.json @@ -16,7 +16,7 @@ "Cypress", "CLI" ], - "main": "./index.js", + "main": "./index", "typings": "./index.d.ts", "author": "Victor Savkin", "license": "MIT", diff --git a/packages/nx-plugin/package.json b/packages/nx-plugin/package.json index 33e9202bbbf66..f86dab5de1051 100644 --- a/packages/nx-plugin/package.json +++ b/packages/nx-plugin/package.json @@ -13,7 +13,7 @@ "Nx", "CLI" ], - "main": "./index.js", + "main": "./index", "types": "./index.d.ts", "author": "Nrwl", "license": "MIT", diff --git a/packages/nx/.eslintrc.json b/packages/nx/.eslintrc.json index 634134c1f3b32..2160aa7bb5821 100644 --- a/packages/nx/.eslintrc.json +++ b/packages/nx/.eslintrc.json @@ -8,8 +8,22 @@ "rules": {} }, { - "files": ["*.ts", "*.tsx"], - "rules": {} + "files": ["*.ts"], + "excludedFiles": ["*.spec.ts"], + "rules": { + "@typescript-eslint/no-restricted-imports": [ + "error", + { + "paths": [ + { + "name": "typescript", + "message": "TypeScript is an optional dependency for Nx. If you need to use it, make sure its installed first with ensureTypescript.", + "allowTypeImports": true + } + ] + } + ] + } }, { "files": ["*.js", "*.jsx"], diff --git a/packages/nx/src/utils/nx-plugin.ts b/packages/nx/src/utils/nx-plugin.ts index 6d48b5abc3e48..0d0e2072eefc7 100644 --- a/packages/nx/src/utils/nx-plugin.ts +++ b/packages/nx/src/utils/nx-plugin.ts @@ -24,6 +24,9 @@ import { import { normalizePath } from './path'; import { join } from 'path'; import { getNxRequirePaths } from './installation-directory'; +import { readTsConfig } from './typescript'; + +import type * as ts from 'typescript'; export type ProjectTargetConfigurator = ( file: string @@ -197,17 +200,25 @@ export function registerPluginTSTranspiler() { if (!tsNodeAndPathsRegistered) { // nx-ignore-next-line const ts: typeof import('typescript') = require('typescript'); + const tsConfigName = existsSync('tsconfig.base.json') + ? 'tsconfig.base.json' + : existsSync('tsconfig.json') + ? 'tsconfig.json' + : null; + const tsConfig: Partial = tsConfigName + ? readTsConfig(tsConfigName) + : {}; registerTsConfigPaths(join(workspaceRoot, 'tsconfig.base.json')); registerTranspiler({ + experimentalDecorators: true, + emitDecoratorMetadata: true, + ...tsConfig.options, lib: ['es2021'], module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2021, inlineSourceMap: true, - esModuleInterop: true, skipLibCheck: true, - experimentalDecorators: true, - emitDecoratorMetadata: true, }); } tsNodeAndPathsRegistered = true; diff --git a/packages/nx/src/utils/register.ts b/packages/nx/src/utils/register.ts index 5739d3a9dd6c8..de87ed3f8ce3b 100644 --- a/packages/nx/src/utils/register.ts +++ b/packages/nx/src/utils/register.ts @@ -1,5 +1,5 @@ import { dirname, join } from 'path'; -import type { CompilerOptions } from 'typescript'; +import type { CompilerOptions, ModuleResolutionKind } from 'typescript'; import { logger, NX_PREFIX, stripIndent } from './logger'; const swcNodeInstalled = packageIsInstalled('@swc-node/register'); @@ -178,9 +178,9 @@ export function getTsNodeCompilerOptions(compilerOptions: CompilerOptions) { ts = require('typescript'); } - const flagMap: Partial< - Record, keyof typeof ts> - > = { + const flagMap: Partial<{ + [key in keyof RemoveIndex]: keyof typeof ts; + }> = { module: 'ModuleKind', target: 'ScriptTarget', moduleDetection: 'ModuleDetectionKind', @@ -189,7 +189,9 @@ export function getTsNodeCompilerOptions(compilerOptions: CompilerOptions) { importsNotUsedAsValues: 'ImportsNotUsedAsValues', }; - const result = { ...compilerOptions }; + const result: { [key in keyof CompilerOptions]: any } = { + ...compilerOptions, + }; for (const flag in flagMap) { if (compilerOptions[flag]) { @@ -197,6 +199,15 @@ export function getTsNodeCompilerOptions(compilerOptions: CompilerOptions) { } } + delete result.pathsBasePath; + delete result.configFilePath; + if (result.moduleResolution) { + result.moduleResolution = + result.moduleResolution === 'NodeJs' + ? 'node' + : result.moduleResolution.toLowerCase(); + } + return result; } diff --git a/packages/react-native/package.json b/packages/react-native/package.json index cef45b341b79c..8e60769625d56 100644 --- a/packages/react-native/package.json +++ b/packages/react-native/package.json @@ -22,7 +22,7 @@ }, "license": "MIT", "author": "Victor Savkin", - "main": "index.js", + "main": "./index", "types": "index.d.ts", "dependencies": { "@nrwl/detox": "file:../detox", diff --git a/packages/react/package.json b/packages/react/package.json index ce4e0237ccffa..617bd64a410c5 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -16,7 +16,7 @@ "Cypress", "CLI" ], - "main": "./index.js", + "main": "./index", "typings": "./index.d.ts", "author": "Victor Savkin", "license": "MIT", diff --git a/packages/rollup/package.json b/packages/rollup/package.json index eea7331434da0..1271e2d369951 100644 --- a/packages/rollup/package.json +++ b/packages/rollup/package.json @@ -14,7 +14,7 @@ "Web", "CLI" ], - "main": "./index.js", + "main": "./index", "typings": "./index.d.ts", "author": "Victor Savkin", "license": "MIT", diff --git a/packages/storybook/package.json b/packages/storybook/package.json index d3d93510415a2..48a44cdb6f893 100644 --- a/packages/storybook/package.json +++ b/packages/storybook/package.json @@ -15,7 +15,7 @@ "Storybook", "Cypress" ], - "main": "./index.js", + "main": "./index", "typings": "./index.d.ts", "author": "Isaac Mann", "license": "MIT", diff --git a/packages/tao/package.json b/packages/tao/package.json index 195e17c5b22e8..f8a9bb31acd69 100644 --- a/packages/tao/package.json +++ b/packages/tao/package.json @@ -19,7 +19,7 @@ "Cypress", "CLI" ], - "main": "./index.js", + "main": "./index", "typings": "./index.d.ts", "author": "Victor Savkin", "license": "MIT", @@ -27,7 +27,7 @@ "url": "https://github.com/nrwl/nx/issues" }, "bin": { - "tao": "./index.js" + "tao": "./index" }, "homepage": "https://nx.dev", "dependencies": { diff --git a/packages/vite/package.json b/packages/vite/package.json index a0d1649f2a1d2..47819969d03a0 100644 --- a/packages/vite/package.json +++ b/packages/vite/package.json @@ -14,7 +14,7 @@ "Web", "CLI" ], - "main": "./index.js", + "main": "./index", "typings": "./index.d.ts", "author": "Victor Savkin", "license": "MIT", diff --git a/packages/web/package.json b/packages/web/package.json index 00d7db2171a7e..0499c17fda005 100644 --- a/packages/web/package.json +++ b/packages/web/package.json @@ -15,7 +15,7 @@ "Cypress", "CLI" ], - "main": "./index.js", + "main": "./index", "typings": "./index.d.ts", "author": "Victor Savkin", "license": "MIT", diff --git a/packages/webpack/package.json b/packages/webpack/package.json index d5cc148e4970f..942a505f142ba 100644 --- a/packages/webpack/package.json +++ b/packages/webpack/package.json @@ -14,7 +14,7 @@ "Web", "CLI" ], - "main": "./index.js", + "main": "./index", "typings": "./index.d.ts", "author": "Victor Savkin", "license": "MIT", diff --git a/packages/workspace/package.json b/packages/workspace/package.json index 690187291790c..9b170186674ce 100644 --- a/packages/workspace/package.json +++ b/packages/workspace/package.json @@ -19,7 +19,7 @@ "Cypress", "CLI" ], - "main": "./index.js", + "main": "./index", "typings": "./index.d.ts", "author": "Victor Savkin", "license": "MIT",