@fchc8/vite-plugin-multi-page 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 vite-plugin-multi-page
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README-EN.md ADDED
@@ -0,0 +1,424 @@
1
+ # vite-plugin-multi-page
2
+
3
+ > English Documentation | [中文文档](./README.md)
4
+
5
+ A powerful Vite plugin for building multi-page applications with smart file routing and multiple build strategies.
6
+
7
+ ## ✨ Features
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
16
+
17
+ ## 📦 Installation
18
+
19
+ ```bash
20
+ npm install vite-plugin-multi-page
21
+ # or
22
+ yarn add vite-plugin-multi-page
23
+ # or
24
+ pnpm add vite-plugin-multi-page
25
+ ```
26
+
27
+ ## 🚀 Quick Start
28
+
29
+ ### Basic Usage
30
+
31
+ ```typescript
32
+ // vite.config.ts
33
+ import { defineConfig } from 'vite';
34
+ import viteMultiPage from 'vite-plugin-multi-page';
35
+
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
+ ```
47
+
48
+ ### Project Structure Example
49
+
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
+ ```
64
+
65
+ ## 🎯 Advanced Configuration
66
+
67
+ ### Multiple Build Strategies
68
+
69
+ ```typescript
70
+ import { defineConfig } from 'vite';
71
+ import viteMultiPage from '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
+ }
96
+ },
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
+ }
114
+ },
115
+
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
+ }
130
+ }
131
+ }
132
+ })
133
+ ]
134
+ });
135
+ ```
136
+
137
+ ### Function Configuration
138
+
139
+ ```typescript
140
+ viteMultiPage({
141
+ entry: 'src/pages/**/*.{ts,js}',
142
+
143
+ // Use function for dynamic configuration
144
+ pageConfigs: (context) => {
145
+ const { pageName, filePath, relativePath } = context;
146
+
147
+ // Admin pages
148
+ if (pageName.startsWith('admin')) {
149
+ return {
150
+ strategy: 'default',
151
+ template: 'admin.html',
152
+ define: {
153
+ 'process.env.API_BASE': '"https://admin-api.example.com"'
154
+ }
155
+ };
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
+ })
175
+ ```
176
+
177
+ ### Object Configuration with Pattern Matching
178
+
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
+ },
189
+
190
+ // Wildcard match
191
+ 'admin*': {
192
+ strategy: 'default',
193
+ template: 'admin.html'
194
+ },
195
+
196
+ // Pattern match
197
+ 'mobile-app': {
198
+ strategy: 'mobile',
199
+ match: ['**/mobile/**', '*mobile*'],
200
+ template: 'mobile.html'
201
+ }
202
+ }
203
+ })
204
+ ```
205
+
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 |
219
+
220
+ ### BuildStrategy
221
+
222
+ ```typescript
223
+ interface BuildStrategy {
224
+ // Full Vite configuration support
225
+ viteConfig?: Omit<UserConfig, 'plugins' | 'build'> & {
226
+ build?: BuildOptions;
227
+ };
228
+
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
+ };
239
+
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
+ };
251
+
252
+ // Environment variables
253
+ define?: Record<string, any>;
254
+
255
+ // Alias configuration
256
+ alias?: Record<string, string>;
257
+
258
+ // Server configuration
259
+ server?: ServerOptions;
260
+
261
+ // CSS configuration
262
+ css?: CSSOptions;
263
+
264
+ // Dependency optimization
265
+ optimizeDeps?: DepOptimizationOptions;
266
+ }
267
+ ```
268
+
269
+ ### PageConfig
270
+
271
+ ```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
280
+ }
281
+ ```
282
+
283
+ ## 🌟 Use Cases
284
+
285
+ ### 1. Enterprise Multi-Page Application
286
+
287
+ ```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
+ },
298
+
299
+ public: {
300
+ viteConfig: {
301
+ define: { 'process.env.APP_TYPE': '"public"' }
302
+ },
303
+ build: {
304
+ target: 'es5',
305
+ minify: 'terser'
306
+ }
307
+ }
308
+ }
309
+ ```
310
+
311
+ ### 2. Mobile Optimization
312
+
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
+ }
325
+ }
326
+ }
327
+ ```
328
+
329
+ ### 3. Component Library Development
330
+
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
+ ```
346
+
347
+ ## 📱 Example Project
348
+
349
+ Check the `example/` directory for a complete example project including:
350
+
351
+ - Admin dashboard (modern syntax)
352
+ - Mobile application (compatible syntax)
353
+ - Component library documentation
354
+ - Different HTML templates
355
+ - Function configuration examples
356
+
357
+ ### Quick Start
358
+
359
+ ```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
373
+ ```
374
+
375
+ ### Example Pages
376
+
377
+ After building, visit these pages:
378
+
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)
384
+
385
+ ## 🔧 Development
386
+
387
+ ```bash
388
+ # Clone the project
389
+ git clone <repository-url>
390
+ cd vite-plugin-multi-page
391
+
392
+ # Install dependencies
393
+ pnpm install
394
+
395
+ # Development mode
396
+ pnpm dev
397
+
398
+ # Type checking
399
+ pnpm type-check
400
+
401
+ # Code formatting
402
+ pnpm format
403
+
404
+ # Code linting
405
+ pnpm lint
406
+
407
+ # Build
408
+ pnpm build
409
+ ```
410
+
411
+ ## 🤝 Contributing
412
+
413
+ Issues and Pull Requests are welcome!
414
+
415
+ ## 📄 License
416
+
417
+ MIT License
418
+
419
+ ## 🔗 Related Links
420
+
421
+ - [Vite Official Documentation](https://vitejs.dev/)
422
+ - [TypeScript](https://www.typescriptlang.org/)
423
+ - [ESLint](https://eslint.org/)
424
+ - [Prettier](https://prettier.io/)