Skip to content

Commit

Permalink
feat(core): targets inferred from plugins override targetDefaults (nr…
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo authored Dec 6, 2023
1 parent 199cf84 commit ab8c929
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 206 deletions.
7 changes: 7 additions & 0 deletions packages/cypress/src/plugins/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ describe('@nx/cypress/plugin', () => {
});
context = {
nxJsonConfiguration: {
// These defaults should be overridden by plugin
targetDefaults: {
e2e: {
cache: false,
inputs: ['foo', '^foo'],
},
},
namedInputs: {
default: ['{projectRoot}/**/*'],
production: ['!{projectRoot}/**/*.spec.ts'],
Expand Down
62 changes: 10 additions & 52 deletions packages/cypress/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { registerTsProject } from '@nx/js/src/internal';
import { getLockFileName, getRootTsConfigPath } from '@nx/js';

import { CypressExecutorOptions } from '../executors/cypress/cypress.impl';
import { readTargetDefaultsForTarget } from 'nx/src/project-graph/utils/project-configuration-utils';
import { getNamedInputs } from '@nx/devkit/src/utils/get-named-inputs';
import { existsSync, readdirSync } from 'fs';
import { globWithWorkspaceContext } from 'nx/src/utils/workspace-context';
Expand Down Expand Up @@ -139,6 +138,7 @@ function getOutputs(

return outputs;
}

function buildCypressTargets(
configFilePath: string,
projectRoot: string,
Expand All @@ -164,33 +164,12 @@ function buildCypressTargets(
if ('e2e' in cypressConfig) {
targets[options.targetName] = {
command: `cypress run --config-file ${relativeConfigPath} --e2e`,
options: {
cwd: projectRoot,
},
options: { cwd: projectRoot },
cache: true,
inputs: getInputs(namedInputs),
outputs: getOutputs(projectRoot, cypressConfig, 'e2e'),
};

const e2eTargetDefaults = readTargetDefaultsForTarget(
options.targetName,
context.nxJsonConfiguration.targetDefaults,
'run-commands'
);

if (e2eTargetDefaults?.cache === undefined) {
targets[options.targetName].cache = true;
}

if (e2eTargetDefaults?.inputs === undefined) {
targets[options.targetName].inputs = getInputs(namedInputs);
}

if (e2eTargetDefaults?.outputs === undefined) {
targets[options.targetName].outputs = getOutputs(
projectRoot,
cypressConfig,
'e2e'
);
}

if (webServerCommands?.default) {
delete webServerCommands.default;
}
Expand Down Expand Up @@ -258,36 +237,14 @@ function buildCypressTargets(
}

if ('component' in cypressConfig) {
const componentTestingTargetDefaults = readTargetDefaultsForTarget(
options.componentTestingTargetName,
context.nxJsonConfiguration.targetDefaults,
'nx:run-commands'
);

// This will not override the e2e target if it is the same
targets[options.componentTestingTargetName] ??= {
command: `cypress open --config-file ${relativeConfigPath} --component`,
options: {
cwd: projectRoot,
},
options: { cwd: projectRoot },
cache: true,
inputs: getInputs(namedInputs),
outputs: getOutputs(projectRoot, cypressConfig, 'component'),
};

if (componentTestingTargetDefaults?.cache === undefined) {
targets[options.componentTestingTargetName].cache = true;
}

if (componentTestingTargetDefaults?.inputs === undefined) {
targets[options.componentTestingTargetName].inputs =
getInputs(namedInputs);
}

if (componentTestingTargetDefaults?.outputs === undefined) {
targets[options.componentTestingTargetName].outputs = getOutputs(
projectRoot,
cypressConfig,
'component'
);
}
}

return targets;
Expand Down Expand Up @@ -326,6 +283,7 @@ function normalizeOptions(options: CypressPluginOptions): CypressPluginOptions {
options.ciTargetName ??= 'e2e-ci';
return options;
}

function getInputs(
namedInputs: NxJsonConfiguration['namedInputs']
): TargetConfiguration['inputs'] {
Expand Down
7 changes: 7 additions & 0 deletions packages/eslint/src/plugins/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ describe('@nx/eslint/plugin', () => {
beforeEach(async () => {
context = {
nxJsonConfiguration: {
// These defaults should be overridden by plugin
targetDefaults: {
lint: {
cache: false,
inputs: ['foo', '^foo'],
},
},
namedInputs: {
default: ['{projectRoot}/**/*'],
production: ['!{projectRoot}/**/*.spec.ts'],
Expand Down
11 changes: 2 additions & 9 deletions packages/eslint/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
TargetConfiguration,
} from '@nx/devkit';
import { dirname, join } from 'path';
import { readTargetDefaultsForTarget } from 'nx/src/project-graph/utils/project-configuration-utils';
import { readdirSync } from 'fs';
import { combineGlobPatterns } from 'nx/src/utils/globs';
import {
Expand Down Expand Up @@ -103,12 +102,6 @@ function buildEslintTargets(
options: EslintPluginOptions,
context: CreateNodesContext
) {
const targetDefaults = readTargetDefaultsForTarget(
options.targetName,
context.nxJsonConfiguration.targetDefaults,
'@nx/eslint:lint'
);

const isRootProject = projectRoot === '.';

const targets: Record<string, TargetConfiguration> = {};
Expand All @@ -127,8 +120,8 @@ function buildEslintTargets(

targets[options.targetName] = {
...baseTargetConfig,
cache: targetDefaults?.cache ?? true,
inputs: targetDefaults?.inputs ?? [
cache: true,
inputs: [
'default',
...eslintConfigs.map((config) => `{workspaceRoot}/${config}`),
'{workspaceRoot}/tools/eslint-rules/**/*',
Expand Down
7 changes: 7 additions & 0 deletions packages/nuxt/src/plugins/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ describe('@nx/nuxt/plugin', () => {
beforeEach(async () => {
context = {
nxJsonConfiguration: {
// These defaults should be overridden by plugin
targetDefaults: {
build: {
cache: false,
inputs: ['foo', '^foo'],
},
},
namedInputs: {
default: ['{projectRoot}/**/*'],
production: ['!{projectRoot}/**/*.spec.ts'],
Expand Down
78 changes: 17 additions & 61 deletions packages/nuxt/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ import {
CreateDependencies,
CreateNodes,
CreateNodesContext,
TargetConfiguration,
detectPackageManager,
joinPathFragments,
offsetFromRoot,
readJsonFile,
TargetConfiguration,
writeJsonFile,
} from '@nx/devkit';
import { basename, dirname, join } from 'path';
import { projectGraphCacheDirectory } from 'nx/src/utils/cache-directory';
import { readTargetDefaultsForTarget } from 'nx/src/project-graph/utils/project-configuration-utils';
import { getNamedInputs } from '@nx/devkit/src/utils/get-named-inputs';
import { existsSync, readdirSync } from 'fs';
import { loadNuxtKitDynamicImport } from '../utils/executor-utils';
Expand Down Expand Up @@ -102,67 +101,44 @@ async function buildNuxtTargets(
const targets: Record<string, TargetConfiguration> = {};

targets[options.buildTargetName] = buildTarget(
context,
namedInputs,
buildOutputs,
projectRoot,
options
projectRoot
);

targets[options.serveTargetName] = serveTarget(projectRoot);

targets[options.testTargetName] = testTarget(
context,
namedInputs,
testOutputs,
options,
projectRoot
);

return targets;
}

function buildTarget(
context: CreateNodesContext,
namedInputs: {
[inputName: string]: any[];
},
buildOutputs: string[],
projectRoot: string,
options: NuxtPluginOptions
projectRoot: string
) {
const targetDefaults = readTargetDefaultsForTarget(
options.buildTargetName,
context.nxJsonConfiguration.targetDefaults
);

const targetConfig: TargetConfiguration = {
return {
command: `nuxi build`,
options: {
cwd: projectRoot,
},
};

if (targetDefaults?.outputs === undefined) {
targetConfig.outputs = buildOutputs;
}

if (targetDefaults?.cache === undefined) {
targetConfig.cache = true;
}

if (targetDefaults?.inputs === undefined) {
targetConfig.inputs = [
options: { cwd: projectRoot },
cache: true,
inputs: [
...('production' in namedInputs
? ['default', '^production']
: ['default', '^default']),

{
externalDependencies: ['nuxi'],
},
];
}
return targetConfig;
],
outputs: buildOutputs,
};
}

function serveTarget(projectRoot: string) {
Expand All @@ -177,47 +153,27 @@ function serveTarget(projectRoot: string) {
}

function testTarget(
context: CreateNodesContext,
namedInputs: {
[inputName: string]: any[];
},
outputs: string[],
options: NuxtPluginOptions,
projectRoot: string
) {
const targetDefaults = readTargetDefaultsForTarget(
options.testTargetName,
context.nxJsonConfiguration.targetDefaults
);

const targetConfig: TargetConfiguration = {
return {
command: `vitest run`,
options: {
cwd: projectRoot,
},
};

if (targetDefaults?.outputs === undefined) {
targetConfig.outputs = outputs;
}

if (targetDefaults?.cache === undefined) {
targetConfig.cache = true;
}

if (targetDefaults?.inputs === undefined) {
targetConfig.inputs = [
options: { cwd: projectRoot },
cache: true,
inputs: [
...('production' in namedInputs
? ['default', '^production']
: ['default', '^default']),

{
externalDependencies: ['vitest'],
},
];
}

return targetConfig;
],
outputs,
};
}

async function getInfoFromNuxtConfig(
Expand Down
7 changes: 7 additions & 0 deletions packages/vite/src/plugins/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ describe('@nx/vite/plugin', () => {
beforeEach(async () => {
context = {
nxJsonConfiguration: {
// These defaults should be overridden by plugin
targetDefaults: {
build: {
cache: false,
inputs: ['foo', '^foo'],
},
},
namedInputs: {
default: ['{projectRoot}/**/*'],
production: ['!{projectRoot}/**/*.spec.ts'],
Expand Down
Loading

0 comments on commit ab8c929

Please sign in to comment.