0
# App Configuration Management
1
2
The AppHelper class provides utilities for parsing and managing Taro app configurations, handling pages, components, and build entry points.
3
4
## Capabilities
5
6
### AppHelper Class
7
8
Helper class for managing Taro app configuration and entry points with automatic parsing of app structure.
9
10
```typescript { .api }
11
/**
12
* Helper class for managing Taro app configuration and entry points
13
* Handles parsing of Taro app structure, pages, components, and configurations
14
*/
15
class AppHelper {
16
/**
17
* Creates an AppHelper instance
18
* @param entry - Webpack entry configuration or path (defaults to empty object)
19
* @param options - Configuration options for the helper (defaults to empty object)
20
*/
21
constructor(entry?: TEntry, options?: Partial<IOptions>);
22
23
/** Returns the main app entry file path */
24
readonly appEntry: string;
25
26
/** Returns parsed Taro app configuration object */
27
readonly appConfig: AppConfig;
28
29
/** Returns Set of page objects with name and path properties */
30
readonly pages: Set<{ name: string; path: string }>;
31
32
/** Returns Set of component objects with name and path properties (only for native component builds) */
33
readonly comps: Set<{ name: string; path: string }>;
34
35
/** Returns Map of page names to their config file paths */
36
readonly pagesConfigList: Map<string, string>;
37
38
/** Returns Map of component names to their config file paths */
39
readonly compsConfigList: Map<string, string>;
40
41
/**
42
* Returns config file path for the given file path
43
* @param filePath - Source file path to get config for
44
* @returns Path to the corresponding config file
45
*/
46
getConfigFilePath(filePath: string): string;
47
48
/** Clears all cached configuration data */
49
clear(): void;
50
}
51
```
52
53
**Constructor Parameters:**
54
55
- `entry` (TEntry): Webpack entry configuration - can be string, string array, Entry object, or EntryFunc
56
- `options` (Partial<IOptions>): Configuration options object
57
58
**Important Notes:**
59
60
- The `comps` property is only available when building native components and will throw an error "全局配置缺少 components 字段,请检查!" if accessed when the `components` field doesn't exist in the app configuration
61
- For H5 builds, use `pages`, `appConfig`, and related properties - do not access `comps`
62
- All configuration access is lazy-loaded and cached for performance
63
- Constructor parameters are optional - defaults will be applied for missing values
64
65
**Usage Examples:**
66
67
```typescript
68
import { AppHelper } from "@tarojs/webpack-runner";
69
70
// Basic usage with default options
71
const appHelper = new AppHelper("./src/app.tsx", {
72
sourceDir: "/path/to/src",
73
entryFileName: "app",
74
frameworkExts: [".tsx", ".ts", ".jsx", ".js"]
75
});
76
77
// Access app configuration
78
console.log(appHelper.appConfig.pages); // Array of page paths
79
console.log(appHelper.appConfig.window); // Window configuration
80
81
// Iterate through pages
82
appHelper.pages.forEach(({ name, path }) => {
83
console.log(`Page: ${name} at ${path}`);
84
const configPath = appHelper.getConfigFilePath(path);
85
console.log(`Config: ${configPath}`);
86
});
87
88
// Work with components (only available for native component builds)
89
try {
90
if (appHelper.appConfig.components) {
91
appHelper.comps.forEach(({ name, path }) => {
92
console.log(`Component: ${name} at ${path}`);
93
});
94
}
95
} catch (error) {
96
// Components field not available in H5 builds
97
console.log("Components not configured for this build type");
98
}
99
100
// Advanced entry configuration
101
const complexAppHelper = new AppHelper({
102
app: ["./src/app.tsx", "./src/polyfills.ts"],
103
vendor: ["react", "react-dom"]
104
}, {
105
sourceDir: "/project/src",
106
frameworkExts: [".tsx", ".ts"],
107
modifyAppConfig: (config) => {
108
// Modify app config before processing
109
config.window.navigationBarTitleText = "My App";
110
}
111
});
112
```
113
114
### App Configuration Properties
115
116
The AppHelper provides access to parsed Taro app configuration:
117
118
```typescript { .api }
119
/**
120
* Taro app configuration interface
121
*/
122
interface AppConfig {
123
pages?: string[];
124
components?: string[];
125
window?: WindowConfig;
126
tabBar?: TabBarConfig;
127
subPackages?: SubPackageConfig[];
128
subpackages?: SubPackageConfig[]; // Alternative spelling
129
entryPagePath?: string;
130
// Additional Taro-specific configuration properties
131
}
132
133
interface WindowConfig {
134
navigationBarTitleText?: string;
135
navigationBarBackgroundColor?: string;
136
backgroundColor?: string;
137
// Additional window configuration properties
138
}
139
140
interface TabBarConfig {
141
color?: string;
142
selectedColor?: string;
143
backgroundColor?: string;
144
list: TabBarItem[];
145
// Additional tab bar configuration properties
146
}
147
148
interface TabBarItem {
149
pagePath: string;
150
text: string;
151
iconPath?: string;
152
selectedIconPath?: string;
153
}
154
155
interface SubPackageConfig {
156
root: string;
157
pages: string[];
158
// Additional subpackage configuration properties
159
}
160
```
161
162
### Configuration Management Methods
163
164
Methods for working with configuration files and paths:
165
166
```typescript { .api }
167
/**
168
* Returns the main app entry file path
169
* Handles various entry configuration formats
170
*/
171
readonly appEntry: string;
172
173
/**
174
* Returns config file path for the given file path
175
* Automatically resolves .config extensions
176
* @param filePath - Source file path (e.g., "src/pages/index/index.tsx")
177
* @returns Config file path (e.g., "src/pages/index/index.config.ts")
178
*/
179
getConfigFilePath(filePath: string): string;
180
181
/**
182
* Clears all cached configuration data
183
* Useful for rebuilding configuration after changes
184
*/
185
clear(): void;
186
```
187
188
**Configuration Processing:**
189
190
```typescript
191
// Example of how AppHelper processes configurations
192
const appHelper = new AppHelper("./src/app.tsx", options);
193
194
// 1. App entry resolution
195
const entryPath = appHelper.appEntry;
196
// Resolves to actual file path: "/project/src/app.tsx"
197
198
// 2. App config parsing
199
const config = appHelper.appConfig;
200
// Parses app.config.js/ts and returns structured configuration
201
202
// 3. Pages processing
203
const pages = appHelper.pages;
204
// Processes pages array and subpackages, resolving file paths
205
// Set([
206
// { name: "pages/index/index", path: "/project/src/pages/index/index.tsx" },
207
// { name: "pages/profile/profile", path: "/project/src/pages/profile/profile.tsx" }
208
// ])
209
210
// 4. Config file mapping
211
const pageConfigs = appHelper.pagesConfigList;
212
// Maps page names to their config file paths
213
// Map([
214
// ["pages/index/index", "/project/src/pages/index/index.config.ts"],
215
// ["pages/profile/profile", "/project/src/pages/profile/profile.config.ts"]
216
// ])
217
```
218
219
### Error Handling
220
221
The AppHelper includes comprehensive error handling:
222
223
```typescript
224
// Missing app configuration
225
try {
226
const config = appHelper.appConfig;
227
} catch (error) {
228
// Throws: "缺少 app 全局配置,请检查!"
229
}
230
231
// Missing pages configuration
232
try {
233
const pages = appHelper.pages;
234
} catch (error) {
235
// Throws: "全局配置缺少 pages 字段,请检查!"
236
}
237
238
// Missing components configuration (when accessed)
239
try {
240
const comps = appHelper.comps;
241
} catch (error) {
242
// Throws: "全局配置缺少 components 字段,请检查!"
243
}
244
```
245
246
**Subpackage Processing:**
247
248
The AppHelper automatically processes subpackages defined in the app configuration:
249
250
```typescript
251
// App config with subpackages
252
const appConfig = {
253
pages: ["pages/index/index"],
254
subPackages: [
255
{
256
root: "packageA",
257
pages: ["pages/cat/cat", "pages/dog/dog"]
258
},
259
{
260
root: "packageB",
261
pages: ["pages/apple/apple"]
262
}
263
]
264
};
265
266
// AppHelper processes all pages including subpackage pages
267
const allPages = appHelper.pages;
268
// Includes main pages and subpackage pages with proper root prefixes
269
```