From 8530b4598816cdc7362730616fc492e934e79999 Mon Sep 17 00:00:00 2001 From: Craigory Coppola Date: Wed, 20 Sep 2023 09:07:34 -0400 Subject: [PATCH] chore(nextjs): pass full executor context where available to parseTargetOptions (#19138) --- packages/next/plugins/component-testing.ts | 9 +++++++- packages/next/plugins/with-nx.ts | 21 +++++++++++++++---- .../next/src/executors/export/export.impl.ts | 5 +---- .../executors/server/custom-server.impl.ts | 2 +- 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/packages/next/plugins/component-testing.ts b/packages/next/plugins/component-testing.ts index aa5284f08f4fe..2280ef836da36 100644 --- a/packages/next/plugins/component-testing.ts +++ b/packages/next/plugins/component-testing.ts @@ -12,6 +12,7 @@ import { readCachedProjectGraph, readTargetOptions, stripIndents, + workspaceRoot, } from '@nx/devkit'; import { withReact } from '@nx/react'; import { @@ -57,7 +58,13 @@ export function nxComponentTestingPreset( let buildFileReplacements = []; let buildOuputPath = `dist/${ctProjectName}/.next`; if (buildTarget) { - const parsedBuildTarget = parseTargetString(buildTarget, graph); + const parsedBuildTarget = parseTargetString(buildTarget, { + cwd: process.cwd(), + root: workspaceRoot, + isVerbose: false, + projectName: ctProjectName, + projectGraph: graph, + }); const buildProjectConfig = graph.nodes[parsedBuildTarget.project]?.data; const buildExecutorContext = createExecutorContext( graph, diff --git a/packages/next/plugins/with-nx.ts b/packages/next/plugins/with-nx.ts index e445aac268a9a..f6455c05347c3 100644 --- a/packages/next/plugins/with-nx.ts +++ b/packages/next/plugins/with-nx.ts @@ -6,7 +6,12 @@ import type { NextConfig } from 'next'; import type { NextConfigFn } from '../src/utils/config'; import type { NextBuildBuilderOptions } from '../src/utils/types'; import type { DependentBuildableProjectNode } from '@nx/js/src/utils/buildable-libs-utils'; -import type { ProjectGraph, ProjectGraphProjectNode, Target } from '@nx/devkit'; +import type { + ExecutorContext, + ProjectGraph, + ProjectGraphProjectNode, + Target, +} from '@nx/devkit'; const baseNXEnvironmentVariables = [ 'NX_BASE', @@ -83,7 +88,7 @@ function getNxContext( targetName: string; configurationName?: string; } { - const { parseTargetString } = require('@nx/devkit'); + const { parseTargetString, workspaceRoot } = require('@nx/devkit'); const projectNode = graph.nodes[target.project]; const targetConfig = projectNode.data.targets[target.target]; const targetOptions = targetConfig.options; @@ -94,17 +99,25 @@ function getNxContext( ); } + const partialExecutorContext: Partial = { + projectName: target.project, + targetName: target.target, + projectGraph: graph, + configurationName: target.configuration, + root: workspaceRoot, + }; + if (targetOptions.devServerTarget) { // Executors such as @nx/cypress:cypress define the devServerTarget option. return getNxContext( graph, - parseTargetString(targetOptions.devServerTarget, graph) + parseTargetString(targetOptions.devServerTarget, partialExecutorContext) ); } else if (targetOptions.buildTarget) { // Executors such as @nx/next:server or @nx/next:export define the buildTarget option. return getNxContext( graph, - parseTargetString(targetOptions.buildTarget, graph) + parseTargetString(targetOptions.buildTarget, partialExecutorContext) ); } diff --git a/packages/next/src/executors/export/export.impl.ts b/packages/next/src/executors/export/export.impl.ts index 417fabeb59f1d..5892555c77580 100644 --- a/packages/next/src/executors/export/export.impl.ts +++ b/packages/next/src/executors/export/export.impl.ts @@ -53,10 +53,7 @@ export default async function exportExecutor( dependencies = result.dependencies; } - const buildTarget = parseTargetString( - options.buildTarget, - context.projectGraph - ); + const buildTarget = parseTargetString(options.buildTarget, context); try { const args = getBuildTargetCommand(options); diff --git a/packages/next/src/executors/server/custom-server.impl.ts b/packages/next/src/executors/server/custom-server.impl.ts index 0c7f30794ea0f..e6ec5f795a94f 100644 --- a/packages/next/src/executors/server/custom-server.impl.ts +++ b/packages/next/src/executors/server/custom-server.impl.ts @@ -33,7 +33,7 @@ async function* runCustomServer( const baseUrl = `http://${options.hostname || 'localhost'}:${options.port}`; const customServerBuild = await runExecutor( - parseTargetString(options.customServerTarget, context.projectGraph), + parseTargetString(options.customServerTarget, context), { watch: options.dev ? true : false, },