@meng-xi/vite-plugin 0.1.4 → 0.1.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 (151) hide show
  1. package/README-en.md +34 -20
  2. package/README.md +33 -19
  3. package/dist/common/format/index.cjs +1 -1
  4. package/dist/common/format/index.d.cts +19 -114
  5. package/dist/common/format/index.d.mts +19 -114
  6. package/dist/common/format/index.d.ts +19 -114
  7. package/dist/common/format/index.mjs +1 -1
  8. package/dist/common/fs/index.cjs +1 -1
  9. package/dist/common/fs/index.d.cts +36 -89
  10. package/dist/common/fs/index.d.mts +36 -89
  11. package/dist/common/fs/index.d.ts +36 -89
  12. package/dist/common/fs/index.mjs +1 -1
  13. package/dist/common/html/index.cjs +2 -2
  14. package/dist/common/html/index.d.cts +12 -307
  15. package/dist/common/html/index.d.mts +12 -307
  16. package/dist/common/html/index.d.ts +12 -307
  17. package/dist/common/html/index.mjs +2 -2
  18. package/dist/common/index.cjs +1 -1
  19. package/dist/common/index.d.cts +7 -11
  20. package/dist/common/index.d.mts +7 -11
  21. package/dist/common/index.d.ts +7 -11
  22. package/dist/common/index.mjs +1 -1
  23. package/dist/common/script/index.cjs +1 -1
  24. package/dist/common/script/index.d.cts +1 -44
  25. package/dist/common/script/index.d.mts +1 -44
  26. package/dist/common/script/index.d.ts +1 -44
  27. package/dist/common/script/index.mjs +1 -1
  28. package/dist/common/ui/index.cjs +1 -1
  29. package/dist/common/ui/index.d.cts +1 -111
  30. package/dist/common/ui/index.d.mts +1 -111
  31. package/dist/common/ui/index.d.ts +1 -111
  32. package/dist/common/ui/index.mjs +1 -1
  33. package/dist/common/validation/index.cjs +1 -1
  34. package/dist/common/validation/index.d.cts +2 -80
  35. package/dist/common/validation/index.d.mts +2 -80
  36. package/dist/common/validation/index.d.ts +2 -80
  37. package/dist/common/validation/index.mjs +1 -1
  38. package/dist/factory/index.cjs +1 -1
  39. package/dist/factory/index.d.cts +1 -1
  40. package/dist/factory/index.d.mts +1 -1
  41. package/dist/factory/index.d.ts +1 -1
  42. package/dist/factory/index.mjs +1 -1
  43. package/dist/index.cjs +1 -1
  44. package/dist/index.d.cts +10 -12
  45. package/dist/index.d.mts +10 -12
  46. package/dist/index.d.ts +10 -12
  47. package/dist/index.mjs +1 -1
  48. package/dist/logger/index.d.cts +92 -1
  49. package/dist/logger/index.d.mts +92 -1
  50. package/dist/logger/index.d.ts +92 -1
  51. package/dist/plugins/autoImport/index.cjs +19 -0
  52. package/dist/plugins/autoImport/index.d.cts +291 -0
  53. package/dist/plugins/autoImport/index.d.mts +291 -0
  54. package/dist/plugins/autoImport/index.d.ts +291 -0
  55. package/dist/plugins/autoImport/index.mjs +19 -0
  56. package/dist/plugins/buildProgress/index.cjs +1 -1
  57. package/dist/plugins/buildProgress/index.d.cts +1 -1
  58. package/dist/plugins/buildProgress/index.d.mts +1 -1
  59. package/dist/plugins/buildProgress/index.d.ts +1 -1
  60. package/dist/plugins/buildProgress/index.mjs +2 -2
  61. package/dist/plugins/bundleAnalyzer/index.cjs +21 -21
  62. package/dist/plugins/bundleAnalyzer/index.d.cts +1 -1
  63. package/dist/plugins/bundleAnalyzer/index.d.mts +1 -1
  64. package/dist/plugins/bundleAnalyzer/index.d.ts +1 -1
  65. package/dist/plugins/bundleAnalyzer/index.mjs +18 -18
  66. package/dist/plugins/compressAssets/index.cjs +1 -1
  67. package/dist/plugins/compressAssets/index.d.cts +1 -1
  68. package/dist/plugins/compressAssets/index.d.mts +1 -1
  69. package/dist/plugins/compressAssets/index.d.ts +1 -1
  70. package/dist/plugins/compressAssets/index.mjs +1 -1
  71. package/dist/plugins/copyFile/index.cjs +1 -1
  72. package/dist/plugins/copyFile/index.d.cts +1 -1
  73. package/dist/plugins/copyFile/index.d.mts +1 -1
  74. package/dist/plugins/copyFile/index.d.ts +1 -1
  75. package/dist/plugins/copyFile/index.mjs +1 -1
  76. package/dist/plugins/envGuard/index.cjs +11 -11
  77. package/dist/plugins/envGuard/index.d.cts +101 -2
  78. package/dist/plugins/envGuard/index.d.mts +101 -2
  79. package/dist/plugins/envGuard/index.d.ts +101 -2
  80. package/dist/plugins/envGuard/index.mjs +9 -9
  81. package/dist/plugins/faviconManager/index.cjs +1 -1
  82. package/dist/plugins/faviconManager/index.d.cts +1 -1
  83. package/dist/plugins/faviconManager/index.d.mts +1 -1
  84. package/dist/plugins/faviconManager/index.d.ts +1 -1
  85. package/dist/plugins/faviconManager/index.mjs +1 -1
  86. package/dist/plugins/generateRouter/index.cjs +5 -4
  87. package/dist/plugins/generateRouter/index.d.cts +34 -1
  88. package/dist/plugins/generateRouter/index.d.mts +34 -1
  89. package/dist/plugins/generateRouter/index.d.ts +34 -1
  90. package/dist/plugins/generateRouter/index.mjs +5 -4
  91. package/dist/plugins/generateVersion/index.cjs +1 -1
  92. package/dist/plugins/generateVersion/index.d.cts +1 -1
  93. package/dist/plugins/generateVersion/index.d.mts +1 -1
  94. package/dist/plugins/generateVersion/index.d.ts +1 -1
  95. package/dist/plugins/generateVersion/index.mjs +1 -1
  96. package/dist/plugins/htmlInject/index.cjs +7 -1
  97. package/dist/plugins/htmlInject/index.d.cts +2 -2
  98. package/dist/plugins/htmlInject/index.d.mts +2 -2
  99. package/dist/plugins/htmlInject/index.d.ts +2 -2
  100. package/dist/plugins/htmlInject/index.mjs +7 -1
  101. package/dist/plugins/index.cjs +1 -1
  102. package/dist/plugins/index.d.cts +3 -3
  103. package/dist/plugins/index.d.mts +3 -3
  104. package/dist/plugins/index.d.ts +3 -3
  105. package/dist/plugins/index.mjs +1 -1
  106. package/dist/plugins/loadingManager/index.cjs +26 -26
  107. package/dist/plugins/loadingManager/index.d.cts +1 -1
  108. package/dist/plugins/loadingManager/index.d.mts +1 -1
  109. package/dist/plugins/loadingManager/index.d.ts +1 -1
  110. package/dist/plugins/loadingManager/index.mjs +10 -10
  111. package/dist/plugins/versionUpdateChecker/index.cjs +5 -5
  112. package/dist/plugins/versionUpdateChecker/index.d.cts +1 -1
  113. package/dist/plugins/versionUpdateChecker/index.d.mts +1 -1
  114. package/dist/plugins/versionUpdateChecker/index.d.ts +1 -1
  115. package/dist/plugins/versionUpdateChecker/index.mjs +6 -6
  116. package/dist/shared/vite-plugin.B8FuZce1.d.cts +45 -0
  117. package/dist/shared/vite-plugin.B8FuZce1.d.mts +45 -0
  118. package/dist/shared/vite-plugin.B8FuZce1.d.ts +45 -0
  119. package/dist/shared/{vite-plugin.FfJ-Wwfu.d.cts → vite-plugin.BI9taN75.d.cts} +1 -22
  120. package/dist/shared/{vite-plugin.FfJ-Wwfu.d.mts → vite-plugin.BI9taN75.d.mts} +1 -22
  121. package/dist/shared/{vite-plugin.FfJ-Wwfu.d.ts → vite-plugin.BI9taN75.d.ts} +1 -22
  122. package/dist/shared/vite-plugin.CcvHfrL8.cjs +1 -0
  123. package/dist/shared/vite-plugin.CuXEJAWX.mjs +1 -0
  124. package/package.json +7 -17
  125. package/dist/common/compress/index.cjs +0 -1
  126. package/dist/common/compress/index.d.cts +0 -23
  127. package/dist/common/compress/index.d.mts +0 -23
  128. package/dist/common/compress/index.d.ts +0 -23
  129. package/dist/common/compress/index.mjs +0 -1
  130. package/dist/common/object/index.cjs +0 -1
  131. package/dist/common/object/index.d.cts +0 -30
  132. package/dist/common/object/index.d.mts +0 -30
  133. package/dist/common/object/index.d.ts +0 -30
  134. package/dist/common/object/index.mjs +0 -1
  135. package/dist/common/path/index.cjs +0 -1
  136. package/dist/common/path/index.d.cts +0 -22
  137. package/dist/common/path/index.d.mts +0 -22
  138. package/dist/common/path/index.d.ts +0 -22
  139. package/dist/common/path/index.mjs +0 -1
  140. package/dist/shared/vite-plugin.BCuhU1au.mjs +0 -7
  141. package/dist/shared/vite-plugin.BrI73DHA.cjs +0 -7
  142. package/dist/shared/vite-plugin.CLr0ttuO.d.cts +0 -135
  143. package/dist/shared/vite-plugin.CLr0ttuO.d.mts +0 -135
  144. package/dist/shared/vite-plugin.CLr0ttuO.d.ts +0 -135
  145. package/dist/shared/vite-plugin.CmtcnItg.d.cts +0 -261
  146. package/dist/shared/vite-plugin.CmtcnItg.d.mts +0 -261
  147. package/dist/shared/vite-plugin.CmtcnItg.d.ts +0 -261
  148. package/dist/shared/vite-plugin.DnFDPjNf.mjs +0 -1
  149. package/dist/shared/vite-plugin.Dumot0up.mjs +0 -1
  150. package/dist/shared/vite-plugin.soT9a-KD.cjs +0 -1
  151. package/dist/shared/vite-plugin.vwox4bU0.cjs +0 -1
