The Workspace plugin contains executors and generators that are useful for any Nx workspace and serves as a foundation for other plugins.
npx @tessl/cli install tessl/npm-nx--workspace@21.4.0Nx Workspace is the core plugin for Nx monorepo build system, providing essential executors and generators that form the foundation for any Nx workspace. It serves as a base plugin that other Nx plugins build upon, offering utilities for project management, code generation, and build orchestration.
npm install @nx/workspaceimport {
moveGenerator,
removeGenerator,
runCommandsGenerator,
convertToNxProjectGenerator,
npmPackageGenerator
} from "@nx/workspace";For utilities:
import {
readTsConfig,
ProjectType,
projectRootDir,
updateJsonFile,
copyFile,
renameSync,
createDirectory,
Linter,
LinterType,
names,
output,
readPackageJson
} from "@nx/workspace";CommonJS:
const {
moveGenerator,
removeGenerator,
runCommandsGenerator
} = require("@nx/workspace");import { Tree } from "@nx/devkit";
import { runCommandsGenerator, ProjectType, updateJsonFile } from "@nx/workspace";
// Create a run-commands target for a project
await runCommandsGenerator(tree, {
project: "my-app",
name: "custom-build",
command: "webpack --mode production",
cwd: "apps/my-app"
});
// Work with project types
const appsDir = projectRootDir(ProjectType.Application); // returns "apps"
const libsDir = projectRootDir(ProjectType.Library); // returns "libs"
// Update configuration files
updateJsonFile("./package.json", (json) => {
json.scripts.build = "nx build";
return json;
});Nx Workspace is built around several key components:
Core generators for creating, moving, and managing projects within Nx workspaces. Essential for workspace organization and project lifecycle management.
function moveGenerator(tree: Tree, schema: Schema): Promise<GeneratorCallback>;
function removeGenerator(tree: Tree, schema: Schema): Promise<void>;
function runCommandsGenerator(tree: Tree, schema: Schema): Promise<void>;
function convertToNxProjectGenerator(tree: Tree, schema: Schema): Promise<void>;
function npmPackageGenerator(tree: Tree, options: ProjectOptions): Promise<GeneratorCallback>;Safe file and directory operations with error handling and workspace-aware functionality.
function updateJsonFile(path: string, callback: (json: any) => any): void;
function copyFile(file: string, target: string): void;
function renameSync(from: string, to: string, cb: (err: Error | null) => void): void;
function createDirectory(path: string): void;Tools for reading, parsing, and managing TypeScript configuration files across the workspace.
function readTsConfig(tsConfigPath: string): ParsedCommandLine;
function getRootTsConfigPathInTree(tree: Tree): string | null;
function getRelativePathToRootTsConfig(tree: Tree, targetPath: string): string;
function getRootTsConfigFileName(): string | null;Enums and utilities for managing different types of projects in Nx workspaces.
enum ProjectType {
Application = 'application',
Library = 'library'
}
function projectRootDir(projectType: ProjectType): string;Parsers and manipulators for workspace configuration and build targets.
function getWorkspacePath(host: Tree): string;
function parseTarget(targetString: string): ParsedTarget;
function editTarget(targetString: string, callback: Function): string;
function serializeTarget(target: ParsedTarget): string;Support for ESLint integration and linter configuration management.
enum Linter {
EsLint = 'eslint',
None = 'none'
}
type LinterType = 'eslint' | 'none';Terminal output formatting utilities (re-exported from nx/src/utils/output).
const output: {
log: (message: string) => void;
warn: (message: string) => void;
error: (message: string) => void;
success: (message: string) => void;
// ... other output utilities
};Utilities for reading package.json files (re-exported from nx/src/project-graph/file-utils).
function readPackageJson(path?: string): any;String manipulation utilities for consistent naming conventions (re-exported from @nx/devkit).
function names(name: string): Names;
interface Names {
name: string;
className: string;
propertyName: string;
constantName: string;
fileName: string;
}interface ParsedTarget {
project: string;
target: string;
config?: string;
}
interface ProjectOptions {
directory: string;
name?: string;
}
interface Schema {
[key: string]: any;
}
enum ProjectType {
Application = 'application',
Library = 'library'
}
enum Linter {
EsLint = 'eslint',
None = 'none'
}
type LinterType = 'eslint' | 'none';
interface Names {
name: string;
className: string;
propertyName: string;
constantName: string;
fileName: string;
}