@fchc8/vite-plugin-multi-page 1.1.1 → 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 CHANGED
@@ -1,424 +1,270 @@
1
1
  # vite-plugin-multi-page
2
2
 
3
- > English Documentation | [中文文档](./README.md)
3
+ > 中文文档 | [中文文档](./README.md)
4
4
 
5
- A powerful Vite plugin for building multi-page applications with smart file routing and multiple build strategies.
5
+ A powerful Vite plugin for building multi-page applications with smart file routing and multi-strategy builds.
6
6
 
7
- ## Features
7
+ ## Features
8
8
 
9
- - 🚀 **Automatic Page Discovery**: Automatically scan and configure entry pages based on file patterns
10
- - 🎯 **Multiple Build Strategies**: Configure different build options and optimization strategies for different pages
11
- - 🧩 **Flexible Configuration**: Support object configuration, function configuration, and pattern matching
12
- - 📱 **Responsive Templates**: Different pages can use different HTML templates
13
- - 🔧 **Full Vite Integration**: Inherit all Vite configuration options
14
- - 🌍 **Environment Variables Support**: Page-level and strategy-level environment variable definitions
15
- - 🎨 **Developer Friendly**: Detailed debug logs and hot reload support
9
+ - 🎯 **Multi-page support**: Automatically discover page entry files
10
+ - 🔧 **Multi-strategy builds**: Support configuring different build strategies for different pages
11
+ - 📝 **TypeScript configuration**: Support TypeScript configuration files
12
+ - 🚀 **CLI tool**: Provide a command-line batch build tool
13
+ - 🔄 **Hot reload**: Development server supports page hot reload
14
+ - 📦 **Smart merge**: Automatically merge multi-strategy build results
16
15
 
17
- ## 📦 Installation
16
+ ## Installation
18
17
 
19
18
  ```bash
20
- npm install @fchc8/vite-plugin-multi-page
21
- # or
22
- yarn add @fchc8/vite-plugin-multi-page
23
- # or
24
- pnpm add @fchc8/vite-plugin-multi-page
19
+ npm install vite-plugin-multi-page --save-dev
25
20
  ```
26
21
 
27
- ## 🚀 Quick Start
22
+ ## Quick Start
28
23
 
29
- ### Basic Usage
24
+ ### 1. Create a configuration file
25
+
26
+ Create a `multipage.config.ts` or `multipage.config.js`:
30
27
 
