@fchc8/vite-plugin-multi-page 1.1.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README-EN.md +183 -337
- package/README.md +175 -423
- package/dist/cli.js +649 -0
- package/dist/index.d.mts +43 -69
- package/dist/index.d.ts +43 -69
- package/dist/index.js +653 -428
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +649 -429
- package/dist/index.mjs.map +1 -1
- package/package.json +11 -3
package/README.md
CHANGED
|
@@ -1,518 +1,270 @@
|
|
|
1
1
|
# vite-plugin-multi-page
|
|
2
2
|
|
|
3
|
-
> [English Documentation](./README-EN.md)
|
|
3
|
+
> English Documentation | [English Documentation](./README-EN.md)
|
|
4
4
|
|
|
5
|
-
一个强大的 Vite
|
|
5
|
+
一个强大的 Vite 插件,支持多页面应用开发,提供多策略构建、TypeScript 配置支持和命令行工具。
|
|
6
6
|
|
|
7
|
-
##
|
|
7
|
+
## 特性
|
|
8
8
|
|
|
9
|
-
-
|
|
10
|
-
-
|
|
11
|
-
-
|
|
12
|
-
-
|
|
13
|
-
-
|
|
14
|
-
-
|
|
15
|
-
- 🎨 **开发友好**:详细的调试日志和热重载支持
|
|
16
|
-
- ⚡ **开发构建一致性**:确保开发模式与构建模式使用相同的配置逻辑
|
|
17
|
-
- 🔄 **配置同步**:环境变量、模板选择、构建策略在开发和生产环境保持一致
|
|
9
|
+
- 🎯 **多页面支持**: 自动发现页面入口文件
|
|
10
|
+
- 🔧 **多策略构建**: 支持为不同页面配置不同的构建策略
|
|
11
|
+
- 📝 **TypeScript 配置**: 支持 TypeScript 配置文件
|
|
12
|
+
- 🚀 **CLI 工具**: 提供命令行批量构建工具
|
|
13
|
+
- 🔄 **热重载**: 开发服务器支持页面热重载
|
|
14
|
+
- 📦 **智能合并**: 自动合并多策略构建结果
|
|
18
15
|
|
|
19
|
-
##
|
|
16
|
+
## 安装
|
|
20
17
|
|
|
21
18
|
```bash
|
|
22
|
-
npm install
|
|
23
|
-
# 或
|
|
24
|
-
yarn add @fchc8/vite-plugin-multi-page
|
|
25
|
-
# 或
|
|
26
|
-
pnpm add @fchc8/vite-plugin-multi-page
|
|
19
|
+
npm install vite-plugin-multi-page --save-dev
|
|
27
20
|
```
|
|
28
21
|
|
|
29
|
-
##
|
|
22
|
+
## 快速开始
|
|
30
23
|
|
|
31
|
-
###
|
|
24
|
+
### 1. 创建配置文件
|
|
32
25
|
|
|
33
|
-
|
|
34
|
-
// vite.config.ts
|
|
35
|
-
import { defineConfig } from 'vite';
|
|
36
|
-
import viteMultiPage from '@fchc8/vite-plugin-multi-page';
|
|
26
|
+
创建 `multipage.config.ts` 或 `multipage.config.js`:
|
|
37
27
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
template: 'index.html',
|
|
43
|
-
exclude: ['src/main.ts'],
|
|
44
|
-
debug: true,
|
|
45
|
-
}),
|
|
46
|
-
],
|
|
47
|
-
});
|
|
48
|
-
```
|
|
28
|
+
```typescript
|
|
29
|
+
export default context => {
|
|
30
|
+
const { mode, command, isCLI } = context;
|
|
31
|
+
const isProduction = mode === 'production';
|
|
49
32
|
|
|
50
|
-
|
|
33
|
+
return {
|
|
34
|
+
// 页面入口匹配规则
|
|
35
|
+
entry: 'src/pages/**/*.{ts,js}',
|
|
51
36
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
├── src/
|
|
55
|
-
│ └── pages/
|
|
56
|
-
│ ├── home.ts → /home.html
|
|
57
|
-
│ ├── about.ts → /about.html
|
|
58
|
-
│ ├── admin/
|
|
59
|
-
│ │ └── dashboard.ts → /dashboard.html
|
|
60
|
-
│ └── mobile/
|
|
61
|
-
│ └── app.js → /app.html
|
|
62
|
-
├── index.html
|
|
63
|
-
├── admin.html
|
|
64
|
-
└── mobile.html
|
|
65
|
-
```
|
|
37
|
+
// HTML 模板
|
|
38
|
+
template: 'index.html',
|
|
66
39
|
|
|
67
|
-
|
|
40
|
+
// 模板占位符
|
|
41
|
+
placeholder: '{{ENTRY_FILE}}',
|
|
68
42
|
|
|
69
|
-
|
|
43
|
+
// 排除的文件
|
|
44
|
+
exclude: ['src/shared/**/*.ts'],
|
|
70
45
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
import viteMultiPage from '@fchc8/vite-plugin-multi-page';
|
|
46
|
+
// 调试模式
|
|
47
|
+
debug: !isProduction || isCLI,
|
|
74
48
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
buildStrategies: {
|
|
82
|
-
// 现代浏览器策略
|
|
83
|
-
default: {
|
|
84
|
-
viteConfig: {
|
|
85
|
-
define: {
|
|
86
|
-
'process.env.BUILD_TYPE': '"modern"',
|
|
87
|
-
},
|
|
88
|
-
},
|
|
89
|
-
output: {
|
|
90
|
-
format: 'es',
|
|
91
|
-
entryFileNames: 'assets/[name]-[hash].js',
|
|
92
|
-
},
|
|
93
|
-
build: {
|
|
94
|
-
target: 'es2015',
|
|
95
|
-
minify: 'esbuild',
|
|
96
|
-
sourcemap: true,
|
|
97
|
-
},
|
|
49
|
+
// 构建策略
|
|
50
|
+
strategies: {
|
|
51
|
+
default: {
|
|
52
|
+
define: {
|
|
53
|
+
IS_DEFAULT: true,
|
|
54
|
+
API_BASE: isProduction ? '"https://api.example.com"' : '"http://localhost:3001/api"',
|
|
98
55
|
},
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
viteConfig: {
|
|
103
|
-
define: {
|
|
104
|
-
'process.env.BUILD_TYPE': '"legacy"',
|
|
105
|
-
},
|
|
106
|
-
},
|
|
107
|
-
output: {
|
|
108
|
-
format: 'iife',
|
|
109
|
-
entryFileNames: 'legacy/[name].js',
|
|
110
|
-
},
|
|
111
|
-
build: {
|
|
112
|
-
target: 'es5',
|
|
113
|
-
minify: 'terser',
|
|
114
|
-
sourcemap: false,
|
|
115
|
-
},
|
|
56
|
+
build: {
|
|
57
|
+
sourcemap: !isProduction,
|
|
58
|
+
minify: isProduction ? 'esbuild' : false,
|
|
116
59
|
},
|
|
60
|
+
},
|
|
117
61
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
build: {
|
|
129
|
-
target: 'es2018',
|
|
130
|
-
chunkSizeWarningLimit: 300,
|
|
131
|
-
},
|
|
62
|
+
mobile: {
|
|
63
|
+
define: {
|
|
64
|
+
IS_MOBILE: true,
|
|
65
|
+
API_BASE: isProduction
|
|
66
|
+
? '"https://mobile-api.example.com"'
|
|
67
|
+
: '"http://localhost:3001/mobile-api"',
|
|
68
|
+
},
|
|
69
|
+
build: {
|
|
70
|
+
target: ['es2015', 'chrome58', 'safari11'],
|
|
71
|
+
minify: isProduction ? 'terser' : false,
|
|
132
72
|
},
|
|
133
73
|
},
|
|
134
|
-
}
|
|
135
|
-
],
|
|
136
|
-
});
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
### 函数配置
|
|
140
|
-
|
|
141
|
-
```typescript
|
|
142
|
-
viteMultiPage({
|
|
143
|
-
entry: 'src/pages/**/*.{ts,js}',
|
|
74
|
+
},
|
|
144
75
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
76
|
+
// 页面配置函数
|
|
77
|
+
pageConfigs: context => {
|
|
78
|
+
// 根据文件路径判断应用的策略
|
|
79
|
+
if (context.relativePath.includes('/mobile/')) {
|
|
80
|
+
return {
|
|
81
|
+
strategy: 'mobile',
|
|
82
|
+
define: {
|
|
83
|
+
PAGE_NAME: context.pageName,
|
|
84
|
+
MOBILE_PAGE: true,
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
}
|
|
148
88
|
|
|
149
|
-
|
|
150
|
-
if (pageName.startsWith('admin')) {
|
|
89
|
+
// 默认策略
|
|
151
90
|
return {
|
|
152
91
|
strategy: 'default',
|
|
153
|
-
template: 'admin.html',
|
|
154
92
|
define: {
|
|
155
|
-
|
|
93
|
+
PAGE_NAME: context.pageName,
|
|
94
|
+
DEFAULT_PAGE: true,
|
|
156
95
|
},
|
|
157
96
|
};
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
// 移动端页面
|
|
161
|
-
if (relativePath.includes('/mobile/')) {
|
|
162
|
-
return {
|
|
163
|
-
strategy: 'mobile',
|
|
164
|
-
template: 'mobile.html',
|
|
165
|
-
define: {
|
|
166
|
-
'process.env.API_BASE': '"https://mobile-api.example.com"',
|
|
167
|
-
},
|
|
168
|
-
};
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
// 默认配置
|
|
172
|
-
return {
|
|
173
|
-
strategy: 'default',
|
|
174
|
-
};
|
|
175
|
-
},
|
|
176
|
-
});
|
|
177
|
-
```
|
|
178
|
-
|
|
179
|
-
### 对象配置与模式匹配
|
|
180
|
-
|
|
181
|
-
```typescript
|
|
182
|
-
viteMultiPage({
|
|
183
|
-
entry: 'src/pages/**/*.{ts,js}',
|
|
184
|
-
|
|
185
|
-
pageConfigs: {
|
|
186
|
-
// 精确匹配
|
|
187
|
-
home: {
|
|
188
|
-
strategy: 'default',
|
|
189
|
-
template: 'home.html',
|
|
190
97
|
},
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
'admin*': {
|
|
194
|
-
strategy: 'default',
|
|
195
|
-
template: 'admin.html',
|
|
196
|
-
},
|
|
197
|
-
|
|
198
|
-
// 模式匹配
|
|
199
|
-
'mobile-app': {
|
|
200
|
-
strategy: 'mobile',
|
|
201
|
-
match: ['**/mobile/**', '*mobile*'],
|
|
202
|
-
template: 'mobile.html',
|
|
203
|
-
},
|
|
204
|
-
},
|
|
205
|
-
});
|
|
98
|
+
};
|
|
99
|
+
};
|
|
206
100
|
```
|
|
207
101
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
### MultiPageOptions
|
|
102
|
+
### 2. 配置 Vite
|
|
211
103
|
|
|
212
|
-
|
|
213
|
-
| ----------------- | -------------------------------------------------- | -------------------------------------- | ---------------- |
|
|
214
|
-
| `entry` | `string` | `"src/**/*.{ts,js}"` | 入口文件匹配模式 |
|
|
215
|
-
| `template` | `string` | `"index.html"` | 默认 HTML 模板 |
|
|
216
|
-
| `exclude` | `string[]` | `["src/main.ts", "src/vite-env.d.ts"]` | 排除的文件 |
|
|
217
|
-
| `placeholder` | `string` | `"{{ENTRY_FILE}}"` | 模板中的占位符 |
|
|
218
|
-
| `debug` | `boolean` | `false` | 启用调试日志 |
|
|
219
|
-
| `buildStrategies` | `Record<string, BuildStrategy>` | `{}` | 构建策略定义 |
|
|
220
|
-
| `pageConfigs` | `Record<string, PageConfig> \| PageConfigFunction` | `{}` | 页面配置 |
|
|
221
|
-
|
|
222
|
-
### BuildStrategy
|
|
104
|
+
在 `vite.config.ts` 中添加插件:
|
|
223
105
|
|
|
224
106
|
```typescript
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
viteConfig?: Omit<UserConfig, 'plugins' | 'build'> & {
|
|
228
|
-
build?: BuildOptions;
|
|
229
|
-
};
|
|
230
|
-
|
|
231
|
-
// 输出配置
|
|
232
|
-
output?: {
|
|
233
|
-
format?: 'es' | 'cjs' | 'umd' | 'iife';
|
|
234
|
-
dir?: string;
|
|
235
|
-
entryFileNames?: string;
|
|
236
|
-
chunkFileNames?: string;
|
|
237
|
-
assetFileNames?: string;
|
|
238
|
-
globals?: Record<string, string>;
|
|
239
|
-
external?: string | string[] | ((id: string) => boolean);
|
|
240
|
-
};
|
|
241
|
-
|
|
242
|
-
// 构建配置
|
|
243
|
-
build?: {
|
|
244
|
-
target?: string | string[];
|
|
245
|
-
minify?: boolean | 'terser' | 'esbuild';
|
|
246
|
-
sourcemap?: boolean | 'inline' | 'hidden';
|
|
247
|
-
lib?: boolean | LibraryOptions;
|
|
248
|
-
cssCodeSplit?: boolean;
|
|
249
|
-
cssTarget?: string | string[];
|
|
250
|
-
rollupOptions?: any;
|
|
251
|
-
// ... 更多 Vite 构建选项
|
|
252
|
-
};
|
|
253
|
-
|
|
254
|
-
// 环境变量
|
|
255
|
-
define?: Record<string, any>;
|
|
107
|
+
import { defineConfig } from 'vite';
|
|
108
|
+
import viteMultiPage from 'vite-plugin-multi-page';
|
|
256
109
|
|
|
257
|
-
|
|
258
|
-
|
|
110
|
+
export default defineConfig({
|
|
111
|
+
plugins: [viteMultiPage()],
|
|
112
|
+
});
|
|
113
|
+
```
|
|
259
114
|
|
|
260
|
-
|
|
261
|
-
server?: ServerOptions;
|
|
115
|
+
### 3. 创建页面文件
|
|
262
116
|
|
|
263
|
-
|
|
264
|
-
css?: CSSOptions;
|
|
117
|
+
按照约定创建页面文件:
|
|
265
118
|
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
119
|
+
```
|
|
120
|
+
src/pages/
|
|
121
|
+
├── home.js # → /home.html
|
|
122
|
+
├── about.js # → /about.html
|
|
123
|
+
├── mobile/
|
|
124
|
+
│ └── main.ts # → /mobile.html (移动端策略)
|
|
125
|
+
└── admin/
|
|
126
|
+
└── main.ts # → /admin.html
|
|
269
127
|
```
|
|
270
128
|
|
|
271
|
-
|
|
129
|
+
## 页面发现规则
|
|
272
130
|
|
|
273
|
-
|
|
274
|
-
interface PageConfig {
|
|
275
|
-
strategy?: string; // 使用的构建策略
|
|
276
|
-
template?: string; // 页面模板
|
|
277
|
-
exclude?: string[]; // 排除规则
|
|
278
|
-
define?: Record<string, any>; // 环境变量
|
|
279
|
-
alias?: Record<string, string>; // 别名
|
|
280
|
-
build?: Partial<BuildStrategy['build']>; // 构建配置
|
|
281
|
-
match?: string | string[]; // 匹配模式
|
|
282
|
-
}
|
|
283
|
-
```
|
|
131
|
+
插件按照以下规则发现页面入口:
|
|
284
132
|
|
|
285
|
-
|
|
133
|
+
1. **第一级文件** (优先级 1): `src/pages/home.js` → `/home.html`
|
|
134
|
+
2. **目录main文件** (优先级 2): `src/pages/mobile/main.ts` → `/mobile.html`
|
|
286
135
|
|
|
287
|
-
|
|
136
|
+
**目录优先原则**: 如果同时存在 `src/pages/about.js` 和 `src/pages/about/main.ts`,将使用 `src/pages/about/main.ts`。
|
|
288
137
|
|
|
289
|
-
|
|
290
|
-
buildStrategies: {
|
|
291
|
-
admin: {
|
|
292
|
-
viteConfig: {
|
|
293
|
-
define: { 'process.env.APP_TYPE': '"admin"' }
|
|
294
|
-
},
|
|
295
|
-
build: {
|
|
296
|
-
target: 'es2015',
|
|
297
|
-
sourcemap: true
|
|
298
|
-
}
|
|
299
|
-
},
|
|
138
|
+
## 构建策略
|
|
300
139
|
|
|
301
|
-
|
|
302
|
-
viteConfig: {
|
|
303
|
-
define: { 'process.env.APP_TYPE': '"public"' }
|
|
304
|
-
},
|
|
305
|
-
build: {
|
|
306
|
-
target: 'es5',
|
|
307
|
-
minify: 'terser'
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
```
|
|
140
|
+
### 策略配置
|
|
312
141
|
|
|
313
|
-
|
|
142
|
+
策略配置支持所有 Vite 配置选项:
|
|
314
143
|
|
|
315
144
|
```typescript
|
|
316
|
-
|
|
145
|
+
strategies: {
|
|
317
146
|
mobile: {
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
optimizeDeps: { include: ['@mobile/utils'] }
|
|
147
|
+
define: {
|
|
148
|
+
IS_MOBILE: true,
|
|
321
149
|
},
|
|
322
150
|
build: {
|
|
323
|
-
target: '
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
}
|
|
329
|
-
```
|
|
330
|
-
|
|
331
|
-
### 3. 组件库开发
|
|
332
|
-
|
|
333
|
-
```typescript
|
|
334
|
-
buildStrategies: {
|
|
335
|
-
library: {
|
|
336
|
-
build: {
|
|
337
|
-
lib: {
|
|
338
|
-
entry: 'src/index.ts',
|
|
339
|
-
name: 'MyLibrary',
|
|
340
|
-
formats: ['es', 'umd']
|
|
151
|
+
target: ['es2015'],
|
|
152
|
+
minify: 'terser',
|
|
153
|
+
terserOptions: {
|
|
154
|
+
compress: {
|
|
155
|
+
drop_console: true,
|
|
156
|
+
},
|
|
341
157
|
},
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
}
|
|
158
|
+
},
|
|
159
|
+
// 其他 Vite 配置...
|
|
160
|
+
},
|
|
346
161
|
}
|
|
347
162
|
```
|
|
348
163
|
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
查看 `example/` 目录获取完整的示例项目,包含:
|
|
164
|
+
### 页面策略分配
|
|
352
165
|
|
|
353
|
-
|
|
354
|
-
- 移动端应用(兼容语法)
|
|
355
|
-
- 组件库文档
|
|
356
|
-
- 不同的 HTML 模板
|
|
357
|
-
- 函数配置示例
|
|
166
|
+
通过 `pageConfigs` 函数为页面分配策略:
|
|
358
167
|
|
|
359
|
-
|
|
168
|
+
```typescript
|
|
169
|
+
pageConfigs: context => {
|
|
170
|
+
const { pageName, relativePath } = context;
|
|
360
171
|
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
npm run example:build # 构建
|
|
365
|
-
npm run example:preview # 预览构建结果
|
|
366
|
-
|
|
367
|
-
# 方法二:手动设置
|
|
368
|
-
npm run build # 先构建插件
|
|
369
|
-
cd example
|
|
370
|
-
npm install # 安装示例依赖
|
|
371
|
-
npm run dev # 运行开发服务器
|
|
372
|
-
```
|
|
172
|
+
if (relativePath.includes('/mobile/')) {
|
|
173
|
+
return { strategy: 'mobile' };
|
|
174
|
+
}
|
|
373
175
|
|
|
374
|
-
|
|
176
|
+
if (pageName.startsWith('admin')) {
|
|
177
|
+
return { strategy: 'admin' };
|
|
178
|
+
}
|
|
375
179
|
|
|
376
|
-
|
|
180
|
+
return { strategy: 'default' };
|
|
181
|
+
};
|
|
182
|
+
```
|
|
377
183
|
|
|
378
|
-
|
|
379
|
-
- `/about.html` - 关于页面(默认策略)
|
|
380
|
-
- `/mobile.html` - 移动端页面(移动端模板)
|
|
184
|
+
## 命令行工具
|
|
381
185
|
|
|
382
|
-
|
|
186
|
+
### 批量构建
|
|
383
187
|
|
|
384
188
|
```bash
|
|
385
|
-
#
|
|
386
|
-
|
|
387
|
-
cd vite-plugin-multi-page
|
|
388
|
-
|
|
389
|
-
# 项目初始化
|
|
390
|
-
./scripts/setup.sh
|
|
391
|
-
|
|
392
|
-
# 开发模式
|
|
393
|
-
pnpm dev
|
|
394
|
-
|
|
395
|
-
# 类型检查
|
|
396
|
-
pnpm type-check
|
|
397
|
-
|
|
398
|
-
# 代码格式化
|
|
399
|
-
pnpm format
|
|
189
|
+
# 构建所有策略
|
|
190
|
+
npx vite-mp
|
|
400
191
|
|
|
401
|
-
#
|
|
402
|
-
|
|
192
|
+
# 传递额外的 Vite 参数
|
|
193
|
+
npx vite-mp --host --port 3000
|
|
403
194
|
|
|
404
|
-
#
|
|
405
|
-
|
|
195
|
+
# 启用调试模式
|
|
196
|
+
npx vite-mp --debug
|
|
406
197
|
```
|
|
407
198
|
|
|
408
|
-
|
|
199
|
+
### 开发服务器
|
|
409
200
|
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
MIT License
|
|
415
|
-
|
|
416
|
-
## 🔗 相关链接
|
|
201
|
+
```bash
|
|
202
|
+
# 启动开发服务器 (所有页面)
|
|
203
|
+
npm run dev
|
|
417
204
|
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
- [Prettier](https://prettier.io/)
|
|
422
|
-
- [Git Flow](https://nvie.com/posts/a-successful-git-branching-model/)
|
|
423
|
-
- [语义化版本](https://semver.org/lang/zh-CN/)
|
|
205
|
+
# 只显示特定策略的页面
|
|
206
|
+
npm run dev -- --strategy mobile
|
|
207
|
+
```
|
|
424
208
|
|
|
425
|
-
##
|
|
209
|
+
## 环境变量
|
|
426
210
|
|
|
427
|
-
|
|
211
|
+
- `VITE_BUILD_STRATEGY`: 指定单个策略构建
|
|
212
|
+
- `IS_MOBILE`: 移动端标识 (在 define 中配置)
|
|
213
|
+
- `API_BASE`: API 基础地址 (在 define 中配置)
|
|
428
214
|
|
|
429
|
-
|
|
215
|
+
## TypeScript 支持
|
|
430
216
|
|
|
431
|
-
|
|
217
|
+
插件完全支持 TypeScript 配置文件:
|
|
432
218
|
|
|
433
219
|
```typescript
|
|
434
|
-
//
|
|
435
|
-
|
|
436
|
-
const { pageName, relativePath } = context;
|
|
220
|
+
// multipage.config.ts
|
|
221
|
+
import type { ConfigFunction } from 'vite-plugin-multi-page';
|
|
437
222
|
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
'process.env.API_BASE': '"https://mobile-api.com"',
|
|
444
|
-
},
|
|
445
|
-
};
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
return { template: 'index.html' };
|
|
223
|
+
const config: ConfigFunction = context => {
|
|
224
|
+
return {
|
|
225
|
+
entry: 'src/pages/**/*.{ts,js}',
|
|
226
|
+
// ... 其他配置
|
|
227
|
+
};
|
|
449
228
|
};
|
|
450
|
-
```
|
|
451
|
-
|
|
452
|
-
#### 2. 环境变量同步
|
|
453
|
-
|
|
454
|
-
**构建时**:注入到 Vite 配置
|
|
455
229
|
|
|
456
|
-
|
|
457
|
-
// 环境变量会被 Vite 编译时替换
|
|
458
|
-
config.define = { 'process.env.API_BASE': '"https://api.com"' };
|
|
230
|
+
export default config;
|
|
459
231
|
```
|
|
460
232
|
|
|
461
|
-
|
|
233
|
+
## API 参考
|
|
462
234
|
|
|
463
|
-
|
|
464
|
-
<!-- 开发服务器自动注入到页面 -->
|
|
465
|
-
<script>
|
|
466
|
-
// 页面级环境变量
|
|
467
|
-
window['process.env.API_BASE'] = 'https://api.com';
|
|
468
|
-
</script>
|
|
469
|
-
```
|
|
235
|
+
### 配置选项
|
|
470
236
|
|
|
471
|
-
|
|
237
|
+
| 选项 | 类型 | 默认值 | 描述 |
|
|
238
|
+
| ------------- | -------------------------- | -------------------------- | ---------------- |
|
|
239
|
+
| `entry` | `string` | `'src/pages/**/*.{ts,js}'` | 页面入口匹配规则 |
|
|
240
|
+
| `template` | `string` | `'index.html'` | HTML 模板文件 |
|
|
241
|
+
| `placeholder` | `string` | `'{{ENTRY_FILE}}'` | 模板占位符 |
|
|
242
|
+
| `exclude` | `string[]` | `[]` | 排除的文件模式 |
|
|
243
|
+
| `debug` | `boolean` | `false` | 启用调试日志 |
|
|
244
|
+
| `strategies` | `Record<string, Strategy>` | `{}` | 构建策略配置 |
|
|
245
|
+
| `pageConfigs` | `Function \| Object` | `{}` | 页面配置 |
|
|
472
246
|
|
|
473
|
-
|
|
474
|
-
// 开发模式
|
|
475
|
-
if (pageConfig?.template) {
|
|
476
|
-
const templatePath = path.resolve(pageConfig.template);
|
|
477
|
-
// 使用指定模板
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
// 构建模式
|
|
481
|
-
const templateFile = pageConfig?.template || options.template;
|
|
482
|
-
// 使用相同的模板选择逻辑
|
|
483
|
-
```
|
|
484
|
-
|
|
485
|
-
#### 4. 构建策略应用
|
|
247
|
+
### 工具函数
|
|
486
248
|
|
|
487
249
|
```typescript
|
|
488
|
-
|
|
489
|
-
if (strategy.viteConfig) {
|
|
490
|
-
config.css = { ...config.css, ...strategy.viteConfig.css };
|
|
491
|
-
config.define = { ...config.define, ...strategy.define };
|
|
492
|
-
}
|
|
250
|
+
import { defineConfig, defineConfigTransform } from 'vite-plugin-multi-page';
|
|
493
251
|
|
|
494
|
-
//
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
}
|
|
498
|
-
```
|
|
499
|
-
|
|
500
|
-
### 验证一致性
|
|
252
|
+
// 定义配置
|
|
253
|
+
export default defineConfig(context => ({
|
|
254
|
+
// 配置选项
|
|
255
|
+
}));
|
|
501
256
|
|
|
502
|
-
|
|
257
|
+
// 配置转换
|
|
258
|
+
const transform = defineConfigTransform((config, context) => {
|
|
259
|
+
// 修改配置
|
|
260
|
+
return config;
|
|
261
|
+
});
|
|
262
|
+
```
|
|
503
263
|
|
|
504
|
-
|
|
505
|
-
# 开发模式
|
|
506
|
-
cd example
|
|
507
|
-
npm run dev
|
|
508
|
-
# 访问 http://localhost:5173/mobile.html
|
|
264
|
+
## 示例项目
|
|
509
265
|
|
|
510
|
-
|
|
511
|
-
npm run build
|
|
512
|
-
npm run preview
|
|
513
|
-
# 访问 http://localhost:4173/mobile.html
|
|
266
|
+
查看 [example](./example) 目录获取完整的示例项目。
|
|
514
267
|
|
|
515
|
-
|
|
516
|
-
```
|
|
268
|
+
## 许可证
|
|
517
269
|
|
|
518
|
-
|
|
270
|
+
MIT License
|