0
# CLI Integration
1
2
Command-line interface functionality for build scripts and development workflows. Provides programmatic access to Rsbuild's CLI commands and configuration.
3
4
## Capabilities
5
6
### Run CLI
7
8
Execute the Rsbuild CLI programmatically with command-line arguments.
9
10
```typescript { .api }
11
/**
12
* Execute Rsbuild CLI with current process arguments
13
* @returns Promise that resolves when CLI execution completes
14
*/
15
function runCLI(): Promise<void>;
16
```
17
18
**Usage Examples:**
19
20
```typescript
21
import { runCLI } from "@rsbuild/core";
22
23
// Run CLI with current process arguments
24
// Equivalent to running: rsbuild build, rsbuild dev, etc.
25
await runCLI();
26
```
27
28
### CLI Commands
29
30
Available commands when using the Rsbuild CLI:
31
32
- **`rsbuild dev`** - Start development server
33
- **`rsbuild build`** - Build for production
34
- **`rsbuild preview`** - Preview production build
35
- **`rsbuild inspect`** - Inspect resolved configuration
36
37
### CLI Options
38
39
Common command-line options:
40
41
```bash
42
# Development server
43
rsbuild dev [options]
44
--port <port> # Server port (default: 3000)
45
--host <host> # Server host (default: localhost)
46
--open # Open browser automatically
47
--config <path> # Config file path
48
49
# Production build
50
rsbuild build [options]
51
--config <path> # Config file path
52
--mode <mode> # Build mode (development/production)
53
--analyze # Bundle analyzer
54
--env <env> # Environment variables file
55
56
# Preview build
57
rsbuild preview [options]
58
--port <port> # Preview server port
59
--host <host> # Preview server host
60
--open # Open browser automatically
61
62
# Inspect configuration
63
rsbuild inspect [options]
64
--env <env> # Environment to inspect
65
--output <path> # Output file path
66
--verbose # Verbose output
67
```
68
69
### Programmatic CLI Usage
70
71
```typescript
72
// In a Node.js script
73
import { runCLI } from "@rsbuild/core";
74
75
// Set command line arguments programmatically
76
process.argv = [
77
"node",
78
"script.js",
79
"build",
80
"--mode", "production",
81
"--config", "./rsbuild.prod.config.ts"
82
];
83
84
await runCLI();
85
```
86
87
### Binary Integration
88
89
The `@rsbuild/core` package includes a binary executable located at `./bin/rsbuild.js`:
90
91
```json
92
{
93
"bin": {
94
"rsbuild": "./bin/rsbuild.js"
95
}
96
}
97
```
98
99
This allows direct execution when installed:
100
101
```bash
102
# Global installation
103
npm install -g @rsbuild/core
104
rsbuild dev
105
106
# Local installation
107
npx rsbuild dev
108
109
# Package.json scripts
110
{
111
"scripts": {
112
"dev": "rsbuild dev",
113
"build": "rsbuild build",
114
"preview": "rsbuild preview"
115
}
116
}
117
```
118
119
### Environment Integration
120
121
CLI automatically detects and sets appropriate environment variables:
122
123
```typescript { .api }
124
// CLI sets NODE_ENV based on command
125
// rsbuild dev -> NODE_ENV=development
126
// rsbuild build -> NODE_ENV=production
127
// rsbuild preview -> NODE_ENV=production
128
```
129
130
### Configuration File Resolution
131
132
CLI automatically searches for configuration files in order:
133
134
1. `rsbuild.config.ts`
135
2. `rsbuild.config.js`
136
3. `rsbuild.config.mjs`
137
4. `rsbuild.config.mts`
138
5. `rsbuild.config.cts`
139
6. `rsbuild.config.cjs`
140
141
Custom config path:
142
```bash
143
rsbuild build --config custom.config.ts
144
```