@fchc8/vite-plugin-multi-page 1.11.4 → 2.1.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
@@ -11,7 +11,9 @@ A powerful Vite plugin for multi-page application development, providing multi-s
11
11
  - 📝 **TypeScript configuration**: Support TypeScript configuration files
12
12
  - 🚀 **CLI tool**: Provide command-line batch build tools
13
13
  - 🔄 **Hot reload**: Development server supports page hot reload
14
- - 📦 **Smart merge**: Automatically merge multi-strategy build results
14
+ - 📦 **Flattened output**: Support flattened build results to reduce directory levels
15
+ - ⚡ **Concurrent builds**: Support controlling concurrent build count to improve build efficiency
16
+ - 🗂️ **Resource deduplication**: Automatically deduplicate shared resources to reduce build output size
15
17
 
16
18
  ## Install
17
19
 
@@ -60,20 +62,10 @@ export default defineConfig(() => ({}));
60
62
  Create `multipage.config.ts` or `multipage.config.js`:
61
63
 
62
64
  ```typescript
63
- import { defineConfig } from 'vite-plugin-multi-page';
64
-
65
- // Method 1: Object Configuration (Recommended)
66
- export default defineConfig({
67
- entry: 'src/pages/**/*.{ts,js}',
68
- template: 'index.html',
69
- strategies: {
70
- // Strategy Configuration...
71
- },
72
- });
65
+ import { defineConfig } from '@fchc8/vite-plugin-multi-page';
73
66
 
74
- // Method 2: Function Configuration (Dynamic Configuration)
75
67
  export default defineConfig(context => {
76
- const { mode, command, isCLI } = context;
68
+ const { mode } = context;
77
69
  const isProduction = mode === 'production';
78
70
 
79
71
  return {
@@ -83,15 +75,9 @@ export default defineConfig(context => {
83
75
  // HTML Template
84
76
  template: 'index.html',
85
77
 
86
- // Template Placeholder
87
- placeholder: '{{ENTRY_FILE}}',
88
-
89
78
  // Excluded Files
90
79
  exclude: ['src/shared/**/*.ts'],
91
80
 
92
- // Debug Mode
93
- debug: !isProduction || isCLI,
94
-
95
81
  // Build Strategy
96
82
  strategies: {
97
83
  default: {
@@ -121,25 +107,10 @@ export default defineConfig(context => {
121
107
 
122
108
  // Page Configuration Function
123
109
  pageConfigs: context => {
124
- // Determine the strategy based on the file path
125
110
  if (context.relativePath.includes('/mobile/')) {
126
- return {
127
- strategy: 'mobile',
128
- define: {
129
- PAGE_NAME: context.pageName,
130
- MOBILE_PAGE: true,
131
- },
132
- };
111
+ return { strategy: 'mobile' };
133
112
  }
134
-
135
- // Default Strategy
136
- return {
137
- strategy: 'default',
138
- define: {
139
- PAGE_NAME: context.pageName,
140
- DEFAULT_PAGE: true,
141
- },
142
- };
113
+ return { strategy: 'default' };
143
114
  },
144
115
  };
145
116
  });
@@ -149,16 +120,14 @@ export default defineConfig(context => {
149
120
 
150
121
  Create page files according to the convention:
151
122
 
152
- **Note**: Even if you use the empty configuration `defineConfig({})`, the plugin will automatically use the default strategy to process all pages, ensuring maximum compatibility.
153
-
154
123
  ```
155
124
  src/pages/
156
125
  ├── home.js # → /home.html
157
- ├── about.js # → /about.html (Default Strategy)
126
+ ├── about.js # → /about.html
158
127
  ├── mobile/
159
128
  │ └── main.ts # → /mobile.html (Mobile Strategy)
160
129
  └── admin/
161
- └── main.ts # → /admin.html (Admin Strategy)
130
+ └── main.ts # → /admin.html
162
131
  ```
163
132
 
164
133
  ## Page Discovery Rules
@@ -196,67 +165,95 @@ strategies: {
196
165
  }
197
166
  ```
198
167
 
199
- ### Build Output Merge Strategy
168
+ ### Build Output Organization Strategy
200
169
 
201
- Control how build outputs are organized using the `merge` option:
170
+ #### Strategy Grouping Mode (Default)
202
171
 
