css2class 2.0.0 → 2.0.1

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 (40) hide show
  1. package/.github/workflows/deploy-docs.yml +53 -0
  2. package/.head_config.mjs +68 -0
  3. package/CONFIG.md +38 -1
  4. package/README.md +595 -633
  5. package/bin/class2css.js +32 -4
  6. package/common.css +1 -1
  7. package/configs/typography.config.js +1 -0
  8. package/docs/.vitepress/config.mjs +68 -65
  9. package/docs/guide/cli.md +97 -97
  10. package/docs/guide/config-template.md +16 -1
  11. package/docs/guide/config.md +129 -64
  12. package/docs/guide/faq.md +202 -202
  13. package/docs/guide/getting-started.md +86 -83
  14. package/docs/guide/incremental.md +164 -162
  15. package/docs/guide/rules-reference.md +73 -1
  16. package/docs/index.md +71 -68
  17. package/examples/weapp/README.md +15 -0
  18. package/examples/weapp/class2css.config.js +70 -0
  19. package/examples/weapp/src/placeholder.wxml +0 -0
  20. package/examples/weapp/styles.config.js +201 -0
  21. package/examples/web/README.md +25 -0
  22. package/examples/web/class2css.config.js +70 -0
  23. package/examples/web/demo.html +105 -0
  24. package/examples/web/src/placeholder.html +5 -0
  25. package/examples/web/styles.config.js +201 -0
  26. package/package.json +7 -2
  27. package/src/README.md +99 -0
  28. package/src/core/ConfigManager.js +440 -431
  29. package/src/core/FullScanManager.js +438 -430
  30. package/src/generators/DynamicClassGenerator.js +270 -72
  31. package/src/index.js +1091 -1046
  32. package/src/parsers/ClassParser.js +8 -2
  33. package/src/utils/CssFormatter.js +400 -47
  34. package/src/utils/UnitProcessor.js +4 -3
  35. package/src/watchers/ConfigWatcher.js +413 -413
  36. package/src/watchers/FileWatcher.js +148 -133
  37. package/src/writers/FileWriter.js +444 -302
  38. package/src/writers/UnifiedWriter.js +413 -370
  39. package/class2css.config.js +0 -124
  40. package/styles.config.js +0 -250