package/README-en.md CHANGED
@@ -15,10 +15,10 @@
15
15
 
16
16
  ## Features
17
17
 
18
- - **Ready to Use** - 11 practical plugins covering build progress, bundle analysis & compression, file copying, environment variable validation, route generation, version management, HTML injection, favicon management,
19
- global Loading, and more
18
+ - **Ready to Use** - 12 practical plugins covering auto-import, build progress, bundle analysis & compression, file copying, environment variable validation, route generation, version management, HTML injection, favicon
19
+ management, global Loading, and more
20
20
  - **Plugin Development Framework** - Exports core components like BasePlugin, Logger, and Validator to quickly build custom Vite plugins
21
- - **Common Utility Library** - Built-in Common utility modules supporting on-demand sub-path imports
21
+ - **Common Utility Library** - Built-in 6 Common utility modules supporting on-demand sub-path imports
22
22
  - **Type Safe** - Complete TypeScript type definitions with configuration validators
23
23
  - **On-demand Import** - Supports sub-path exports to reduce bundle size
24
24
 
@@ -41,10 +41,11 @@ pnpm add @meng-xi/vite-plugin -D
41
41
 
42
42
  ```typescript
43
43
  import { defineConfig } from 'vite'
44
- import { buildProgress, bundleAnalyzer, compressAssets, copyFile, envGuard, generateRouter, generateVersion, versionUpdateChecker, htmlInject, faviconManager, loadingManager } from '@meng-xi/vite-plugin'
44
+ import { autoImport, buildProgress, bundleAnalyzer, compressAssets, copyFile, envGuard, generateRouter, generateVersion, versionUpdateChecker, htmlInject, faviconManager, loadingManager } from '@meng-xi/vite-plugin'
45
45
 
46
46
  export default defineConfig({
47
47
  plugins: [
48
+ autoImport({ imports: { vue: ['ref', 'reactive', 'computed'] }, dts: 'src/auto-imports.d.ts' }),
48
49
  buildProgress(),
49
50
  bundleAnalyzer({ outputFormat: 'both' }),
50
51
  compressAssets({ algorithm: 'gzip' }),
@@ -64,13 +65,14 @@ export default defineConfig({
64
65
 
65
66
  | Plugin | Description |
66
67
  | ---------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
68
+ | [autoImport](https://mengxi-studio.github.io/vite-plugin/en/plugins/auto-import.html) | Auto-inject import statements with preset mappings, directory scanning, and Vue template auto-import |
67
69
  | [buildProgress](https://mengxi-studio.github.io/vite-plugin/en/plugins/build-progress.html) | Real-time terminal build progress bar, supports bar / spinner / minimal |
68
70
  | [bundleAnalyzer](https://mengxi-studio.github.io/vite-plugin/en/plugins/bundle-analyzer.html) | Bundle volume analysis with JSON/HTML reports, gzip calculation, threshold alerts, and build diff |
69
71
  | [compressAssets](https://mengxi-studio.github.io/vite-plugin/en/plugins/compress-assets.html) | Asset compression with gzip / brotli / both, concurrent compression and statistics report |
70
72
  | [copyFile](https://mengxi-studio.github.io/vite-plugin/en/plugins/copy-file.html) | Copy files or directories after build, supports incremental copying |
71
- | [envGuard](https://mengxi-studio.github.io/vite-plugin/en/plugins/) | Environment variable validation with type checking, range validation, custom rules and runtime guard |
73
+ | [envGuard](https://mengxi-studio.github.io/vite-plugin/en/plugins/env-guard.html) | Environment variable validation with type checking, range validation, custom rules and runtime guard |
72
74
  | [faviconManager](https://mengxi-studio.github.io/vite-plugin/en/plugins/favicon-manager.html) | Manage website favicon link injection and file copying, supports string shorthand config |
73
- | [generateRouter](https://mengxi-studio.github.io/vite-plugin/en/plugins/generate-router.html) | Auto-generate route config from pages.json (uni-app) |
75
+ | [generateRouter](https://mengxi-studio.github.io/vite-plugin/en/plugins/generate-router.html) | Auto-generate route config and type declarations from pages.json (uni-app) |
74
76
  | [generateVersion](https://mengxi-studio.github.io/vite-plugin/en/plugins/generate-version.html) | Auto-generate version numbers with file output and global variable injection |
75
77
  | [htmlInject](https://mengxi-studio.github.io/vite-plugin/en/plugins/html-inject.html) | HTML content injection with multiple positions, selector targeting, conditional injection, template variables, and security filtering |
76
78
  | [loadingManager](https://mengxi-studio.github.io/vite-plugin/en/plugins/loading-manager.html) | Global Loading state management with request interception, debounce, transition animations, and white-screen Loading |
@@ -130,22 +132,33 @@ export const myPlugin = createPluginFactory(MyPlugin)
130
132
  Built-in general-purpose utility function library, organized by functional modules, supporting on-demand sub-path imports.
131
133
 
132
134
  ```typescript
