feat(core)!: drop support for create nodes v1 in favor of only calling create nodes v2 (#30616)

This commit is contained in:
Craigory Coppola 2025-04-28 15:47:44 -04:00 committed by GitHub
parent bb8c727681
commit eb54b1d249
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 163 additions and 108 deletions

View File

@ -20,7 +20,7 @@ export default async function* execute(
`; `;
export const NX_PLUGIN_V2_CONTENTS = `import { basename, dirname } from "path"; 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 = { type PluginOptions = {
inferredTags: string[] inferredTags: string[]
@ -38,32 +38,40 @@ export const createMetadata: CreateMetadata = (graph) => {
return metadata; return metadata;
} }
export const createNodes: CreateNodes<PluginOptions> = [ export const createNodesV2: CreateNodesV2<PluginOptions> = [
"**/my-project-file", "**/my-project-file",
(f, options, ctx) => { (files, options, ctx) => {
// f = path/to/my/file/my-project-file const results = [];
const root = dirname(f); for (const f of files) {
// root = path/to/my/file // f = path/to/my/file/my-project-file
const name = basename(root); const root = dirname(f);
// name = file // root = path/to/my/file
const name = basename(root);
// name = file
return { results.push([
projects: { f,
[root]: { {
root, projects: {
name, [root]: {
targets: { root,
build: { name,
executor: "nx:run-commands", targets: {
options: { build: {
command: "echo 'custom registered target'", executor: "nx:run-commands",
}, options: {
}, command: "echo 'custom registered target'",
}, },
tags: options.inferredTags
}, },
},
tags: options.inferredTags,
}, },
}; },
}, },
]);
}
return results;
},
]; ];
`; `;

View File

@ -8,8 +8,8 @@ import {
runTasksInSerial, runTasksInSerial,
Tree, Tree,
} from '@nx/devkit'; } 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 { detoxVersion, nxVersion } from '../../utils/versions'; import { detoxVersion, nxVersion } from '../../utils/versions';
import { Schema } from './schema'; import { Schema } from './schema';
@ -33,11 +33,11 @@ export async function detoxInitGeneratorInternal(host: Tree, schema: Schema) {
} }
if (schema.addPlugin) { if (schema.addPlugin) {
await addPluginV1( await addPlugin(
host, host,
await createProjectGraphAsync(), await createProjectGraphAsync(),
'@nx/detox/plugin', '@nx/detox/plugin',
createNodes, createNodesV2,
{ {
buildTargetName: ['build', 'detox:build', 'detox-build'], buildTargetName: ['build', 'detox:build', 'detox-build'],
startTargetName: ['start', 'detox:start', 'detox-start'], startTargetName: ['start', 'detox:start', 'detox-start'],

View File

@ -8,8 +8,8 @@ import {
runTasksInSerial, runTasksInSerial,
Tree, Tree,
} from '@nx/devkit'; } 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 { import {
expoCliVersion, expoCliVersion,
expoVersion, expoVersion,
@ -36,11 +36,11 @@ export async function expoInitGeneratorInternal(host: Tree, schema: Schema) {
addGitIgnoreEntry(host); addGitIgnoreEntry(host);
if (schema.addPlugin) { if (schema.addPlugin) {
await addPluginV1( await addPlugin(
host, host,
await createProjectGraphAsync(), await createProjectGraphAsync(),
'@nx/expo/plugin', '@nx/expo/plugin',
createNodes, createNodesV2,
{ {
startTargetName: ['start', 'expo:start', 'expo-start'], startTargetName: ['start', 'expo:start', 'expo-start'],
buildTargetName: ['build', 'expo:build', 'expo-build'], buildTargetName: ['build', 'expo:build', 'expo-build'],

View File

@ -1 +1,5 @@
export { createNodes, NuxtPluginOptions } from './src/plugins/plugin'; export {
createNodes,
createNodesV2,
NuxtPluginOptions,
} from './src/plugins/plugin';

View File

@ -1,16 +1,16 @@
import { createProjectGraphAsync, GeneratorCallback, Tree } from '@nx/devkit'; 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 { InitSchema } from './schema';
import { updateDependencies } from './lib/utils'; import { updateDependencies } from './lib/utils';
export async function nuxtInitGenerator(host: Tree, schema: InitSchema) { export async function nuxtInitGenerator(host: Tree, schema: InitSchema) {
await addPluginV1( await addPlugin(
host, host,
await createProjectGraphAsync(), await createProjectGraphAsync(),
'@nx/nuxt/plugin', '@nx/nuxt/plugin',
createNodes, createNodesV2,
{ {
buildTargetName: ['build', 'nuxt:build', 'nuxt-build'], buildTargetName: ['build', 'nuxt:build', 'nuxt-build'],
serveTargetName: ['serve', 'nuxt:serve', 'nuxt-serve'], serveTargetName: ['serve', 'nuxt:serve', 'nuxt-serve'],

View File

@ -3,6 +3,8 @@ import {
CreateDependencies, CreateDependencies,
CreateNodes, CreateNodes,
CreateNodesContext, CreateNodesContext,
createNodesFromFiles,
CreateNodesV2,
detectPackageManager, detectPackageManager,
getPackageManagerCommand, getPackageManagerCommand,
readJsonFile, readJsonFile,
@ -40,11 +42,6 @@ function writeTargetsToCache() {
}); });
} }
export const createDependencies: CreateDependencies = () => {
writeTargetsToCache();
return [];
};
export interface NuxtPluginOptions { export interface NuxtPluginOptions {
buildTargetName?: string; buildTargetName?: string;
serveTargetName?: string; serveTargetName?: string;
@ -54,6 +51,21 @@ export interface NuxtPluginOptions {
watchDepsTargetName?: string; watchDepsTargetName?: string;
} }
export const createNodesV2: CreateNodesV2<NuxtPluginOptions> = [
'**/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<NuxtPluginOptions> = [ export const createNodes: CreateNodes<NuxtPluginOptions> = [
'**/nuxt.config.{js,ts,mjs,mts,cjs,cts}', '**/nuxt.config.{js,ts,mjs,mts,cjs,cts}',
async (configFilePath, options, context) => { async (configFilePath, options, context) => {

View File

@ -14,6 +14,17 @@ export const NxAngularJsonPlugin: NxPluginV2 = {
projects: readAngularJson(ctx.workspaceRoot), projects: readAngularJson(ctx.workspaceRoot),
}), }),
], ],
createNodesV2: [
'angular.json',
(f, _, ctx) => [
[
'angular.json',
{
projects: readAngularJson(ctx.workspaceRoot),
},
],
],
],
}; };
export default NxAngularJsonPlugin; export default NxAngularJsonPlugin;

View File

@ -8,6 +8,8 @@ import {
CreateDependencies, CreateDependencies,
CreateDependenciesContext, CreateDependenciesContext,
CreateNodes, CreateNodes,
createNodesFromFiles,
CreateNodesV2,
} from '../../project-graph/plugins'; } from '../../project-graph/plugins';
import { import {
getLockFileDependencies, getLockFileDependencies,
@ -34,6 +36,13 @@ interface ParsedLockFile {
let parsedLockFile: ParsedLockFile = {}; let parsedLockFile: ParsedLockFile = {};
export const createNodesV2: CreateNodesV2 = [
combineGlobPatterns(LOCKFILES),
(files, _, context) => {
return createNodesFromFiles(createNodes[1], files, _, context);
},
];
export const createNodes: CreateNodes = [ export const createNodes: CreateNodes = [
// Look for all lockfiles // Look for all lockfiles
combineGlobPatterns(LOCKFILES), combineGlobPatterns(LOCKFILES),

View File

@ -60,16 +60,9 @@ export class LoadedNxPlugin {
} }
if (plugin.createNodes && !plugin.createNodesV2) { if (plugin.createNodes && !plugin.createNodesV2) {
this.createNodes = [ throw new Error(
plugin.createNodes[0], `Plugin ${plugin.name} only provides \`createNodes\` which was removed in Nx 21, it should provide a \`createNodesV2\` implementation.`
(configFiles, context) => );
createNodesFromFiles(
plugin.createNodes[1],
configFiles,
this.options,
context
).then((results) => results.map((r) => [this.name, r[0], r[1]])),
];
} }
if (plugin.createNodesV2) { if (plugin.createNodesV2) {

View File

@ -14,7 +14,7 @@ import {
readProjectConfigurationsFromRootMap, readProjectConfigurationsFromRootMap,
readTargetDefaultsForTarget, readTargetDefaultsForTarget,
} from './project-configuration-utils'; } from './project-configuration-utils';
import { NxPluginV2 } from '../plugins'; import { createNodesFromFiles, NxPluginV2 } from '../plugins';
import { LoadedNxPlugin } from '../plugins/loaded-nx-plugin'; import { LoadedNxPlugin } from '../plugins/loaded-nx-plugin';
import { dirname } from 'path'; import { dirname } from 'path';
import { isProjectConfigurationsError } from '../error-types'; import { isProjectConfigurationsError } from '../error-types';
@ -1712,63 +1712,81 @@ describe('project-configuration-utils', () => {
/* A fake plugin that sets `fake-lib` tag to libs. */ /* A fake plugin that sets `fake-lib` tag to libs. */
const fakeTagPlugin: NxPluginV2 = { const fakeTagPlugin: NxPluginV2 = {
name: 'fake-tag-plugin', name: 'fake-tag-plugin',
createNodes: [ createNodesV2: [
'libs/*/project.json', 'libs/*/project.json',
(vitestConfigPath) => { (vitestConfigPaths) =>
const [_libs, name, _config] = vitestConfigPath.split('/'); createNodesFromFiles(
return { (vitestConfigPath) => {
projects: { const [_libs, name, _config] = vitestConfigPath.split('/');
[name]: { return {
name: name, projects: {
root: `libs/${name}`, [name]: {
tags: ['fake-lib'], name: name,
}, root: `libs/${name}`,
tags: ['fake-lib'],
},
},
};
}, },
}; vitestConfigPaths,
}, null,
null
),
], ],
}; };
const fakeTargetsPlugin: NxPluginV2 = { const fakeTargetsPlugin: NxPluginV2 = {
name: 'fake-targets-plugin', name: 'fake-targets-plugin',
createNodes: [ createNodesV2: [
'libs/*/project.json', 'libs/*/project.json',
(projectJsonPath) => { (projectJsonPaths) =>
const root = dirname(projectJsonPath); createNodesFromFiles(
return { (projectJsonPath) => {
projects: { const root = dirname(projectJsonPath);
[root]: { return {
root, projects: {
targets: { [root]: {
build: { root,
executor: 'nx:run-commands', targets: {
options: { build: {
command: 'echo {projectName} @ {projectRoot}', executor: 'nx:run-commands',
options: {
command: 'echo {projectName} @ {projectRoot}',
},
},
}, },
}, },
}, },
}, };
}, },
}; projectJsonPaths,
}, null,
null
),
], ],
}; };
const sameNamePlugin: NxPluginV2 = { const sameNamePlugin: NxPluginV2 = {
name: 'same-name-plugin', name: 'same-name-plugin',
createNodes: [ createNodesV2: [
'libs/*/project.json', 'libs/*/project.json',
(projectJsonPath) => { (projectJsonPaths) =>
const root = dirname(projectJsonPath); createNodesFromFiles(
return { (projectJsonPath) => {
projects: { const root = dirname(projectJsonPath);
[root]: { return {
root, projects: {
name: 'same-name', [root]: {
}, root,
name: 'same-name',
},
},
};
}, },
}; projectJsonPaths,
}, null,
null
),
], ],
}; };

View File

@ -8,8 +8,8 @@ import {
runTasksInSerial, runTasksInSerial,
Tree, Tree,
} from '@nx/devkit'; } 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 { import {
nxVersion, nxVersion,
reactDomVersion, reactDomVersion,
@ -39,11 +39,11 @@ export async function reactNativeInitGeneratorInternal(
schema.addPlugin ??= addPluginDefault; schema.addPlugin ??= addPluginDefault;
if (schema.addPlugin) { if (schema.addPlugin) {
await addPluginV1( await addPlugin(
host, host,
await createProjectGraphAsync(), await createProjectGraphAsync(),
'@nx/react-native/plugin', '@nx/react-native/plugin',
createNodes, createNodesV2,
{ {
startTargetName: ['start', 'react-native:start', 'react-native-start'], startTargetName: ['start', 'react-native:start', 'react-native-start'],
upgradeTargetName: [ upgradeTargetName: [

View File

@ -1,10 +1,10 @@
import { createProjectGraphAsync, formatFiles, type Tree } from '@nx/devkit'; import { createProjectGraphAsync, formatFiles, type Tree } from '@nx/devkit';
import { AggregatedLog } from '@nx/devkit/src/generators/plugin-migrations/aggregate-log-util'; import { AggregatedLog } from '@nx/devkit/src/generators/plugin-migrations/aggregate-log-util';
import { import {
migrateProjectExecutorsToPluginV1, migrateProjectExecutorsToPlugin,
NoTargetsToMigrateError, NoTargetsToMigrateError,
} from '@nx/devkit/src/generators/plugin-migrations/executor-to-plugin-migrator'; } 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 { buildPostTargetTransformer } from './lib/build-post-target-transformer';
import { servePostTargetTransformer } from './lib/serve-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) { export async function convertToInferred(tree: Tree, options: Schema) {
const projectGraph = await createProjectGraphAsync(); const projectGraph = await createProjectGraphAsync();
const migrationLogs = new AggregatedLog(); const migrationLogs = new AggregatedLog();
const migratedProjects = await migrateProjectExecutorsToPluginV1( const migratedProjects = await migrateProjectExecutorsToPlugin(
tree, tree,
projectGraph, projectGraph,
'@nx/remix/plugin', '@nx/remix/plugin',
createNodes, createNodesV2,
{ {
buildTargetName: 'build', buildTargetName: 'build',
devTargetName: 'dev', devTargetName: 'dev',

View File

@ -8,8 +8,8 @@ import {
} from '@nx/devkit'; } from '@nx/devkit';
import { nxVersion, rollupVersion } from '../../utils/versions'; import { nxVersion, rollupVersion } from '../../utils/versions';
import { Schema } from './schema'; import { Schema } from './schema';
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';
export async function rollupInitGenerator(tree: Tree, schema: Schema) { export async function rollupInitGenerator(tree: Tree, schema: Schema) {
let task: GeneratorCallback = () => {}; let task: GeneratorCallback = () => {};
@ -34,11 +34,11 @@ export async function rollupInitGenerator(tree: Tree, schema: Schema) {
} }
if (schema.addPlugin) { if (schema.addPlugin) {
await addPluginV1( await addPlugin(
tree, tree,
await createProjectGraphAsync(), await createProjectGraphAsync(),
'@nx/rollup/plugin', '@nx/rollup/plugin',
createNodes, createNodesV2,
{ {
buildTargetName: ['build', 'rollup:build', 'rollup-build'], buildTargetName: ['build', 'rollup:build', 'rollup-build'],
buildDepsTargetName: [ buildDepsTargetName: [

View File

@ -6,8 +6,8 @@ import {
readNxJson, readNxJson,
Tree, Tree,
} from '@nx/devkit'; } 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 { nxVersion, webpackCliVersion } from '../../utils/versions'; import { nxVersion, webpackCliVersion } from '../../utils/versions';
import { Schema } from './schema'; import { Schema } from './schema';
@ -23,11 +23,11 @@ export async function webpackInitGeneratorInternal(tree: Tree, schema: Schema) {
schema.addPlugin ??= addPluginDefault; schema.addPlugin ??= addPluginDefault;
if (schema.addPlugin) { if (schema.addPlugin) {
await addPluginV1( await addPlugin(
tree, tree,
await createProjectGraphAsync(), await createProjectGraphAsync(),
'@nx/webpack/plugin', '@nx/webpack/plugin',
createNodes, createNodesV2,
{ {
buildTargetName: [ buildTargetName: [
'build', 'build',