jinbi-utils 1.0.19 → 1.0.21

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 (81) hide show
  1. package/.babelrc +19 -0
  2. package/.cz-config.js +55 -0
  3. package/.dockerignore +3 -0
  4. package/.editorconfig +12 -0
  5. package/.eslintignore +8 -0
  6. package/.eslintrc.js +54 -0
  7. package/.versionrc.json +9 -0
  8. package/CHUNK_OPTIMIZER_USAGE.md +132 -0
  9. package/Dockerfile +3 -0
  10. package/QUICK_RELEASE.md +85 -0
  11. package/RELEASE_GUIDE.md +243 -0
  12. package/api-extractor.json +15 -0
  13. package/commitlint.config.js +3 -0
  14. package/jest.config.js +15 -0
  15. package/package.json +8 -35
  16. package/rollup.config.chunk-optimizer.js +32 -0
  17. package/rollup.config.js +73 -0
  18. package/src/array/index.ts +85 -0
  19. package/src/build/chunk-optimizer/ARCHITECTURE.md +347 -0
  20. package/src/build/chunk-optimizer/QUICK_START.md +370 -0
  21. package/src/build/chunk-optimizer/README.md +240 -0
  22. package/src/build/chunk-optimizer/core/chunk-generator.ts +166 -0
  23. package/src/build/chunk-optimizer/core/classifier.ts +148 -0
  24. package/src/build/chunk-optimizer/core/dependency-reader.ts +138 -0
  25. package/src/build/chunk-optimizer/examples/basic-usage.ts +234 -0
  26. package/src/build/chunk-optimizer/index.ts +166 -0
  27. package/src/build/chunk-optimizer/rules/common-rules.ts +131 -0
  28. package/src/build/chunk-optimizer/rules/framework-rules.ts +93 -0
  29. package/src/build/chunk-optimizer/rules/index.ts +27 -0
  30. package/src/build/chunk-optimizer/test.ts +94 -0
  31. package/src/build/chunk-optimizer/types.ts +128 -0
  32. package/src/color/index.ts +58 -0
  33. package/src/common/index.ts +353 -0
  34. package/src/constant/common.constant.ts +13 -0
  35. package/src/date/index.ts +143 -0
  36. package/src/dom/index.ts +198 -0
  37. package/src/file/index.ts +319 -0
  38. package/src/http/apiBuilder/README.md +648 -0
  39. package/src/http/apiBuilder/api-builder.ts +502 -0
  40. package/src/http/apiBuilder/example.ts +243 -0
  41. package/src/http/apiBuilder/index.ts +1 -0
  42. package/src/http/apiBuilder//345/277/253/351/200/237/345/217/202/350/200/203.md +199 -0
  43. package/src/http/http.ts +79 -0
  44. package/src/http/httpEnums.ts +61 -0
  45. package/src/iam/index.ts +46 -0
  46. package/src/index.ts +20 -0
  47. package/src/middleware/requestLogger.middware.ts +371 -0
  48. package/src/middleware/requestLoggerUnified.ts +371 -0
  49. package/src/number/index.ts +362 -0
  50. package/src/object/index.ts +54 -0
  51. package/src/print/index.ts +102 -0
  52. package/src/string/index.ts +189 -0
  53. package/src/utils/curl.ts +108 -0
  54. package/src/validate/index.ts +100 -0
  55. package/src/websocket/emitter.ts +39 -0
  56. package/src/websocket/index.ts +6 -0
  57. package/src/websocket/manager.ts +151 -0
  58. package/src/websocket/pinia-store.ts +91 -0
  59. package/src/websocket/service.ts +34 -0
  60. package/src/websocket/types.ts +45 -0
  61. package/test/common/index.test.ts +19 -0
  62. package/test/date/index.test.ts +107 -0
  63. package/test/file/index.test.ts +104 -0
  64. package/test/number/index.test.ts +108 -0
  65. package/test/object/index.test.ts +20 -0
  66. package/test/string/index.test.ts +82 -0
  67. package/tsconfig.json +39 -0
  68. package/typedoc.json +12 -0
  69. package/types/file/index.d.ts +7 -0
  70. package/types/index.d.ts +1 -0
  71. package/types/websocket/emitter.d.ts +16 -0
  72. package/types/websocket/index.d.ts +6 -0
  73. package/types/websocket/manager.d.ts +36 -0
  74. package/types/websocket/pinia-store.d.ts +25 -0
  75. package/types/websocket/service.d.ts +13 -0
  76. package/types/websocket/types.d.ts +34 -0
  77. package/dist/chunk-optimizer.cjs +0 -703
  78. package/dist/index.esm.js +0 -2791
  79. package/dist/index.esm.min.js +0 -15
  80. package/dist/index.umd.js +0 -2899
  81. package/dist/index.umd.min.js +0 -16