133
- import { formatFileSize } from '@meng-xi/vite-plugin/common/format'
134
- import { scanDirectory } from '@meng-xi/vite-plugin/common/fs'
135
- import { calculateGzipSize } from '@meng-xi/vite-plugin/common/compress'
135
+ // Formatting: date params, template variable replacement, file size, date formatting
136
+ import { getDateFormatParams, parseTemplate, formatDate, formatFileSize } from '@meng-xi/vite-plugin/common/format'
137
+
138
+ // File system: file copy, directory scan, safe write, change detection
139
+ import { copySourceToTarget, scanDirectory, writeFileSyncSafely, shouldUpdateFileContent } from '@meng-xi/vite-plugin/common/fs'
140
+
141
+ // HTML: tag injection, content sanitization, attribute escaping
142
+ import { injectBeforeTag, injectHeadAndBody, sanitizeContent, escapeHtmlAttr } from '@meng-xi/vite-plugin/common/html'
143
+
144
+ // Script generation: callback function wrapping
145
+ import { makeCallback } from '@meng-xi/vite-plugin/common/script'
146
+
147
+ // Terminal UI: ANSI color codes
148
+ import { ANSI } from '@meng-xi/vite-plugin/common/ui'
149
+
150
+ // Validation: chain validator, common validation functions
151
+ import { Validator, validateGlobalName, validateNoScriptInTemplate } from '@meng-xi/vite-plugin/common/validation'
136
152
  ```
137
153
 
138
- | Sub-path | Description |
139
- | -------------------------------------------------------------------------------------------- | ----------------------- |
140
- | [`common/compress`](https://mengxi-studio.github.io/vite-plugin/en/common/compress.html) | Compression utilities |
141
- | [`common/format`](https://mengxi-studio.github.io/vite-plugin/en/common/format.html) | Formatting utilities |
142
- | [`common/fs`](https://mengxi-studio.github.io/vite-plugin/en/common/fs.html) | File system utilities |
143
- | [`common/html`](https://mengxi-studio.github.io/vite-plugin/en/common/html.html) | HTML injection utils |
144
- | [`common/object`](https://mengxi-studio.github.io/vite-plugin/en/common/object.html) | Object operation utils |
145
- | [`common/path`](https://mengxi-studio.github.io/vite-plugin/en/common/path.html) | Path handling utils |
146
- | [`common/script`](https://mengxi-studio.github.io/vite-plugin/en/common/script.html) | Script generation utils |
147
- | [`common/ui`](https://mengxi-studio.github.io/vite-plugin/en/common/) | Terminal UI utils |
148
- | [`common/validation`](https://mengxi-studio.github.io/vite-plugin/en/common/validation.html) | Validation utilities |
154
+ | Sub-path | Description |
155
+ | -------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------- |
156
+ | [`common/format`](https://mengxi-studio.github.io/vite-plugin/en/common/format.html) | Date param extraction, template variable replacement `{{key}}`, date formatting `{YYYY}`, file size |
157
+ | [`common/fs`](https://mengxi-studio.github.io/vite-plugin/en/common/fs.html) | File/directory copy, directory scan, sync safe write, file change detection |
158
+ | [`common/html`](https://mengxi-studio.github.io/vite-plugin/en/common/html.html) | HTML tag injection, dual-zone injection, content sanitization, HTML attribute value escaping |
159
+ | [`common/script`](https://mengxi-studio.github.io/vite-plugin/en/common/script.html) | Callback body wrapping into safe function expressions (with try-catch) |
160
+ | [`common/ui`](https://mengxi-studio.github.io/vite-plugin/en/common/ui.html) | Terminal ANSI color code constants |
161
+ | [`common/validation`](https://mengxi-studio.github.io/vite-plugin/en/common/validation.html) | Chain-style config validator, global name validation, script detection, callback field validation |
149
162
 
150
163
  ## Sub-path Exports
151
164
 
@@ -157,6 +170,7 @@ import { calculateGzipSize } from '@meng-xi/vite-plugin/common/compress'
157
170
  | `@meng-xi/vite-plugin/plugins` | All plugins |
158
171
  | `@meng-xi/vite-plugin/common` | All utility functions |
159
172
  | `@meng-xi/vite-plugin/common/*` | Utility sub-modules |
173
+ | `@meng-xi/vite-plugin/plugins/auto-import` | autoImport plugin |
160
174
  | `@meng-xi/vite-plugin/plugins/build-progress` | buildProgress plugin |
161
175
  | `@meng-xi/vite-plugin/plugins/bundle-analyzer` | bundleAnalyzer plugin |
162
176
  | `@meng-xi/vite-plugin/plugins/compress-assets` | compressAssets plugin |
package/README.md CHANGED
@@ -15,9 +15,9 @@
15
15
 
16
16
  ## 特性
17
17
 
18
- - **开箱即用** - 11 个实用插件,覆盖构建进度、产物分析与压缩、文件复制、环境变量校验、路由生成、版本管理、HTML 注入、图标管理、全局 Loading 等场景
18
+ - **开箱即用** - 12 个实用插件,覆盖自动导入、构建进度、产物分析与压缩、文件复制、环境变量校验、路由生成、版本管理、HTML 注入、图标管理、全局 Loading 等场景
19
19
  - **插件开发框架** - 导出 BasePlugin、Logger、Validator 等核心组件,快速构建自定义 Vite 插件
20
- - **通用工具库** - 内置 Common 工具模块,支持按需子路径导入
20
+ - **通用工具库** - 内置 6 大 Common 工具模块,支持按需子路径导入
21
21
  - **类型安全** - 完整 TypeScript 类型定义与配置验证器
22
22
  - **按需导入** - 支持子路径导出,减少打包体积
23
23
 
@@ -40,10 +40,11 @@ pnpm add @meng-xi/vite-plugin -D
40
40
 
41
41
  ```typescript
42
42
  import { defineConfig } from 'vite'
43
- import { buildProgress, bundleAnalyzer, compressAssets, copyFile, envGuard, generateRouter, generateVersion, versionUpdateChecker, htmlInject, faviconManager, loadingManager } from '@meng-xi/vite-plugin'
43
+ import { autoImport, buildProgress, bundleAnalyzer, compressAssets, copyFile, envGuard, generateRouter, generateVersion, versionUpdateChecker, htmlInject, faviconManager, loadingManager } from '@meng-xi/vite-plugin'
44
44
 
45
45
  export default defineConfig({
46
46
  plugins: [
47
+ autoImport({ imports: { vue: ['ref', 'reactive', 'computed'] }, dts: 'src/auto-imports.d.ts' }),
47
48
  buildProgress(),
48
49
  bundleAnalyzer({ outputFormat: 'both' }),
49
50
  compressAssets({ algorithm: 'gzip' }),
@@ -63,13 +64,14 @@ export default defineConfig({
63
64
 
64
65
  | 插件 | 说明 |
65
66
  | ------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------- |
67
+ | [autoImport](https://mengxi-studio.github.io/vite-plugin/plugins/auto-import.html) | 自动注入 import 语句,支持预设映射、目录扫描和 Vue 模板自动导入 |
66
68
  | [buildProgress](https://mengxi-studio.github.io/vite-plugin/plugins/build-progress.html) | 终端实时构建进度条,支持 bar / spinner / minimal |
67
69
  | [bundleAnalyzer](https://mengxi-studio.github.io/vite-plugin/plugins/bundle-analyzer.html) | 构建产物体积分析,支持 JSON/HTML 报告、gzip 计算、阈值告警和构建对比 |
68
70
  | [compressAssets](https://mengxi-studio.github.io/vite-plugin/plugins/compress-assets.html) | 构建产物压缩,支持 gzip / brotli / both,并发压缩和统计报告 |
69
71
  | [copyFile](https://mengxi-studio.github.io/vite-plugin/plugins/copy-file.html) | 构建完成后复制文件或目录,支持增量复制 |
70
- | [envGuard](https://mengxi-studio.github.io/vite-plugin/plugins/) | 环境变量校验,支持类型检查、范围验证、自定义规则和运行时守卫 |
72
+ | [envGuard](https://mengxi-studio.github.io/vite-plugin/plugins/env-guard.html) | 环境变量校验,支持类型检查、范围验证、自定义规则和运行时守卫 |
71
73
  | [faviconManager](https://mengxi-studio.github.io/vite-plugin/plugins/favicon-manager.html) | 管理网站图标链接注入和文件复制,支持字符串简写配置 |
72
- | [generateRouter](https://mengxi-studio.github.io/vite-plugin/plugins/generate-router.html) | 根据 pages.json 自动生成路由配置(uni-app) |
74
+ | [generateRouter](https://mengxi-studio.github.io/vite-plugin/plugins/generate-router.html) | 根据 pages.json 自动生成路由配置与类型声明(uni-app) |
73
75
  | [generateVersion](https://mengxi-studio.github.io/vite-plugin/plugins/generate-version.html) | 自动生成版本号,支持文件输出和全局变量注入 |
74
76
  | [htmlInject](https://mengxi-studio.github.io/vite-plugin/plugins/html-inject.html) | HTML 内容注入,支持多种位置、选择器定位、条件注入、模板变量和安全过滤 |
75
77
  | [loadingManager](https://mengxi-studio.github.io/vite-plugin/plugins/loading-manager.html) | 全局 Loading 状态管理,支持请求拦截、防抖、过渡动画和白屏 Loading |
@@ -129,22 +131,33 @@ export const myPlugin = createPluginFactory(MyPlugin)
129
131
  内置通用工具函数库,按功能模块组织,支持子路径按需导入。
130
132
 
131
133
  ```typescript
132
- import { formatFileSize } from '@meng-xi/vite-plugin/common/format'
133
- import { scanDirectory } from '@meng-xi/vite-plugin/common/fs'
134
- import { calculateGzipSize } from '@meng-xi/vite-plugin/common/compress'
134
+ // 格式化:日期参数、模板变量替换、文件大小、日期格式化
135
+ import { getDateFormatParams, parseTemplate, formatDate, formatFileSize } from '@meng-xi/vite-plugin/common/format'
136
+
137
+ // 文件系统:文件复制、目录扫描、安全写入、变更检测
138
+ import { copySourceToTarget, scanDirectory, writeFileSyncSafely, shouldUpdateFileContent } from '@meng-xi/vite-plugin/common/fs'
139
+
140
+ // HTML:标签注入、内容消毒、属性转义
141
+ import { injectBeforeTag, injectHeadAndBody, sanitizeContent, escapeHtmlAttr } from '@meng-xi/vite-plugin/common/html'
142
+
143
+ // 脚本生成:回调函数包装
144
+ import { makeCallback } from '@meng-xi/vite-plugin/common/script'
145
+
146
+ // 终端 UI:ANSI 颜色码
147
+ import { ANSI } from '@meng-xi/vite-plugin/common/ui'
148
+
149
+ // 参数验证:链式验证器、通用校验函数
150
+ import { Validator, validateGlobalName, validateNoScriptInTemplate } from '@meng-xi/vite-plugin/common/validation'
135
151
  ```
136
152
 
137
- | 子路径 | 描述 |
138
- | ----------------------------------------------------------------------------------------- | ------------- |
139
- | [`common/compress`](https://mengxi-studio.github.io/vite-plugin/common/compress.html) | 压缩算法工具 |
140
- | [`common/format`](https://mengxi-studio.github.io/vite-plugin/common/format.html) | 格式化工具 |
141
- | [`common/fs`](https://mengxi-studio.github.io/vite-plugin/common/fs.html) | 文件系统工具 |
142
- | [`common/html`](https://mengxi-studio.github.io/vite-plugin/common/html.html) | HTML 注入工具 |
143
- | [`common/object`](https://mengxi-studio.github.io/vite-plugin/common/object.html) | 对象操作工具 |
144
- | [`common/path`](https://mengxi-studio.github.io/vite-plugin/common/path.html) | 路径处理工具 |
145
- | [`common/script`](https://mengxi-studio.github.io/vite-plugin/common/script.html) | 脚本生成工具 |
146
- | [`common/ui`](https://mengxi-studio.github.io/vite-plugin/common/) | 终端 UI 工具 |
147
- | [`common/validation`](https://mengxi-studio.github.io/vite-plugin/common/validation.html) | 参数验证工具 |
153
+ | 子路径 | 描述 |
154
+ | ----------------------------------------------------------------------------------------- | ------------------------------------------------------------------------- |
155
+ | [`common/format`](https://mengxi-studio.github.io/vite-plugin/common/format.html) | 日期参数提取、模板变量替换 `{{key}}`、日期格式化 `{YYYY}`、文件大小格式化 |
156
+ | [`common/fs`](https://mengxi-studio.github.io/vite-plugin/common/fs.html) | 文件/目录复制、目录扫描、同步安全写入、文件变更检测 |
157
+ | [`common/html`](https://mengxi-studio.github.io/vite-plugin/common/html.html) | HTML 标签注入、双区域注入、内容安全消毒、HTML 属性值转义 |
158
+ | [`common/script`](https://mengxi-studio.github.io/vite-plugin/common/script.html) | 回调函数体包装为安全的函数表达式(含 try-catch) |
159
+ | [`common/ui`](https://mengxi-studio.github.io/vite-plugin/common/ui.html) | 终端 ANSI 颜色码常量 |
160
+ | [`common/validation`](https://mengxi-studio.github.io/vite-plugin/common/validation.html) | 链式配置验证器、全局名称校验、脚本检测、回调字段校验 |
148
161
 
149
162
  ## 子路径导出
150
163
 
@@ -156,6 +169,7 @@ import { calculateGzipSize } from '@meng-xi/vite-plugin/common/compress'
156
169
  | `@meng-xi/vite-plugin/plugins` | 所有插件 |
157
170
  | `@meng-xi/vite-plugin/common` | 所有工具函数 |
158
171
  | `@meng-xi/vite-plugin/common/*` | 各工具子模块 |
172
+ | `@meng-xi/vite-plugin/plugins/auto-import` | autoImport 插件 |
159
173
  | `@meng-xi/vite-plugin/plugins/build-progress` | buildProgress 插件 |
160
174
  | `@meng-xi/vite-plugin/plugins/bundle-analyzer` | bundleAnalyzer 插件 |
161
175
  | `@meng-xi/vite-plugin/plugins/compress-assets` | compressAssets 插件 |
@@ -1 +1 @@
1
- "use strict";const crypto=require("crypto"),a=require("node:path");function _interopDefaultCompat(e){return e&&typeof e=="object"&&"default"in e?e.default:e}const a__default=_interopDefaultCompat(a);function padNumber(e,t=2){return e.toString().padStart(t,"0")}function generateRandomHash(e=8){const t=Math.max(1,Math.min(64,e));return crypto.randomBytes(Math.ceil(t/2)).toString("hex").slice(0,t)}function getDateFormatParams(e=new Date){return{YYYY:e.getFullYear().toString(),YY:e.getFullYear().toString().slice(-2),MM:padNumber(e.getMonth()+1),DD:padNumber(e.getDate()),HH:padNumber(e.getHours()),mm:padNumber(e.getMinutes()),ss:padNumber(e.getSeconds()),SSS:padNumber(e.getMilliseconds(),3),timestamp:e.getTime().toString()}}function formatDate(e,t){const r=getDateFormatParams(e);let o=t;for(const[n,s]of Object.entries(r))o=o.replace(new RegExp(`\\{${n}\\}`,"g"),s);return o}function parseTemplate(e,t){let r=e;for(const[o,n]of Object.entries(t))r=r.replace(new RegExp(`\\{${o}\\}`,"g"),n);return r}function toCamelCase(e,t=/[/-]/){return e.replace(/^\/+/,"").split(t).filter(Boolean).map((r,o)=>o===0?r.toLowerCase():r.charAt(0).toUpperCase()+r.slice(1).toLowerCase()).join("")}function toPascalCase(e,t=/[/-]/){return e.replace(/^\/+/,"").split(t).filter(Boolean).map(r=>r.charAt(0).toUpperCase()+r.slice(1).toLowerCase()).join("")}function stripJsonComments(e){return e.replace(/\/\/.*$/gm,"").replace(/\/\*[\s\S]*?\*\//g,"")}function escapeHtmlAttr(e){return e.replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function formatFileSize(e){return e<1024?`${e}B`:e<1024*1024?`${(e/1024).toFixed(1)}KB`:`${(e/(1024*1024)).toFixed(2)}MB`}function getExtension(e){return a__default.extname(e).toLowerCase()}exports.escapeHtmlAttr=escapeHtmlAttr,exports.formatDate=formatDate,exports.formatFileSize=formatFileSize,exports.generateRandomHash=generateRandomHash,exports.getDateFormatParams=getDateFormatParams,exports.getExtension=getExtension,exports.padNumber=padNumber,exports.parseTemplate=parseTemplate,exports.stripJsonComments=stripJsonComments,exports.toCamelCase=toCamelCase,exports.toPascalCase=toPascalCase;
1
+ "use strict";function t(e,r=2){return e.toString().padStart(r,"0")}function getDateFormatParams(e=new Date){return{YYYY:e.getFullYear().toString(),YY:e.getFullYear().toString().slice(-2),MM:t(e.getMonth()+1),DD:t(e.getDate()),HH:t(e.getHours()),mm:t(e.getMinutes()),ss:t(e.getSeconds()),SSS:t(e.getMilliseconds(),3),timestamp:e.getTime().toString()}}function parseTemplate(e,r){let o=e;for(const[n,a]of Object.entries(r)){const s=n.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),i=a.replace(/\$/g,"$$$$");o=o.replace(new RegExp(`\\{\\{${s}\\}\\}`,"g"),i)}return o}function formatDate(e,r){const o=getDateFormatParams(e);let n=r;for(const[a,s]of Object.entries(o))n=n.replace(new RegExp(`\\{${a}\\}`,"g"),s);return n}function formatFileSize(e){return e<1024?`${e}B`:e<1024*1024?`${(e/1024).toFixed(1)}KB`:`${(e/(1024*1024)).toFixed(2)}MB`}exports.formatDate=formatDate,exports.formatFileSize=formatFileSize,exports.getDateFormatParams=getDateFormatParams,exports.parseTemplate=parseTemplate;
@@ -22,34 +22,6 @@ interface DateFormatOptions {
22
22
  timestamp: string;
23
23
  }
24
24
 
25
- /**
26
- * 数字补零格式化
27
- *
28
- * @param num 要格式化的数字
29
- * @param length 目标长度
30
- * @returns 补零后的字符串
31
- *
32
- * @example
33
- * ```typescript
34
- * padNumber(5, 2) // '05'
35
- * padNumber(12, 3) // '012'
36
- * padNumber(123, 2) // '123'
37
- * ```
38
- */
39
- declare function padNumber(num: number, length?: number): string;
40
- /**
41
- * 生成随机哈希字符串
42
- *
43
- * @param length 哈希长度,范围 1-64
44
- * @returns 随机哈希字符串
45
- *
46
- * @example
47
- * ```typescript
48
- * generateRandomHash(8) // 'a1b2c3d4'
49
- * generateRandomHash(16) // 'a1b2c3d4e5f6g7h8'
50
- * ```
51
- */
52
- declare function generateRandomHash(length?: number): string;
53
25
  /**
54
26
  * 获取日期格式化参数
55
27
  *
@@ -64,93 +36,39 @@ declare function generateRandomHash(length?: number): string;
64
36
  */
