nx/packages/next/plugins/component-testing.ts
Jason Jean 23bebd91e7
feat(devkit): bump compatibility to Nx 19 - 21.x (#28243)
BREAKING CHANGE

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

* `@nx/devkit` supports Nx 17 - 20.
* Node 18 - 22 is supported 
* `ExecutorContext.projectGraph`, `ExecutorContext.nxJsonConfiguration`,
and `ExecutorContext.projectsConfigurations` is marked as optional
because `ExecutorContext` in some versions of Nx did not have them.
* `ExecutorContext.workspace` is marked as optional because
`ExecutorContext` in some versions of Nx did not have the above
properties which contain the same information.
* `ProjectGraphNode` is deprecated.
* `NxPluginV1.processProjectGraph` was deprecated long ago and there has
been a warning since.
* `appRootPath` has been deprecated for a long time.
* `parseTargetString` had a variant that did not take either the project
graph or the executor context.
* `readNxJson` has a variant which does not take a tree. This was not
clearly deprecated.
* There are handlers to require from `@nx/` instead of `@nrwl`
* Nx tries to get a root install from `@nrwl/cli`


## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

* `@nx/devkit` supports Nx 19 - 21.
* Node 20 - 22 is supported 
* `ExecutorContext.projectGraph`, `ExecutorContext.nxJsonConfiguration`,
and `ExecutorContext.projectsConfigurations` is marked as required
because `ExecutorContext` in Nx 19+ is guaranteed to have them.
* `ExecutorContext.workspace` is removed because the same information is
available in the above properties
* `ProjectGraphNode` is removed.
* `NxPluginV1` is no more. All plugins should be `NxPluginV2`.
* `workspaceRoot` is the replacement for `appRootPath`. `appRootPath` is
removed.
* `parseTargetString` no longer has a variant that did not take either
the project graph or the executor context.
* `readNxJson` still has a variant which does not take a tree but it's
clearly deprecated to be removed in Nx 21.
* `@nrwl` packages are no more so we don't have to redirect requires
anymore.
* `@nrwl/cli` is no more so Nx shouldn't try to get a root install there
* 

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
2024-10-03 17:35:47 -04:00

167 lines
4.8 KiB
TypeScript

import {
createExecutorContext,
getProjectConfigByPath,
} from '@nx/cypress/src/utils/ct-helpers';
import {
nxBaseCypressPreset,
NxComponentTestingOptions,
} from '@nx/cypress/plugins/cypress-preset';
import {
ExecutorContext,
parseTargetString,
readCachedProjectGraph,
readProjectsConfigurationFromProjectGraph,
readTargetOptions,
stripIndents,
workspaceRoot,
} from '@nx/devkit';
import { withReact } from '@nx/react';
import {
AssetGlobPattern,
composePluginsSync,
NormalizedWebpackExecutorOptions,
withNx,
} from '@nx/webpack';
import { join } from 'path';
import { NextBuildBuilderOptions } from '../src/utils/types';
import { CypressExecutorOptions } from '@nx/cypress/src/executors/cypress/cypress.impl';
import { readNxJson } from 'nx/src/config/configuration';
export function nxComponentTestingPreset(
pathToConfig: string,
options?: NxComponentTestingOptions
) {
if (global.NX_GRAPH_CREATION) {
// this is only used by plugins, so we don't need the component testing
// options, cast to any to avoid type errors
return nxBaseCypressPreset(pathToConfig) as any;
}
const graph = readCachedProjectGraph();
const { targets: ctTargets, name: ctProjectName } = getProjectConfigByPath(
graph,
pathToConfig
);
const ctTargetName = options?.ctTargetName || 'component-test';
const ctConfigurationName = process.env.NX_CYPRESS_TARGET_CONFIGURATION;
const ctExecutorContext: ExecutorContext = createExecutorContext(
graph,
ctTargets,
ctProjectName,
ctTargetName,
ctConfigurationName
);
let buildTarget: string = options?.buildTarget;
if (!buildTarget) {
const ctExecutorOptions = readTargetOptions<CypressExecutorOptions>(
{
project: ctProjectName,
target: ctTargetName,
configuration: ctConfigurationName,
},
ctExecutorContext
);
buildTarget = ctExecutorOptions.devServerTarget;
}
let buildAssets: AssetGlobPattern[] = [];
let buildFileReplacements = [];
let buildOuputPath = `dist/${ctProjectName}/.next`;
if (buildTarget) {
const parsedBuildTarget = parseTargetString(buildTarget, {
cwd: process.cwd(),
root: workspaceRoot,
projectsConfigurations: readProjectsConfigurationFromProjectGraph(graph),
nxJsonConfiguration: readNxJson(workspaceRoot),
isVerbose: false,
projectName: ctProjectName,
projectGraph: graph,
});
const buildProjectConfig = graph.nodes[parsedBuildTarget.project]?.data;
if (
buildProjectConfig?.targets?.[parsedBuildTarget.target]?.executor !==
'@nx/next:build'
) {
throw new Error(
`The '${parsedBuildTarget.target}' target of the '${[
parsedBuildTarget.project,
]}' project is not using the '@nx/next:build' executor. ` +
`Please make sure to use '@nx/next:build' executor in that target to use Cypress Component Testing.`
);
}
const buildExecutorContext = createExecutorContext(
graph,
buildProjectConfig.targets,
parsedBuildTarget.project,
parsedBuildTarget.target,
parsedBuildTarget.configuration
);
const buildExecutorOptions = readTargetOptions<NextBuildBuilderOptions>(
{
project: parsedBuildTarget.project,
target: parsedBuildTarget.target,
configuration: parsedBuildTarget.configuration,
},
buildExecutorContext
);
buildAssets ??= buildExecutorOptions.assets;
buildFileReplacements ??= buildExecutorOptions.fileReplacements;
buildOuputPath ??= buildExecutorOptions.outputPath;
}
const ctProjectConfig = graph.nodes[ctProjectName]?.data;
if (!ctProjectConfig) {
throw new Error(stripIndents`Unable to load project configs from the project graph.
Provided build target, ${buildTarget}.
Able to find CT project, ${!!ctProjectConfig}.`);
}
const webpackOptions: NormalizedWebpackExecutorOptions = {
root: ctExecutorContext.root,
projectRoot: ctProjectConfig.root,
sourceRoot: ctProjectConfig.sourceRoot,
main: '',
fileReplacements: buildFileReplacements,
assets: buildAssets,
outputPath: buildOuputPath,
outputFileName: 'main.js',
compiler: options?.compiler || 'swc',
tsConfig: join(
ctExecutorContext.root,
ctProjectConfig.root,
'tsconfig.json'
),
};
const configure = composePluginsSync(
withNx({
target: 'web',
styles: [],
scripts: [],
postcssConfig: ctProjectConfig.root,
}),
withReact({})
);
const webpackConfig = configure(
{},
{
options: webpackOptions,
context: ctExecutorContext,
}
);
return {
...nxBaseCypressPreset(pathToConfig),
specPattern: '**/*.cy.{js,jsx,ts,tsx}',
devServer: {
...({ framework: 'react', bundler: 'webpack' } as const),
webpackConfig,
},
};
}