31
28
  ```typescript
32
- // vite.config.ts
33
- import { defineConfig } from 'vite';
34
- import viteMultiPage from '@fchc8/vite-plugin-multi-page';
29
+ export default context => {
30
+ const { mode, command, isCLI } = context;
31
+ const isProduction = mode === 'production';
35
32
 
36
- export default defineConfig({
37
- plugins: [
38
- viteMultiPage({
39
- entry: 'src/pages/**/*.{ts,js}',
40
- template: 'index.html',
41
- exclude: ['src/main.ts'],
42
- debug: true,
43
- }),
44
- ],
45
- });
46
- ```
33
+ return {
34
+ // Page entry matching rule
35
+ entry: 'src/pages/**/*.{ts,js}',
47
36
 
48
- ### Project Structure Example
37
+ // HTML template
38
+ template: 'index.html',
49
39
 
50
- ```
51
- project/
52
- ├── src/
53
- │ └── pages/
54
- │ ├── home.ts → /home.html
55
- │ ├── about.ts → /about.html
56
- │ ├── admin/
57
- │ │ └── dashboard.ts → /dashboard.html
58
- │ └── mobile/
59
- │ └── app.js → /app.html
60
- ├── index.html
61
- ├── admin.html
62
- └── mobile.html
63
- ```
40
+ // Template placeholder
41
+ placeholder: '{{ENTRY_FILE}}',
64
42
 
65
- ## 🎯 Advanced Configuration
43
+ // Excluded files
44
+ exclude: ['src/shared/**/*.ts'],
66
45
 
67
- ### Multiple Build Strategies
46
+ // Debug mode
47
+ debug: !isProduction || isCLI,
68
48
 
69
- ```typescript
70
- import { defineConfig } from 'vite';
71
- import viteMultiPage from '@fchc8/vite-plugin-multi-page';
72
-
73
- export default defineConfig({
74
- plugins: [
75
- viteMultiPage({
76
- entry: 'src/pages/**/*.{ts,js}',
77
-
78
- // Define build strategies
79
- buildStrategies: {
80
- // Modern browser strategy
81
- default: {
82
- viteConfig: {
83
- define: {
84
- 'process.env.BUILD_TYPE': '"modern"',
85
- },
86
- },
87
- output: {
88
- format: 'es',
89
- entryFileNames: 'assets/[name]-[hash].js',
90
- },
91
- build: {
92
- target: 'es2015',
93
- minify: 'esbuild',
94
- sourcemap: true,
95
- },
49
+ // Build strategy
50
+ strategies: {
51
+ default: {
52
+ define: {
53
+ IS_DEFAULT: true,
54
+ API_BASE: isProduction ? '"https://api.example.com"' : '"http://localhost:3001/api"',
96
55
  },
97
-
98
- // Legacy compatibility strategy
99
- legacy: {
100
- viteConfig: {
101
- define: {
102
- 'process.env.BUILD_TYPE': '"legacy"',
103
- },
104
- },
105
- output: {
106
- format: 'iife',
107
- entryFileNames: 'legacy/[name].js',
108
- },
109
- build: {
110
- target: 'es5',
111
- minify: 'terser',
112
- sourcemap: false,
113
- },
56
+ build: {
57
+ sourcemap: !isProduction,
58
+ minify: isProduction ? 'esbuild' : false,
114
59
  },
60
+ },
115
61
 
116
- // Mobile optimization strategy
117
- mobile: {
118
- viteConfig: {
119
- css: {
120
- devSourcemap: true,
121
- },
122
- optimizeDeps: {
123
- include: ['mobile-utils'],
124
- },
125
- },
126
- build: {
127
- target: 'es2018',
128
- chunkSizeWarningLimit: 300,
129
- },
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,
130
72
  },
131
73
  },
132
- }),
133
- ],
134
- });
135
- ```
136
-
137
- ### Function Configuration
138
-
139
- ```typescript
140
- viteMultiPage({
141
- entry: 'src/pages/**/*.{ts,js}',
74
+ },
142
75
 
143
- // Use function for dynamic configuration
144
- pageConfigs: context => {
145
- const { pageName, filePath, relativePath } = context;
76
+ // Page configuration function
77
+ pageConfigs: context => {
78
+ // Determine the application strategy based on the file path
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
+ }
146
88
 
147
- // Admin pages
148
- if (pageName.startsWith('admin')) {
89
+ // Default strategy
149
90
  return {
150
91
  strategy: 'default',
151
- template: 'admin.html',
152
92
  define: {
153
- 'process.env.API_BASE': '"https://admin-api.example.com"',
93
+ PAGE_NAME: context.pageName,
94
+ DEFAULT_PAGE: true,
154
95
  },
155
96
  };
156
- }
157
-
158
- // Mobile pages
159
- if (relativePath.includes('/mobile/')) {
160
- return {
161
- strategy: 'mobile',
162
- template: 'mobile.html',
163
- define: {
164
- 'process.env.API_BASE': '"https://mobile-api.example.com"',
165
- },
166
- };
167
- }
168
-
169
- // Default configuration
170
- return {
171
- strategy: 'default',
172
- };
173
- },
174
- });
97
+ },
98
+ };
99
+ };
175
100
  ```
176
101
 
177
- ### Object Configuration with Pattern Matching
102
+ ### 2. Configure Vite
178
103
 
179
- ```typescript
180
- viteMultiPage({
181
- entry: 'src/pages/**/*.{ts,js}',
182
-
183
- pageConfigs: {
184
- // Exact match
185
- home: {
186
- strategy: 'default',
187
- template: 'home.html',
188
- },
104
+ Add the plugin in `vite.config.ts`:
189
105
 
190
- // Wildcard match
191
- 'admin*': {
192
- strategy: 'default',
193
- template: 'admin.html',
194
- },
106
+ ```typescript
107
+ import { defineConfig } from 'vite';
108
+ import viteMultiPage from 'vite-plugin-multi-page';
195
109
 
196
- // Pattern match
197
- 'mobile-app': {
198
- strategy: 'mobile',
199
- match: ['**/mobile/**', '*mobile*'],
200
- template: 'mobile.html',
201
- },
202
- },
110
+ export default defineConfig({
111
+ plugins: [viteMultiPage()],
203
112
  });
