@meng-xi/vite-plugin 0.0.5 → 0.0.6

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.
Files changed (46) hide show
  1. package/README-en.md +186 -25
  2. package/README.md +343 -182
  3. package/dist/common/index.cjs +1 -1
  4. package/dist/common/index.d.cts +9 -1
  5. package/dist/common/index.d.mts +9 -1
  6. package/dist/common/index.d.ts +9 -1
  7. package/dist/common/index.mjs +1 -1
  8. package/dist/factory/index.cjs +1 -1
  9. package/dist/factory/index.d.cts +71 -6
  10. package/dist/factory/index.d.mts +71 -6
  11. package/dist/factory/index.d.ts +71 -6
  12. package/dist/factory/index.mjs +1 -1
  13. package/dist/index.cjs +1 -1
  14. package/dist/index.d.cts +4 -5
  15. package/dist/index.d.mts +4 -5
  16. package/dist/index.d.ts +4 -5
  17. package/dist/index.mjs +1 -1
  18. package/dist/logger/index.cjs +1 -1
  19. package/dist/logger/index.d.cts +1 -1
  20. package/dist/logger/index.d.mts +1 -1
  21. package/dist/logger/index.d.ts +1 -1
  22. package/dist/logger/index.mjs +1 -1
  23. package/dist/plugins/index.cjs +1 -1
  24. package/dist/plugins/index.d.cts +82 -7
  25. package/dist/plugins/index.d.mts +82 -7
  26. package/dist/plugins/index.d.ts +82 -7
  27. package/dist/plugins/index.mjs +1 -1
  28. package/dist/shared/vite-plugin.B5wW4CiL.mjs +36 -0
  29. package/dist/shared/vite-plugin.Ba9646wL.cjs +1 -0
  30. package/dist/shared/vite-plugin.C3ejdBNf.mjs +1 -0
  31. package/dist/shared/{vite-plugin.B3PARlU9.d.cts → vite-plugin.CLr0ttuO.d.cts} +16 -0
  32. package/dist/shared/{vite-plugin.B3PARlU9.d.mts → vite-plugin.CLr0ttuO.d.mts} +16 -0
  33. package/dist/shared/{vite-plugin.B3PARlU9.d.ts → vite-plugin.CLr0ttuO.d.ts} +16 -0
  34. package/dist/shared/vite-plugin.CXlzkIgT.cjs +36 -0
  35. package/dist/shared/vite-plugin.CawoITTT.cjs +1 -0
  36. package/dist/shared/vite-plugin.DSb6XzBn.mjs +1 -0
  37. package/package.json +72 -72
  38. package/dist/shared/vite-plugin.BZsetDCm.cjs +0 -1
  39. package/dist/shared/vite-plugin.C7isVPKg.mjs +0 -1
  40. package/dist/shared/vite-plugin.CS9a5kjK.mjs +0 -36
  41. package/dist/shared/vite-plugin.CgnG5_UX.cjs +0 -36
  42. package/dist/shared/vite-plugin.DqWt65U-.cjs +0 -1
  43. package/dist/shared/vite-plugin.UkE7CdSe.d.cts +0 -43
  44. package/dist/shared/vite-plugin.UkE7CdSe.d.mts +0 -43
  45. package/dist/shared/vite-plugin.UkE7CdSe.d.ts +0 -43
  46. package/dist/shared/vite-plugin.YvjM8LxW.mjs +0 -1
package/README-en.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  <div align="center">
4
4
  <a href="https://github.com/MengXi-Studio/vite-plugin">
5
- <img alt="MengXi Studio Logo" width="215" src="https://github.com/MengXi-Studio/vite-plugin/blob/master/packages/docs/src/public/logo.svg">
5
+ <img alt="MengXi Studio Logo" width="215" src="https://github.com/MengXi-Studio/vite-plugin/blob/master/packages/docs/src/public/logo.png">
6
6
  </a>
7
7
  <br>
8
8
  <h1>@meng-xi/vite-plugin</h1>
@@ -17,8 +17,10 @@
17
17
 
18
18
  - **Ready to Use** - Provides practical plugins for file copying, router generation, version management, icon injection
19
19
  - **Plugin Development Framework** - Exports core components like BasePlugin, Logger, Validator for building custom plugins
20
+ - **Complete Lifecycle** - Supports initialization, config resolution, destroy lifecycle management with automatic hook composition
20
21
  - **Type Safe** - Complete TypeScript type definitions with configuration validators ensuring parameter correctness