203
- ```typescript
204
- export default defineConfig({
205
- // ... other configurations
206
- merge: 'all' | 'page',
207
- });
172
+ By default, build results are grouped by strategy:
173
+
174
+ ```
175
+ dist/
176
+ ├── default/
177
+ │ ├── home.html
178
+ │ ├── about.html
179
+ │ ├── assets/
180
+ │ │ ├── home-xxx.js
181
+ │ │ ├── about-xxx.js
182
+ │ │ └── shared-resource.svg
183
+ │ └── images/
184
+ ├── mobile/
185
+ │ ├── mobile.html
186
+ │ ├── assets/
187
+ │ │ ├── mobile-xxx.js
188
+ │ │ └── button-loading.svg
189
+ │ └── images/
190
+ └── tablet/
191
+ ├── tablet.html
192
+ ├── assets/
193
+ │ ├── tablet-xxx.js
194
+ │ └── button-loading.svg
195
+ └── images/
196
+ ```
197
+
198
+ #### Flattened Mode (Default)
199
+
200
+ Flattened output is enabled by default. Use the `--no-flatten` parameter to disable it:
201
+
202
+ ```bash
203
+ # Default behavior (flattened output)
204
+ npx vite-mp
205
+
206
+ # Disable flattened output
207
+ npx vite-mp --no-flatten
208
+ ```
209
+
210
+ Flattened structure:
211
+
212
+ ```
213
+ dist/
214
+ ├── home.html
215
+ ├── about.html
216
+ ├── mobile.html
217
+ ├── tablet.html
218
+ ├── assets/
219
+ │ ├── home-xxx.js
220
+ │ ├── about-xxx.js
221
+ │ ├── mobile-xxx.js
222
+ │ ├── tablet-xxx.js
223
+ │ └── shared-resource.svg
224
+ ├── images/
225
+ ├── favicon.ico
226
+ └── some.css
227
+ ```
228
+
229
+ #### Advantages of Flattened Mode
230
+
231
+ - ✅ **Simplified Deployment**: All files in root directory, easier deployment
232
+ - ✅ **Resource Deduplication**: Automatically deduplicate identical resource files
233
+ - ✅ **Reduced Hierarchy**: Avoid deeply nested directory structures
234
+ - ✅ **Unified Management**: All resources centrally managed, easy CDN configuration
235
+
236
+ ### Concurrent Build Control
237
+
238
+ Control concurrent build count through the `--concurrency` parameter:
239
+
240
+ ```bash
241
+ # Default concurrency is 3
242
+ npx vite-mp
243
+
244
+ # Set concurrency to 1 (serial build)
245
+ npx vite-mp --concurrency 1
246
+
247
+ # Set concurrency to 5 (high concurrency)
248
+ npx vite-mp --concurrency 5
208
249
  ```
209
250
 
210
- #### Available Modes
211
-
212
- - **`all`** (default): All HTML files in root directory, assets merged into `/dist/assets/`
213
-
214
- ```
215
- dist/
216
- ├── home.html
217
- ├── about.html
218
- ├── mobile.html
219
- └── assets/
220
- ├── home-xxx.js
221
- ├── about-xxx.js
222
- └── shared-resource.svg
223
- ```
224
-
225
- - **`page`**: Each page is independently packaged with its own complete set of resource copies
226
- ```
227
- dist/
228
- ├── home/
229
- │ ├── index.html
230
- │ ├── assets/
231
- │ │ ├── home-xxx.js
232
- │ │ └── button-loading.svg
233
- │ └── images/
234
- ├── about/
235
- │ ├── index.html
236
- │ ├── assets/
237
- │ │ ├── about-xxx.js
238
- │ │ └── button-loading.svg
239
- │ └── images/
240
- └── mobile/
241
- ├── index.html
242
- ├── assets/
243
- │ ├── mobile-xxx.js
244
- │ └── button-loading.svg
245
- └── images/
246
- ```
247
-
248
- #### Advantages of Page Mode
249
-
250
- - ✅ **Fully Independent**: Each page directory contains all required resources, can be deployed independently
251
- - ✅ **Avoid Conflicts**: Completely resolves shared resource attribution issues
252
- - ✅ **Clean Naming**: Resource files use clean file names without page prefixes
253
- - ✅ **Deployment Friendly**: Supports CDN distribution, micro-frontend architectures
254
-
255
- > **Note**:
256
- >
257
- > - Page mode creates resource copies for each page, which may increase overall build output size
258
- > - Suitable for scenarios requiring independent deployment or strict resource isolation
259
- > - Static assets from the `public/` directory are automatically copied to each page directory
251
+ #### Advantages of Concurrent Builds
252
+
253
+ - **Improved Efficiency**: Multi-strategy parallel builds, reduce total build time
254
+ - 🔧 **Resource Control**: Avoid overloading system resources with too many concurrent builds
255
+ - 🎯 **Flexible Configuration**: Adjust concurrency based on machine performance
256
+ - 📊 **Progress Visibility**: Debug mode shows batch build progress
260
257
 
261
258
  ### Page Strategy Assignment
262
259
 
@@ -283,14 +280,26 @@ pageConfigs: context => {
283
280
  ### Batch Build
284
281
 
285
282
  ```bash