204
113
  ```
205
114
 
206
- ## 📋 Configuration Options
207
-
208
- ### MultiPageOptions
209
-
210
- | Option | Type | Default | Description |
211
- | ----------------- | -------------------------------------------------- | -------------------------------------- | --------------------------- |
212
- | `entry` | `string` | `"src/**/*.{ts,js}"` | Entry file matching pattern |
213
- | `template` | `string` | `"index.html"` | Default HTML template |
214
- | `exclude` | `string[]` | `["src/main.ts", "src/vite-env.d.ts"]` | Files to exclude |
215
- | `placeholder` | `string` | `"{{ENTRY_FILE}}"` | Placeholder in template |
216
- | `debug` | `boolean` | `false` | Enable debug logs |
217
- | `buildStrategies` | `Record<string, BuildStrategy>` | `{}` | Build strategy definitions |
218
- | `pageConfigs` | `Record<string, PageConfig> \| PageConfigFunction` | `{}` | Page configurations |
115
+ ### 3. Create page files
219
116
 
220
- ### BuildStrategy
117
+ Create page files according to the convention:
221
118
 
222
- ```typescript
223
- interface BuildStrategy {
224
- // Full Vite configuration support
225
- viteConfig?: Omit<UserConfig, 'plugins' | 'build'> & {
226
- build?: BuildOptions;
227
- };
119
+ ```
120
+ src/pages/
121
+ ├── home.js # /home.html
122
+ ├── about.js # /about.html
123
+ ├── mobile/
124
+ │ └── main.ts # → /mobile.html (mobile strategy)
125
+ └── admin/
126
+ └── main.ts # → /admin.html
127
+ ```
228
128
 
229
- // Output configuration
230
- output?: {
231
- format?: 'es' | 'cjs' | 'umd' | 'iife';
232
- dir?: string;
233
- entryFileNames?: string;
234
- chunkFileNames?: string;
235
- assetFileNames?: string;
236
- globals?: Record<string, string>;
237
- external?: string | string[] | ((id: string) => boolean);
238
- };
129
+ ## Page discovery rules
239
130
 
240
- // Build configuration
241
- build?: {
242
- target?: string | string[];
243
- minify?: boolean | 'terser' | 'esbuild';
244
- sourcemap?: boolean | 'inline' | 'hidden';
245
- lib?: boolean | LibraryOptions;
246
- cssCodeSplit?: boolean;
247
- cssTarget?: string | string[];
248
- rollupOptions?: any;
249
- // ... more Vite build options
250
- };
131
+ The plugin discovers page entries according to the following rules:
251
132
 
252
- // Environment variables
253
- define?: Record<string, any>;
133
+ 1. **First-level files** (priority 1): `src/pages/home.js` → `/home.html`
134
+ 2. **Directory main files** (priority 2): `src/pages/mobile/main.ts` → `/mobile.html`
254
135
 
255
- // Alias configuration
256
- alias?: Record<string, string>;
136
+ **Directory priority rule**: If both `src/pages/about.js` and `src/pages/about/main.ts` exist, `src/pages/about/main.ts` will be used.
257
137
 
258
- // Server configuration
259
- server?: ServerOptions;
138
+ ## Build strategies
260
139
 
261
- // CSS configuration
262
- css?: CSSOptions;
140
+ ### Strategy configuration
263
141
 
264
- // Dependency optimization
265
- optimizeDeps?: DepOptimizationOptions;
266
- }
267
- ```
268
-
269
- ### PageConfig
142
+ 策略配置支持所有 Vite 配置选项:
270
143
 
271
144
  ```typescript