21
22
  - **Flexible Configuration** - All plugins support detailed configuration to meet diverse scenario requirements
23
+ - **Safe Execution** - Built-in error handling strategies (throw / log / ignore) for unified exception management
22
24
 
23
25
  ## Documentation
24
26
 
@@ -73,17 +75,29 @@ export default defineConfig({
73
75
  })
74
76
  ```
75
77
 
78
+ ### Accessing Plugin Instance
79
+
80
+ All built-in plugins return an object with a `pluginInstance` property for accessing internal state:
81
+
82
+ ```typescript
83
+ import type { PluginWithInstance } from '@meng-xi/vite-plugin/factory'
84
+ import type { GenerateRouterOptions } from '@meng-xi/vite-plugin'
85
+
86
+ const routerPlugin = generateRouter({ watch: true }) as PluginWithInstance<GenerateRouterOptions>
87
+
88
+ // Access plugin internals via pluginInstance
89
+ console.log(routerPlugin.pluginInstance?.options)
90
+ ```
91
+
76
92
  ### Developing Custom Plugins
77
93
 
78
94
  ```typescript
79
95
  import { BasePlugin, createPluginFactory } from '@meng-xi/vite-plugin'
96
+ import type { BasePluginOptions, PluginWithInstance } from '@meng-xi/vite-plugin/factory'
80
97
  import type { Plugin } from 'vite'
81
98
 
82
- interface MyPluginOptions {
99
+ interface MyPluginOptions extends BasePluginOptions {
83
100
  path: string
84
- enabled?: boolean
85
- verbose?: boolean
86
- errorStrategy?: 'throw' | 'log' | 'ignore'
87
101
  }
88
102
 
89
103
  class MyPlugin extends BasePlugin<MyPluginOptions> {
@@ -104,11 +118,123 @@ class MyPlugin extends BasePlugin<MyPluginOptions> {
104
118
  this.logger.info(`Plugin started with path: ${this.options.path}`)
105
119
  }
106
120
  }
121
+
122
+ protected destroy(): void {
123
+ super.destroy()
124
+ // Custom cleanup logic, e.g. close connections, stop watchers
125
+ }
107
126
  }
108
127
 
109
128
  export const myPlugin = createPluginFactory(MyPlugin)
