From eb54b1d249c634d2038d2c2f7755f599964c0ceb Mon Sep 17 00:00:00 2001 From: Craigory Coppola Date: Mon, 28 Apr 2025 15:47:44 -0400 Subject: [PATCH] feat(core)!: drop support for create nodes v1 in favor of only calling create nodes v2 (#30616) --- e2e/plugin/src/nx-plugin.fixtures.ts | 58 ++++++----- packages/detox/src/generators/init/init.ts | 8 +- packages/expo/src/generators/init/init.ts | 8 +- packages/nuxt/plugin.ts | 6 +- packages/nuxt/src/generators/init/init.ts | 8 +- packages/nuxt/src/plugins/plugin.ts | 22 ++++- packages/nx/src/adapter/angular-json.ts | 11 +++ packages/nx/src/plugins/js/index.ts | 9 ++ .../project-graph/plugins/loaded-nx-plugin.ts | 13 +-- .../utils/project-configuration-utils.spec.ts | 96 +++++++++++-------- .../react-native/src/generators/init/init.ts | 8 +- .../convert-to-inferred.ts | 8 +- packages/rollup/src/generators/init/init.ts | 8 +- packages/webpack/src/generators/init/init.ts | 8 +- 14 files changed, 163 insertions(+), 108 deletions(-) diff --git a/e2e/plugin/src/nx-plugin.fixtures.ts b/e2e/plugin/src/nx-plugin.fixtures.ts index da4f2d55e1..0135cc7a64 100644 --- a/e2e/plugin/src/nx-plugin.fixtures.ts +++ b/e2e/plugin/src/nx-plugin.fixtures.ts @@ -20,7 +20,7 @@ export default async function* execute( `; export const NX_PLUGIN_V2_CONTENTS = `import { basename, dirname } from "path"; -import { CreateNodes, CreateMetadata, ProjectsMetadata } from "@nx/devkit"; +import { CreateNodesV2, CreateMetadata, ProjectsMetadata } from "@nx/devkit"; type PluginOptions = { inferredTags: string[] @@ -38,32 +38,40 @@ export const createMetadata: CreateMetadata = (graph) => { return metadata; } -export const createNodes: CreateNodes = [ - "**/my-project-file", - (f, options, ctx) => { - // f = path/to/my/file/my-project-file - const root = dirname(f); - // root = path/to/my/file - const name = basename(root); - // name = file +export const createNodesV2: CreateNodesV2 = [ + "**/my-project-file", + (files, options, ctx) => { + const results = []; + for (const f of files) { + // f = path/to/my/file/my-project-file + const root = dirname(f); + // root = path/to/my/file + const name = basename(root); + // name = file - return { - projects: { - [root]: { - root, - name, - targets: { - build: { - executor: "nx:run-commands", - options: { - command: "echo 'custom registered target'", - }, - }, - }, - tags: options.inferredTags + results.push([ + f, + { + projects: { + [root]: { + root, + name, + targets: { + build: { + executor: "nx:run-commands", + options: { + command: "echo 'custom registered target'", + }, }, + }, + tags: options.inferredTags, }, - }; - }, + }, + }, + ]); + } + return results; + }, ]; + `; diff --git a/packages/detox/src/generators/init/init.ts b/packages/detox/src/generators/init/init.ts index 2e4c51d5eb..8d6d4790e8 100644 --- a/packages/detox/src/generators/init/init.ts +++ b/packages/detox/src/generators/init/init.ts @@ -8,8 +8,8 @@ import { runTasksInSerial, Tree, } from '@nx/devkit'; -import { addPluginV1 } from '@nx/devkit/src/utils/add-plugin'; -import { createNodes } from '../../plugins/plugin'; +import { addPlugin } from '@nx/devkit/src/utils/add-plugin'; +import { createNodesV2 } from '../../plugins/plugin'; import { detoxVersion, nxVersion } from '../../utils/versions'; import { Schema } from './schema'; @@ -33,11 +33,11 @@ export async function detoxInitGeneratorInternal(host: Tree, schema: Schema) { } if (schema.addPlugin) { - await addPluginV1( + await addPlugin( host, await createProjectGraphAsync(), '@nx/detox/plugin', - createNodes, + createNodesV2, { buildTargetName: ['build', 'detox:build', 'detox-build'], startTargetName: ['start', 'detox:start', 'detox-start'], diff --git a/packages/expo/src/generators/init/init.ts b/packages/expo/src/generators/init/init.ts index 8cabbcab2b..cb02a09993 100644 --- a/packages/expo/src/generators/init/init.ts +++ b/packages/expo/src/generators/init/init.ts @@ -8,8 +8,8 @@ import { runTasksInSerial, Tree, } from '@nx/devkit'; -import { addPluginV1 } from '@nx/devkit/src/utils/add-plugin'; -import { createNodes } from '../../../plugins/plugin'; +import { addPlugin } from '@nx/devkit/src/utils/add-plugin'; +import { createNodesV2 } from '../../../plugins/plugin'; import { expoCliVersion, expoVersion, @@ -36,11 +36,11 @@ export async function expoInitGeneratorInternal(host: Tree, schema: Schema) { addGitIgnoreEntry(host); if (schema.addPlugin) { - await addPluginV1( + await addPlugin( host, await createProjectGraphAsync(), '@nx/expo/plugin', - createNodes, + createNodesV2, { startTargetName: ['start', 'expo:start', 'expo-start'], buildTargetName: ['build', 'expo:build', 'expo-build'], diff --git a/packages/nuxt/plugin.ts b/packages/nuxt/plugin.ts index e6d6bb3274..4c2e48c303 100644 --- a/packages/nuxt/plugin.ts +++ b/packages/nuxt/plugin.ts @@ -1 +1,5 @@ -export { createNodes, NuxtPluginOptions } from './src/plugins/plugin'; +export { + createNodes, + createNodesV2, + NuxtPluginOptions, +} from './src/plugins/plugin'; diff --git a/packages/nuxt/src/generators/init/init.ts b/packages/nuxt/src/generators/init/init.ts index 0efba3c4c8..c04bad91ba 100644 --- a/packages/nuxt/src/generators/init/init.ts +++ b/packages/nuxt/src/generators/init/init.ts @@ -1,16 +1,16 @@ import { createProjectGraphAsync, GeneratorCallback, Tree } from '@nx/devkit'; -import { addPluginV1 } from '@nx/devkit/src/utils/add-plugin'; +import { addPlugin } from '@nx/devkit/src/utils/add-plugin'; -import { createNodes } from '../../plugins/plugin'; +import { createNodesV2 } from '../../plugins/plugin'; import { InitSchema } from './schema'; import { updateDependencies } from './lib/utils'; export async function nuxtInitGenerator(host: Tree, schema: InitSchema) { - await addPluginV1( + await addPlugin( host, await createProjectGraphAsync(), '@nx/nuxt/plugin', - createNodes, + createNodesV2, { buildTargetName: ['build', 'nuxt:build', 'nuxt-build'], serveTargetName: ['serve', 'nuxt:serve', 'nuxt-serve'], diff --git a/packages/nuxt/src/plugins/plugin.ts b/packages/nuxt/src/plugins/plugin.ts index 6e0012a0c9..ad78788636 100644 --- a/packages/nuxt/src/plugins/plugin.ts +++ b/packages/nuxt/src/plugins/plugin.ts @@ -3,6 +3,8 @@ import { CreateDependencies, CreateNodes, CreateNodesContext, + createNodesFromFiles, + CreateNodesV2, detectPackageManager, getPackageManagerCommand, readJsonFile, @@ -40,11 +42,6 @@ function writeTargetsToCache() { }); } -export const createDependencies: CreateDependencies = () => { - writeTargetsToCache(); - return []; -}; - export interface NuxtPluginOptions { buildTargetName?: string; serveTargetName?: string; @@ -54,6 +51,21 @@ export interface NuxtPluginOptions { watchDepsTargetName?: string; } +export const createNodesV2: CreateNodesV2 = [ + '**/nuxt.config.{js,ts,mjs,mts,cjs,cts}', + async (files, options, context) => { + //TODO(@nrwl/nx-vue-reviewers): This should batch hashing like our other plugins. + const result = await createNodesFromFiles( + createNodes[1], + files, + options, + context + ); + writeTargetsToCache(); + return result; + }, +]; + export const createNodes: CreateNodes = [ '**/nuxt.config.{js,ts,mjs,mts,cjs,cts}', async (configFilePath, options, context) => { diff --git a/packages/nx/src/adapter/angular-json.ts b/packages/nx/src/adapter/angular-json.ts index eab874e82c..bc74035ab9 100644 --- a/packages/nx/src/adapter/angular-json.ts +++ b/packages/nx/src/adapter/angular-json.ts @@ -14,6 +14,17 @@ export const NxAngularJsonPlugin: NxPluginV2 = { projects: readAngularJson(ctx.workspaceRoot), }), ], + createNodesV2: [ + 'angular.json', + (f, _, ctx) => [ + [ + 'angular.json', + { + projects: readAngularJson(ctx.workspaceRoot), + }, + ], + ], + ], }; export default NxAngularJsonPlugin; diff --git a/packages/nx/src/plugins/js/index.ts b/packages/nx/src/plugins/js/index.ts index 7d4cd546d2..0322178c2c 100644 --- a/packages/nx/src/plugins/js/index.ts +++ b/packages/nx/src/plugins/js/index.ts @@ -8,6 +8,8 @@ import { CreateDependencies, CreateDependenciesContext, CreateNodes, + createNodesFromFiles, + CreateNodesV2, } from '../../project-graph/plugins'; import { getLockFileDependencies, @@ -34,6 +36,13 @@ interface ParsedLockFile { let parsedLockFile: ParsedLockFile = {}; +export const createNodesV2: CreateNodesV2 = [ + combineGlobPatterns(LOCKFILES), + (files, _, context) => { + return createNodesFromFiles(createNodes[1], files, _, context); + }, +]; + export const createNodes: CreateNodes = [ // Look for all lockfiles combineGlobPatterns(LOCKFILES), diff --git a/packages/nx/src/project-graph/plugins/loaded-nx-plugin.ts b/packages/nx/src/project-graph/plugins/loaded-nx-plugin.ts index 26d4e7e6db..439acf6f4b 100644 --- a/packages/nx/src/project-graph/plugins/loaded-nx-plugin.ts +++ b/packages/nx/src/project-graph/plugins/loaded-nx-plugin.ts @@ -60,16 +60,9 @@ export class LoadedNxPlugin { } if (plugin.createNodes && !plugin.createNodesV2) { - this.createNodes = [ - plugin.createNodes[0], - (configFiles, context) => - createNodesFromFiles( - plugin.createNodes[1], - configFiles, - this.options, - context - ).then((results) => results.map((r) => [this.name, r[0], r[1]])), - ]; + throw new Error( + `Plugin ${plugin.name} only provides \`createNodes\` which was removed in Nx 21, it should provide a \`createNodesV2\` implementation.` + ); } if (plugin.createNodesV2) { diff --git a/packages/nx/src/project-graph/utils/project-configuration-utils.spec.ts b/packages/nx/src/project-graph/utils/project-configuration-utils.spec.ts index 14f363db3e..3c570574f2 100644 --- a/packages/nx/src/project-graph/utils/project-configuration-utils.spec.ts +++ b/packages/nx/src/project-graph/utils/project-configuration-utils.spec.ts @@ -14,7 +14,7 @@ import { readProjectConfigurationsFromRootMap, readTargetDefaultsForTarget, } from './project-configuration-utils'; -import { NxPluginV2 } from '../plugins'; +import { createNodesFromFiles, NxPluginV2 } from '../plugins'; import { LoadedNxPlugin } from '../plugins/loaded-nx-plugin'; import { dirname } from 'path'; import { isProjectConfigurationsError } from '../error-types'; @@ -1712,63 +1712,81 @@ describe('project-configuration-utils', () => { /* A fake plugin that sets `fake-lib` tag to libs. */ const fakeTagPlugin: NxPluginV2 = { name: 'fake-tag-plugin', - createNodes: [ + createNodesV2: [ 'libs/*/project.json', - (vitestConfigPath) => { - const [_libs, name, _config] = vitestConfigPath.split('/'); - return { - projects: { - [name]: { - name: name, - root: `libs/${name}`, - tags: ['fake-lib'], - }, + (vitestConfigPaths) => + createNodesFromFiles( + (vitestConfigPath) => { + const [_libs, name, _config] = vitestConfigPath.split('/'); + return { + projects: { + [name]: { + name: name, + root: `libs/${name}`, + tags: ['fake-lib'], + }, + }, + }; }, - }; - }, + vitestConfigPaths, + null, + null + ), ], }; const fakeTargetsPlugin: NxPluginV2 = { name: 'fake-targets-plugin', - createNodes: [ + createNodesV2: [ 'libs/*/project.json', - (projectJsonPath) => { - const root = dirname(projectJsonPath); - return { - projects: { - [root]: { - root, - targets: { - build: { - executor: 'nx:run-commands', - options: { - command: 'echo {projectName} @ {projectRoot}', + (projectJsonPaths) => + createNodesFromFiles( + (projectJsonPath) => { + const root = dirname(projectJsonPath); + return { + projects: { + [root]: { + root, + targets: { + build: { + executor: 'nx:run-commands', + options: { + command: 'echo {projectName} @ {projectRoot}', + }, + }, }, }, }, - }, + }; }, - }; - }, + projectJsonPaths, + null, + null + ), ], }; const sameNamePlugin: NxPluginV2 = { name: 'same-name-plugin', - createNodes: [ + createNodesV2: [ 'libs/*/project.json', - (projectJsonPath) => { - const root = dirname(projectJsonPath); - return { - projects: { - [root]: { - root, - name: 'same-name', - }, + (projectJsonPaths) => + createNodesFromFiles( + (projectJsonPath) => { + const root = dirname(projectJsonPath); + return { + projects: { + [root]: { + root, + name: 'same-name', + }, + }, + }; }, - }; - }, + projectJsonPaths, + null, + null + ), ], }; diff --git a/packages/react-native/src/generators/init/init.ts b/packages/react-native/src/generators/init/init.ts index c4719d5bf6..c11a235356 100644 --- a/packages/react-native/src/generators/init/init.ts +++ b/packages/react-native/src/generators/init/init.ts @@ -8,8 +8,8 @@ import { runTasksInSerial, Tree, } from '@nx/devkit'; -import { addPluginV1 } from '@nx/devkit/src/utils/add-plugin'; -import { createNodes } from '../../../plugins/plugin'; +import { addPlugin } from '@nx/devkit/src/utils/add-plugin'; +import { createNodesV2 } from '../../../plugins/plugin'; import { nxVersion, reactDomVersion, @@ -39,11 +39,11 @@ export async function reactNativeInitGeneratorInternal( schema.addPlugin ??= addPluginDefault; if (schema.addPlugin) { - await addPluginV1( + await addPlugin( host, await createProjectGraphAsync(), '@nx/react-native/plugin', - createNodes, + createNodesV2, { startTargetName: ['start', 'react-native:start', 'react-native-start'], upgradeTargetName: [ diff --git a/packages/remix/src/generators/convert-to-inferred/convert-to-inferred.ts b/packages/remix/src/generators/convert-to-inferred/convert-to-inferred.ts index 37e0ed1b82..cb42ddfe4c 100644 --- a/packages/remix/src/generators/convert-to-inferred/convert-to-inferred.ts +++ b/packages/remix/src/generators/convert-to-inferred/convert-to-inferred.ts @@ -1,10 +1,10 @@ import { createProjectGraphAsync, formatFiles, type Tree } from '@nx/devkit'; import { AggregatedLog } from '@nx/devkit/src/generators/plugin-migrations/aggregate-log-util'; import { - migrateProjectExecutorsToPluginV1, + migrateProjectExecutorsToPlugin, NoTargetsToMigrateError, } from '@nx/devkit/src/generators/plugin-migrations/executor-to-plugin-migrator'; -import { createNodes } from '../../plugins/plugin'; +import { createNodesV2 } from '../../plugins/plugin'; import { buildPostTargetTransformer } from './lib/build-post-target-transformer'; import { servePostTargetTransformer } from './lib/serve-post-target-transformer'; @@ -16,11 +16,11 @@ interface Schema { export async function convertToInferred(tree: Tree, options: Schema) { const projectGraph = await createProjectGraphAsync(); const migrationLogs = new AggregatedLog(); - const migratedProjects = await migrateProjectExecutorsToPluginV1( + const migratedProjects = await migrateProjectExecutorsToPlugin( tree, projectGraph, '@nx/remix/plugin', - createNodes, + createNodesV2, { buildTargetName: 'build', devTargetName: 'dev', diff --git a/packages/rollup/src/generators/init/init.ts b/packages/rollup/src/generators/init/init.ts index 2ee2fe49fe..6ed1301bce 100644 --- a/packages/rollup/src/generators/init/init.ts +++ b/packages/rollup/src/generators/init/init.ts @@ -8,8 +8,8 @@ import { } from '@nx/devkit'; import { nxVersion, rollupVersion } from '../../utils/versions'; import { Schema } from './schema'; -import { addPluginV1 } from '@nx/devkit/src/utils/add-plugin'; -import { createNodes } from '../../plugins/plugin'; +import { addPlugin } from '@nx/devkit/src/utils/add-plugin'; +import { createNodesV2 } from '../../plugins/plugin'; export async function rollupInitGenerator(tree: Tree, schema: Schema) { let task: GeneratorCallback = () => {}; @@ -34,11 +34,11 @@ export async function rollupInitGenerator(tree: Tree, schema: Schema) { } if (schema.addPlugin) { - await addPluginV1( + await addPlugin( tree, await createProjectGraphAsync(), '@nx/rollup/plugin', - createNodes, + createNodesV2, { buildTargetName: ['build', 'rollup:build', 'rollup-build'], buildDepsTargetName: [ diff --git a/packages/webpack/src/generators/init/init.ts b/packages/webpack/src/generators/init/init.ts index fb3a32b12e..a811c51a21 100644 --- a/packages/webpack/src/generators/init/init.ts +++ b/packages/webpack/src/generators/init/init.ts @@ -6,8 +6,8 @@ import { readNxJson, Tree, } from '@nx/devkit'; -import { addPluginV1 } from '@nx/devkit/src/utils/add-plugin'; -import { createNodes } from '../../plugins/plugin'; +import { addPlugin } from '@nx/devkit/src/utils/add-plugin'; +import { createNodesV2 } from '../../plugins/plugin'; import { nxVersion, webpackCliVersion } from '../../utils/versions'; import { Schema } from './schema'; @@ -23,11 +23,11 @@ export async function webpackInitGeneratorInternal(tree: Tree, schema: Schema) { schema.addPlugin ??= addPluginDefault; if (schema.addPlugin) { - await addPluginV1( + await addPlugin( tree, await createProjectGraphAsync(), '@nx/webpack/plugin', - createNodes, + createNodesV2, { buildTargetName: [ 'build',