Skip to content

Commit

Permalink
chore(nextjs): pass full executor context where available to parseTar…
Browse files Browse the repository at this point in the history
…getOptions (#19138)
  • Loading branch information
AgentEnder authored Sep 20, 2023
1 parent a18dd48 commit 8530b45
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
9 changes: 8 additions & 1 deletion packages/next/plugins/component-testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
readCachedProjectGraph,
readTargetOptions,
stripIndents,
workspaceRoot,
} from '@nx/devkit';
import { withReact } from '@nx/react';
import {
Expand Down Expand Up @@ -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,
Expand Down
21 changes: 17 additions & 4 deletions packages/next/plugins/with-nx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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;
Expand All @@ -94,17 +99,25 @@ function getNxContext(
);
}

const partialExecutorContext: Partial<ExecutorContext> = {
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)
);
}

Expand Down
5 changes: 1 addition & 4 deletions packages/next/src/executors/export/export.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/executors/server/custom-server.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down

0 comments on commit 8530b45

Please sign in to comment.