@@ -0,0 +1,201 @@
1
+ // ========== 样式规则配置(Web 示例)==========
2
+ // 说明:
3
+ // - 该示例更偏向 Web(px)
4
+ // - 如需小程序示例(rpx),请参考 examples/weapp/styles.config.js
5
+
6
+ const DEFAULT_UNIT = 'px';
7
+
8
+ // ========== 原子化规则配置 ==========
9
+ const atomicRules = {
10
+ spacing: {
11
+ m: { properties: ['margin'], defaultUnit: DEFAULT_UNIT },
12
+ mt: { properties: ['margin-top'], defaultUnit: DEFAULT_UNIT },
13
+ mr: { properties: ['margin-right'], defaultUnit: DEFAULT_UNIT },
14
+ mb: { properties: ['margin-bottom'], defaultUnit: DEFAULT_UNIT },
15
+ ml: { properties: ['margin-left'], defaultUnit: DEFAULT_UNIT },
16
+ mx: { properties: ['margin-left', 'margin-right'], defaultUnit: DEFAULT_UNIT },
17
+ my: { properties: ['margin-top', 'margin-bottom'], defaultUnit: DEFAULT_UNIT },
18
+ p: { properties: ['padding'], defaultUnit: DEFAULT_UNIT },
19
+ pt: { properties: ['padding-top'], defaultUnit: DEFAULT_UNIT },
20
+ pr: { properties: ['padding-right'], defaultUnit: DEFAULT_UNIT },
21
+ pb: { properties: ['padding-bottom'], defaultUnit: DEFAULT_UNIT },
22
+ pl: { properties: ['padding-left'], defaultUnit: DEFAULT_UNIT },
23
+ px: { properties: ['padding-left', 'padding-right'], defaultUnit: DEFAULT_UNIT },
24
+ py: { properties: ['padding-top', 'padding-bottom'], defaultUnit: DEFAULT_UNIT },
25
+ gap: { properties: ['gap'], defaultUnit: DEFAULT_UNIT },
26
+ },
27
+ sizing: {
28
+ w: { properties: ['width'], defaultUnit: DEFAULT_UNIT },
29
+ h: { properties: ['height'], defaultUnit: DEFAULT_UNIT },
30
+ 'max-w': { properties: ['max-width'], defaultUnit: DEFAULT_UNIT },
31
+ 'max-h': { properties: ['max-height'], defaultUnit: DEFAULT_UNIT },
32
+ 'min-w': { properties: ['min-width'], defaultUnit: DEFAULT_UNIT },
33
+ 'min-h': { properties: ['min-height'], defaultUnit: DEFAULT_UNIT },
34
+ size: { properties: ['width', 'height'], defaultUnit: DEFAULT_UNIT },
35
+ },
36
+ typography: {
37
+ 'text-size': { properties: ['font-size'], defaultUnit: DEFAULT_UNIT },
38
+ text: { properties: ['font-size'], defaultUnit: DEFAULT_UNIT },
39
+ font: { properties: ['font-weight'], defaultUnit: '' },
40
+ leading: { properties: ['line-height'], defaultUnit: '' },
41
+ lh: { properties: ['line-height'], defaultUnit: DEFAULT_UNIT, skipSpecialProcessing: true },
42
+ tracking: { properties: ['letter-spacing'], defaultUnit: DEFAULT_UNIT },
43
+ },
44
+ positioning: {
45
+ top: { properties: ['top'], defaultUnit: DEFAULT_UNIT },
46
+ right: { properties: ['right'], defaultUnit: DEFAULT_UNIT },
47
+ bottom: { properties: ['bottom'], defaultUnit: DEFAULT_UNIT },
48
+ left: { properties: ['left'], defaultUnit: DEFAULT_UNIT },
49
+ inset: { properties: ['top', 'right', 'bottom', 'left'], defaultUnit: DEFAULT_UNIT },
50
+ 'inset-x': { properties: ['left', 'right'], defaultUnit: DEFAULT_UNIT },
51
+ 'inset-y': { properties: ['top', 'bottom'], defaultUnit: DEFAULT_UNIT },
52
+ },
53
+ borders: {
54
+ rounded: { properties: ['border-radius'], defaultUnit: DEFAULT_UNIT },
55
+ border: { properties: ['border-width'], defaultUnit: DEFAULT_UNIT },
56
+ bordert: { properties: ['border-top-width'], defaultUnit: DEFAULT_UNIT },
57
+ borderr: { properties: ['border-right-width'], defaultUnit: DEFAULT_UNIT },
58
+ borderb: { properties: ['border-bottom-width'], defaultUnit: DEFAULT_UNIT },
59
+ borderl: { properties: ['border-left-width'], defaultUnit: DEFAULT_UNIT },
60
+ b_r: { properties: ['border-radius'], defaultUnit: DEFAULT_UNIT },
61
+ },
62
+ effects: {
63
+ opacity: { properties: ['opacity'], defaultUnit: '' },
64
+ transition: { properties: ['transition'], defaultUnit: 'ms', skipConversion: true },
65
+ op: { properties: ['opacity'], defaultUnit: '' },
66
+ z: { properties: ['z-index'], defaultUnit: '' },
67
+ },
68
+ };
69
+
70
+ // ========== Tailwind 风格静态 class 配置 ==========
71
+ const baseClassName = {
72
+ // 颜色族(会在下面把 baseColor 注入进来)
73
+ color: { ABBR: 'color' },
74
+ bg: { ABBR: 'background-color' },
75
+ bcolor: { ABBR: 'border-color' },
76
+
77
+ // display / layout
78
+ block: 'display: block;',
79
+ inline: 'display: inline;',
80
+ 'inline-block': 'display: inline-block;',
81
+ flex: 'display: flex;',
82
+ 'inline-flex': 'display: inline-flex;',
83
+ grid: 'display: grid;',
84
+ 'inline-grid': 'display: inline-grid;',
85
+ table: 'display: table;',
86
+ hidden: 'display: none;',
87
+
88
+ // sizing
89
+ 'w-full': 'width: 100%;',
90
+ 'h-full': 'height: 100%;',
91
+ 'w-screen': 'width: 100vw;',
92
+ 'h-screen': 'height: 100vh;',
93
+
94
+ // flex helpers
95
+ 'flex-1': 'flex: 1;',
96
+ 'shrink-0': 'flex-shrink: 0;',
97
+ 'flex-row': 'flex-direction: row;',
98
+ 'flex-col': 'flex-direction: column;',
99
+ 'flex-wrap': 'flex-wrap: wrap;',
100
+ 'flex-nowrap': 'flex-wrap: nowrap;',
101
+ 'items-start': 'align-items: flex-start;',
102
+ 'items-center': 'align-items: center;',
103
+ 'items-end': 'align-items: flex-end;',
104
+ 'items-stretch': 'align-items: stretch;',
105
+ 'justify-start': 'justify-content: flex-start;',
106
+ 'justify-center': 'justify-content: center;',
107
+ 'justify-end': 'justify-content: flex-end;',
108
+ 'justify-between': 'justify-content: space-between;',
109
+ 'justify-around': 'justify-content: space-around;',
110
+ 'justify-evenly': 'justify-content: space-evenly;',
111
+ // 注意:不包含 display:flex,通常配合 `flex` 一起用
112
+ 'flex-cen': 'align-items: center;justify-content: center;',
113
+
114
+ // position
115
+ static: 'position: static;',
116
+ fixed: 'position: fixed;',
117
+ absolute: 'position: absolute;',
118
+ relative: 'position: relative;',
119
+ sticky: 'position: sticky;',
120
+
121
+ // overflow
122
+ 'overflow-auto': 'overflow: auto;',
123
+ 'overflow-hidden': 'overflow: hidden;',
124
+ 'overflow-visible': 'overflow: visible;',
125
+ 'overflow-scroll': 'overflow: scroll;',
126
+
127
+ // text
128
+ underline: 'text-decoration: underline;',
129
+ 'line-through': 'text-decoration: line-through;',
130
+ ellipsis: 'overflow: hidden;text-overflow: ellipsis;white-space: nowrap;',
131
+ 'text-left': 'text-align: left;',
132
+ 'text-center': 'text-align: center;',
133
+ 'text-right': 'text-align: right;',
134
+ 'text-justify': 'text-align: justify;',
135
+
136
+ // box
137
+ 'box-border': 'box-sizing: border-box;',
138
+ 'box-content': 'box-sizing: content-box;',
139
+
140
+ // border styles
141
+ 'border-solid': 'border-style: solid;',
142
+ 'border-dashed': 'border-style: dashed;',
143
+ 'border-dotted': 'border-style: dotted;',
144
+ 'border-none': 'border: none;',
145
+ };
146
+
147
+ // ========== 变体规则(响应式、伪类等) ==========
148
+ const variants = {
149
+ responsive: ['sm', 'md', 'lg', 'xl', '2xl'],
150
+ states: ['hover', 'focus', 'active', 'disabled', 'first', 'last', 'odd', 'even'],
151
+ darkMode: ['dark'],
152
+ };
153
+
154
+ // ========== 响应式断点配置(可选) ==========
155
+ const breakpoints = {
156
+ sm: '640px',
157
+ md: '768px',
158
+ lg: '1024px',
159
+ xl: '1280px',
160
+ '2xl': '1536px',
161
+ };
162
+
163
+ // ========== 颜色配置(示例,可按项目裁剪) ==========
164
+ const baseColor = {
165
+ white: '#ffffff',
166
+ black: '#000000',
167
+ transparent: 'transparent',
168
+ gray: '#6b7280',
169
+ red: '#ef4444',
170
+ green: '#22c55e',
171
+ sky: '#0ea5e9',
172
+ violet: '#8b5cf6',
173
+ };
174
+
175
+ // ========== Important 标识配置 ==========
176
+ const importantFlags = {
177
+ // prefix: ['!', '$$'],
178
+ suffix: ['-i', '_i'],
179
+ // custom: ['--important'],
180
+ };
181
+
182
+ function processStyles() {
183
+ const processedBaseClassName = { ...baseClassName };
184
+
185
+ // 将颜色 token 合并到具有 ABBR 的类族中(color/bg/bcolor)
186
+ Object.values(processedBaseClassName).forEach((item) => {
187
+ if (item && item.ABBR) {
188
+ Object.assign(item, baseColor);
189
+ }
190
+ });
191
+
192
+ return {
193
+ atomicRules,
194
+ baseClassName: processedBaseClassName,
195
+ variants,
196
+ breakpoints,
197
+ importantFlags,
198
+ };
199
+ }
200
+
201
+ module.exports = processStyles();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "css2class",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "Enterprise-grade Atomic CSS Generator for WeChat Mini Program",
5
5
  "main": "src/index.js",