65
37
  declare function getDateFormatParams(date?: Date): DateFormatOptions;
66
38
  /**
67
- * 格式化日期
39
+ * 替换模板字符串中的变量占位符
68
40
  *
69
- * @param date 日期对象
70
- * @param format 格式模板
71
- * @returns 格式化后的日期字符串
72
- *
73
- * @example
74
- * ```typescript
75
- * formatDate(new Date(), '{YYYY}-{MM}-{DD}') // '2026-02-03'
76
- * formatDate(new Date(), '{YYYY}{MM}{DD}{HH}{mm}{ss}') // '20260203153000'
77
- * formatDate(new Date(), '{YYYY}.{MM}.{DD}') // '2026.02.03'
78
- * ```
79
- */
80
- declare function formatDate(date: Date, format: string): string;
81
- /**
82
- * 解析模板字符串,替换占位符
41
+ * @param template - 包含 `{{key}}` 占位符的模板字符串
42
+ * @param values - 占位符键值映射,支持合并多组变量(后者覆盖前者)
43
+ * @returns 替换占位符后的字符串
83
44
  *
84
- * @param template 模板字符串
85
- * @param values 占位符值映射
86
- * @returns 替换后的字符串
45
+ * @description 将模板中的 `{{key}}` 占位符替换为对应的值。
46
+ * 键名中的正则特殊字符会被自动转义,值中的 `$` 也会被安全处理。
87
47
  *
88
48
  * @example
89
49
  * ```typescript
90
- * parseTemplate('{name}-{version}', { name: 'app', version: '1.0.0' })
91
- * // 'app-1.0.0'
92
- * ```
93
- */
94
- declare function parseTemplate(template: string, values: Record<string, string>): string;
95
- /**
96
- * 将字符串转换为驼峰命名(camelCase)
97
- *
98
- * @param str 输入字符串
99
- * @param separators 分隔符正则,默认为斜杠和横线
100
- * @returns 驼峰命名字符串
50
+ * parseTemplate('Hello {{name}}!', { name: 'World' })
51
+ * // 'Hello World!'
101
52
  *
102
- * @example
103
- * ```typescript
104
- * toCamelCase('pages/user/profile') // 'pagesUserProfile'
105
- * toCamelCase('user-profile-page') // 'userProfilePage'
106
- * toCamelCase('/pages/index') // 'pagesIndex'
107
- * ```
108
- */
109
- declare function toCamelCase(str: string, separators?: RegExp): string;
110
- /**
111
- * 将字符串转换为帕斯卡命名(PascalCase)
112
- *
113
- * @param str 输入字符串
114
- * @param separators 分隔符正则,默认为斜杠和横线
115
- * @returns 帕斯卡命名字符串
116
- *
117
- * @example
118
- * ```typescript
119
- * toPascalCase('pages/user/profile') // 'PagesUserProfile'
120
- * toPascalCase('user-profile-page') // 'UserProfilePage'
121
- * toPascalCase('/pages/index') // 'PagesIndex'
53
+ * parseTemplate('{{YYYY}}-{{MM}}-{{DD}}', getDateFormatParams())
54
+ * // '2026-06-06'
122
55
  * ```
123
56
  */
