@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.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
- - 📱 **响应式模板**:不同页面可使用不同的 HTML 模板
13
- - 🔧 **完整 Vite 集成**:继承所有 Vite 配置选项
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
- // vite.config.ts
35
- import { defineConfig } from 'vite';
36
- import viteMultiPage from '@fchc8/vite-plugin-multi-page';
29
+ export default context => {
30
+ const { mode, command, isCLI } = context;
31
+ const isProduction = mode === 'production';
37
32
 
38
- export default defineConfig({
39
- plugins: [
40
- viteMultiPage({
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
- project/
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
- ```typescript
72
- import { defineConfig } from 'vite';
73
- import viteMultiPage from '@fchc8/vite-plugin-multi-page';
74
-
75
- export default defineConfig({
76
- plugins: [
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
- legacy: {
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
- mobile: {
120
- viteConfig: {
121
- css: {
122
- devSourcemap: true,
123
- },
124
- optimizeDeps: {
125
- include: ['mobile-utils'],
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
- pageConfigs: context => {
147
- const { pageName, filePath, relativePath } = context;
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
- 'process.env.API_BASE': '"https://admin-api.example.com"',
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
- ```typescript
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
- 'admin*': {
194
- strategy: 'default',
195
- template: 'admin.html',
196
- },
106
+ ```typescript
107
+ import { defineConfig } from 'vite';
108
+ import viteMultiPage from 'vite-plugin-multi-page';
197
109
 
198
- // 模式匹配
199
- 'mobile-app': {
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
- ### MultiPageOptions
117
+ 按照约定创建页面文件:
211
118
 
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` | `{}` | 页面配置 |
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
- ### BuildStrategy
129
+ ## 页面发现规则
223
130
 
224
- ```typescript
225
- interface BuildStrategy {
226
- // 完整的 Vite 配置支持
227
- viteConfig?: Omit<UserConfig, 'plugins' | 'build'> & {
228
- build?: BuildOptions;
229
- };
131
+ 插件按照以下规则发现页面入口:
230
132
 
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
- };
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
- // CSS 配置
264
- css?: CSSOptions;
142
+ 策略配置支持所有 Vite 配置选项:
265
143
 
266
- // 依赖优化
267
- optimizeDeps?: DepOptimizationOptions;
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
- ### PageConfig
164
+ ### 页面策略分配
165
+
166
+ 通过 `pageConfigs` 函数为页面分配策略:
272
167
 
273
168
  ```typescript
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
- ```
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
- 查看 `example/` 目录获取完整的示例项目,包含:
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
- ```bash
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
- git clone https://github.com/fchc7/vite-plugin-multi-page.git
321
- cd vite-plugin-multi-page
202
+ # 启动开发服务器 (所有页面)
203
+ npm run dev
322
204
 
323
- # 项目初始化
324
- ./scripts/setup.sh
205
+ # 只显示特定策略的页面
206
+ npm run dev -- --strategy mobile
207
+ ```
208
+
209
+ ## 环境变量
325
210
 
326
- # 开发模式
327
- pnpm dev
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
- pnpm lint
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
- 欢迎提交 Issue 和 Pull Request!
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
- MIT License
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
- - [Vite 官方文档](https://vitejs.dev/)
353
- - [TypeScript](https://www.typescriptlang.org/)
354
- - [ESLint](https://eslint.org/)
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