Skip to content

Commit

Permalink
fix(web): application generator should add e2e-ci target defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 committed Aug 2, 2024
1 parent 7f1789a commit 7c787d4
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 8 deletions.
8 changes: 8 additions & 0 deletions packages/web/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ describe('app', () => {
expect(readProjectConfiguration(tree, 'my-app-e2e').root).toEqual(
'my-app-e2e'
);
expect(readNxJson(tree).targetDefaults['e2e-ci--**/*'])
.toMatchInlineSnapshot(`
{
"dependsOn": [
"^build",
],
}
`);
}, 60_000);

it('should update tags and implicit dependencies', async () => {
Expand Down
90 changes: 82 additions & 8 deletions packages/web/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
joinPathFragments,
names,
offsetFromRoot,
type PluginConfiguration,
readNxJson,
readProjectConfiguration,
runTasksInSerial,
Expand Down Expand Up @@ -37,12 +38,15 @@ import { webInitGenerator } from '../init/init';
import { Schema } from './schema';
import { getNpmScope } from '@nx/js/src/utils/package-json/get-npm-scope';
import { hasWebpackPlugin } from '../../utils/has-webpack-plugin';
import { addBuildTargetDefaults } from '@nx/devkit/src/generators/target-defaults-utils';
import {
addBuildTargetDefaults,
addE2eCiTargetDefaults,
} from '@nx/devkit/src/generators/target-defaults-utils';
import { logShowProjectCommand } from '@nx/devkit/src/utils/log-show-project-command';
import { VitePluginOptions } from '@nx/vite/src/plugins/plugin';
import { WebpackPluginOptions } from '@nx/webpack/src/plugins/plugin';
import { hasVitePlugin } from '../../utils/has-vite-plugin';
import staticServeConfiguration from '../static-serve/static-serve-configuration';
import { findPluginForConfigFile } from '@nx/devkit/src/utils/find-plugin-for-config-file';

interface NormalizedSchema extends Schema {
projectName: string;
Expand Down Expand Up @@ -368,10 +372,20 @@ export async function applicationGeneratorInternal(host: Tree, schema: Schema) {
tasks.push(lintTask);
}

const hasNxBuildPlugin =
(options.bundler === 'webpack' && hasWebpackPlugin(host)) ||
(options.bundler === 'vite' && hasVitePlugin(host));
if (!hasNxBuildPlugin) {
const nxJson = readNxJson(host);
let hasPlugin: PluginConfiguration | undefined;
let buildPlugin: string;
let buildConfigFile: string;
if (options.bundler === 'webpack' || options.bundler === 'vite') {
buildPlugin = `@nx/${options.bundler}/plugin`;
buildConfigFile =
options.bundler === 'webpack' ? 'webpack.config.js' : `vite.config.ts`;
hasPlugin = nxJson.plugins?.find((p) =>
typeof p === 'string' ? p === buildPlugin : p.plugin === buildPlugin
);
}

if (!hasPlugin) {
await staticServeConfiguration(host, {
buildTarget: `${options.projectName}:build`,
spa: true,
Expand All @@ -396,17 +410,47 @@ export async function applicationGeneratorInternal(host: Tree, schema: Schema) {
baseUrl: options.e2eWebServerAddress,
directory: 'src',
skipFormat: true,
webServerCommands: hasNxBuildPlugin
webServerCommands: hasPlugin
? {
default: `nx run ${options.projectName}:${options.e2eWebServerTarget}`,
production: `nx run ${options.projectName}:preview`,
}
: undefined,
ciWebServerCommand: hasNxBuildPlugin
ciWebServerCommand: hasPlugin
? `nx run ${options.projectName}:${options.e2eCiWebServerTarget}`
: undefined,
ciBaseUrl: options.bundler === 'vite' ? options.e2eCiBaseUrl : undefined,
});

if (
options.addPlugin ||
readNxJson(host).plugins?.find((p) =>
typeof p === 'string'
? p === '@nx/cypress/plugin'
: p.plugin === '@nx/cypress/plugin'
)
) {
let buildTarget = '^build';
if (hasPlugin) {
const matchingPlugin = await findPluginForConfigFile(
host,
buildPlugin,
joinPathFragments(options.appProjectRoot, buildConfigFile)
);
if (matchingPlugin && typeof matchingPlugin !== 'string') {
buildTarget = `^${
(matchingPlugin.options as any)?.buildTargetName ?? 'build'
}`;
}
}
await addE2eCiTargetDefaults(
host,
'@nx/cypress/plugin',
buildTarget,
joinPathFragments(options.e2eProjectRoot, `cypress.config.ts`)
);
}

tasks.push(cypressTask);
} else if (options.e2eTestRunner === 'playwright') {
const { configurationGenerator: playwrightConfigGenerator } = ensurePackage<
Expand Down Expand Up @@ -434,6 +478,36 @@ export async function applicationGeneratorInternal(host: Tree, schema: Schema) {
webServerAddress: options.e2eCiBaseUrl,
addPlugin: options.addPlugin,
});

if (
options.addPlugin ||
readNxJson(host).plugins?.find((p) =>
typeof p === 'string'
? p === '@nx/playwright/plugin'
: p.plugin === '@nx/playwright/plugin'
)
) {
let buildTarget = '^build';
if (hasPlugin) {
const matchingPlugin = await findPluginForConfigFile(
host,
buildPlugin,
joinPathFragments(options.appProjectRoot, buildConfigFile)
);
if (matchingPlugin && typeof matchingPlugin !== 'string') {
buildTarget = `^${
(matchingPlugin.options as any)?.buildTargetName ?? 'build'
}`;
}
}
await addE2eCiTargetDefaults(
host,
'@nx/playwright/plugin',
buildTarget,
joinPathFragments(options.e2eProjectRoot, `playwright.config.ts`)
);
}

tasks.push(playwrightTask);
}
if (options.unitTestRunner === 'jest') {
Expand Down

0 comments on commit 7c787d4

Please sign in to comment.