124
- declare function toPascalCase(str: string, separators?: RegExp): string;
125
- /**
126
- * 移除 JSON 字符串中的注释
127
- *
128
- * @param jsonString 包含注释的 JSON 字符串
129
- * @returns 移除注释后的 JSON 字符串
130
- *
131
- * @example
132
- * ```typescript
133
- * stripJsonComments('{\n // comment\n "name": "test"\n}')
134
- * // '{\n "name": "test"\n}'
135
- * ```
136
- */
137
- declare function stripJsonComments(jsonString: string): string;
57
+ declare function parseTemplate(template: string, values: Record<string, string>): string;
138
58
  /**
139
- * 转义 HTML 属性值中的特殊字符,防止 XSS 注入
59
+ * 格式化日期字符串
140
60
  *
141
- * @param str - 需要转义的字符串
142
- * @returns 转义后的安全字符串
61
+ * @param date - 日期对象
62
+ * @param format - 格式字符串,支持 `{YYYY}`、`{MM}`、`{DD}`、`{HH}`、`{mm}`、`{ss}` 等占位符
63
+ * @returns 格式化后的日期字符串
143
64
  *
144
65
  * @example
145
66
  * ```typescript
146
- * escapeHtmlAttr('hello "world"')
147
- * // 'hello &quot;world&quot;'
148
- *
149
- * escapeHtmlAttr('<script>alert(1)</script>')
150
- * // '&lt;script&gt;alert(1)&lt;/script&gt;'
67
+ * formatDate(new Date(), '{YYYY}-{MM}-{DD}T{HH}:{mm}:{ss}')
68
+ * // '2026-06-06T15:30:00'
151
69
  * ```
152
70
  */
