Skip to content

Commit

Permalink
feat(core): add target defaults in configuration generators rather th… (
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz authored Jan 17, 2024
1 parent 38307fa commit 1ffd673
Show file tree
Hide file tree
Showing 20 changed files with 131 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Tree } from '@nx/devkit';
import { addProjectConfiguration } from '@nx/devkit';
import { addProjectConfiguration, Tree } from '@nx/devkit';
import type { AngularProjectConfiguration } from '../../../utils/types';
import { getInstalledAngularVersionInfo } from '../../utils/version-utils';
import type { NormalizedSchema } from './normalized-schema';
import { addBuildTargetDefaults } from '@nx/devkit/src/generators/add-build-target-defaults';

export function createProject(tree: Tree, options: NormalizedSchema) {
const { major: angularMajorVersion } = getInstalledAngularVersionInfo(tree);
Expand All @@ -20,6 +20,8 @@ export function createProject(tree: Tree, options: NormalizedSchema) {
? 'browser'
: 'main';

addBuildTargetDefaults(tree, buildExecutor);

let budgets = undefined;
if (options.bundler === 'webpack' || angularMajorVersion >= 17) {
if (options.strict) {
Expand Down
22 changes: 22 additions & 0 deletions packages/devkit/src/generators/add-build-target-defaults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { Tree } from 'nx/src/devkit-exports';
import { requireNx } from '../../nx';

const { readNxJson, updateNxJson } = requireNx();

export function addBuildTargetDefaults(
tree: Tree,
executorName: string,
buildTargetName = 'build'
): void {
const nxJson = readNxJson(tree);
nxJson.targetDefaults ??= {};
nxJson.targetDefaults[executorName] ??= {
cache: true,
dependsOn: [`^${buildTargetName}`],
inputs:
nxJson.namedInputs && 'production' in nxJson.namedInputs
? ['production', '^production']
: ['default', '^default'],
};
updateNxJson(tree, nxJson);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Tree } from '@nx/devkit';
import {
formatFiles,
joinPathFragments,
readProjectConfiguration,
Tree,
updateProjectConfiguration,
writeJson,
} from '@nx/devkit';
Expand All @@ -12,6 +12,7 @@ import { getImportPath } from '@nx/js/src/utils/get-import-path';
import { esbuildInitGenerator } from '../init/init';
import { EsBuildExecutorOptions } from '../../executors/esbuild/schema';
import { EsBuildProjectSchema } from './schema';
import { addBuildTargetDefaults } from '@nx/devkit/src/generators/add-build-target-defaults';

export async function configurationGenerator(
tree: Tree,
Expand Down Expand Up @@ -39,6 +40,7 @@ function checkForTargetConflicts(tree: Tree, options: EsBuildProjectSchema) {
}

function addBuildTarget(tree: Tree, options: EsBuildProjectSchema) {
addBuildTargetDefaults(tree, '@nx/esbuild:esbuild', options.buildTarget);
const project = readProjectConfiguration(tree, options.project);
const packageJsonPath = joinPathFragments(project.root, 'package.json');

Expand Down
5 changes: 5 additions & 0 deletions packages/expo/src/generators/application/lib/add-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ import {

import { hasExpoPlugin } from '../../../utils/has-expo-plugin';
import { NormalizedSchema } from './normalize-options';
import { addBuildTargetDefaults } from '@nx/devkit/src/generators/add-build-target-defaults';

export function addProject(host: Tree, options: NormalizedSchema) {
const nxJson = readNxJson(host);
const hasPlugin = hasExpoPlugin(host);

if (!hasPlugin) {
addBuildTargetDefaults(host, '@nx/expo:build');
}

const projectConfiguration: ProjectConfiguration = {
root: options.appProjectRoot,
sourceRoot: `${options.appProjectRoot}/src`,
Expand Down
3 changes: 3 additions & 0 deletions packages/expo/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { NormalizedSchema, normalizeOptions } from './lib/normalize-options';
import { Schema } from './schema';
import { ensureDependencies } from '../../utils/ensure-dependencies';
import { initRootBabelConfig } from '../../utils/init-root-babel-config';
import { addBuildTargetDefaults } from '@nx/devkit/src/generators/add-build-target-defaults';

export async function expoLibraryGenerator(
host: Tree,
Expand Down Expand Up @@ -144,6 +145,8 @@ async function addProject(

const external = ['react/jsx-runtime', 'react-native', 'react', 'react-dom'];

addBuildTargetDefaults(host, '@nx/rollup:rollup');

project.targets.build = {
executor: '@nx/rollup:rollup',
outputs: ['{options.outputPath}'],
Expand Down
5 changes: 5 additions & 0 deletions packages/js/src/generators/setup-build/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { addSwcConfig } from '../../utils/swc/add-swc-config';
import { addSwcDependencies } from '../../utils/swc/add-swc-dependencies';
import { nxVersion } from '../../utils/versions';
import { SetupBuildGeneratorSchema } from './schema';
import { addBuildTargetDefaults } from '@nx/devkit/src/generators/add-build-target-defaults';

export async function setupBuildGenerator(
tree: Tree,
Expand Down Expand Up @@ -122,6 +123,8 @@ export async function setupBuildGenerator(
break;
}
case 'tsc': {
addBuildTargetDefaults(tree, '@nx/js:tsc');

const outputPath = joinPathFragments('dist', project.root);
project.targets[buildTarget] = {
executor: `@nx/js:tsc`,
Expand All @@ -137,6 +140,8 @@ export async function setupBuildGenerator(
break;
}
case 'swc': {
addBuildTargetDefaults(tree, '@nx/js:swc');

const outputPath = joinPathFragments('dist', project.root);
project.targets[buildTarget] = {
executor: `@nx/js:swc`,
Expand Down
3 changes: 3 additions & 0 deletions packages/next/src/generators/application/lib/add-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
readNxJson,
Tree,
} from '@nx/devkit';
import { addBuildTargetDefaults } from '@nx/devkit/src/generators/add-build-target-defaults';

export function addProject(host: Tree, options: NormalizedSchema) {
const targets: Record<string, any> = {};
Expand All @@ -20,6 +21,8 @@ export function addProject(host: Tree, options: NormalizedSchema) {
);

if (!hasPlugin) {
addBuildTargetDefaults(host, '@nx/next:build');

targets.build = {
executor: '@nx/next:build',
outputs: ['{options.outputPath}'],
Expand Down
3 changes: 3 additions & 0 deletions packages/node/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { initGenerator } from '../init/init';
import { setupDockerGenerator } from '../setup-docker/setup-docker';
import { Schema } from './schema';
import { hasWebpackPlugin } from '../../utils/has-webpack-plugin';
import { addBuildTargetDefaults } from '@nx/devkit/src/generators/add-build-target-defaults';

export interface NormalizedSchema extends Schema {
appProjectRoot: string;
Expand Down Expand Up @@ -160,9 +161,11 @@ function addProject(tree: Tree, options: NormalizedSchema) {
};

if (options.bundler === 'esbuild') {
addBuildTargetDefaults(tree, '@nx/esbuild:esbuild');
project.targets.build = getEsBuildConfig(project, options);
} else if (options.bundler === 'webpack') {
if (!hasWebpackPlugin(tree)) {
addBuildTargetDefaults(tree, `@nx/webpack:webpack`);
project.targets.build = getWebpackBuildConfig(project, options);
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/node/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { join } from 'path';
import { tslibVersion, typesNodeVersion } from '../../utils/versions';
import { initGenerator } from '../init/init';
import { Schema } from './schema';
import { addBuildTargetDefaults } from '@nx/devkit/src/generators/add-build-target-defaults';

export interface NormalizedSchema extends Schema {
fileName: string;
Expand Down Expand Up @@ -164,6 +165,7 @@ function updateProject(tree: Tree, options: NormalizedSchema) {
const rootProject = options.projectRoot === '.' || options.projectRoot === '';

project.targets = project.targets || {};
addBuildTargetDefaults(tree, `@nx/js:${options.compiler}`);
project.targets.build = {
executor: `@nx/js:${options.compiler}`,
outputs: ['{options.outputPath}'],
Expand Down
3 changes: 3 additions & 0 deletions packages/remix/src/generators/application/application.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { NxRemixGeneratorSchema } from './schema';
import { updateDependencies } from '../utils/update-dependencies';
import initGenerator from '../init/init';
import { initGenerator as jsInitGenerator } from '@nx/js';
import { addBuildTargetDefaults } from '@nx/devkit/src/generators/add-build-target-defaults';

export default async function (tree: Tree, _options: NxRemixGeneratorSchema) {
const options = await normalizeOptions(tree, _options);
Expand All @@ -44,6 +45,8 @@ export default async function (tree: Tree, _options: NxRemixGeneratorSchema) {
await jsInitGenerator(tree, { skipFormat: true }),
];

addBuildTargetDefaults(tree, '@nx/remix:build');

addProjectConfiguration(tree, options.projectName, {
root: options.projectRoot,
sourceRoot: `${options.projectRoot}`,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { GeneratorCallback, Tree } from '@nx/devkit';
import {
formatFiles,
joinPathFragments,
readProjectConfiguration,
Tree,
GeneratorCallback,
runTasksInSerial,
updateProjectConfiguration,
writeJson,
Expand All @@ -12,6 +13,7 @@ import { getImportPath } from '@nx/js/src/utils/get-import-path';
import { rollupInitGenerator } from '../init/init';
import { RollupExecutorOptions } from '../../executors/rollup/schema';
import { RollupProjectSchema } from './schema';
import { addBuildTargetDefaults } from '@nx/devkit/src/generators/add-build-target-defaults';
import { ensureDependencies } from '../../utils/ensure-dependencies';

export async function configurationGenerator(
Expand Down Expand Up @@ -46,6 +48,7 @@ function checkForTargetConflicts(tree: Tree, options: RollupProjectSchema) {
}

function addBuildTarget(tree: Tree, options: RollupProjectSchema) {
addBuildTargetDefaults(tree, '@nx/rollup:rollup', options.buildTarget);
const project = readProjectConfiguration(tree, options.project);
const packageJsonPath = joinPathFragments(project.root, 'package.json');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
addStaticTarget,
addStorybookTarget,
addStorybookToNamedInputs,
addStorybookToTargetDefaults,
configureTsProjectConfig,
configureTsSolutionConfig,
createProjectStorybookDir,
Expand Down Expand Up @@ -153,6 +154,9 @@ export async function configurationGenerator(

addBuildStorybookToCacheableOperations(tree);
addStorybookToNamedInputs(tree);
if (!hasPlugin) {
addStorybookToTargetDefaults(tree);
}

let devDeps = {};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,45 +481,53 @@ export function addStorybookToNamedInputs(tree: Tree) {
}
}

nxJson.targetDefaults ??= {};
nxJson.targetDefaults['build-storybook'] ??= {};
nxJson.targetDefaults['build-storybook'].inputs ??= [
'default',
hasProductionFileset ? '^production' : '^default',
];
updateNxJson(tree, nxJson);
}
}

if (
!nxJson.targetDefaults['build-storybook'].inputs.includes(
'{projectRoot}/.storybook/**/*'
)
) {
nxJson.targetDefaults['build-storybook'].inputs.push(
'{projectRoot}/.storybook/**/*'
);
}
export function addStorybookToTargetDefaults(tree: Tree) {
const nxJson = readNxJson(tree);

nxJson.targetDefaults ??= {};
nxJson.targetDefaults['build-storybook'] ??= {};
nxJson.targetDefaults['build-storybook'].inputs ??= [
'default',
nxJson.namedInputs && 'production' in nxJson.namedInputs
? '^production'
: '^default',
];

// Delete the !{projectRoot}/.storybook/**/* glob from build-storybook
// because we want to rebuild Storybook if the .storybook folder changes
const index = nxJson.targetDefaults['build-storybook'].inputs.indexOf(
'!{projectRoot}/.storybook/**/*'
if (
!nxJson.targetDefaults['build-storybook'].inputs.includes(
'{projectRoot}/.storybook/**/*'
)
) {
nxJson.targetDefaults['build-storybook'].inputs.push(
'{projectRoot}/.storybook/**/*'
);
}

if (index !== -1) {
nxJson.targetDefaults['build-storybook'].inputs.splice(index, 1);
}
// Delete the !{projectRoot}/.storybook/**/* glob from build-storybook
// because we want to rebuild Storybook if the .storybook folder changes
const index = nxJson.targetDefaults['build-storybook'].inputs.indexOf(
'!{projectRoot}/.storybook/**/*'
);

if (
!nxJson.targetDefaults['build-storybook'].inputs.includes(
'{projectRoot}/tsconfig.storybook.json'
)
) {
nxJson.targetDefaults['build-storybook'].inputs.push(
'{projectRoot}/tsconfig.storybook.json'
);
}
if (index !== -1) {
nxJson.targetDefaults['build-storybook'].inputs.splice(index, 1);
}

updateNxJson(tree, nxJson);
if (
!nxJson.targetDefaults['build-storybook'].inputs.includes(
'{projectRoot}/tsconfig.storybook.json'
)
) {
nxJson.targetDefaults['build-storybook'].inputs.push(
'{projectRoot}/tsconfig.storybook.json'
);
}

updateNxJson(tree, nxJson);
}

export function createProjectStorybookDir(
Expand Down
4 changes: 2 additions & 2 deletions packages/storybook/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ function moveToDevDependencies(tree: Tree): GeneratorCallback {
}

export async function initGenerator(tree: Tree, schema: Schema) {
addCacheableOperation(tree);

if (process.env.NX_PCV3 === 'true') {
addPlugin(tree);
} else {
addCacheableOperation(tree);
}

const tasks: GeneratorCallback[] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { getProjects, joinPathFragments, Tree, formatFiles } from '@nx/devkit';
import { forEachExecutorOptions } from '@nx/devkit/src/generators/executor-options-utils';
import {
addStorybookToNamedInputs,
addStorybookToTargetDefaults,
renameAndMoveOldTsConfig,
} from '../../generators/configuration/lib/util-functions';

Expand All @@ -26,5 +27,6 @@ export default async function (tree: Tree) {
);

addStorybookToNamedInputs(tree);
addStorybookToTargetDefaults(tree);
await formatFiles(tree);
}
2 changes: 2 additions & 0 deletions packages/vite/src/utils/generator-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { VitePreviewServerExecutorOptions } from '../executors/preview-server/sc
import { VitestExecutorOptions } from '../executors/test/schema';
import { ViteConfigurationGeneratorSchema } from '../generators/configuration/schema';
import { ensureViteConfigIsCorrect } from './vite-config-edit-utils';
import { addBuildTargetDefaults } from '@nx/devkit/src/generators/add-build-target-defaults';

export type Target = 'build' | 'serve' | 'test' | 'preview';
export type TargetFlags = Partial<Record<Target, boolean>>;
Expand Down Expand Up @@ -200,6 +201,7 @@ export function addOrChangeBuildTarget(
options: ViteConfigurationGeneratorSchema,
target: string
) {
addBuildTargetDefaults(tree, '@nx/vite:build');
const project = readProjectConfiguration(tree, options.project);

const buildOptions: ViteBuildExecutorOptions = {
Expand Down
2 changes: 2 additions & 0 deletions packages/web/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ 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/add-build-target-defaults';

interface NormalizedSchema extends Schema {
projectName: string;
Expand Down Expand Up @@ -175,6 +176,7 @@ async function setupBundler(tree: Tree, options: NormalizedSchema) {
// TODO(jack): Flush this out... no bundler should be possible for web but the experience isn't holistic due to missing features (e.g. writing index.html).
} else if (options.bundler === 'none') {
const project = readProjectConfiguration(tree, options.projectName);
addBuildTargetDefaults(tree, `@nx/js:${options.compiler}`);
project.targets.build = {
executor: `@nx/js:${options.compiler}`,
outputs: ['{options.outputPath}'],
Expand Down
Loading

0 comments on commit 1ffd673

Please sign in to comment.