@@ -0,0 +1,166 @@
1
+ /**
2
+ * Vite Chunk Optimizer
3
+ * 智能分包优化工具 - 自动分析依赖并生成最优的 manualChunks 配置
4
+ *
5
+ * @author Your Name
6
+ * @license MIT
7
+ */
8
+
9
+ import type {
10
+ ChunkOptimizerOptions,
11
+ ManualChunksFunction,
12
+ AnalysisResult,
13
+ } from './types';
14
+ import { DependencyReader } from './core/dependency-reader';
15
+ import { DependencyClassifier } from './core/classifier';
16
+ import { ChunkGenerator } from './core/chunk-generator';
17
+
18
+ /**
19
+ * Chunk 优化器类
20
+ */
21
+ export class ChunkOptimizer {
22
+ private reader: DependencyReader;
23
+ private classifier: DependencyClassifier;
24
+ private generator: ChunkGenerator | null = null;
25
+ private options: Required<ChunkOptimizerOptions>;
26
+
27
+ constructor(options: ChunkOptimizerOptions = {}) {
28
+ // 设置默认选项
29
+ this.options = {
30
+ framework: options.framework || 'auto',
31
+ strategy: options.strategy || 'balanced',
32
+ customRules: options.customRules || {},
33
+ exclude: options.exclude || ['@types/*'],
34
+ minChunkSize: options.minChunkSize || 0,
35
+ debug: options.debug || false,
36
+ sourceCodeStrategy: {
37
+ views: true,
38
+ components: true,
39
+ utils: true,
40
+ store: true,
41
+ ...options.sourceCodeStrategy,
42
+ },
43
+ packageJsonPath: options.packageJsonPath || '',
44
+ };
45
+
46
+ // 初始化依赖读取器
47
+ this.reader = new DependencyReader(this.options.packageJsonPath);
48
+
49
+ // 自动检测框架
50
+ if (this.options.framework === 'auto') {
51
+ this.options.framework = this.reader.detectFramework();
52
+ if (this.options.debug) {
53
+ console.log(`🔍 Detected framework: ${this.options.framework}`);
54
+ }
55
+ }
56
+
57
+ // 初始化分类器
58
+ this.classifier = new DependencyClassifier(
59
+ this.options.framework,
60
+ this.options.customRules,
61
+ this.options.exclude
62
+ );
63
+ }
64
+
65
+ /**
66
+ * 分析依赖
67
+ */
68
+ analyze(): AnalysisResult {
69
+ const dependencies = this.reader.getDependencies();
70
+ const classified = this.classifier.classifyAll(dependencies);
71
+ const stats = this.classifier.getCategoryStats(classified);
72
+
73
+ return {
74
+ dependencies: classified,
75
+ detectedFramework: this.options.framework,
76
+ categoryStats: stats,
77
+ };
78
+ }
79
+
80
+ /**
81
+ * 生成 manualChunks 函数
82
+ */
83
+ generate(): ManualChunksFunction {
84
+ if (this.generator) {
85
+ return this.generator.generate();
86
+ }
87
+
88
+ // 分析依赖
89
+ const result = this.analyze();
90
+
91
+ // 如果开启调试模式,打印分类信息
92
+ if (this.options.debug) {
93
+ this.classifier.printClassification(result.dependencies);
94
+ }
95
+
96
+ // 创建生成器
97
+ this.generator = new ChunkGenerator(
98
+ result.dependencies,
99
+ this.options.sourceCodeStrategy,
100
+ this.options.debug
101
+ );
102
+
103
+ // 如果开启调试模式,打印统计信息
104
+ if (this.options.debug) {
105
+ this.generator.printStats();
106
+ }
107
+
108
+ return this.generator.generate();
109
+ }
110
+
111
+ /**
112
+ * 获取分析结果
113
+ */
114
+ getAnalysisResult(): AnalysisResult {
115
+ return this.analyze();
116
+ }
117
+
118
+ /**
119
+ * 打印分析报告
120
+ */
121
+ printReport(): void {
122
+ const result = this.analyze();
123
+
124
+ console.log('\n' + '='.repeat(60));
125
+ console.log('📦 Vite Chunk Optimizer - Analysis Report');
126
+ console.log('='.repeat(60) + '\n');
127
+
128
+ console.log(`Framework: ${result.detectedFramework}`);
129
+ console.log(`Strategy: ${this.options.strategy}`);
130
+ console.log(`Total Dependencies: ${result.dependencies.length}\n`);
131
+
132
+ console.log('Chunk Distribution:');
133
+ Object.entries(result.categoryStats)
134
+ .sort(([, a], [, b]) => b - a)
135
+ .forEach(([category, count]) => {
136
+ const percentage = ((count / result.dependencies.length) * 100).toFixed(1);
137
+ console.log(` ${category.padEnd(25)} ${count.toString().padStart(3)} (${percentage}%)`);
138
+ });
139
+
140
+ console.log('\n' + '='.repeat(60) + '\n');
141
+ }
142
+ }
143
+
144
+ /**
145
+ * 创建 Chunk 优化器(工厂函数)
146
+ */
147
+ export function createChunkOptimizer(
148
+ options: ChunkOptimizerOptions = {}
149
+ ): ChunkOptimizer {
150
+ return new ChunkOptimizer(options);
151
+ }
152
+
153
+ // 导出类型
154
+ export type {
155
+ ChunkOptimizerOptions,
156
+ ManualChunksFunction,
157
+ AnalysisResult,
158
+ Framework,
159
+ Strategy,
160
+ DependencyCategory,
161
+ SourceCodeStrategy,
162
+ } from './types';
163
+
164
+ // 默认导出
165
+ export default createChunkOptimizer;
166
+
@@ -0,0 +1,131 @@
1
+ /**
2
+ * 通用库的分包规则
3
+ */
4
+ import type { Rule } from '../types';
5
+
6
+ /**
7
+ * 工具库规则
8
+ */
9
+ export const utilsRules: Rule[] = [
10
+ // 日期时间
11
+ { match: 'dayjs', category: 'vendor-utils', priority: 60, description: '日期时间库' },
12
+ { match: 'moment', category: 'vendor-utils', priority: 60, description: 'Moment.js' },
13
+ { match: 'date-fns', category: 'vendor-utils', priority: 60, description: 'date-fns' },
14
+
15
+ // HTTP 请求
16
+ { match: 'axios', category: 'vendor-utils', priority: 70, description: 'Axios HTTP 客户端' },
17
+ { match: 'ky', category: 'vendor-utils', priority: 70, description: 'Ky HTTP 客户端' },
18
+
19
+ // 工具函数
20
+ { match: 'lodash', category: 'vendor-utils', priority: 60, description: 'Lodash' },
21
+ { match: 'lodash-es', category: 'vendor-utils', priority: 60, description: 'Lodash ES' },
22
+ { match: 'ramda', category: 'vendor-utils', priority: 60, description: 'Ramda' },
23
+
24
+ // Cookie & Storage
25
+ { match: 'js-cookie', category: 'vendor-utils', priority: 50, description: 'JS Cookie' },
26
+ { match: 'localforage', category: 'vendor-utils', priority: 50, description: 'LocalForage' },
27
+ { match: 'responsive-storage', category: 'vendor-utils', priority: 50, description: 'Responsive Storage' },
28
+
29
+ // 查询字符串
30
+ { match: 'qs', category: 'vendor-utils', priority: 50, description: 'Query String' },
31
+
32
+ // 事件总线
33
+ { match: 'mitt', category: 'vendor-utils', priority: 50, description: 'Mitt 事件总线' },
34
+ { match: 'eventemitter3', category: 'vendor-utils', priority: 50, description: 'EventEmitter3' },
35
+
36
+ // 数学计算
37
+ { match: 'decimal.js', category: 'vendor-utils', priority: 50, description: 'Decimal.js' },
38
+ { match: 'big.js', category: 'vendor-utils', priority: 50, description: 'Big.js' },
39
+
40
+ // 拖拽
41
+ { match: 'sortablejs', category: 'vendor-utils', priority: 50, description: 'SortableJS' },
42
+
43
+ // 动画
44
+ { match: 'animate.css', category: 'vendor-utils', priority: 40, description: 'Animate.css' },
45
+ { match: '@vueuse/motion', category: 'vendor-utils', priority: 40, description: 'VueUse Motion' },
46
+
47
+ // 其他工具
48
+ { match: 'nprogress', category: 'vendor-utils', priority: 40, description: 'NProgress' },
49
+ { match: 'path-browserify', category: 'vendor-utils', priority: 40, description: 'Path Browserify' },
50
+ ];
51
+
52
+ /**
53
+ * 图标库规则
54
+ */
55
+ export const iconRules: Rule[] = [
56
+ { match: /^@iconify\//, category: 'vendor-icons', priority: 60, description: 'Iconify' },
57
+ { match: 'unplugin-icons', category: 'vendor-icons', priority: 60, description: 'Unplugin Icons' },
58
+ { match: /-icons?$/, category: 'vendor-icons', priority: 50, description: '图标库(通用匹配)' },
59
+ ];
60
+
61
+ /**
62
+ * 表单和验证规则
63
+ */
64
+ export const formRules: Rule[] = [
65
+ { match: 'vee-validate', category: 'vendor-utils', priority: 50, description: 'VeeValidate' },
66
+ { match: 'yup', category: 'vendor-utils', priority: 50, description: 'Yup 验证' },
67
+ { match: 'zod', category: 'vendor-utils', priority: 50, description: 'Zod 验证' },
68
+ { match: '@zxcvbn-ts/core', category: 'vendor-utils', priority: 40, description: 'Zxcvbn 密码强度' },
69
+ ];
70
+
71
+ /**
72
+ * 图表库规则
73
+ */
74
+ export const chartRules: Rule[] = [
75
+ { match: 'echarts', category: 'vendor-charts', priority: 70, description: 'ECharts' },
76
+ { match: 'chart.js', category: 'vendor-charts', priority: 70, description: 'Chart.js' },
77
+ { match: 'd3', category: 'vendor-charts', priority: 70, description: 'D3.js' },
78
+ { match: /^@antv\//, category: 'vendor-charts', priority: 70, description: 'AntV 图表' },
79
+ ];
80
+
81
+ /**
82
+ * 富文本编辑器规则
83
+ */
84
+ export const editorRules: Rule[] = [
85
+ { match: 'quill', category: 'vendor-editor', priority: 60, description: 'Quill 编辑器' },
86
+ { match: '@wangeditor/editor', category: 'vendor-editor', priority: 60, description: 'wangEditor' },
87
+ { match: 'tinymce', category: 'vendor-editor', priority: 60, description: 'TinyMCE' },
88
+ { match: '@tiptap/vue-3', category: 'vendor-editor', priority: 60, description: 'Tiptap' },
89
+ ];
90
+
91
+ /**
92
+ * 表格库规则
93
+ */
94
+ export const tableRules: Rule[] = [
95
+ { match: 'vxe-table', category: 'vendor-table', priority: 70, description: 'VXE Table' },
96
+ { match: 'ag-grid', category: 'vendor-table', priority: 70, description: 'AG Grid' },
97
+ ];
98
+
99
+ /**
100
+ * 中国特色库规则
101
+ */
102
+ export const chinaRules: Rule[] = [
103
+ { match: 'china-area-data', category: 'vendor-utils', priority: 40, description: '中国地区数据' },
104
+ { match: 'pinyin-pro', category: 'vendor-utils', priority: 40, description: '拼音转换' },
105
+ ];
106
+
107
+ /**
108
+ * 业务组件库规则
109
+ */
110
+ export const businessRules: Rule[] = [
111
+ { match: 'plus-pro-components', category: 'vendor-pro', priority: 70, description: 'Plus Pro Components' },
112
+ { match: 'vue-tippy', category: 'vendor-utils', priority: 40, description: 'Vue Tippy' },
113
+ { match: 'vue-types', category: 'vendor-utils', priority: 40, description: 'Vue Types' },
114
+ ];
115
+
116
+ /**
117
+ * 获取所有通用规则
118
+ */
119
+ export function getCommonRules(): Rule[] {
120
+ return [
121
+ ...utilsRules,
122
+ ...iconRules,
123
+ ...formRules,
124
+ ...chartRules,
125
+ ...editorRules,
126
+ ...tableRules,
127
+ ...chinaRules,
128
+ ...businessRules,
129
+ ];
130
+ }
131
+
@@ -0,0 +1,93 @@
1
+ /**
2
+ * 框架相关的分包规则
3
+ */
4
+ import type { Rule } from '../types';
5
+
6
+ /**
7
+ * Vue 生态规则
8
+ */
9
+ export const vueRules: Rule[] = [
10
+ // Vue 核心
11
+ { match: 'vue', category: 'vendor-vue', priority: 100, description: 'Vue 核心框架' },
12
+ { match: /^@vue\//, category: 'vendor-vue', priority: 100, description: 'Vue 官方包' },
13
+
14
+ // 状态管理
15
+ { match: 'pinia', category: 'vendor-vue', priority: 100, description: 'Pinia 状态管理' },
16
+ { match: 'vuex', category: 'vendor-state', priority: 90, description: 'Vuex 状态管理' },
17
+
18
+ // 路由
19
+ { match: 'vue-router', category: 'vendor-router', priority: 100, description: 'Vue Router' },
20
+
21
+ // UI 框架
22
+ { match: 'element-plus', category: 'vendor-element', priority: 80, description: 'Element Plus UI' },
23
+ { match: 'ant-design-vue', category: 'vendor-ui', priority: 80, description: 'Ant Design Vue' },
24
+ { match: 'naive-ui', category: 'vendor-ui', priority: 80, description: 'Naive UI' },
25
+ { match: 'vant', category: 'vendor-ui', priority: 80, description: 'Vant UI' },
26
+ { match: 'arco-design', category: 'vendor-ui', priority: 80, description: 'Arco Design' },
27
+
28
+ // VueUse
29
+ { match: /^@vueuse\//, category: 'vendor-vueuse', priority: 70, description: 'VueUse 工具集' },
30
+
31
+ // 国际化
32
+ { match: 'vue-i18n', category: 'vendor-i18n', priority: 60, description: 'Vue I18n' },
33
+ { match: /^@intlify\//, category: 'vendor-i18n', priority: 60, description: 'Intlify' },
34
+ ];
35
+
36
+ /**
37
+ * React 生态规则
38
+ */
39
+ export const reactRules: Rule[] = [
40
+ // React 核心
41
+ { match: 'react', category: 'vendor-react', priority: 100, description: 'React 核心' },
42
+ { match: 'react-dom', category: 'vendor-react', priority: 100, description: 'React DOM' },
43
+
44
+ // 状态管理
45
+ { match: 'redux', category: 'vendor-state', priority: 90, description: 'Redux' },
46
+ { match: 'react-redux', category: 'vendor-state', priority: 90, description: 'React Redux' },
47
+ { match: 'zustand', category: 'vendor-state', priority: 90, description: 'Zustand' },
48
+ { match: 'mobx', category: 'vendor-state', priority: 90, description: 'MobX' },
49
+ { match: 'mobx-react', category: 'vendor-state', priority: 90, description: 'MobX React' },
50
+
51
+ // 路由
52
+ { match: 'react-router', category: 'vendor-router', priority: 100, description: 'React Router' },
53
+ { match: 'react-router-dom', category: 'vendor-router', priority: 100, description: 'React Router DOM' },
54
+
55
+ // UI 框架
56
+ { match: 'antd', category: 'vendor-ui', priority: 80, description: 'Ant Design' },
57
+ { match: '@ant-design/icons', category: 'vendor-icons', priority: 70, description: 'Ant Design Icons' },
58
+ { match: '@mui/material', category: 'vendor-ui', priority: 80, description: 'Material UI' },
59
+ { match: 'chakra-ui', category: 'vendor-ui', priority: 80, description: 'Chakra UI' },
60
+ ];
61
+
62
+ /**
63
+ * Angular 生态规则
64
+ */
65
+ export const angularRules: Rule[] = [
66
+ { match: /^@angular\//, category: 'vendor-angular', priority: 100, description: 'Angular 框架' },
67
+ ];
68
+
69
+ /**
70
+ * Svelte 生态规则
71
+ */
72
+ export const svelteRules: Rule[] = [
73
+ { match: 'svelte', category: 'vendor-svelte', priority: 100, description: 'Svelte 框架' },
74
+ ];
75
+
76
+ /**
77
+ * 获取框架规则
78
+ */
79
+ export function getFrameworkRules(framework: string): Rule[] {
80
+ switch (framework) {
81
+ case 'vue':
82
+ return vueRules;
83
+ case 'react':
84
+ return reactRules;
85
+ case 'angular':
86
+ return angularRules;
87
+ case 'svelte':
88
+ return svelteRules;
89
+ default:
90
+ return [...vueRules, ...reactRules, ...angularRules, ...svelteRules];
91
+ }
92
+ }
93
+
@@ -0,0 +1,27 @@
1
+ /**
2
+ * 规则导出
3
+ */
4
+ import type { Rule, Framework } from '../types';
5
+ import { getFrameworkRules } from './framework-rules';
6
+ import { getCommonRules } from './common-rules';
7
+
8
+ /**
9
+ * 获取所有规则
10
+ */
11
+ export function getAllRules(framework: Framework = 'auto'): Rule[] {
12
+ const frameworkRules = getFrameworkRules(framework === 'auto' ? 'all' : framework);
13
+ const commonRules = getCommonRules();
14
+
15
+ return [...frameworkRules, ...commonRules];
16
+ }
17
+
18
+ /**
19
+ * 根据优先级排序规则
20
+ */
21
+ export function sortRulesByPriority(rules: Rule[]): Rule[] {
22
+ return rules.sort((a, b) => (b.priority || 0) - (a.priority || 0));
23
+ }
24
+
25
+ export { getFrameworkRules } from './framework-rules';
26
+ export { getCommonRules } from './common-rules';
27
+
@@ -0,0 +1,94 @@
1
+ /**
2
+ * Chunk Optimizer 测试脚本
3
+ * 运行: npx tsx build/chunk-optimizer/test.ts
4
+ */
5
+
6
+ import { createChunkOptimizer } from './index';
7
+
8
+ console.log('\n' + '='.repeat(70));
9
+ console.log('🧪 Testing Chunk Optimizer');
10
+ console.log('='.repeat(70) + '\n');
11
+
12
+ // 创建优化器实例
13
+ const optimizer = createChunkOptimizer({
14
+ framework: 'vue',
15
+ debug: true,
16
+ customRules: {
17
+ // 测试自定义规则
18
+ 'test-lib': 'vendor-test',
19
+ },
20
+ exclude: ['@types/*'],
21
+ });
22
+
23
+ // 打印分析报告
24
+ optimizer.printReport();
25
+
26
+ // 获取分析结果
27
+ const result = optimizer.analyze();
28
+
29
+ console.log('\n' + '='.repeat(70));
30
+ console.log('📊 Detailed Analysis');
31
+ console.log('='.repeat(70) + '\n');
32
+
33
+ console.log(`Framework: ${result.detectedFramework}`);
34
+ console.log(`Total Dependencies: ${result.dependencies.length}`);
35
+ console.log(`Production Dependencies: ${result.dependencies.filter(d => !d.isDev).length}`);
36
+ console.log(`Dev Dependencies: ${result.dependencies.filter(d => d.isDev).length}`);
37
+
38
+ console.log('\n📦 Top 10 Dependencies by Category:\n');
39
+
40
+ // 按分类分组
41
+ const grouped = result.dependencies.reduce((acc, dep) => {
42
+ if (dep.category) {
43
+ if (!acc[dep.category]) {
44
+ acc[dep.category] = [];
45
+ }
46
+ acc[dep.category].push(dep.name);
47
+ }
48
+ return acc;
49
+ }, {} as Record<string, string[]>);
50
+
51
+ // 打印每个分类的前几个包
52
+ Object.entries(grouped)
53
+ .sort(([, a], [, b]) => b.length - a.length)
54
+ .slice(0, 10)
55
+ .forEach(([category, packages]) => {
56
+ console.log(`${category}:`);
57
+ packages.slice(0, 5).forEach(pkg => {
58
+ console.log(` - ${pkg}`);
59
+ });
60
+ if (packages.length > 5) {
61
+ console.log(` ... and ${packages.length - 5} more`);
62
+ }
63
+ console.log('');
64
+ });
65
+
66
+ // 生成 manualChunks 函数
67
+ const manualChunks = optimizer.generate();
68
+
69
+ console.log('='.repeat(70));
70
+ console.log('✅ Test completed successfully!');
71
+ console.log('='.repeat(70) + '\n');
72
+
73
+ // 测试 manualChunks 函数
74
+ console.log('🧪 Testing manualChunks function:\n');
75
+
76
+ const testCases = [
77
+ 'node_modules/vue/dist/vue.js',
78
+ 'node_modules/element-plus/es/index.js',
79
+ 'node_modules/axios/index.js',
80
+ 'node_modules/@iconify/vue/dist/index.js',
81
+ '/src/views/system/user/index.vue',
82
+ '/src/views/dashboard/index.vue',
83
+ '/src/components/Button/index.vue',
84
+ '/src/utils/request.ts',
85
+ '/src/store/user.ts',
86
+ ];
87
+
88
+ testCases.forEach(testCase => {
89
+ const result = manualChunks(testCase, { getModuleInfo: () => null });
90
+ console.log(`${testCase.padEnd(50)} -> ${result || '(default)'}`);
91
+ });
92
+
93
+ console.log('\n' + '='.repeat(70) + '\n');
94
+
@@ -0,0 +1,128 @@
1
+ /**
2
+ * Chunk Optimizer 类型定义
3
+ */
4
+
5
+ /**
6
+ * 框架类型
7
+ */
8
+ export type Framework = 'vue' | 'react' | 'angular' | 'svelte' | 'auto';
9
+
10
+ /**
11
+ * 分包策略
12
+ */
13
+ export type Strategy = 'balanced' | 'aggressive' | 'conservative';
14
+
15
+ /**
16
+ * 依赖分类
17
+ */
18
+ export type DependencyCategory =
19
+ | 'vendor-vue'
20
+ | 'vendor-react'
21
+ | 'vendor-router'
22
+ | 'vendor-state'
23
+ | 'vendor-element'
24
+ | 'vendor-ui'
25
+ | 'vendor-utils'
26
+ | 'vendor-icons'
27
+ | 'vendor-i18n'
28
+ | 'vendor-vueuse'
29
+ | 'vendor-pro'
30
+ | 'vendor-libs'
31
+ | string;
32
+
33
+ /**
34
+ * 规则定义
35
+ */
36
+ export interface Rule {
37
+ /** 包名或模式 */
38
+ match: string | RegExp;
39
+ /** 分类 */
40
+ category: DependencyCategory;
41
+ /** 优先级(数字越大优先级越高) */
42
+ priority?: number;
43
+ /** 描述 */
44
+ description?: string;
45
+ }
46
+
47
+ /**
48
+ * 业务代码分包策略
49
+ */
50
+ export interface SourceCodeStrategy {
51
+ /** 是否按 views 模块分包 */
52
+ views?: boolean;
53
+ /** 是否将公共组件单独分包 */
54
+ components?: boolean;
55
+ /** 是否将工具函数分包 */
56
+ utils?: boolean;
57
+ /** 是否将 store 分包 */
58
+ store?: boolean;
59
+ /** 自定义业务代码分包规则 */
60
+ custom?: Array<{
61
+ pattern: RegExp;
62
+ chunkName: string;
63
+ }>;
64
+ }
65
+
66
+ /**
67
+ * 优化器配置选项
68
+ */
69
+ export interface ChunkOptimizerOptions {
70
+ /** 框架类型,默认 'auto' 自动检测 */
71
+ framework?: Framework;
72
+
73
+ /** 分包策略,默认 'balanced' */
74
+ strategy?: Strategy;
75
+
76
+ /** 自定义规则(会覆盖默认规则) */
77
+ customRules?: Record<string, DependencyCategory>;
78
+
79
+ /** 排除某些包不参与分包 */
80
+ exclude?: string[];
81
+
82
+ /** 最小 chunk 大小(KB),小于此大小的包会合并,默认 0 */
83
+ minChunkSize?: number;
84
+
85
+ /** 是否启用调试模式,会输出详细的分包信息 */
86
+ debug?: boolean;
87
+
88
+ /** 业务代码分包策略 */
89
+ sourceCodeStrategy?: SourceCodeStrategy;
90
+
91
+ /** package.json 路径,默认为项目根目录 */
92
+ packageJsonPath?: string;
93
+ }
94
+
95
+ /**
96
+ * 依赖信息
97
+ */
98
+ export interface DependencyInfo {
99
+ /** 包名 */
100
+ name: string;
101
+ /** 版本 */
102
+ version: string;
103
+ /** 是否为开发依赖 */
104
+ isDev: boolean;
105
+ /** 分类 */
106
+ category?: DependencyCategory;
107
+ }
108
+
109
+ /**
110
+ * 分析结果
111
+ */
112
+ export interface AnalysisResult {
113
+ /** 所有依赖 */
114
+ dependencies: DependencyInfo[];
115
+ /** 检测到的框架 */
116
+ detectedFramework?: Framework;
117
+ /** 分类统计 */
118
+ categoryStats: Record<DependencyCategory, number>;
119
+ }
120
+
121
+ /**
122
+ * ManualChunks 函数类型
123
+ */
124
+ export type ManualChunksFunction = (
125
+ id: string,
126
+ { getModuleInfo }: { getModuleInfo: (id: string) => any }
127
+ ) => string | undefined;
128
+
@@ -0,0 +1,58 @@
1
+ /**
2
+ * 颜色处理相关
3
+ * @packageDocumentation
4
+ * @module Color
5
+ * @preferred
6
+ */
7
+
8
+ /**
9
+ * 将 Hex 颜色转换为 RGB 数组
10
+ * @param hex - Hex 格式颜色值(如 #ffffff)
11
+ * @returns RGB 数组 [r, g, b]
12
+ */
13
+ export function hexToRgb(hex: string): number[] {
14
+ const result = hex.replace('#', '').match(/../g);
15
+ return result ? result.map(val => parseInt(val, 16)) : [0, 0, 0];
16
+ }
17
+
18
+ /**
19
+ * 将 RGB 值转换为 Hex 颜色
20
+ * @param r - 红色值 (0-255)
21
+ * @param g - 绿色值 (0-255)
22
+ * @param b - 蓝色值 (0-255)
23
+ * @returns Hex 格式颜色值(如 #ffffff)
24
+ */
25
+ export function rgbToHex(r: number, g: number, b: number): string {
26
+ const toHex = (n: number) => {
27
+ const hex = n.toString(16);
28
+ return hex.length === 1 ? '0' + hex : hex;
29
+ };
30
+ return `#${toHex(r)}${toHex(g)}${toHex(b)}`;
31
+ }
32
+
33
+ /**
34
+ * 颜色加深
35
+ * 使用 RGB 线性计算:color * (1 - level)
36
+ * @param color - Hex 格式颜色值
37
+ * @param level - 加深程度 (0-1)
38
+ * @returns 加深后的 Hex 颜色值
39
+ */
40
+ export function darken(color: string, level: number): string {
41
+ const rgb = hexToRgb(color);
42
+ const darkened = rgb.map(val => Math.floor(val * (1 - level)));
43
+ return rgbToHex(darkened[0], darkened[1], darkened[2]);
44
+ }
45
+
46
+ /**
47
+ * 颜色变浅
48
+ * 使用 RGB 线性计算:(255 - color) * level + color
49
+ * @param color - Hex 格式颜色值
50
+ * @param level - 变浅程度 (0-1)
51
+ * @returns 变浅后的 Hex 颜色值
52
+ */
53
+ export function lighten(color: string, level: number): string {
54
+ const rgb = hexToRgb(color);
55
+ const lightened = rgb.map(val => Math.floor((255 - val) * level + val));
56
+ return rgbToHex(lightened[0], lightened[1], lightened[2]);
57
+ }
58
+