153
- declare function escapeHtmlAttr(str: string): string;
71
+ declare function formatDate(date: Date, format: string): string;
154
72
  /**
155
73
  * 将字节数格式化为人类可读的文件大小字符串
156
74
  *
@@ -170,19 +88,6 @@ declare function escapeHtmlAttr(str: string): string;
170
88
  * ```
171
89
  */
172
90
  declare function formatFileSize(bytes: number): string;
173
- /**
174
- * 获取文件扩展名
175
- *
176
- * @param {string} filePath - 文件路径
177
- * @returns {string} 小写的文件扩展名(如 '.js')
178
- *
179
- * @example
180
- * ```typescript
181
- * getExtension('dist/app.js') // '.js'
182
- * getExtension('dist/style.CSS') // '.css'
183
- * ```
184
- */
185
- declare function getExtension(filePath: string): string;
186
91
 
187
- export { escapeHtmlAttr, formatDate, formatFileSize, generateRandomHash, getDateFormatParams, getExtension, padNumber, parseTemplate, stripJsonComments, toCamelCase, toPascalCase };
92
+ export { formatDate, formatFileSize, getDateFormatParams, parseTemplate };
188
93
  export type { DateFormatOptions };
@@ -22,34 +22,6 @@ interface DateFormatOptions {
22
22
  timestamp: string;
23
23
  }