272
- interface PageConfig {
273
- strategy?: string; // Build strategy to use
274
- template?: string; // Page template
275
- exclude?: string[]; // Exclude rules
276
- define?: Record<string, any>; // Environment variables
277
- alias?: Record<string, string>; // Aliases
278
- build?: Partial<BuildStrategy['build']>; // Build configuration
279
- match?: string | string[]; // Match patterns
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
+ // Other Vite configurations...
160
+ },
280
161
  }
281
162
  ```
282
163
 
283
- ## 🌟 Use Cases
164
+ ### Page strategy assignment
284
165
 
285
- ### 1. Enterprise Multi-Page Application
166
+ Assign strategies to pages through the `pageConfigs` function:
286
167
 
287
168
  ```typescript
288
- buildStrategies: {
289
- admin: {
290
- viteConfig: {
291
- define: { 'process.env.APP_TYPE': '"admin"' }
292
- },
293
- build: {
294
- target: 'es2015',
295
- sourcemap: true
296
- }
297
- },
169
+ pageConfigs: context => {
170
+ const { pageName, relativePath } = context;
298
171
 
299
- public: {
300
- viteConfig: {
301
- define: { 'process.env.APP_TYPE': '"public"' }
302
- },
303
- build: {
304
- target: 'es5',
305
- minify: 'terser'
306
- }
172
+ if (relativePath.includes('/mobile/')) {
173
+ return { strategy: 'mobile' };
307
174
  }
308
- }
309
- ```
310
-
311
- ### 2. Mobile Optimization
312
175
 
313
- ```typescript
314
- buildStrategies: {
315
- mobile: {
316
- viteConfig: {
317
- css: { devSourcemap: true },
318
- optimizeDeps: { include: ['@mobile/utils'] }
319
- },
320
- build: {
321
- target: 'es2018',
322
- chunkSizeWarningLimit: 300,
323
- cssCodeSplit: true
324
- }
176
+ if (pageName.startsWith('admin')) {
177
+ return { strategy: 'admin' };
325
178
  }
326
- }
179
+
180
+ return { strategy: 'default' };
181
+ };
327
182
  ```
328
183
 
329
- ### 3. Component Library Development
184
+ ## 命令行工具
330
185
 
331
- ```typescript
332
- buildStrategies: {
333
- library: {
334
- build: {
335
- lib: {
336
- entry: 'src/index.ts',
337
- name: 'MyLibrary',
338
- formats: ['es', 'umd']
339
- },
340
- minify: false,
341
- sourcemap: true
342
- }
343
- }
344
- }
345
- ```
186
+ ### 批量构建
346
187
 
347
- ## 📱 Example Project
188
+ ```bash
189
+ # Build all strategies
190
+ npx vite-mp
348
191
 
349
- Check the `example/` directory for a complete example project including:
192
+ # Pass additional Vite parameters
193
+ npx vite-mp --host --port 3000
350
194
 
351
- - Admin dashboard (modern syntax)
352
- - Mobile application (compatible syntax)
353
- - Component library documentation
354
- - Different HTML templates
355
- - Function configuration examples
195
+ # Enable debug mode
196
+ npx vite-mp --debug
197
+ ```
356
198
 
357
- ### Quick Start
199
+ ### 开发服务器
358
200
 
359
201
  ```bash
360
- # Method 1: Use setup script (recommended)
361
- ./setup-example.sh
362
-
363
- # Method 2: Manual setup
364
- npm run build # Build the plugin first
365
- cd example
366
- npm install # Install example dependencies
367
- npm run dev # Run development server
368
-
369
- # Method 3: Use root scripts
370
- npm run example:dev # Development mode
371
- npm run example:build # Build
372
- npm run example:preview # Preview build results
202
+ # Start development server (all pages)
203
+ npm run dev
204
+
205
+ # Only display pages with specific strategies
206
+ npm run dev -- --strategy mobile
373
207
  ```
374
208
 
375
- ### Example Pages
209
+ ## Environment variables
376
210
 
377
- After building, visit these pages:
211
+ - `VITE_BUILD_STRATEGY`: Specify a single strategy build
212
+ - `IS_MOBILE`: Mobile identifier (configured in define)
213
+ - `API_BASE`: API base address (configured in define)
378
214
 
379
- - `/home.html` - Home page (default strategy)
380
- - `/about.html` - About page (default strategy)
381
- - `/dashboard.html` - Admin dashboard (modern ES syntax + dedicated template)
382
- - `/app.html` - Mobile app (ES5 compatible syntax + mobile template)
383
- - `/index.html` - Component library docs (library mode + docs template)
215
+ ## TypeScript support
384
216
 
385
- ## 🔧 Development
217
+ The plugin fully supports TypeScript configuration files:
386
218
 
387
- ```bash
388
- # Clone the project
389
- git clone https://github.com/fchc7/vite-plugin-multi-page.git
390
- cd vite-plugin-multi-page
219
+ ```typescript
220
+ // multipage.config.ts
221
+ import type { ConfigFunction } from 'vite-plugin-multi-page';
391
222
 
392
- # Install dependencies
393
- pnpm install
223
+ const config: ConfigFunction = context => {
224
+ return {
225
+ entry: 'src/pages/**/*.{ts,js}',
226
+ // ... other configurations
227
+ };
228
+ };
394
229
 
395
- # Development mode
396
- pnpm dev
230
+ export default config;
231
+ ```
397
232
 
398
- # Type checking
399
- pnpm type-check
233
+ ## API reference
400
234
 
401
- # Code formatting
402
- pnpm format
235
+ ### Configuration options
403
236
 
404
- # Code linting
405
- pnpm lint
237
+ | Option | Type | Default | Description |
238
+ | ------------- | -------------------------- | -------------------------- | ---------------------------- |
239
+ | `entry` | `string` | `'src/pages/**/*.{ts,js}'` | Page entry matching rule |
240
+ | `template` | `string` | `'index.html'` | HTML template file |
241
+ | `placeholder` | `string` | `'{{ENTRY_FILE}}'` | Template placeholder |
242
+ | `exclude` | `string[]` | `[]` | Excluded file patterns |
243
+ | `debug` | `boolean` | `false` | Enable debug log |
244
+ | `strategies` | `Record<string, Strategy>` | `{}` | Build strategy configuration |
245
+ | `pageConfigs` | `Function \| Object` | `{}` | Page |
406
246
 
407
- # Build
408
- pnpm build
409
- ```
247
+ ### Utility functions
410
248
 
411
- ## 🤝 Contributing
249
+ ```typescript
250
+ import { defineConfig, defineConfigTransform } from 'vite-plugin-multi-page';
251
+
252
+ // Define configuration
253
+ export default defineConfig(context => ({
254
+ // Configuration options
255
+ }));
412
256
 
413
- Issues and Pull Requests are welcome!
257
+ // Configuration transformation
258
+ const transform = defineConfigTransform((config, context) => {
259
+ // Modify configuration
260
+ return config;
261
+ });
262
+ ```
414
263
 
415
- ## 📄 License
264
+ ## Example project
416
265
 
417
- MIT License
266
+ See [example](./example) directory for a complete example project.
418
267
 
419
- ## 🔗 Related Links
268
+ ## License
420
269
 
421
- - [Vite Official Documentation](https://vitejs.dev/)
422
- - [TypeScript](https://www.typescriptlang.org/)
423
- - [ESLint](https://eslint.org/)
424
- - [Prettier](https://prettier.io/)
270
+ MIT License