286
- # Build all strategies
283
+ # Build all strategies (default flattened output)
287
284
  npx vite-mp
288
285
 
286
+ # Disable flattened output structure
287
+ npx vite-mp --no-flatten
288
+
289
+ # Control concurrent build count
290
+ npx vite-mp --concurrency 2
291
+
292
+ # Build specified strategies
293
+ npx vite-mp --strategy mobile,tablet
294
+
289
295
  # Pass additional Vite parameters
290
296
  npx vite-mp --host --port 3000
291
297
 
292
298
  # Enable debug mode
293
299
  npx vite-mp --debug
300
+
301
+ # Combined usage
302
+ npx vite-mp --concurrency 4 --debug
294
303
  ```
295
304
 
296
305
  ### Development Server
@@ -303,133 +312,95 @@ npm run dev
303
312
  npm run dev -- --strategy mobile
304
313
  ```
305
314
 
306
- ## Usage Examples
315
+ ### Static Resource Preview
307
316
 
308
- ### Page Mode for Independent Deployment
317
+ After building, you can preview static resources in multiple ways:
309
318
 
310
- Configure Page mode where each page gets complete independent resources:
319
+ ```bash
320
+ # Using serve tool (recommended)
321
+ npx serve dist -p 3000
311
322
 
312
- ```typescript
313
- // multipage.config.ts
314
- export default defineConfig({
315
- entry: 'src/pages/**/*.{ts,js}',
316
- template: 'index.html',
317
- merge: 'page', // Enable Page mode
318
- strategies: {
319
- default: {
320
- build: {
321
- sourcemap: false,
322
- minify: 'esbuild',
323
- },
324
- },
325
- mobile: {
326
- build: {
327
- target: ['es2015'],
328
- minify: 'terser',
329
- },
330
- },
331
- },
332
- pageConfigs: context => {
333
- if (context.relativePath.includes('/mobile/')) {
334
- return { strategy: 'mobile' };
335
- }
336
- return { strategy: 'default' };
337
- },
338
- });
339
- ```
323
+ # Using http-server
324
+ npx http-server dist -p 3000
340
325
 
341
- Build result: Each page has independent resource files, avoiding shared resource missing issues.
326
+ # Using Python simple server
327
+ python -m http.server 3000 --directory dist
342
328
 
343
- ### Shared Resource Handling
329
+ # Using Node.js simple server
330
+ npx http-server dist
331
+ ```
344
332
 
345
- In Page mode, shared resources (such as icons, style files) are copied to each page directory:
333
+ Access URLs:
346
334
 
347
- ```typescript
348
- // src/pages/about/main.ts
349
- import buttonIcon from '../button-loading.svg'; // Shared resource
335
+ - Default (flattened mode): `http://localhost:3000/home.html`
336
+ - Strategy grouping mode: `http://localhost:3000/default/home.html` (use `--no-flatten`)
350
337
 
351
- // src/pages/mobile/main.ts
352
- import buttonIcon from '../button-loading.svg'; // Same shared resource
353
- ```
338
+ ## Usage Examples
354
339
 
355
- After building, both pages will have their own resource copies:
340
+ ### Default Flattened Build
356
341
 
357
- - `dist/about/assets/button-loading-xxx.svg`
358
- - `dist/mobile/assets/button-loading-xxx.svg`
342
+ The plugin uses flattened output by default for multi-strategy applications:
359
343
 
360
- ## Environment Variables
344
+ ```bash
345
+ # Default flattened build, all files in root directory
346
+ npx vite-mp
361
347
 