110
129
  ```
111
130
 
131
+ ## Plugin Development Framework
132
+
133
+ ### BasePlugin Core Concepts
134
+
135
+ `BasePlugin` is the base class for all plugins, providing complete lifecycle management and development conventions:
136
+
137
+ #### Lifecycle
138
+
139
+ | Phase | Method | Description |
140
+ | ----------------- | ------------------ | -------------------------------------------------------------- |
141
+ | Initialization | `constructor` | Merge options, initialize logger and validator |
142
+ | Config Resolution | `onConfigResolved` | Called when Vite config is resolved |
143
+ | Hook Registration | `addPluginHooks` | Register Vite plugin hooks |
144
+ | Destroy | `destroy` | Automatically called during `closeBundle` for resource cleanup |
145
+
146
+ #### Automatic Hook Composition
147
+
148
+ The `toPlugin()` method automatically composes the following hooks:
149
+
150
+ - **configResolved** - Base class `onConfigResolved` runs first, then subclass hook
151
+ - **closeBundle** - Subclass hook runs first, then base class `destroy`
152
+
153
+ > Subclasses don't need to manually register `closeBundle` hooks for cleanup — just override the `destroy()` method.
154
+
155
+ #### Required Methods
156
+
157
+ | Method | Description |
158
+ | ------------------------ | --------------------- |
159
+ | `getPluginName()` | Return plugin name |
160
+ | `addPluginHooks(plugin)` | Add Vite plugin hooks |
161
+
162
+ #### Optional Methods
163
+
164
+ | Method | Default Behavior | Description |
165
+ | -------------------------- | ----------------- | ------------------------------------------- |
166
+ | `getDefaultOptions()` | Returns `{}` | Provide plugin default options |
167
+ | `validateOptions()` | No validation | Validate configuration parameters |
168
+ | `getEnforce()` | `undefined` | Plugin execution order (`'pre'` / `'post'`) |
169
+ | `onConfigResolved(config)` | Store config | Config resolved callback |
170
+ | `destroy()` | Unregister logger | Cleanup logic when plugin is destroyed |
171
+
172
+ #### Built-in Properties
173
+
174
+ | Property | Type | Description |
175
+ | ------------ | ------------------------ | ----------------------------- |
176
+ | `options` | `Required<T>` | Merged complete configuration |
177
+ | `logger` | `PluginLogger` | Plugin logger |
178
+ | `validator` | `Validator<T>` | Configuration validator |
179
+ | `viteConfig` | `ResolvedConfig \| null` | Resolved Vite configuration |
180
+
181
+ #### Error Handling Strategy
182
+
183
+ Control error behavior via the `errorStrategy` configuration option:
184
+
185
+ - `'throw'` (default) - Log error and throw exception, halting the build
186
+ - `'log'` - Log error but don't throw, continue execution
187
+ - `'ignore'` - Log error but don't throw, continue execution
188
+
189
+ Wrap error-prone operations with `safeExecute` / `safeExecuteSync`:
190
+
191
+ ```typescript
192
+ const result = await this.safeExecute(async () => {
193
+ return await someAsyncOperation()
194
+ }, 'Execute async operation')
195
+ ```
196
+
197
+ ### createPluginFactory
198
+
199
+ Create plugin factory functions with optional normalizer support:
200
+
201
+ ```typescript
202
+ // Basic usage
203
+ const myPlugin = createPluginFactory(MyPlugin)
204
+
205
+ // With normalizer (supports shorthand string config)
206
+ const myPlugin = createPluginFactory(MyPlugin, opt => (typeof opt === 'string' ? { path: opt } : opt))
207
+
208
+ // Usage with shorthand
209
+ myPlugin('./custom-path')
210
+ ```
211
+
212
+ ### Logger
213
+
214
+ Global singleton log manager providing independent log control for each plugin:
215
+
216
+ ```typescript
217
+ import { Logger } from '@meng-xi/vite-plugin/logger'
218
+
219
+ // Create logger (usually called automatically by BasePlugin)
220
+ Logger.create({ name: 'my-plugin', enabled: true })
221
+
222
+ // Unregister plugin log config (automatically called on plugin destroy)
223
+ Logger.unregister('my-plugin')
224
+
225
+ // Destroy singleton (for test scenarios)
226
+ Logger.destroy()
227
+ ```
228
+
229
+ Log output format:
230
+
231
+ ```
232
+ ℹ️ [@meng-xi/vite-plugin:my-plugin] Info message
233
+ ✅ [@meng-xi/vite-plugin:my-plugin] Success message
234
+ ⚠️ [@meng-xi/vite-plugin:my-plugin] Warning message
235
+ ❌ [@meng-xi/vite-plugin:my-plugin] Error message
236
+ ```
237
+
112
238
  ## Built-in Plugins
113
239
 
114
240
  ### copyFile
@@ -127,29 +253,35 @@ Copy files or directories to specified locations after Vite build is completed.
127
253
 
128
254
  Automatically generate router configuration files based on uni-app project's `pages.json`.
129
255
 
130
- | Option | Type | Default | Description |
131
- | -------------------- | ------------ | ---------------------- | ------------------------------------------------ |
132
- | pagesJsonPath | string | 'src/pages.json' | Path to pages.json file |
133
- | outputPath | string | 'src/router.config.ts' | Output file path |
134
- | outputFormat | 'ts' \| 'js' | 'ts' | Output file format |
135
- | nameStrategy | string | 'camelCase' | Route name strategy |
136
- | includeSubPackages | boolean | true | Whether to include sub-package routes |
137
- | watch | boolean | true | Whether to watch changes and auto-regenerate |
138
- | metaMapping | object | - | Mapping from page style fields to meta |
139
- | preserveRouteChanges | boolean | true | Whether to preserve user modifications to routes |
256
+ | Option | Type | Default | Description |
257
+ | -------------------- | ------------------------------------------------- | ---------------------- | ------------------------------------------------ |
258
+ | pagesJsonPath | string | 'src/pages.json' | Path to pages.json file |
259
+ | outputPath | string | 'src/router.config.ts' | Output file path |
260
+ | outputFormat | 'ts' \| 'js' | 'ts' | Output file format |
261
+ | nameStrategy | 'path' \| 'camelCase' \| 'pascalCase' \| 'custom' | 'camelCase' | Route name strategy |
262
+ | customNameGenerator | (path: string) => string | - | Custom route name generator function |
263
+ | includeSubPackages | boolean | true | Whether to include sub-package routes |
264
+ | watch | boolean | true | Whether to watch changes and auto-regenerate |
265
+ | metaMapping | Record\<string, string\> | - | Mapping from page style fields to meta |
266
+ | exportTypes | boolean | true | Whether to export type definitions |
267
+ | preserveRouteChanges | boolean | true | Whether to preserve user modifications to routes |
140
268
 
141
269
  ### generateVersion
142
270
 
143
271
  Automatically generate version numbers during the Vite build process.
144
272
 
145
- | Option | Type | Default | Description |
146
- | ---------- | ------ | --------------------- | ------------------------------ |
147
- | format | string | 'timestamp' | Version format |
148
- | outputType | string | 'file' | Output type |
149
- | outputFile | string | 'version.json' | Output file path |
150
- | defineName | string | '\_\_APP_VERSION\_\_' | Global variable name to inject |
151
- | prefix | string | - | Version number prefix |
152
- | suffix | string | - | Version number suffix |
273
+ | Option | Type | Default | Description |
274
+ | ------------ | --------------------------------------------------------------------- | ----------------- | ------------------------------ |
275
+ | format | 'timestamp' \| 'date' \| 'datetime' \| 'semver' \| 'hash' \| 'custom' | 'timestamp' | Version format |
276
+ | customFormat | string | - | Custom format template |
277
+ | semverBase | string | '1.0.0' | Semantic version base |
278
+ | outputType | 'file' \| 'define' \| 'both' | 'file' | Output type |
279
+ | outputFile | string | 'version.json' | Output file path |
280
+ | defineName | string | '**APP_VERSION**' | Global variable name to inject |
281
+ | hashLength | number | 8 | Hash length (1-32) |
282
+ | prefix | string | - | Version number prefix |
283
+ | suffix | string | - | Version number suffix |
284
+ | extra | Record\<string, unknown\> | - | Extra info (JSON file only) |
153
285
 
154
286
  ### injectIco
155
287
 
@@ -157,12 +289,41 @@ Inject website icon links into the head of HTML files during the Vite build proc
157
289
 
158
290
  | Option | Type | Default | Description |
159
291
  | ----------- | ------ | ------- | ------------------------------- |
160
- | base | string | - | Base path for icon files |
292
+ | base | string | '/' | Base path for icon files |
161
293
  | url | string | - | Complete URL for the icon |
162
294
  | link | string | - | Custom complete link tag HTML |
163
- | icons | array | - | Custom icon array |
295
+ | icons | Icon[] | - | Custom icon array |
164
296
  | copyOptions | object | - | Icon file copying configuration |
165
297
 
298
+ `Icon` interface definition:
299
+
300
+ | Property | Type | Required | Description |
301
+ | -------- | ------ | -------- | ------------------ |
302
+ | rel | string | Yes | Icon relation type |
303
+ | href | string | Yes | Icon URL |
304
+ | sizes | string | No | Icon sizes |
305
+ | type | string | No | Icon MIME type |
306
+
307
+ ## Sub-path Exports
308
+
309
+ Support importing modules on demand to reduce bundle size:
310
+
311
+ ```typescript
312
+ // Full import
313
+ import { copyFile, BasePlugin, Logger } from '@meng-xi/vite-plugin'
314
+
315
+ // Module-level import
316
+ import { BasePlugin, createPluginFactory } from '@meng-xi/vite-plugin/factory'
317
+ import { Logger } from '@meng-xi/vite-plugin/logger'
318
+ import { copyFile, generateRouter } from '@meng-xi/vite-plugin/plugins'
319
+ import { Validator, readFileContent, writeFileContent } from '@meng-xi/vite-plugin/common'
320
+
321
+ // Type imports (on-demand type definitions from sub-paths)
322
+ import type { PluginWithInstance, PluginFactory, BasePluginOptions } from '@meng-xi/vite-plugin/factory'
323
+ import type { GenerateVersionOptions, InjectIcoOptions, Icon } from '@meng-xi/vite-plugin/plugins'
324
+ import type { DateFormatOptions } from '@meng-xi/vite-plugin/common'
325
+ ```
326
+
166
327
  ## Changelog
167
328
 
168
329
  See [GitHub Releases](https://github.com/MengXi-Studio/vite-plugin/releases)