From a89c73483ee0035eeee039bab1522e788a37d54a Mon Sep 17 00:00:00 2001 From: Craigory Coppola Date: Thu, 29 Feb 2024 15:18:46 -0500 Subject: [PATCH] feat(core): use flag in nx.json for toggling crystal (#21980) --- docs/generated/devkit/NxJsonConfiguration.md | 9 ++++ docs/generated/devkit/Workspace.md | 13 ++++++ .../src/generators/application/lib/add-e2e.ts | 6 ++- .../ng-add/migrators/projects/e2e.migrator.ts | 7 +++- .../component-configuration.ts | 14 +++++-- .../generators/configuration/configuration.ts | 5 +++ packages/cypress/src/generators/init/init.ts | 7 +++- .../application/lib/normalize-options.ts | 9 ++-- packages/detox/src/generators/init/init.ts | 7 +++- packages/eslint/src/generators/init/init.ts | 6 ++- .../generators/lint-project/lint-project.ts | 7 +++- .../workspace-rule/workspace-rule.ts | 9 +++- .../application/lib/normalize-options.ts | 8 +++- packages/expo/src/generators/init/init.ts | 6 ++- .../library/lib/normalize-options.ts | 8 +++- .../src/generators/application/application.ts | 7 +++- .../generators/configuration/configuration.ts | 7 +++- packages/jest/src/generators/init/init.ts | 6 ++- packages/js/src/generators/library/library.ts | 7 +++- .../application/lib/normalize-options.ts | 9 +++- .../library/lib/normalize-options.ts | 9 +++- .../application/lib/normalize-options.ts | 10 ++++- packages/next/src/generators/init/init.ts | 8 +++- .../library/lib/normalize-options.ts | 9 +++- .../src/generators/application/application.ts | 8 +++- .../src/generators/e2e-project/e2e-project.ts | 8 +++- .../node/src/generators/library/library.ts | 9 +++- packages/nx/migrations.json | 2 +- packages/nx/schemas/nx-schema.json | 4 ++ packages/nx/src/adapter/compat.ts | 1 + packages/nx/src/command-line/add/add.ts | 1 + .../src/command-line/init/command-object.ts | 5 ++- packages/nx/src/config/nx-json.ts | 5 +++ ...ble-crysal-for-existing-workspaces.spec.ts | 41 +++++++++---------- ...disable-crystal-for-existing-workspaces.ts | 33 ++------------- .../generators/configuration/configuration.ts | 5 ++- .../playwright/src/generators/init/init.ts | 7 +++- .../plugin/src/generators/e2e-project/e2e.ts | 8 +++- .../application/lib/normalize-options.ts | 8 +++- .../react-native/src/generators/init/init.ts | 6 ++- .../library/lib/normalize-options.ts | 8 +++- .../storybook-configuration/configuration.ts | 8 +++- .../application/lib/normalize-options.ts | 10 ++++- .../cypress-component-configuration.ts | 8 +++- .../library/lib/normalize-options.ts | 8 +++- .../storybook-configuration/configuration.ts | 7 +++- .../application/lib/normalize-options.ts | 8 +++- .../cypress-component-configuration.impl.ts | 7 +++- packages/remix/src/generators/init/init.ts | 6 ++- .../library/lib/normalize-options.ts | 9 +++- .../src/generators/preset/preset.impl.ts | 9 +++- .../storybook-configuration.impl.ts | 7 +++- .../generators/configuration/configuration.ts | 10 ++++- .../storybook/src/generators/init/init.ts | 6 ++- .../generators/configuration/configuration.ts | 7 +++- packages/vite/src/generators/init/init.ts | 6 ++- .../src/generators/application/application.ts | 6 ++- .../generators/configuration/configuration.ts | 7 +++- packages/webpack/src/generators/init/init.ts | 6 ++- .../new/generate-workspace-files.ts | 1 + .../src/generators/preset/preset.spec.ts | 4 ++ .../workspace/src/generators/preset/preset.ts | 15 ++++++- 62 files changed, 385 insertions(+), 122 deletions(-) diff --git a/docs/generated/devkit/NxJsonConfiguration.md b/docs/generated/devkit/NxJsonConfiguration.md index 22c30cebb6623..9db108e67346a 100644 --- a/docs/generated/devkit/NxJsonConfiguration.md +++ b/docs/generated/devkit/NxJsonConfiguration.md @@ -38,6 +38,7 @@ Nx.json configuration - [targetDefaults](../../devkit/documents/NxJsonConfiguration#targetdefaults): TargetDefaults - [tasksRunnerOptions](../../devkit/documents/NxJsonConfiguration#tasksrunneroptions): Object - [useDaemonProcess](../../devkit/documents/NxJsonConfiguration#usedaemonprocess): boolean +- [useInferencePlugins](../../devkit/documents/NxJsonConfiguration#useinferenceplugins): boolean - [workspaceLayout](../../devkit/documents/NxJsonConfiguration#workspacelayout): Object ## Properties @@ -248,6 +249,14 @@ Set this to false to disable the daemon. --- +### useInferencePlugins + +• `Optional` **useInferencePlugins**: `boolean` + +Set this to false to disable adding inference plugins when generating new projects + +--- + ### workspaceLayout • `Optional` **workspaceLayout**: `Object` diff --git a/docs/generated/devkit/Workspace.md b/docs/generated/devkit/Workspace.md index 1a86757ce6906..05c15bc3a935e 100644 --- a/docs/generated/devkit/Workspace.md +++ b/docs/generated/devkit/Workspace.md @@ -37,6 +37,7 @@ use ProjectsConfigurations or NxJsonConfiguration - [targetDefaults](../../devkit/documents/Workspace#targetdefaults): TargetDefaults - [tasksRunnerOptions](../../devkit/documents/Workspace#tasksrunneroptions): Object - [useDaemonProcess](../../devkit/documents/Workspace#usedaemonprocess): boolean +- [useInferencePlugins](../../devkit/documents/Workspace#useinferenceplugins): boolean - [version](../../devkit/documents/Workspace#version): number - [workspaceLayout](../../devkit/documents/Workspace#workspacelayout): Object @@ -340,6 +341,18 @@ Set this to false to disable the daemon. --- +### useInferencePlugins + +• `Optional` **useInferencePlugins**: `boolean` + +Set this to false to disable adding inference plugins when generating new projects + +#### Inherited from + +[NxJsonConfiguration](../../devkit/documents/NxJsonConfiguration).[useInferencePlugins](../../devkit/documents/NxJsonConfiguration#useinferenceplugins) + +--- + ### version • **version**: `number` diff --git a/packages/angular/src/generators/application/lib/add-e2e.ts b/packages/angular/src/generators/application/lib/add-e2e.ts index 13da1b65720b0..5ce0352dd0153 100644 --- a/packages/angular/src/generators/application/lib/add-e2e.ts +++ b/packages/angular/src/generators/application/lib/add-e2e.ts @@ -8,6 +8,7 @@ import { joinPathFragments, readProjectConfiguration, updateProjectConfiguration, + readNxJson, } from '@nx/devkit'; import { nxVersion } from '../../../utils/versions'; import { getInstalledAngularVersionInfo } from '../../utils/version-utils'; @@ -15,7 +16,10 @@ import type { NormalizedSchema } from './normalized-schema'; export async function addE2e(tree: Tree, options: NormalizedSchema) { // since e2e are separate projects, default to adding plugins - const addPlugin = process.env.NX_ADD_PLUGINS !== 'false'; + const nxJson = readNxJson(tree); + const addPlugin = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; if (options.e2eTestRunner === 'cypress') { // TODO: This can call `@nx/web:static-config` generator when ready diff --git a/packages/angular/src/generators/ng-add/migrators/projects/e2e.migrator.ts b/packages/angular/src/generators/ng-add/migrators/projects/e2e.migrator.ts index 53b793b14574d..15aad5844476f 100644 --- a/packages/angular/src/generators/ng-add/migrators/projects/e2e.migrator.ts +++ b/packages/angular/src/generators/ng-add/migrators/projects/e2e.migrator.ts @@ -11,6 +11,7 @@ import { joinPathFragments, offsetFromRoot, readJson, + readNxJson, readProjectConfiguration, stripIndents, updateJson, @@ -342,6 +343,10 @@ export class E2eMigrator extends ProjectMigrator { tags: [], implicitDependencies: [this.appName], }); + const nxJson = readNxJson(this.tree) ?? {}; + const addPlugin = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; await configurationGenerator(this.tree, { project: this.project.name, linter: this.isProjectUsingEsLint ? Linter.EsLint : Linter.None, @@ -349,7 +354,7 @@ export class E2eMigrator extends ProjectMigrator { // any target would do, we replace it later with the target existing in the project being migrated devServerTarget: `${this.appName}:serve`, baseUrl: 'http://localhost:4200', - addPlugin: process.env.NX_ADD_PLUGINS !== 'false', + addPlugin, }); const cypressConfigFilePath = this.updateOrCreateCypressConfigFile( diff --git a/packages/cypress/src/generators/component-configuration/component-configuration.ts b/packages/cypress/src/generators/component-configuration/component-configuration.ts index 6875aa38a0490..3dac12bad71e7 100644 --- a/packages/cypress/src/generators/component-configuration/component-configuration.ts +++ b/packages/cypress/src/generators/component-configuration/component-configuration.ts @@ -43,7 +43,7 @@ export async function componentConfigurationGeneratorInternal( options: CypressComponentConfigurationSchema ) { const tasks: GeneratorCallback[] = []; - const opts = normalizeOptions(options); + const opts = normalizeOptions(tree, options); tasks.push( await init(tree, { @@ -78,7 +78,10 @@ export async function componentConfigurationGeneratorInternal( return runTasksInSerial(...tasks); } -function normalizeOptions(options: CypressComponentConfigurationSchema) { +function normalizeOptions( + tree: Tree, + options: CypressComponentConfigurationSchema +) { const cyVersion = installedCypressVersion(); if (cyVersion && cyVersion < 10) { throw new Error( @@ -86,8 +89,13 @@ function normalizeOptions(options: CypressComponentConfigurationSchema) { ); } + const nxJson = readNxJson(tree); + const addPlugin = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + return { - addPlugin: process.env.NX_ADD_PLUGINS !== 'false', + addPlugin, ...options, directory: options.directory ?? 'cypress', }; diff --git a/packages/cypress/src/generators/configuration/configuration.ts b/packages/cypress/src/generators/configuration/configuration.ts index be3b2c514c19d..999cb5faca921 100644 --- a/packages/cypress/src/generators/configuration/configuration.ts +++ b/packages/cypress/src/generators/configuration/configuration.ts @@ -150,6 +150,11 @@ In this case you need to provide a devServerTarget,':[: throw new Error('Either baseUrl or devServerTarget must be provided'); } + const nxJson = readNxJson(tree); + options.addPlugin ??= + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + return { ...options, bundler: options.bundler ?? 'webpack', diff --git a/packages/cypress/src/generators/init/init.ts b/packages/cypress/src/generators/init/init.ts index 72e794d76b66a..0431d0bffdfa3 100644 --- a/packages/cypress/src/generators/init/init.ts +++ b/packages/cypress/src/generators/init/init.ts @@ -105,7 +105,12 @@ export async function cypressInitGeneratorInternal( options: Schema ) { updateProductionFileset(tree); - options.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + + const nxJson = readNxJson(tree); + + options.addPlugin ??= + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; if (options.addPlugin) { addPlugin(tree); diff --git a/packages/detox/src/generators/application/lib/normalize-options.ts b/packages/detox/src/generators/application/lib/normalize-options.ts index f72ebd54c86e2..32972616501e3 100644 --- a/packages/detox/src/generators/application/lib/normalize-options.ts +++ b/packages/detox/src/generators/application/lib/normalize-options.ts @@ -1,4 +1,4 @@ -import { names, readProjectConfiguration, Tree } from '@nx/devkit'; +import { names, readNxJson, readProjectConfiguration, Tree } from '@nx/devkit'; import { determineProjectNameAndRootOptions } from '@nx/devkit/src/generators/project-name-and-root-utils'; import { Schema } from '../schema'; @@ -23,8 +23,11 @@ export async function normalizeOptions( projectNameAndRootFormat: options.projectNameAndRootFormat, callingGenerator: '@nx/detox:application', }); - - options.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + const nxJson = readNxJson(host); + const addPlugin = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + options.addPlugin ??= addPlugin; const { fileName: appFileName, className: appClassName } = names( options.appName || options.appProject diff --git a/packages/detox/src/generators/init/init.ts b/packages/detox/src/generators/init/init.ts index 71de5f68aa590..ac48480f3ce6e 100644 --- a/packages/detox/src/generators/init/init.ts +++ b/packages/detox/src/generators/init/init.ts @@ -20,7 +20,12 @@ export function detoxInitGenerator(host: Tree, schema: Schema) { export async function detoxInitGeneratorInternal(host: Tree, schema: Schema) { const tasks: GeneratorCallback[] = []; - schema.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + const nxJson = readNxJson(host); + const addPluginDefault = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + + schema.addPlugin ??= addPluginDefault; if (!schema.skipPackageJson) { tasks.push(moveDependency(host)); diff --git a/packages/eslint/src/generators/init/init.ts b/packages/eslint/src/generators/init/init.ts index c8a91d94a50ea..d6f91ed8c31cb 100644 --- a/packages/eslint/src/generators/init/init.ts +++ b/packages/eslint/src/generators/init/init.ts @@ -74,7 +74,11 @@ export async function initEsLint( tree: Tree, options: LinterInitOptions ): Promise { - options.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + const nxJson = readNxJson(tree); + const addPluginDefault = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + options.addPlugin ??= addPluginDefault; const hasPlugin = hasEslintPlugin(tree); const rootEslintFile = findEslintFile(tree); diff --git a/packages/eslint/src/generators/lint-project/lint-project.ts b/packages/eslint/src/generators/lint-project/lint-project.ts index 3e9acec2af810..f6babf108066e 100644 --- a/packages/eslint/src/generators/lint-project/lint-project.ts +++ b/packages/eslint/src/generators/lint-project/lint-project.ts @@ -5,6 +5,7 @@ import type { Tree, } from '@nx/devkit'; import { + readNxJson, formatFiles, offsetFromRoot, readJson, @@ -67,7 +68,11 @@ export async function lintProjectGeneratorInternal( tree: Tree, options: LintProjectOptions ) { - options.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + const nxJson = readNxJson(tree); + const addPluginDefault = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + options.addPlugin ??= addPluginDefault; const tasks: GeneratorCallback[] = []; const initTask = await lintInitGenerator(tree, { skipPackageJson: options.skipPackageJson, diff --git a/packages/eslint/src/generators/workspace-rule/workspace-rule.ts b/packages/eslint/src/generators/workspace-rule/workspace-rule.ts index 31cca983e5218..316a1d075e2c6 100644 --- a/packages/eslint/src/generators/workspace-rule/workspace-rule.ts +++ b/packages/eslint/src/generators/workspace-rule/workspace-rule.ts @@ -5,6 +5,7 @@ import { generateFiles, joinPathFragments, logger, + readNxJson, Tree, } from '@nx/devkit'; import { camelize } from '@nx/devkit/src/utils/string-utils'; @@ -22,10 +23,16 @@ export async function lintWorkspaceRuleGenerator( tree: Tree, options: LintWorkspaceRuleGeneratorOptions ) { + const nxJson = readNxJson(tree); // Ensure that the workspace rules project has been created const projectGeneratorCallback = await lintWorkspaceRulesProjectGenerator( tree, - { skipFormat: true, addPlugin: process.env.NX_ADD_PLUGINS !== 'false' } + { + skipFormat: true, + addPlugin: + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false, + } ); const ruleDir = joinPathFragments( diff --git a/packages/expo/src/generators/application/lib/normalize-options.ts b/packages/expo/src/generators/application/lib/normalize-options.ts index 28072708b90df..a7e6315c260e1 100644 --- a/packages/expo/src/generators/application/lib/normalize-options.ts +++ b/packages/expo/src/generators/application/lib/normalize-options.ts @@ -1,4 +1,4 @@ -import { names, Tree } from '@nx/devkit'; +import { names, readNxJson, Tree } from '@nx/devkit'; import { determineProjectNameAndRootOptions } from '@nx/devkit/src/generators/project-name-and-root-utils'; import { Schema } from '../schema'; @@ -30,7 +30,11 @@ export async function normalizeOptions( callingGenerator: '@nx/expo:application', }); options.projectNameAndRootFormat = projectNameAndRootFormat; - options.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + const nxJson = readNxJson(host); + const addPluginDefault = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + options.addPlugin ??= addPluginDefault; const { className } = names(options.name); const parsedTags = options.tags diff --git a/packages/expo/src/generators/init/init.ts b/packages/expo/src/generators/init/init.ts index 09b2c116d2131..f1a37634ce677 100644 --- a/packages/expo/src/generators/init/init.ts +++ b/packages/expo/src/generators/init/init.ts @@ -28,7 +28,11 @@ export function expoInitGenerator(tree: Tree, schema: Schema) { } export async function expoInitGeneratorInternal(host: Tree, schema: Schema) { - schema.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + const nxJson = readNxJson(host); + const addPluginDefault = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + schema.addPlugin ??= addPluginDefault; addGitIgnoreEntry(host); diff --git a/packages/expo/src/generators/library/lib/normalize-options.ts b/packages/expo/src/generators/library/lib/normalize-options.ts index d66fe0ea069e5..9749a6accd599 100644 --- a/packages/expo/src/generators/library/lib/normalize-options.ts +++ b/packages/expo/src/generators/library/lib/normalize-options.ts @@ -1,4 +1,4 @@ -import { Tree } from '@nx/devkit'; +import { Tree, readNxJson } from '@nx/devkit'; import { determineProjectNameAndRootOptions } from '@nx/devkit/src/generators/project-name-and-root-utils'; import { Schema } from '../schema'; @@ -28,7 +28,11 @@ export async function normalizeOptions( projectNameAndRootFormat: options.projectNameAndRootFormat, callingGenerator: '@nx/expo:library', }); - options.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + const nxJson = readNxJson(host); + const addPluginDefault = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + options.addPlugin ??= addPluginDefault; const parsedTags = options.tags ? options.tags.split(',').map((s) => s.trim()) diff --git a/packages/express/src/generators/application/application.ts b/packages/express/src/generators/application/application.ts index 3710588dbe4e5..5a366b1c41b99 100644 --- a/packages/express/src/generators/application/application.ts +++ b/packages/express/src/generators/application/application.ts @@ -2,6 +2,7 @@ import type { GeneratorCallback, Tree } from '@nx/devkit'; import { addDependenciesToPackageJson, formatFiles, + readNxJson, runTasksInSerial, toJS, updateJson, @@ -114,7 +115,11 @@ async function normalizeOptions( callingGenerator: '@nx/express:application', }); options.projectNameAndRootFormat = projectNameAndRootFormat; - options.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + const nxJson = readNxJson(host); + const addPlugin = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + options.addPlugin ??= addPlugin; return { ...options, diff --git a/packages/jest/src/generators/configuration/configuration.ts b/packages/jest/src/generators/configuration/configuration.ts index 41c25e7ef2ab8..0595c01e97d41 100644 --- a/packages/jest/src/generators/configuration/configuration.ts +++ b/packages/jest/src/generators/configuration/configuration.ts @@ -36,7 +36,12 @@ function normalizeOptions( options.testEnvironment = 'jsdom'; } - options.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + const nxJson = readNxJson(tree); + const addPlugin = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + + options.addPlugin ??= addPlugin; options.targetName ??= 'test'; diff --git a/packages/jest/src/generators/init/init.ts b/packages/jest/src/generators/init/init.ts index 48d77f8e8fa5d..c0e72f02fcf7c 100644 --- a/packages/jest/src/generators/init/init.ts +++ b/packages/jest/src/generators/init/init.ts @@ -110,7 +110,11 @@ export async function jestInitGeneratorInternal( tree: Tree, options: JestInitSchema ): Promise { - options.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + const nxJson = readNxJson(tree); + const addPluginDefault = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + options.addPlugin ??= addPluginDefault; const presetExt = isPresetCjs(tree) ? 'cjs' : 'js'; diff --git a/packages/js/src/generators/library/library.ts b/packages/js/src/generators/library/library.ts index 4a888313c3bb3..a8066f739d767 100644 --- a/packages/js/src/generators/library/library.ts +++ b/packages/js/src/generators/library/library.ts @@ -9,6 +9,7 @@ import { names, offsetFromRoot, ProjectConfiguration, + readNxJson, readProjectConfiguration, runTasksInSerial, toJS, @@ -585,7 +586,11 @@ async function normalizeOptions( tree: Tree, options: LibraryGeneratorSchema ): Promise { - options.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + const nxJson = readNxJson(tree); + const addPlugin = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + options.addPlugin ??= addPlugin; /** * We are deprecating the compiler and the buildable options. diff --git a/packages/nest/src/generators/application/lib/normalize-options.ts b/packages/nest/src/generators/application/lib/normalize-options.ts index c29907c93ceb6..cdf793226d1c8 100644 --- a/packages/nest/src/generators/application/lib/normalize-options.ts +++ b/packages/nest/src/generators/application/lib/normalize-options.ts @@ -1,4 +1,4 @@ -import { Tree } from '@nx/devkit'; +import { Tree, readNxJson } from '@nx/devkit'; import { determineProjectNameAndRootOptions } from '@nx/devkit/src/generators/project-name-and-root-utils'; import { Linter } from '@nx/eslint'; import type { Schema as NodeApplicationGeneratorOptions } from '@nx/node/src/generators/application/schema'; @@ -23,8 +23,13 @@ export async function normalizeOptions( options.rootProject = appProjectRoot === '.'; options.projectNameAndRootFormat = projectNameAndRootFormat; + const nxJson = readNxJson(tree); + const addPlugin = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + return { - addPlugin: process.env.NX_ADD_PLUGINS !== 'false', + addPlugin, ...options, strict: options.strict ?? false, appProjectName, diff --git a/packages/nest/src/generators/library/lib/normalize-options.ts b/packages/nest/src/generators/library/lib/normalize-options.ts index bf3dbc9369155..beb1e1b823d01 100644 --- a/packages/nest/src/generators/library/lib/normalize-options.ts +++ b/packages/nest/src/generators/library/lib/normalize-options.ts @@ -1,4 +1,4 @@ -import { Tree } from '@nx/devkit'; +import { Tree, readNxJson } from '@nx/devkit'; import { determineProjectNameAndRootOptions } from '@nx/devkit/src/generators/project-name-and-root-utils'; import { getNpmScope } from '@nx/js/src/utils/package-json/get-npm-scope'; import type { LibraryGeneratorSchema as JsLibraryGeneratorSchema } from '@nx/js/src/utils/schema'; @@ -22,7 +22,12 @@ export async function normalizeOptions( projectNameAndRootFormat: options.projectNameAndRootFormat, callingGenerator: '@nx/nest:library', }); - options.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + const nxJson = readNxJson(tree); + const addPlugin = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + + options.addPlugin ??= addPlugin; const fileName = options.simpleName ? projectNames.projectSimpleName diff --git a/packages/next/src/generators/application/lib/normalize-options.ts b/packages/next/src/generators/application/lib/normalize-options.ts index 28c45db49bb46..df3b7fb3655db 100644 --- a/packages/next/src/generators/application/lib/normalize-options.ts +++ b/packages/next/src/generators/application/lib/normalize-options.ts @@ -1,4 +1,4 @@ -import { joinPathFragments, names, Tree } from '@nx/devkit'; +import { joinPathFragments, names, readNxJson, Tree } from '@nx/devkit'; import { determineProjectNameAndRootOptions } from '@nx/devkit/src/generators/project-name-and-root-utils'; import { Linter } from '@nx/eslint'; import { assertValidStyle } from '@nx/react/src/utils/assertion'; @@ -34,7 +34,13 @@ export async function normalizeOptions( }); options.rootProject = appProjectRoot === '.'; options.projectNameAndRootFormat = projectNameAndRootFormat; - options.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + + const nxJson = readNxJson(host); + const addPlugin = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + + options.addPlugin ??= addPlugin; const e2eProjectName = options.rootProject ? 'e2e' : `${appProjectName}-e2e`; const e2eProjectRoot = options.rootProject ? 'e2e' : `${appProjectRoot}-e2e`; diff --git a/packages/next/src/generators/init/init.ts b/packages/next/src/generators/init/init.ts index 3849cddd48364..bb6351de2f51a 100644 --- a/packages/next/src/generators/init/init.ts +++ b/packages/next/src/generators/init/init.ts @@ -4,6 +4,7 @@ import { runTasksInSerial, type GeneratorCallback, type Tree, + readNxJson, } from '@nx/devkit'; import { updatePackageScripts } from '@nx/devkit/src/utils/update-package-scripts'; import { reactDomVersion, reactVersion } from '@nx/react/src/utils/versions'; @@ -44,7 +45,12 @@ export async function nextInitGeneratorInternal( host: Tree, schema: InitSchema ) { - schema.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + const nxJson = readNxJson(host); + const addPluginDefault = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + + schema.addPlugin ??= addPluginDefault; if (schema.addPlugin) { addPlugin(host); } diff --git a/packages/next/src/generators/library/lib/normalize-options.ts b/packages/next/src/generators/library/lib/normalize-options.ts index 0e63da0b0f2e5..7eada6b256314 100644 --- a/packages/next/src/generators/library/lib/normalize-options.ts +++ b/packages/next/src/generators/library/lib/normalize-options.ts @@ -1,4 +1,4 @@ -import { Tree } from '@nx/devkit'; +import { Tree, readNxJson } from '@nx/devkit'; import { determineProjectNameAndRootOptions } from '@nx/devkit/src/generators/project-name-and-root-utils'; import { Schema } from '../schema'; @@ -21,7 +21,12 @@ export async function normalizeOptions( callingGenerator: '@nx/next:library', }); options.projectNameAndRootFormat = projectNameAndRootFormat; - options.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + + const nxJson = readNxJson(host); + const addPlugin = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + options.addPlugin ??= addPlugin; return { ...options, diff --git a/packages/node/src/generators/application/application.ts b/packages/node/src/generators/application/application.ts index a121ef164f89e..e0345d7f84bc9 100644 --- a/packages/node/src/generators/application/application.ts +++ b/packages/node/src/generators/application/application.ts @@ -10,6 +10,7 @@ import { names, offsetFromRoot, ProjectConfiguration, + readNxJson, readProjectConfiguration, runTasksInSerial, TargetConfiguration, @@ -543,8 +544,13 @@ async function normalizeOptions( ? options.tags.split(',').map((s) => s.trim()) : []; + const nxJson = readNxJson(host); + const addPlugin = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + return { - addPlugin: process.env.NX_ADD_PLUGINS !== 'false', + addPlugin, ...options, name: appProjectName, frontendProject: options.frontendProject diff --git a/packages/node/src/generators/e2e-project/e2e-project.ts b/packages/node/src/generators/e2e-project/e2e-project.ts index d9c1a35193070..b5b79420cd15f 100644 --- a/packages/node/src/generators/e2e-project/e2e-project.ts +++ b/packages/node/src/generators/e2e-project/e2e-project.ts @@ -7,6 +7,7 @@ import { joinPathFragments, names, offsetFromRoot, + readNxJson, readProjectConfiguration, runTasksInSerial, Tree, @@ -163,8 +164,13 @@ async function normalizeOptions( callingGenerator: null, }); + const nxJson = readNxJson(tree); + const addPlugin = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + return { - addPlugin: process.env.NX_ADD_PLUGINS !== 'false', + addPlugin, ...options, e2eProjectRoot, e2eProjectName, diff --git a/packages/node/src/generators/library/library.ts b/packages/node/src/generators/library/library.ts index a8d27e8937b7b..9499236ced506 100644 --- a/packages/node/src/generators/library/library.ts +++ b/packages/node/src/generators/library/library.ts @@ -6,6 +6,7 @@ import { joinPathFragments, names, offsetFromRoot, + readNxJson, readProjectConfiguration, runTasksInSerial, toJS, @@ -101,7 +102,13 @@ async function normalizeOptions( callingGenerator: '@nx/node:library', }); options.projectNameAndRootFormat = projectNameAndRootFormat; - options.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + + const nxJson = readNxJson(tree); + const addPluginDefault = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + + options.addPlugin ??= addPluginDefault; const fileName = getCaseAwareFileName({ fileName: options.simpleModuleName diff --git a/packages/nx/migrations.json b/packages/nx/migrations.json index 181d22ac37e9c..f79dbe22b8193 100644 --- a/packages/nx/migrations.json +++ b/packages/nx/migrations.json @@ -86,7 +86,7 @@ "18.0.0-disable-adding-plugins-for-existing-workspaces": { "cli": "nx", "version": "18.0.0-beta.2", - "description": "Updates .env to disabled adding plugins when generating projects in an existing Nx workspace", + "description": "Updates nx.json to disabled adding plugins when generating projects in an existing Nx workspace", "implementation": "./src/migrations/update-18-0-0/disable-crystal-for-existing-workspaces", "x-repair-skip": true }, diff --git a/packages/nx/schemas/nx-schema.json b/packages/nx/schemas/nx-schema.json index ea32715b88ee1..26273a6f0e0c2 100644 --- a/packages/nx/schemas/nx-schema.json +++ b/packages/nx/schemas/nx-schema.json @@ -101,6 +101,10 @@ "type": "boolean", "description": "Specifies whether the daemon should be used for the default tasks runner." }, + "useInferencePlugins": { + "type": "boolean", + "description": "Specifies whether to add inference plugins when generating new projects." + }, "release": { "type": "object", "description": "Configuration for the nx release commands.", diff --git a/packages/nx/src/adapter/compat.ts b/packages/nx/src/adapter/compat.ts index d7934d1a3bcb8..05f8b8ab1b4df 100644 --- a/packages/nx/src/adapter/compat.ts +++ b/packages/nx/src/adapter/compat.ts @@ -75,6 +75,7 @@ export const allowedWorkspaceExtensions = [ 'parallel', 'cacheDirectory', 'useDaemonProcess', + 'useInferencePlugins', ] as const; if (!patched) { diff --git a/packages/nx/src/command-line/add/add.ts b/packages/nx/src/command-line/add/add.ts index 4bb53936015b2..1d2fbb0a4e276 100644 --- a/packages/nx/src/command-line/add/add.ts +++ b/packages/nx/src/command-line/add/add.ts @@ -112,6 +112,7 @@ async function initializePlugin( updatePackageScripts = options.updatePackageScripts; } else { updatePackageScripts = + readNxJson().useInferencePlugins !== false && process.env.NX_ADD_PLUGINS !== 'false' && coreNxPlugins.includes(pkgName); } diff --git a/packages/nx/src/command-line/init/command-object.ts b/packages/nx/src/command-line/init/command-object.ts index 706cde2a87478..edfef2d999bab 100644 --- a/packages/nx/src/command-line/init/command-object.ts +++ b/packages/nx/src/command-line/init/command-object.ts @@ -1,7 +1,10 @@ import { Argv, CommandModule } from 'yargs'; import { parseCSV } from '../yargs-utils/shared-options'; +import { readNxJson } from '../../config/nx-json'; -const useV2 = process.env['NX_ADD_PLUGINS'] !== 'false'; +const useV2 = + process.env['NX_ADD_PLUGINS'] !== 'false' && + readNxJson().useInferencePlugins !== false; export const yargsInitCommand: CommandModule = { command: 'init', diff --git a/packages/nx/src/config/nx-json.ts b/packages/nx/src/config/nx-json.ts index cd72363bfea5c..3abfe052e08b0 100644 --- a/packages/nx/src/config/nx-json.ts +++ b/packages/nx/src/config/nx-json.ts @@ -392,6 +392,11 @@ export interface NxJsonConfiguration { * Set this to false to disable the daemon. */ useDaemonProcess?: boolean; + + /** + * Set this to false to disable adding inference plugins when generating new projects + */ + useInferencePlugins?: boolean; } export type PluginConfiguration = diff --git a/packages/nx/src/migrations/update-18-0-0/disable-crysal-for-existing-workspaces.spec.ts b/packages/nx/src/migrations/update-18-0-0/disable-crysal-for-existing-workspaces.spec.ts index 6c7ed4ef2e0d8..6ae16df790763 100644 --- a/packages/nx/src/migrations/update-18-0-0/disable-crysal-for-existing-workspaces.spec.ts +++ b/packages/nx/src/migrations/update-18-0-0/disable-crysal-for-existing-workspaces.spec.ts @@ -1,29 +1,26 @@ -import { createTree } from '../../generators/testing-utils/create-tree'; +import { createTreeWithEmptyWorkspace } from '../../generators/testing-utils/create-tree-with-empty-workspace'; import migrate from './disable-crystal-for-existing-workspaces'; describe('disable crystal for existing workspaces', () => { - it("should create a .env if it doesn't exist", () => { - const tree = createTree(); + it('should add flag to nx.json', () => { + const tree = createTreeWithEmptyWorkspace(); migrate(tree); - expect(tree.read('.env', 'utf-8')).toMatchInlineSnapshot(` - "# Nx 18 enables using plugins to infer targets by default - # This is disabled for existing workspaces to maintain compatibility - # For more info, see: https://nx.dev/concepts/inferred-tasks - NX_ADD_PLUGINS=false" - `); - }); - - it('should update an existing .env', () => { - const tree = createTree(); - tree.write('.env', 'FOO=bar'); - migrate(tree); - expect(tree.read('.env', 'utf-8')).toMatchInlineSnapshot(` - "FOO=bar - - # Nx 18 enables using plugins to infer targets by default - # This is disabled for existing workspaces to maintain compatibility - # For more info, see: https://nx.dev/concepts/inferred-tasks - NX_ADD_PLUGINS=false" + expect(tree.read('nx.json', 'utf-8')).toMatchInlineSnapshot(` + "{ + "affected": { + "defaultBase": "main" + }, + "targetDefaults": { + "build": { + "cache": true + }, + "lint": { + "cache": true + } + }, + "useInferencePlugins": false + } + " `); }); }); diff --git a/packages/nx/src/migrations/update-18-0-0/disable-crystal-for-existing-workspaces.ts b/packages/nx/src/migrations/update-18-0-0/disable-crystal-for-existing-workspaces.ts index b00573ceb6dd6..951ea0ebc0a05 100644 --- a/packages/nx/src/migrations/update-18-0-0/disable-crystal-for-existing-workspaces.ts +++ b/packages/nx/src/migrations/update-18-0-0/disable-crystal-for-existing-workspaces.ts @@ -1,33 +1,8 @@ +import { readNxJson, updateNxJson } from '../../generators/utils/nx-json'; import { Tree } from '../../generators/tree'; -import { logger } from '../../utils/logger'; -import ignore from 'ignore'; export default function migrate(tree: Tree) { - const ig = ignore(); - try { - ig.add(tree.read('.gitignore', 'utf-8')); - if (ig.ignores('.env')) { - logger.warn( - 'NX The NX_ADD_PLUGINS=false environment variable was added to your .env file for backwards compatibility. However, your .env is ignored by git. Other contributors should add this key to their .env file or ensure that the environment variable is set to false when generating code with Nx.' - ); - } - } catch {} - - if (!tree.exists('.env')) { - tree.write('.env', ''); - } - - const dotenv = tree.read('.env', 'utf-8'); - const newDotenvContents = [ - '# Nx 18 enables using plugins to infer targets by default', - '# This is disabled for existing workspaces to maintain compatibility', - '# For more info, see: https://nx.dev/concepts/inferred-tasks', - 'NX_ADD_PLUGINS=false', - ]; - - if (dotenv.length) { - newDotenvContents.unshift(dotenv, ''); - } - - tree.write('.env', newDotenvContents.join('\n')); + const nxJson = readNxJson(tree); + nxJson.useInferencePlugins = false; + updateNxJson(tree, nxJson); } diff --git a/packages/playwright/src/generators/configuration/configuration.ts b/packages/playwright/src/generators/configuration/configuration.ts index bfdd477e0bae4..2bf2ba29f29fd 100644 --- a/packages/playwright/src/generators/configuration/configuration.ts +++ b/packages/playwright/src/generators/configuration/configuration.ts @@ -39,7 +39,10 @@ export async function configurationGeneratorInternal( tree: Tree, options: ConfigurationGeneratorSchema ) { - options.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + const nxJson = readNxJson(tree); + options.addPlugin ??= + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; const tasks: GeneratorCallback[] = []; tasks.push( await initGenerator(tree, { diff --git a/packages/playwright/src/generators/init/init.ts b/packages/playwright/src/generators/init/init.ts index 4144606963957..ac008f8246db7 100644 --- a/packages/playwright/src/generators/init/init.ts +++ b/packages/playwright/src/generators/init/init.ts @@ -22,7 +22,12 @@ export async function initGeneratorInternal( ) { const tasks: GeneratorCallback[] = []; - options.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + const nxJson = readNxJson(tree); + const addPluginDefault = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + + options.addPlugin ??= addPluginDefault; if (!options.skipPackageJson) { tasks.push( diff --git a/packages/plugin/src/generators/e2e-project/e2e.ts b/packages/plugin/src/generators/e2e-project/e2e.ts index 942f259e9e187..a51ab6679d804 100644 --- a/packages/plugin/src/generators/e2e-project/e2e.ts +++ b/packages/plugin/src/generators/e2e-project/e2e.ts @@ -11,6 +11,7 @@ import { names, offsetFromRoot, readJson, + readNxJson, readProjectConfiguration, runTasksInSerial, updateProjectConfiguration, @@ -37,7 +38,12 @@ async function normalizeOptions( ): Promise { const projectName = options.rootProject ? 'e2e' : `${options.pluginName}-e2e`; - options.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + const nxJson = readNxJson(host); + const addPlugin = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + + options.addPlugin ??= addPlugin; let projectRoot: string; if (options.projectNameAndRootFormat === 'as-provided') { diff --git a/packages/react-native/src/generators/application/lib/normalize-options.ts b/packages/react-native/src/generators/application/lib/normalize-options.ts index b1a17b8a15a0d..75da521f8d904 100644 --- a/packages/react-native/src/generators/application/lib/normalize-options.ts +++ b/packages/react-native/src/generators/application/lib/normalize-options.ts @@ -1,4 +1,4 @@ -import { joinPathFragments, names, Tree } from '@nx/devkit'; +import { joinPathFragments, names, readNxJson, Tree } from '@nx/devkit'; import { determineProjectNameAndRootOptions } from '@nx/devkit/src/generators/project-name-and-root-utils'; import { Schema } from '../schema'; @@ -34,7 +34,11 @@ export async function normalizeOptions( callingGenerator: '@nx/react-native:application', }); options.projectNameAndRootFormat = projectNameAndRootFormat; - options.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + const nxJson = readNxJson(host); + const addPluginDefault = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + options.addPlugin ??= addPluginDefault; const { className, fileName } = names(options.name); const iosProjectRoot = joinPathFragments(appProjectRoot, 'ios'); diff --git a/packages/react-native/src/generators/init/init.ts b/packages/react-native/src/generators/init/init.ts index 2ac47244137b9..5a1e5a21ff05e 100644 --- a/packages/react-native/src/generators/init/init.ts +++ b/packages/react-native/src/generators/init/init.ts @@ -32,7 +32,11 @@ export async function reactNativeInitGeneratorInternal( ) { addGitIgnoreEntry(host); - schema.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + const nxJson = readNxJson(host); + const addPluginDefault = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + schema.addPlugin ??= addPluginDefault; if (schema.addPlugin) { addPlugin(host); diff --git a/packages/react-native/src/generators/library/lib/normalize-options.ts b/packages/react-native/src/generators/library/lib/normalize-options.ts index 70eb4720cb548..d612467c7fab3 100644 --- a/packages/react-native/src/generators/library/lib/normalize-options.ts +++ b/packages/react-native/src/generators/library/lib/normalize-options.ts @@ -1,4 +1,4 @@ -import { Tree } from '@nx/devkit'; +import { Tree, readNxJson } from '@nx/devkit'; import { determineProjectNameAndRootOptions } from '@nx/devkit/src/generators/project-name-and-root-utils'; import { Schema } from '../schema'; @@ -30,7 +30,11 @@ export async function normalizeOptions( callingGenerator: '@nx/react-native:library', }); - options.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + const nxJson = readNxJson(host); + const addPluginDefault = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + options.addPlugin ??= addPluginDefault; const parsedTags = options.tags ? options.tags.split(',').map((s) => s.trim()) diff --git a/packages/react-native/src/generators/storybook-configuration/configuration.ts b/packages/react-native/src/generators/storybook-configuration/configuration.ts index c24bcf82f49d5..85606724e7f22 100644 --- a/packages/react-native/src/generators/storybook-configuration/configuration.ts +++ b/packages/react-native/src/generators/storybook-configuration/configuration.ts @@ -1,4 +1,4 @@ -import { Tree, logger } from '@nx/devkit'; +import { Tree, logger, readNxJson } from '@nx/devkit'; import { storybookConfigurationGenerator as reactStorybookConfigurationGenerator } from '@nx/react'; import { StorybookConfigureSchema } from './schema'; @@ -23,7 +23,11 @@ export async function storybookConfigurationGeneratorInternal( logger.warn( `Please run 'nx run @nx/react:storybook-configuration ${schema.project}' instead.` ); - schema.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + const nxJson = readNxJson(host); + const addPluginDefault = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + schema.addPlugin ??= addPluginDefault; return reactStorybookConfigurationGenerator(host, schema); } diff --git a/packages/react/src/generators/application/lib/normalize-options.ts b/packages/react/src/generators/application/lib/normalize-options.ts index 6596a57d931d7..81d99c9db42f4 100644 --- a/packages/react/src/generators/application/lib/normalize-options.ts +++ b/packages/react/src/generators/application/lib/normalize-options.ts @@ -1,4 +1,4 @@ -import { Tree, extractLayoutDirectory, names } from '@nx/devkit'; +import { Tree, extractLayoutDirectory, names, readNxJson } from '@nx/devkit'; import { determineProjectNameAndRootOptions } from '@nx/devkit/src/generators/project-name-and-root-utils'; import { assertValidStyle } from '../../../utils/assertion'; import { NormalizedSchema, Schema } from '../schema'; @@ -33,7 +33,13 @@ export async function normalizeOptions( rootProject: options.rootProject, callingGenerator, }); - options.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + + const nxJson = readNxJson(host); + const addPlugin = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + + options.addPlugin ??= addPlugin; options.rootProject = appProjectRoot === '.'; options.projectNameAndRootFormat = projectNameAndRootFormat; diff --git a/packages/react/src/generators/cypress-component-configuration/cypress-component-configuration.ts b/packages/react/src/generators/cypress-component-configuration/cypress-component-configuration.ts index 493831b7f05d6..19aea5a6f6c86 100644 --- a/packages/react/src/generators/cypress-component-configuration/cypress-component-configuration.ts +++ b/packages/react/src/generators/cypress-component-configuration/cypress-component-configuration.ts @@ -1,6 +1,7 @@ import { ensurePackage, formatFiles, + readNxJson, readProjectConfiguration, Tree, } from '@nx/devkit'; @@ -32,7 +33,12 @@ export async function cypressComponentConfigGeneratorInternal( typeof import('@nx/cypress') >('@nx/cypress', nxVersion); - options.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + const nxJson = readNxJson(tree); + const addPlugin = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + + options.addPlugin ??= addPlugin; const projectConfig = readProjectConfiguration(tree, options.project); const installTask = await baseCyCtConfig(tree, { diff --git a/packages/react/src/generators/library/lib/normalize-options.ts b/packages/react/src/generators/library/lib/normalize-options.ts index 71bb74ae5d801..bdb96427e8a73 100644 --- a/packages/react/src/generators/library/lib/normalize-options.ts +++ b/packages/react/src/generators/library/lib/normalize-options.ts @@ -3,6 +3,7 @@ import { joinPathFragments, logger, normalizePath, + readNxJson, Tree, } from '@nx/devkit'; import { determineProjectNameAndRootOptions } from '@nx/devkit/src/generators/project-name-and-root-utils'; @@ -26,7 +27,12 @@ export async function normalizeOptions( projectNameAndRootFormat: options.projectNameAndRootFormat, callingGenerator: '@nx/react:library', }); - options.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + const nxJson = readNxJson(host); + const addPlugin = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + + options.addPlugin ??= addPlugin; const fileName = options.simpleName ? projectNames.projectSimpleName diff --git a/packages/react/src/generators/storybook-configuration/configuration.ts b/packages/react/src/generators/storybook-configuration/configuration.ts index d91d083797fce..56dd7c2ea8c62 100644 --- a/packages/react/src/generators/storybook-configuration/configuration.ts +++ b/packages/react/src/generators/storybook-configuration/configuration.ts @@ -4,6 +4,7 @@ import { ensurePackage, formatFiles, joinPathFragments, + readNxJson, readProjectConfiguration, Tree, } from '@nx/devkit'; @@ -47,7 +48,11 @@ export async function storybookConfigurationGeneratorInternal( host: Tree, schema: StorybookConfigureSchema ) { - schema.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + const nxJson = readNxJson(host); + const addPluginDefault = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + schema.addPlugin ??= addPluginDefault; const { configurationGenerator } = ensurePackage< typeof import('@nx/storybook') >('@nx/storybook', nxVersion); diff --git a/packages/remix/src/generators/application/lib/normalize-options.ts b/packages/remix/src/generators/application/lib/normalize-options.ts index 757b79976fa5b..d51cc36721149 100644 --- a/packages/remix/src/generators/application/lib/normalize-options.ts +++ b/packages/remix/src/generators/application/lib/normalize-options.ts @@ -1,4 +1,4 @@ -import { type Tree } from '@nx/devkit'; +import { readNxJson, type Tree } from '@nx/devkit'; import { determineProjectNameAndRootOptions } from '@nx/devkit/src/generators/project-name-and-root-utils'; import { type NxRemixGeneratorSchema } from '../schema'; import { Linter } from '@nx/eslint'; @@ -26,7 +26,11 @@ export async function normalizeOptions( }); options.rootProject = projectRoot === '.'; options.projectNameAndRootFormat = projectNameAndRootFormat; - options.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + const nxJson = readNxJson(tree); + const addPluginDefault = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + options.addPlugin ??= addPluginDefault; const e2eProjectName = options.rootProject ? 'e2e' : `${projectName}-e2e`; const e2eProjectRoot = options.rootProject ? 'e2e' : `${projectRoot}-e2e`; diff --git a/packages/remix/src/generators/cypress-component-configuration/cypress-component-configuration.impl.ts b/packages/remix/src/generators/cypress-component-configuration/cypress-component-configuration.impl.ts index d26fa4077a94f..c3e5d93bf21e3 100644 --- a/packages/remix/src/generators/cypress-component-configuration/cypress-component-configuration.impl.ts +++ b/packages/remix/src/generators/cypress-component-configuration/cypress-component-configuration.impl.ts @@ -2,6 +2,7 @@ import { formatFiles, generateFiles, readProjectConfiguration, + readNxJson, type Tree, } from '@nx/devkit'; import { join } from 'path'; @@ -22,7 +23,11 @@ export async function cypressComponentConfigurationGeneratorInternal( tree: Tree, options: CypressComponentConfigurationSchema ) { - options.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + const nxJson = readNxJson(tree); + const addPluginDefault = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + options.addPlugin ??= addPluginDefault; await cypressComponentConfigGenerator(tree, { project: options.project, generateTests: options.generateTests, diff --git a/packages/remix/src/generators/init/init.ts b/packages/remix/src/generators/init/init.ts index ddc2a4c05f7ee..e08f359995f60 100644 --- a/packages/remix/src/generators/init/init.ts +++ b/packages/remix/src/generators/init/init.ts @@ -62,7 +62,11 @@ export async function remixInitGeneratorInternal(tree: Tree, options: Schema) { tasks.push(installTask); } - options.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + const nxJson = readNxJson(tree); + const addPluginDefault = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + options.addPlugin ??= addPluginDefault; if (options.addPlugin) { addPlugin(tree); } diff --git a/packages/remix/src/generators/library/lib/normalize-options.ts b/packages/remix/src/generators/library/lib/normalize-options.ts index 3ba18128b548b..e8d1f303cc74a 100644 --- a/packages/remix/src/generators/library/lib/normalize-options.ts +++ b/packages/remix/src/generators/library/lib/normalize-options.ts @@ -1,4 +1,4 @@ -import type { Tree } from '@nx/devkit'; +import { type Tree, readNxJson } from '@nx/devkit'; import { determineProjectNameAndRootOptions } from '@nx/devkit/src/generators/project-name-and-root-utils'; import { getImportPath } from '@nx/js/src/utils/get-import-path'; import type { NxRemixGeneratorSchema } from '../schema'; @@ -21,7 +21,11 @@ export async function normalizeOptions( callingGenerator: '@nx/remix:library', }); - options.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + const nxJson = readNxJson(tree); + const addPluginDefault = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + options.addPlugin ??= addPluginDefault; const importPath = options.importPath ?? getImportPath(tree, projectRoot); @@ -31,5 +35,6 @@ export async function normalizeOptions( importPath, projectName, projectRoot, + projectNameAndRootFormat, }; } diff --git a/packages/remix/src/generators/preset/preset.impl.ts b/packages/remix/src/generators/preset/preset.impl.ts index 56c20f5031d1e..af27b68a39231 100644 --- a/packages/remix/src/generators/preset/preset.impl.ts +++ b/packages/remix/src/generators/preset/preset.impl.ts @@ -1,4 +1,4 @@ -import { formatFiles, GeneratorCallback, Tree } from '@nx/devkit'; +import { formatFiles, GeneratorCallback, readNxJson, Tree } from '@nx/devkit'; import { runTasksInSerial } from '@nx/devkit'; import applicationGenerator from '../application/application.impl'; @@ -13,6 +13,11 @@ export default async function (tree: Tree, _options: RemixGeneratorSchema) { const setupGenTask = await setupGenerator(tree); tasks.push(setupGenTask); + const nxJson = readNxJson(tree); + const addPluginDefault = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + const appGenTask = await applicationGenerator(tree, { name: options.appName, tags: options.tags, @@ -21,7 +26,7 @@ export default async function (tree: Tree, _options: RemixGeneratorSchema) { unitTestRunner: options.unitTestRunner ?? 'vitest', e2eTestRunner: options.e2eTestRunner ?? 'cypress', js: options.js ?? false, - addPlugin: process.env.NX_ADD_PLUGINS !== 'false', + addPlugin: addPluginDefault, }); tasks.push(appGenTask); diff --git a/packages/remix/src/generators/storybook-configuration/storybook-configuration.impl.ts b/packages/remix/src/generators/storybook-configuration/storybook-configuration.impl.ts index 15887bc375dea..378621a315bb7 100644 --- a/packages/remix/src/generators/storybook-configuration/storybook-configuration.impl.ts +++ b/packages/remix/src/generators/storybook-configuration/storybook-configuration.impl.ts @@ -2,6 +2,7 @@ import { generateFiles, joinPathFragments, readProjectConfiguration, + readNxJson, type Tree, } from '@nx/devkit'; import { join } from 'path'; @@ -22,7 +23,11 @@ export default async function remixStorybookConfigurationInternal( tree: Tree, schema: StorybookConfigurationSchema ) { - schema.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + const nxJson = readNxJson(tree); + const addPluginDefault = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + schema.addPlugin ??= addPluginDefault; const { root } = readProjectConfiguration(tree, schema.project); if (!tree.exists(joinPathFragments(root, 'vite.config.ts'))) { diff --git a/packages/storybook/src/generators/configuration/configuration.ts b/packages/storybook/src/generators/configuration/configuration.ts index 45fa2609c92cd..97745990fc253 100644 --- a/packages/storybook/src/generators/configuration/configuration.ts +++ b/packages/storybook/src/generators/configuration/configuration.ts @@ -65,7 +65,7 @@ export async function configurationGeneratorInternal( throw new Error(pleaseUpgrade()); } - const schema = normalizeSchema(rawSchema); + const schema = normalizeSchema(tree, rawSchema); const tasks: GeneratorCallback[] = []; @@ -259,14 +259,20 @@ export async function configurationGeneratorInternal( } function normalizeSchema( + tree: Tree, schema: StorybookConfigureSchema ): StorybookConfigureSchema { + const nxJson = readNxJson(tree); + const addPlugin = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + const defaults = { interactionTests: true, linter: Linter.EsLint, js: false, tsConfiguration: true, - addPlugin: process.env.NX_ADD_PLUGINS !== 'false', + addPlugin, }; return { ...defaults, diff --git a/packages/storybook/src/generators/init/init.ts b/packages/storybook/src/generators/init/init.ts index b4b2676094fe2..7bedbaead4e32 100644 --- a/packages/storybook/src/generators/init/init.ts +++ b/packages/storybook/src/generators/init/init.ts @@ -95,7 +95,11 @@ export function initGenerator(tree: Tree, schema: Schema) { } export async function initGeneratorInternal(tree: Tree, schema: Schema) { - schema.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + const nxJson = readNxJson(tree); + const addPluginDefault = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + schema.addPlugin ??= addPluginDefault; if (schema.addPlugin) { addPlugin(tree); diff --git a/packages/vite/src/generators/configuration/configuration.ts b/packages/vite/src/generators/configuration/configuration.ts index e0b3ebf960499..b5322270c0b5c 100644 --- a/packages/vite/src/generators/configuration/configuration.ts +++ b/packages/vite/src/generators/configuration/configuration.ts @@ -40,7 +40,11 @@ export async function viteConfigurationGeneratorInternal( ) { const tasks: GeneratorCallback[] = []; - schema.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + const nxJson = readNxJson(tree); + const addPluginDefault = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + schema.addPlugin ??= addPluginDefault; const projectConfig = readProjectConfiguration(tree, schema.project); const { targets, root: projectRoot } = projectConfig; @@ -73,7 +77,6 @@ export async function viteConfigurationGeneratorInternal( tasks.push(initTask); tasks.push(ensureDependencies(tree, schema)); - const nxJson = readNxJson(tree); const hasPlugin = nxJson.plugins?.some((p) => typeof p === 'string' ? p === '@nx/vite/plugin' diff --git a/packages/vite/src/generators/init/init.ts b/packages/vite/src/generators/init/init.ts index 3c72a80dce609..bd5637531e98c 100644 --- a/packages/vite/src/generators/init/init.ts +++ b/packages/vite/src/generators/init/init.ts @@ -56,7 +56,11 @@ export async function initGeneratorInternal( tree: Tree, schema: InitGeneratorSchema ) { - schema.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + const nxJson = readNxJson(tree); + const addPluginDefault = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + schema.addPlugin ??= addPluginDefault; if (schema.addPlugin) { addPlugin(tree); } diff --git a/packages/web/src/generators/application/application.ts b/packages/web/src/generators/application/application.ts index 728dd93f0489b..a9d393f38ac9f 100644 --- a/packages/web/src/generators/application/application.ts +++ b/packages/web/src/generators/application/application.ts @@ -467,7 +467,11 @@ async function normalizeOptions( callingGenerator: '@nx/web:application', }); options.projectNameAndRootFormat = projectNameAndRootFormat; - options.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + const nxJson = readNxJson(host); + const addPluginDefault = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + options.addPlugin ??= addPluginDefault; const e2eProjectName = `${appProjectName}-e2e`; const e2eProjectRoot = `${appProjectRoot}-e2e`; diff --git a/packages/webpack/src/generators/configuration/configuration.ts b/packages/webpack/src/generators/configuration/configuration.ts index b0d9929da554b..e01c9ba212869 100644 --- a/packages/webpack/src/generators/configuration/configuration.ts +++ b/packages/webpack/src/generators/configuration/configuration.ts @@ -3,6 +3,7 @@ import { GeneratorCallback, joinPathFragments, offsetFromRoot, + readNxJson, readProjectConfiguration, runTasksInSerial, Tree, @@ -29,7 +30,11 @@ export async function configurationGeneratorInternal( options: ConfigurationGeneratorSchema ) { const tasks: GeneratorCallback[] = []; - options.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + const nxJson = readNxJson(tree); + const addPluginDefault = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + options.addPlugin ??= addPluginDefault; const initTask = await webpackInitGenerator(tree, { ...options, diff --git a/packages/webpack/src/generators/init/init.ts b/packages/webpack/src/generators/init/init.ts index 5e1080d15e9f8..361fe52f39b3d 100644 --- a/packages/webpack/src/generators/init/init.ts +++ b/packages/webpack/src/generators/init/init.ts @@ -16,7 +16,11 @@ export function webpackInitGenerator(tree: Tree, schema: Schema) { } export async function webpackInitGeneratorInternal(tree: Tree, schema: Schema) { - schema.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false'; + const nxJson = readNxJson(tree); + const addPluginDefault = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + schema.addPlugin ??= addPluginDefault; if (schema.addPlugin) { addPlugin(tree); diff --git a/packages/workspace/src/generators/new/generate-workspace-files.ts b/packages/workspace/src/generators/new/generate-workspace-files.ts index 358308e9a738c..916001092f8e1 100644 --- a/packages/workspace/src/generators/new/generate-workspace-files.ts +++ b/packages/workspace/src/generators/new/generate-workspace-files.ts @@ -91,6 +91,7 @@ function createNxJson( }; if (process.env.NX_ADD_PLUGINS === 'false') { nxJson.targetDefaults.build.inputs = ['production', '^production']; + nxJson.useInferencePlugins = false; } } diff --git a/packages/workspace/src/generators/preset/preset.spec.ts b/packages/workspace/src/generators/preset/preset.spec.ts index 9dc0bc4af7d56..6696c8d199c58 100644 --- a/packages/workspace/src/generators/preset/preset.spec.ts +++ b/packages/workspace/src/generators/preset/preset.spec.ts @@ -85,12 +85,16 @@ describe('preset', () => { }); it('should create files (preset = react-native)', async () => { + console.log('Hello'); await presetGenerator(tree, { name: 'proj', preset: Preset.ReactNative, linter: 'eslint', }); + process.stderr.write('HELLO'); + process.stderr.write(tree.listChanges().toString()); + expect(tree.exists('/apps/proj/src/app/App.tsx')).toBe(true); }); diff --git a/packages/workspace/src/generators/preset/preset.ts b/packages/workspace/src/generators/preset/preset.ts index e2d48c77d7c40..1b2ee6c5232f0 100644 --- a/packages/workspace/src/generators/preset/preset.ts +++ b/packages/workspace/src/generators/preset/preset.ts @@ -1,4 +1,4 @@ -import { installPackagesTask, names, Tree } from '@nx/devkit'; +import { installPackagesTask, names, readNxJson, Tree } from '@nx/devkit'; import { Schema } from './schema'; import { Preset } from '../utils/presets'; import { join } from 'path'; @@ -15,7 +15,15 @@ export async function presetGenerator(tree: Tree, options: Schema) { export default presetGenerator; async function createPreset(tree: Tree, options: Schema) { - const addPlugin = process.env.NX_ADD_PLUGINS !== 'false'; + console.log('Crate preset'); + const nxJson = readNxJson(tree); + const addPlugin = + process.env.NX_ADD_PLUGINS !== 'false' && + nxJson.useInferencePlugins !== false; + + console.log('Add plugin', addPlugin); + console.log(options.preset, Preset.ReactNative); + if (options.preset === Preset.Apps) { return; } else if (options.preset === Preset.AngularMonorepo) { @@ -210,8 +218,11 @@ async function createPreset(tree: Tree, options: Schema) { addPlugin, }); } else if (options.preset === Preset.ReactNative) { + console.log('Before require'); const { reactNativeApplicationGenerator } = require('@nx' + '/react-native'); + console.log('Hello', reactNativeApplicationGenerator); + console.log('Active require'); return reactNativeApplicationGenerator(tree, { name: options.name, directory: join('apps', options.name),