@fchc8/vite-plugin-multi-page 1.1.1 → 1.3.1
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 +191 -278
- 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 +12 -4
package/README.md
CHANGED
|
@@ -1,357 +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 @fchc8/vite-plugin-multi-page
|
|
23
|
-
# 或
|
|
24
|
-
yarn add @fchc8/vite-plugin-multi-page
|
|
25
|
-
# 或
|
|
26
|
-
pnpm add @fchc8/vite-plugin-multi-page
|
|
19
|
+
npm install @fchc8/vite-plugin-multi-page --save-dev
|
|
27
20
|
```
|
|
28
21
|
|
|
29
|
-
##
|
|
22
|
+
## 快速开始
|
|
30
23
|
|
|
31
|
-
###
|
|
24
|
+
### 1. 创建配置文件
|
|
25
|
+
|
|
26
|
+
创建 `multipage.config.ts` 或 `multipage.config.js`:
|
|
32
27
|
|
|
33
28
|
```typescript
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
29
|
+
export default context => {
|
|
30
|
+
const { mode, command, isCLI } = context;
|
|
31
|
+
const isProduction = mode === 'production';
|
|
37
32
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
entry: 'src/pages/**/*.{ts,js}',
|
|
42
|
-
template: 'index.html',
|
|
43
|
-
exclude: ['src/main.ts'],
|
|
44
|
-
debug: true,
|
|
45
|
-
}),
|
|
46
|
-
],
|
|
47
|
-
});
|
|
48
|
-
```
|
|
33
|
+
return {
|
|
34
|
+
// 页面入口匹配规则
|
|
35
|
+
entry: 'src/pages/**/*.{ts,js}',
|
|
49
36
|
|
|
50
|
-
|
|
37
|
+
// HTML 模板
|
|
38
|
+
template: 'index.html',
|
|
51
39
|
|
|
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
|
-
```
|
|
40
|
+
// 模板占位符
|
|
41
|
+
placeholder: '{{ENTRY_FILE}}',
|
|
66
42
|
|
|
67
|
-
|
|
43
|
+
// 排除的文件
|
|
44
|
+
exclude: ['src/shared/**/*.ts'],
|
|
68
45
|
|
|
69
|
-
|
|
46
|
+
// 调试模式
|
|
47
|
+
debug: !isProduction || isCLI,
|
|
70
48
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
viteMultiPage({
|
|
78
|
-
entry: 'src/pages/**/*.{ts,js}',
|
|
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
|
-
});
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
};
|
|
177
100
|
```
|
|
178
101
|
|
|
179
|
-
###
|
|
102
|
+
### 2. 配置 Vite
|
|
180
103
|
|
|
181
|
-
|
|
182
|
-
viteMultiPage({
|
|
183
|
-
entry: 'src/pages/**/*.{ts,js}',
|
|
184
|
-
|
|
185
|
-
pageConfigs: {
|
|
186
|
-
// 精确匹配
|
|
187
|
-
home: {
|
|
188
|
-
strategy: 'default',
|
|
189
|
-
template: 'home.html',
|
|
190
|
-
},
|
|
104
|
+
在 `vite.config.ts` 中添加插件:
|
|
191
105
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
template: 'admin.html',
|
|
196
|
-
},
|
|
106
|
+
```typescript
|
|
107
|
+
import { defineConfig } from 'vite';
|
|
108
|
+
import viteMultiPage from 'vite-plugin-multi-page';
|
|
197
109
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
strategy: 'mobile',
|
|
201
|
-
match: ['**/mobile/**', '*mobile*'],
|
|
202
|
-
template: 'mobile.html',
|
|
203
|
-
},
|
|
204
|
-
},
|
|
110
|
+
export default defineConfig({
|
|
111
|
+
plugins: [viteMultiPage()],
|
|
205
112
|
});
|
|
206
113
|
```
|
|
207
114
|
|
|
208
|
-
|
|
115
|
+
### 3. 创建页面文件
|
|
209
116
|
|
|
210
|
-
|
|
117
|
+
按照约定创建页面文件:
|
|
211
118
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
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
|
|
127
|
+
```
|
|
221
128
|
|
|
222
|
-
|
|
129
|
+
## 页面发现规则
|
|
223
130
|
|
|
224
|
-
|
|
225
|
-
interface BuildStrategy {
|
|
226
|
-
// 完整的 Vite 配置支持
|
|
227
|
-
viteConfig?: Omit<UserConfig, 'plugins' | 'build'> & {
|
|
228
|
-
build?: BuildOptions;
|
|
229
|
-
};
|
|
131
|
+
插件按照以下规则发现页面入口:
|
|
230
132
|
|
|
231
|
-
|
|
232
|
-
|
|
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
|
-
};
|
|
133
|
+
1. **第一级文件** (优先级 1): `src/pages/home.js` → `/home.html`
|
|
134
|
+
2. **目录main文件** (优先级 2): `src/pages/mobile/main.ts` → `/mobile.html`
|
|
253
135
|
|
|
254
|
-
|
|
255
|
-
define?: Record<string, any>;
|
|
136
|
+
**目录优先原则**: 如果同时存在 `src/pages/about.js` 和 `src/pages/about/main.ts`,将使用 `src/pages/about/main.ts`。
|
|
256
137
|
|
|
257
|
-
|
|
258
|
-
alias?: Record<string, string>;
|
|
138
|
+
## 构建策略
|
|
259
139
|
|
|
260
|
-
|
|
261
|
-
server?: ServerOptions;
|
|
140
|
+
### 策略配置
|
|
262
141
|
|
|
263
|
-
|
|
264
|
-
css?: CSSOptions;
|
|
142
|
+
策略配置支持所有 Vite 配置选项:
|
|
265
143
|
|
|
266
|
-
|
|
267
|
-
|
|
144
|
+
```typescript
|
|
145
|
+
strategies: {
|
|
146
|
+
mobile: {
|
|
147
|
+
define: {
|
|
148
|
+
IS_MOBILE: true,
|
|
149
|
+
},
|
|
150
|
+
build: {
|
|
151
|
+
target: ['es2015'],
|
|
152
|
+
minify: 'terser',
|
|
153
|
+
terserOptions: {
|
|
154
|
+
compress: {
|
|
155
|
+
drop_console: true,
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
},
|
|
159
|
+
// 其他 Vite 配置...
|
|
160
|
+
},
|
|
268
161
|
}
|
|
269
162
|
```
|
|
270
163
|
|
|
271
|
-
###
|
|
164
|
+
### 页面策略分配
|
|
165
|
+
|
|
166
|
+
通过 `pageConfigs` 函数为页面分配策略:
|
|
272
167
|
|
|
273
168
|
```typescript
|
|
274
|
-
|
|
275
|
-
|
|
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
|
-
```
|
|
169
|
+
pageConfigs: context => {
|
|
170
|
+
const { pageName, relativePath } = context;
|
|
284
171
|
|
|
285
|
-
|
|
172
|
+
if (relativePath.includes('/mobile/')) {
|
|
173
|
+
return { strategy: 'mobile' };
|
|
174
|
+
}
|
|
286
175
|
|
|
287
|
-
|
|
176
|
+
if (pageName.startsWith('admin')) {
|
|
177
|
+
return { strategy: 'admin' };
|
|
178
|
+
}
|
|
288
179
|
|
|
289
|
-
|
|
180
|
+
return { strategy: 'default' };
|
|
181
|
+
};
|
|
182
|
+
```
|
|
290
183
|
|
|
291
|
-
##
|
|
184
|
+
## 命令行工具
|
|
292
185
|
|
|
293
|
-
|
|
186
|
+
### 批量构建
|
|
294
187
|
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
-
|
|
298
|
-
- 不同的 HTML 模板
|
|
299
|
-
- 函数配置示例
|
|
188
|
+
```bash
|
|
189
|
+
# 构建所有策略
|
|
190
|
+
npx vite-mp
|
|
300
191
|
|
|
301
|
-
|
|
192
|
+
# 传递额外的 Vite 参数
|
|
193
|
+
npx vite-mp --host --port 3000
|
|
302
194
|
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
npm run example:dev # 开发模式
|
|
306
|
-
npm run example:build # 构建
|
|
307
|
-
npm run example:preview # 预览构建结果
|
|
308
|
-
|
|
309
|
-
# 方法二:手动设置
|
|
310
|
-
npm run build # 先构建插件
|
|
311
|
-
cd example
|
|
312
|
-
npm install # 安装示例依赖
|
|
313
|
-
npm run dev # 运行开发服务器
|
|
195
|
+
# 启用调试模式
|
|
196
|
+
npx vite-mp --debug
|
|
314
197
|
```
|
|
315
198
|
|
|
316
|
-
|
|
199
|
+
### 开发服务器
|
|
317
200
|
|
|
318
201
|
```bash
|
|
319
|
-
#
|
|
320
|
-
|
|
321
|
-
cd vite-plugin-multi-page
|
|
202
|
+
# 启动开发服务器 (所有页面)
|
|
203
|
+
npm run dev
|
|
322
204
|
|
|
323
|
-
#
|
|
324
|
-
|
|
205
|
+
# 只显示特定策略的页面
|
|
206
|
+
npm run dev -- --strategy mobile
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
## 环境变量
|
|
325
210
|
|
|
326
|
-
|
|
327
|
-
|
|
211
|
+
- `VITE_BUILD_STRATEGY`: 指定单个策略构建
|
|
212
|
+
- `IS_MOBILE`: 移动端标识 (在 define 中配置)
|
|
213
|
+
- `API_BASE`: API 基础地址 (在 define 中配置)
|
|
328
214
|
|
|
329
|
-
|
|
330
|
-
pnpm type-check
|
|
215
|
+
## TypeScript 支持
|
|
331
216
|
|
|
332
|
-
|
|
333
|
-
pnpm format
|
|
217
|
+
插件完全支持 TypeScript 配置文件:
|
|
334
218
|
|
|
335
|
-
|
|
336
|
-
|
|
219
|
+
```typescript
|
|
220
|
+
// multipage.config.ts
|
|
221
|
+
import type { ConfigFunction } from 'vite-plugin-multi-page';
|
|
222
|
+
|
|
223
|
+
const config: ConfigFunction = context => {
|
|
224
|
+
return {
|
|
225
|
+
entry: 'src/pages/**/*.{ts,js}',
|
|
226
|
+
// ... 其他配置
|
|
227
|
+
};
|
|
228
|
+
};
|
|
337
229
|
|
|
338
|
-
|
|
339
|
-
pnpm build
|
|
230
|
+
export default config;
|
|
340
231
|
```
|
|
341
232
|
|
|
342
|
-
##
|
|
233
|
+
## API 参考
|
|
343
234
|
|
|
344
|
-
|
|
235
|
+
### 配置选项
|
|
345
236
|
|
|
346
|
-
|
|
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` | `{}` | 页面配置 |
|
|
347
246
|
|
|
348
|
-
|
|
247
|
+
### 工具函数
|
|
248
|
+
|
|
249
|
+
```typescript
|
|
250
|
+
import { defineConfig, defineConfigTransform } from 'vite-plugin-multi-page';
|
|
251
|
+
|
|
252
|
+
// 定义配置
|
|
253
|
+
export default defineConfig(context => ({
|
|
254
|
+
// 配置选项
|
|
255
|
+
}));
|
|
256
|
+
|
|
257
|
+
// 配置转换
|
|
258
|
+
const transform = defineConfigTransform((config, context) => {
|
|
259
|
+
// 修改配置
|
|
260
|
+
return config;
|
|
261
|
+
});
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
## 示例项目
|
|
349
265
|
|
|
350
|
-
|
|
266
|
+
查看 [example](./example) 目录获取完整的示例项目。
|
|
351
267
|
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
- [Prettier](https://prettier.io/)
|
|
356
|
-
- [Git Flow](https://nvie.com/posts/a-successful-git-branching-model/)
|
|
357
|
-
- [语义化版本](https://semver.org/lang/zh-CN/)
|
|
268
|
+
## 许可证
|
|
269
|
+
|
|
270
|
+
MIT License
|