6
6
  "bin": {
@@ -8,8 +8,13 @@
8
8
  },
9
9
  "scripts": {
10
10
  "start": "node bin/class2css.js --docs",
11
+ "dev": "node bin/class2css.js",
11
12
  "build": "node bin/class2css.js --no-watch",
12
- "dev": "node bin/class2css.js --config ./class2css.config.js",
13
+ "example:weapp": "node bin/class2css.js --config ./examples/weapp/class2css.config.js",
14
+ "example:weapp:build": "node bin/class2css.js --no-watch --config ./examples/weapp/class2css.config.js",
15
+ "example:weapp:docs": "node bin/class2css.js --docs --config ./examples/weapp/class2css.config.js",
16
+ "example:web": "node bin/class2css.js --config ./examples/web/class2css.config.js",
17
+ "example:web:build": "node bin/class2css.js --no-watch --config ./examples/web/class2css.config.js",
13
18
  "docs:dev": "vitepress dev docs",
14
19
  "test": "node src/example.js",
15
20
  "help": "node bin/class2css.js --help",
package/src/README.md CHANGED
@@ -362,6 +362,72 @@ module.exports = getConfig();
362
362
  - 便于维护和扩展
363
363
  - 支持热重载
364
364
 
365
+ ### 直接颜色值写法
366
+
367
+ 除了使用预设的颜色映射(如 `bg-red`、`color-white`),现在支持直接在 class 中写入颜色值。该功能适用于所有带 `ABBR` 的颜色族(`bg`、`color`、`bcolor` 等)。
368
+
369
+ #### Hex 颜色格式
370
+ ```html
371
+ <!-- 3位 hex -->
372
+ <div class="bg-hex-fff">白色背景</div>
373
+ <div class="color-hex-000">黑色文字</div>
374
+
375
+ <!-- 4位 hex(含 alpha) -->
376
+ <div class="bg-hex-ffff">不透明白色背景</div>
377
+
378
+ <!-- 6位 hex -->
379
+ <div class="bg-hex-112233">深色背景</div>
380
+ <div class="color-hex-ff0000">红色文字</div>
381
+
382
+ <!-- 8位 hex(含 alpha) -->
383
+ <div class="bg-hex-ffffffff">完全不透明白色背景</div>
384
+ ```
385
+
386
+ #### RGB 颜色格式
387
+ ```html
388
+ <div class="bg-rgb-255-0-0">红色背景</div>
389
+ <div class="color-rgb-0-128-255">蓝色文字</div>
390
+ ```
391
+
392
+ #### RGBA 颜色格式
393
+ ```html
394
+ <!-- alpha 值支持多种写法:
395
+ - 两位数字 < 10:除以 10(如 05 → 0.5, 08 → 0.8)
396
+ - 两位数字 >= 10:除以 100 作为百分比(如 50 → 0.5, 99 → 0.99)
397
+ - 下划线分隔:直接替换为点(如 0_5 → 0.5)
398
+ - 其他格式:直接解析(如 1 → 1.0, 0.5 → 0.5)
399
+ -->
400
+ <div class="bg-rgba-0-0-0-05">半透明黑色背景(05 → 0.5)</div>
401
+ <div class="bg-rgba-255-0-0-0_5">半透明红色背景(0_5 → 0.5)</div>
402
+ <div class="bg-rgba-0-0-0-50">50% 不透明度黑色背景(50 → 0.5)</div>
403
+ <div class="color-rgba-0-0-0-08">80% 不透明度黑色文字(08 → 0.8)</div>
404
+ <div class="bg-rgba-255-0-0-99">99% 不透明度红色背景(99 → 0.99)</div>
405
+ ```
406
+
407
+ #### 与响应式和 Important 组合使用
408
+ ```html
409
+ <!-- 响应式变体 -->
410
+ <div class="sm:bg-hex-fff md:bg-rgb-255-0-0">响应式背景色</div>
411
+
412
+ <!-- Important 标识 -->
413
+ <div class="bg-hex-fff_i">强制白色背景</div>
414
+ <div class="!color-rgb-0-0-0">强制黑色文字</div>
415
+
416
+ <!-- 组合使用 -->
417
+ <div class="sm:bg-rgba-255-0-0-05_i">响应式 + Important</div>
418
+ ```
419
+
420
+ #### 优先级说明
421
+ - **映射优先**:如果配置中存在对应的颜色映射(如 `bg-red`),优先使用映射值
422
+ - **直接值后备**:只有当映射不存在时,才会尝试解析为直接颜色值
423
+ - **解析失败**:如果既不是映射值,也无法解析为有效颜色值,则不会生成 CSS 规则
424
+
425
+ #### 兼容性
426
+ - ✅ 完全兼容 Web(HTML/CSS)
427
+ - ✅ 完全兼容小程序(WXML/WXSS)
428
+ - ✅ 只使用字母、数字、下划线、短横线,无需转义
429
+ - ✅ 与现有颜色映射方案完全兼容,不会产生冲突
430
+
365
431
  ## CLI 工具
366
432
 
367
433
  项目提供了多种启动方式:
@@ -432,6 +498,32 @@ class2css --no-watch # 不监听文件变化
432
498
  - 事件驱动设计
433
499
  - 依赖注入支持
434
500
 
501
+ ### 🆕 直接颜色值写法
502
+ 支持在 class 中直接写入颜色值,无需预先配置颜色映射:
503
+
504
+ ```html
505
+ <!-- Hex 颜色 -->
506
+ <div class="bg-hex-fff">白色背景</div>
507
+ <div class="color-hex-ff0000">红色文字</div>
508
+
509
+ <!-- RGB 颜色 -->
510
+ <div class="bg-rgb-255-0-0">红色背景</div>
511
+
512
+ <!-- RGBA 颜色 -->
513
+ <div class="bg-rgba-0-0-0-05">半透明黑色背景</div>
514
+ <div class="color-rgba-255-0-0-0_5">半透明红色文字</div>
515
+
516
+ <!-- 与响应式和 Important 组合 -->
517
+ <div class="sm:bg-hex-fff">响应式背景</div>
518
+ <div class="bg-hex-fff_i">强制白色背景</div>
519
+ ```
520
+
521
+ **特性:**
522
+ - ✅ 支持 hex(3/4/6/8位)、rgb、rgba 格式
523
+ - ✅ 映射优先:现有颜色映射(如 `bg-red`)优先使用
524
+ - ✅ 完全兼容 Web 和小程序
525
+ - ✅ 与现有颜色映射方案无冲突
526
+
435
527
  ## 优势
436
528
 
437
529
  ### 1. 可维护性
@@ -531,6 +623,13 @@ const yourModule = new YourModule(eventBus, dependencies);
531
623
 
532
624
  ## 版本历史
533
625
 
626
+ ### v2.1.0 (2025-01-20)
627
+ - ✨ **新功能**:直接颜色值写法
628
+ - 支持 hex、rgb、rgba 格式的直接颜色值
629
+ - 无需预先配置颜色映射即可使用
630
+ - 完全兼容现有颜色映射方案(映射优先)
631
+ - 支持与响应式变体和 Important 标识组合使用
632
+
534
633
  ### v2.0.0 (2025-01-30)
535
634
  - 🚀 **重大更新**:完全模块化架构重构
536
635
  - ✨ **新功能**:统一文件模式 (uniFile)