0
# Configuration Management
1
2
Type-safe configuration system with helper functions for defining app configuration and mock data. Essential for setting up routing, plugins, build options, and development settings.
3
4
## Capabilities
5
6
### Define Configuration
7
8
Creates type-safe configuration for umi applications with full TypeScript support.
9
10
```typescript { .api }
11
/**
12
* Type-safe configuration helper that provides TypeScript intellisense
13
* @param config - Configuration object with all umi options
14
* @returns The same configuration object with type checking
15
*/
16
function defineConfig(config: ConfigType): ConfigType;
17
18
type ConfigType = IConfigFromPlugins & IConfig;
19
```
20
21
**Usage Examples:**
22
23
```typescript
24
import { defineConfig } from "umi";
25
26
// Basic configuration
27
export default defineConfig({
28
routes: [
29
{ path: "/", component: "@/pages/index" },
30
{ path: "/about", component: "@/pages/about" },
31
],
32
plugins: ["@umijs/plugin-react"],
33
});
34
35
// Advanced configuration with build and dev options
36
export default defineConfig({
37
routes: [
38
{
39
path: "/",
40
component: "@/layouts/index",
41
routes: [
42
{ path: "/", component: "@/pages/index" },
43
{ path: "/users", component: "@/pages/users" },
44
],
45
},
46
],
47
plugins: [
48
"@umijs/plugin-react",
49
"@umijs/plugin-antd",
50
],
51
antd: {
52
dark: true,
53
},
54
devServer: {
55
port: 3000,
56
https: false,
57
},
58
build: {
59
target: "es2017",
60
minify: true,
61
},
62
alias: {
63
"@/components": "./src/components",
64
"@/utils": "./src/utils",
65
},
66
});
67
```
68
69
### Define Mock Data
70
71
Creates mock data configuration for development server API mocking.
72
73
```typescript { .api }
74
/**
75
* Type-safe mock data helper for API endpoint mocking
76
* @param mockData - Object mapping API paths to mock responses
77
* @returns The same mock data object with type checking
78
*/
79
function defineMock(mockData: { [key: string]: MockDeclare }): { [key: string]: MockDeclare };
80
81
type MockDeclare =
82
| string
83
| number
84
| null
85
| undefined
86
| boolean
87
| Record<string, any>
88
| RequestHandler;
89
```
90
91
**Usage Examples:**
92
93
```typescript
94
import { defineMock } from "umi";
95
96
// Simple mock responses
97
export default defineMock({
98
"GET /api/users": [
99
{ id: 1, name: "Alice", email: "alice@example.com" },
100
{ id: 2, name: "Bob", email: "bob@example.com" },
101
],
102
"POST /api/users": { success: true, message: "User created" },
103
"GET /api/status": "OK",
104
});
105
106
// Dynamic mock with Express middleware function
107
export default defineMock({
108
"GET /api/users": (req, res) => {
109
const { page = 1, limit = 10 } = req.query;
110
res.json({
111
data: generateUsers(Number(limit)),
112
pagination: {
113
page: Number(page),
114
limit: Number(limit),
115
total: 100,
116
},
117
});
118
},
119
"POST /api/login": (req, res) => {
120
const { username, password } = req.body;
121
if (username === "admin" && password === "password") {
122
res.json({ token: "mock-jwt-token", user: { id: 1, username } });
123
} else {
124
res.status(401).json({ error: "Invalid credentials" });
125
}
126
},
127
});
128
```
129
130
## Configuration Constants
131
132
### Framework Configuration
133
134
Core configuration constants used throughout the framework.
135
136
```typescript { .api }
137
// Minimum required Node.js version
138
const MIN_NODE_VERSION: 14;
139
140
// Development command name
141
const DEV_COMMAND: "dev";
142
143
// Framework name (configurable via environment)
144
const FRAMEWORK_NAME: string; // process.env.FRAMEWORK_NAME || 'umi'
145
146
// Default configuration file patterns
147
const DEFAULT_CONFIG_FILES: string[]; // ['.umirc.ts', '.umirc.js', 'config/config.ts', 'config/config.js']
148
149
// Runtime type definition filename
150
const RUNTIME_TYPE_FILE_NAME: "runtimeConfig.d.ts";
151
```
152
153
## Type Definitions
154
155
```typescript { .api }
156
// Complete configuration interface combining plugin and core config
157
type ConfigType = IConfigFromPlugins & IConfig;
158
159
// Mock data declaration types
160
type MockDeclare =
161
| string
162
| number
163
| null
164
| undefined
165
| boolean
166
| Record<string, any>
167
| RequestHandler;
168
169
// Express request handler type for dynamic mocks
170
type RequestHandler = (
171
req: Request,
172
res: Response,
173
next: NextFunction
174
) => void;
175
```