24
24
 
25
- /**
26
- * 数字补零格式化
27
- *
28
- * @param num 要格式化的数字
29
- * @param length 目标长度
30
- * @returns 补零后的字符串
31
- *
32
- * @example
33
- * ```typescript
34
- * padNumber(5, 2) // '05'
35
- * padNumber(12, 3) // '012'
36
- * padNumber(123, 2) // '123'
37
- * ```
38
- */
39
- declare function padNumber(num: number, length?: number): string;
40
- /**
41
- * 生成随机哈希字符串
42
- *
43
- * @param length 哈希长度,范围 1-64
44
- * @returns 随机哈希字符串
45
- *
46
- * @example
47
- * ```typescript
48
- * generateRandomHash(8) // 'a1b2c3d4'
49
- * generateRandomHash(16) // 'a1b2c3d4e5f6g7h8'
50
- * ```
51
- */
52
- declare function generateRandomHash(length?: number): string;
53
25
  /**
54
26
  * 获取日期格式化参数
55
27
  *
@@ -64,93 +36,39 @@ declare function generateRandomHash(length?: number): string;
64
36
  */
65
37
  declare function getDateFormatParams(date?: Date): DateFormatOptions;
66
38
  /**
67
- * 格式化日期
39
+ * 替换模板字符串中的变量占位符
68
40
  *
69
- * @param date 日期对象
70
- * @param format 格式模板
71
- * @returns 格式化后的日期字符串
72
- *
73
- * @example
74
- * ```typescript
75
- * formatDate(new Date(), '{YYYY}-{MM}-{DD}') // '2026-02-03'
76
- * formatDate(new Date(), '{YYYY}{MM}{DD}{HH}{mm}{ss}') // '20260203153000'
77
- * formatDate(new Date(), '{YYYY}.{MM}.{DD}') // '2026.02.03'
78
- * ```
79
- */
80
- declare function formatDate(date: Date, format: string): string;
81
- /**
82
- * 解析模板字符串,替换占位符
41
+ * @param template - 包含 `{{key}}` 占位符的模板字符串
42
+ * @param values - 占位符键值映射,支持合并多组变量(后者覆盖前者)
43
+ * @returns 替换占位符后的字符串
83
44
  *
84
- * @param template 模板字符串
85
- * @param values 占位符值映射
86
- * @returns 替换后的字符串
45
+ * @description 将模板中的 `{{key}}` 占位符替换为对应的值。
46
+ * 键名中的正则特殊字符会被自动转义,值中的 `$` 也会被安全处理。
87
47
  *
88
48
  * @example
89
49
  * ```typescript
90
- * parseTemplate('{name}-{version}', { name: 'app', version: '1.0.0' })
91
- * // 'app-1.0.0'
92
- * ```
93
- */
94
- declare function parseTemplate(template: string, values: Record<string, string>): string;
95
- /**
96
- * 将字符串转换为驼峰命名(camelCase)
97
- *
98
- * @param str 输入字符串
99
- * @param separators 分隔符正则,默认为斜杠和横线
100
- * @returns 驼峰命名字符串
50
+ * parseTemplate('Hello {{name}}!', { name: 'World' })
51
+ * // 'Hello World!'
101
52
  *
102
- * @example
103
- * ```typescript
104
- * toCamelCase('pages/user/profile') // 'pagesUserProfile'
105
- * toCamelCase('user-profile-page') // 'userProfilePage'
106
- * toCamelCase('/pages/index') // 'pagesIndex'
107
- * ```
108
- */
109
- declare function toCamelCase(str: string, separators?: RegExp): string;
110
- /**
111
- * 将字符串转换为帕斯卡命名(PascalCase)
112
- *
113
- * @param str 输入字符串
114
- * @param separators 分隔符正则,默认为斜杠和横线
115
- * @returns 帕斯卡命名字符串
116
- *
117
- * @example
118
- * ```typescript
119
- * toPascalCase('pages/user/profile') // 'PagesUserProfile'
120
- * toPascalCase('user-profile-page') // 'UserProfilePage'
121
- * toPascalCase('/pages/index') // 'PagesIndex'
53
+ * parseTemplate('{{YYYY}}-{{MM}}-{{DD}}', getDateFormatParams())
54
+ * // '2026-06-06'
122
55
  * ```
123
56
  */