362
- - `VITE_BUILD_STRATEGY`: Specify a single strategy build
363
- - `VITE_MULTI_PAGE_BUILD_SINGLE_PAGE`: Specify single page build (used internally by Page mode)
364
- - `VITE_MULTI_PAGE_STRATEGY`: Current build strategy (automatically set)
365
- - `VITE_MULTI_PAGE_CURRENT_PAGE`: Current page name (automatically set in Page mode)
366
- - `VITE_MULTI_PAGE_MERGE_MODE`: Current merge mode (automatically set in Page mode)
367
- - `IS_MOBILE`: Mobile identifier (configured in define)
368
- - `API_BASE`: API base address (configured in define)
348
+ # High concurrency flattened build
349
+ npx vite-mp --concurrency 5
369
350
 
370
- ### Page Mode Environment Variable Injection
351
+ # Only build mobile and tablet strategies (flattened by default)
352
+ npx vite-mp --strategy mobile,tablet
353
+ ```
371
354
 
372
- In Page mode (`merge: 'page'`), you can inject specific environment variables for each page through the `pageEnvs` function:
355
+ Build result:
373
356
 
374
- ```typescript
375
- // multipage.config.ts
376
- export default defineConfig({
377
- merge: 'page', // Enable Page mode
378
-
379
- // Page environment variable injection function
380
- pageEnvs: context => {
381
- const { pageName, strategy, relativePath } = context;
382
-
383
- // Return page-specific environment variables
384
- const envs: Record<string, string> = {
385
- VITE_CURRENT_PAGE_NAME: pageName,
386
- VITE_CURRENT_STRATEGY: strategy || 'default',
387
- VITE_BUILD_TIMESTAMP: new Date().toISOString(),
388
- };
389
-
390
- // Add specific variables based on page path
391
- if (relativePath.includes('/mobile/')) {
392
- envs.VITE_IS_MOBILE = 'true';
393
- envs.VITE_API_URL = 'https://mobile-api.example.com';
394
- }
395
-
396
- return envs;
397
- },
398
- });
357
+ ```
358
+ dist/
359
+ ├── home.html # Default strategy page
360
+ ├── about.html # Default strategy page
361
+ ├── mobile.html # Mobile strategy page
362
+ ├── tablet.html # Tablet strategy page
363
+ ├── assets/ # Unified resource directory
364
+ │ ├── home-xxx.js
365
+ │ ├── about-xxx.js
366
+ │ ├── mobile-xxx.js
367
+ │ ├── tablet-xxx.js
368
+ │ └── shared-resource.svg
369
+ ├── images/
370
+ ├── favicon.ico
371
+ └── some.css
399
372
  ```
400
373
 
401
- #### Page Context
402
-
403
- The `pageEnvs` function receives a page context object containing the following information:
374
+ ### Concurrent Build Optimization
404
375
 
405
- - `pageName`: Page name (e.g., 'home', 'mobile')
406
- - `strategy`: Strategy name assigned to this page
407
- - `filePath`: Absolute path of the page entry file
408
- - `relativePath`: Relative path of the page entry file
376
+ Adjust concurrency based on machine performance:
409
377
 
410
- #### Use Cases
378
+ ```bash
379
+ # Low-spec machine: serial build
380
+ npx vite-mp --concurrency 1
411
381
 
412
- 1. **Page-specific API configuration**: Set different API endpoints for different pages
413
- 2. **Page identification**: Identify the current page type at runtime
414
- 3. **Build information**: Inject build timestamps, version numbers, etc.
415
- 4. **Feature flags**: Enable or disable features for specific pages
382
+ # High-spec machine: high concurrency build
383
+ npx vite-mp --concurrency 8
416
384
 
417
- #### Usage in Code
385
+ # Debug mode to view build progress
386
+ npx vite-mp --concurrency 4 --debug
387
+ ```
418
388
 
419
- Injected environment variables can be accessed in code via `import.meta.env`:
389
+ ### Production Environment Deployment
420
390
 
421
- ```typescript
422
- // src/pages/mobile/main.ts
423
- console.log('Current page:', import.meta.env.VITE_CURRENT_PAGE_NAME);
424
- console.log('Current strategy:', import.meta.env.VITE_CURRENT_STRATEGY);
425
- console.log('Build time:', import.meta.env.VITE_BUILD_TIMESTAMP);
391
+ ```bash
392
+ # Production environment build (flattened by default)
393
+ npx vite-mp --concurrency 4
426
394
 
427
- if (import.meta.env.VITE_IS_MOBILE === 'true') {
428
- // Mobile-specific logic
429
- }
395
+ # After building, can be directly deployed to CDN
396
+ # All resources are in root directory, easy CDN configuration
430
397
  ```
431
398
 
432
- **Note**: The `pageEnvs` feature only works in Page mode (`merge: 'page'`) because only in this mode each page is built independently.
399
+ ## Environment Variables
400
+
401
+ - `VITE_MULTI_PAGE_STRATEGY`: Current build strategy (automatically set)
402
+ - `IS_MOBILE`: Mobile identifier (configured in define)
403
+ - `API_BASE`: API base address (configured in define)
433
404
 
434
405
  ## TypeScript Support
435
406
 
@@ -453,33 +424,37 @@ export default config;
453
424
 
454
425
  ### Configuration Options
455
426
 
456
- | Option | Type | Default Value | Description |
457
- | ------------- | -------------------------- | -------------------------- | ------------------------------------------------------------- |
458
- | `entry` | `string` | `'src/pages/**/*.{ts,js}'` | Page entry matching rules |
459
- | `template` | `string` | `'index.html'` | HTML Template File |
460
- | `placeholder` | `string` | `'{{ENTRY_FILE}}'` | Template placeholder |
461
- | `exclude` | `string[]` | `[]` | Excluded file patterns |
462
- | `debug` | `boolean` | `false` | Enable debug logging |
463
- | `merge` | `'all' \| 'page'` | `'all'` | Build output merge strategy |
464
- | `strategies` | `Record<string, Strategy>` | `{}` | Build strategy configuration |
465
- | `pageConfigs` | `Function \| Object` | `{}` | Page configuration |
466
- | `pageEnvs` | `Function` | `() => null` | Page environment variable injection function (Page mode only) |
427
+ | Option | Type | Default Value | Description |
428
+ | ------------- | -------------------------- | -------------------------- | ---------------------------- |
429
+ | `entry` | `string` | `'src/pages/**/*.{ts,js}'` | Page entry matching rules |
430
+ | `template` | `string` | `'index.html'` | HTML Template File |
431
+ | `placeholder` | `string` | `'{{ENTRY_FILE}}'` | Template placeholder |
432
+ | `exclude` | `string[]` | `[]` | Excluded file patterns |
433
+ | `debug` | `boolean` | `false` | Enable debug logging |
434
+ | `strategies` | `Record<string, Strategy>` | `{}` | Build strategy configuration |
435
+ | `pageConfigs` | `Function \| Object` | `{}` | Page configuration |
436
+
437
+ ### CLI Parameters
438
+
439
+ | Parameter | Type | Default Value | Description |
440
+ | --------------- | --------- | ------------- | ----------------------------------------------------- |
441
+ | `--flatten` | `boolean` | `true` | Enable flattened output structure (default) |
442
+ | `--no-flatten` | `boolean` | `false` | Disable flattened output structure |
443
+ | `--concurrency` | `number` | `3` | Concurrent build count |
444
+ | `--strategy` | `string` | - | Specify build strategies (can be used multiple times) |
445
+ | `--debug` | `boolean` | `false` | Enable debug mode |
446
+ | `--cwd` | `string` | - | Specify working directory |
447
+ | `--help` | `boolean` | `false` | Show help information |
467
448
 
468
449
  ### Utility Functions
469
450
 
470
451
  ```typescript
471
- import { defineConfig, defineConfigTransform } from '@fchc8/vite-plugin-multi-page';
452
+ import { defineConfig } from '@fchc8/vite-plugin-multi-page';
472
453
 
473
454
  // Define configuration
474
455
  export default defineConfig(context => ({
475
456
  // Configuration options
476
457
  }));
477
-
478
- // Configuration transformation
479
- const transform = defineConfigTransform((config, context) => {
480
- // Modify configuration
481
- return config;
482
- });
483
458
  ```
484
459
 
485
460
  ## Example Project