124
- declare function toPascalCase(str: string, separators?: RegExp): string;
125
- /**
126
- * 移除 JSON 字符串中的注释
127
- *
128
- * @param jsonString 包含注释的 JSON 字符串
129
- * @returns 移除注释后的 JSON 字符串
130
- *
131
- * @example
132
- * ```typescript
133
- * stripJsonComments('{\n // comment\n "name": "test"\n}')
134
- * // '{\n "name": "test"\n}'
135
- * ```
136
- */
137
- declare function stripJsonComments(jsonString: string): string;
57
+ declare function parseTemplate(template: string, values: Record<string, string>): string;
138
58
  /**
139
- * 转义 HTML 属性值中的特殊字符,防止 XSS 注入
59
+ * 格式化日期字符串
140
60
  *
141
- * @param str - 需要转义的字符串
142
- * @returns 转义后的安全字符串
61
+ * @param date - 日期对象
62
+ * @param format - 格式字符串,支持 `{YYYY}`、`{MM}`、`{DD}`、`{HH}`、`{mm}`、`{ss}` 等占位符
63
+ * @returns 格式化后的日期字符串
143
64
  *
144
65
  * @example
145
66
  * ```typescript
146
- * escapeHtmlAttr('hello "world"')
147
- * // 'hello &quot;world&quot;'
148
- *
149
- * escapeHtmlAttr('<script>alert(1)</script>')
150
- * // '&lt;script&gt;alert(1)&lt;/script&gt;'
67
+ * formatDate(new Date(), '{YYYY}-{MM}-{DD}T{HH}:{mm}:{ss}')
68
+ * // '2026-06-06T15:30:00'
151
69
  * ```
152
70
  */
153
- declare function escapeHtmlAttr(str: string): string;
71
+ declare function formatDate(date: Date, format: string): string;
154
72
  /**
155
73
  * 将字节数格式化为人类可读的文件大小字符串
156
74
  *
@@ -170,19 +88,6 @@ declare function escapeHtmlAttr(str: string): string;
170
88
  * ```
171
89
  */
172
90
  declare function formatFileSize(bytes: number): string;
173
- /**
174
- * 获取文件扩展名
175
- *
176
- * @param {string} filePath - 文件路径
177
- * @returns {string} 小写的文件扩展名(如 '.js')
178
- *
179
- * @example
180
- * ```typescript
181
- * getExtension('dist/app.js') // '.js'
182
- * getExtension('dist/style.CSS') // '.css'
183
- * ```
184
- */
185
- declare function getExtension(filePath: string): string;
186
91
 
187
- export { escapeHtmlAttr, formatDate, formatFileSize, generateRandomHash, getDateFormatParams, getExtension, padNumber, parseTemplate, stripJsonComments, toCamelCase, toPascalCase };
92
+ export { formatDate, formatFileSize, getDateFormatParams, parseTemplate };
188
93
  export type { DateFormatOptions };