css2class 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/API.md +1143 -0
- package/CHANGELOG.md +291 -0
- package/CONFIG.md +1096 -0
- package/CONTRIBUTING.md +571 -0
- package/MIGRATION.md +402 -0
- package/README.md +634 -0
- package/bin/class2css.js +380 -0
- package/class2css.config.js +124 -0
- package/common.css +3 -0
- package/configs/colors.config.js +62 -0
- package/configs/layout.config.js +110 -0
- package/configs/spacing.config.js +37 -0
- package/configs/typography.config.js +41 -0
- package/docs/.vitepress/config.mjs +65 -0
- package/docs/.vitepress/theme/custom.css +74 -0
- package/docs/.vitepress/theme/index.js +7 -0
- package/docs/guide/cli.md +97 -0
- package/docs/guide/concepts.md +63 -0
- package/docs/guide/config-template.md +365 -0
- package/docs/guide/config.md +275 -0
- package/docs/guide/faq.md +202 -0
- package/docs/guide/getting-started.md +83 -0
- package/docs/guide/important-and-static.md +67 -0
- package/docs/guide/incremental.md +162 -0
- package/docs/guide/rules-reference.md +354 -0
- package/docs/guide/units.md +57 -0
- package/docs/index.md +68 -0
- package/package.json +49 -0
- package/run.js +90 -0
- package/src/README.md +571 -0
- package/src/core/CacheManager.js +650 -0
- package/src/core/CompatibilityAdapter.js +264 -0
- package/src/core/ConfigManager.js +431 -0
- package/src/core/ConfigValidator.js +350 -0
- package/src/core/EventBus.js +77 -0
- package/src/core/FullScanManager.js +430 -0
- package/src/core/StateManager.js +631 -0
- package/src/docs/DocsServer.js +179 -0
- package/src/example.js +106 -0
- package/src/generators/DynamicClassGenerator.js +674 -0
- package/src/index.js +1046 -0
- package/src/parsers/ClassParser.js +572 -0
- package/src/parsers/ImportantParser.js +279 -0
- package/src/parsers/RegexCompiler.js +200 -0
- package/src/utils/ClassChangeTracker.js +366 -0
- package/src/utils/ConfigDiagnostics.js +673 -0
- package/src/utils/CssFormatter.js +261 -0
- package/src/utils/FileUtils.js +230 -0
- package/src/utils/Logger.js +150 -0
- package/src/utils/Throttle.js +172 -0
- package/src/utils/UnitProcessor.js +334 -0
- package/src/utils/WxssClassExtractor.js +137 -0
- package/src/watchers/ConfigWatcher.js +413 -0
- package/src/watchers/FileWatcher.js +133 -0
- package/src/writers/FileWriter.js +302 -0
- package/src/writers/UnifiedWriter.js +370 -0
- package/styles.config.js +250 -0
package/CONFIG.md
ADDED
|
@@ -0,0 +1,1096 @@
|
|
|
1
|
+
# Class2CSS 配置参考文档
|
|
2
|
+
|
|
3
|
+
本文档详细说明 Class2CSS 的所有配置选项。
|
|
4
|
+
|
|
5
|
+
## 📋 目录
|
|
6
|
+
|
|
7
|
+
- [配置文件结构](#配置文件结构)
|
|
8
|
+
- [系统配置 (system)](#系统配置-system)
|
|
9
|
+
- [输出配置 (output)](#输出配置-output)
|
|
10
|
+
- [多文件配置 (multiFile)](#多文件配置-multifile)
|
|
11
|
+
- [原子化规则 (atomicRules)](#原子化规则-atomicrules)
|
|
12
|
+
- [基础类配置 (baseClassName)](#基础类配置-baseclassname)
|
|
13
|
+
- [Important 标识 (importantFlags)](#important-标识-importantflags)
|
|
14
|
+
- [变体规则 (variants)](#变体规则-variants)
|
|
15
|
+
- [配置示例](#配置示例)
|
|
16
|
+
- [最佳实践](#最佳实践)
|
|
17
|
+
|
|
18
|
+
## 配置文件结构
|
|
19
|
+
|
|
20
|
+
配置已拆分为两个文件,便于管理和维护:
|
|
21
|
+
|
|
22
|
+
### 主配置文件:`class2css.config.js`
|
|
23
|
+
|
|
24
|
+
工具运行相关的配置(系统设置、输出路径等):
|
|
25
|
+
|
|
26
|
+
```javascript
|
|
27
|
+
// 引入样式规则配置
|
|
28
|
+
const stylesConfig = require('./styles.config.js');
|
|
29
|
+
|
|
30
|
+
module.exports = {
|
|
31
|
+
system: { /* 系统配置 */ },
|
|
32
|
+
output: { /* 输出配置 */ },
|
|
33
|
+
multiFile: { /* 多文件配置 */ },
|
|
34
|
+
importantFlags: stylesConfig.importantFlags,
|
|
35
|
+
atomicRules: stylesConfig.atomicRules,
|
|
36
|
+
baseClassName: stylesConfig.baseClassName,
|
|
37
|
+
variants: stylesConfig.variants,
|
|
38
|
+
breakpoints: stylesConfig.breakpoints
|
|
39
|
+
};
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### 样式规则配置文件:`styles.config.js`
|
|
43
|
+
|
|
44
|
+
样式解析规则相关的配置(原子化规则、基础类、变体、断点等):
|
|
45
|
+
|
|
46
|
+
```javascript
|
|
47
|
+
module.exports = {
|
|
48
|
+
atomicRules: { /* 原子化规则 */ },
|
|
49
|
+
baseClassName: { /* 基础类配置 */ },
|
|
50
|
+
variants: { /* 变体规则 */ },
|
|
51
|
+
breakpoints: { /* 响应式断点 */ },
|
|
52
|
+
importantFlags: { /* Important 标识 */ }
|
|
53
|
+
};
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
**优势**:
|
|
57
|
+
- **关注点分离**:工具配置与样式规则分离,更清晰
|
|
58
|
+
- **易于维护**:样式规则集中管理,便于团队协作
|
|
59
|
+
- **灵活扩展**:可以轻松替换或扩展样式规则配置
|
|
60
|
+
|
|
61
|
+
## 系统配置 (system)
|
|
62
|
+
|
|
63
|
+
系统级别的全局配置。
|
|
64
|
+
|
|
65
|
+
### 完整配置
|
|
66
|
+
|
|
67
|
+
```javascript
|
|
68
|
+
system: {
|
|
69
|
+
cssFormat: 'compressed',
|
|
70
|
+
baseUnit: 'rpx',
|
|
71
|
+
unitConversion: 2,
|
|
72
|
+
compression: true,
|
|
73
|
+
sortClasses: true,
|
|
74
|
+
unitStrategy: {
|
|
75
|
+
autoDetect: true,
|
|
76
|
+
propertyUnits: {
|
|
77
|
+
'font-size': 'rpx',
|
|
78
|
+
'width|height': 'rpx',
|
|
79
|
+
'opacity': '',
|
|
80
|
+
'z-index': '',
|
|
81
|
+
'line-height': '',
|
|
82
|
+
'border-radius': 'rpx'
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### 配置项说明
|
|
89
|
+
|
|
90
|
+
#### cssFormat
|
|
91
|
+
|
|
92
|
+
**类型**: `string`
|
|
93
|
+
**默认值**: `'compressed'`
|
|
94
|
+
**可选值**: `'multiLine'` | `'singleLine'` | `'compressed'`
|
|
95
|
+
|
|
96
|
+
CSS 输出格式。
|
|
97
|
+
|
|
98
|
+
```javascript
|
|
99
|
+
// multiLine - 多行格式
|
|
100
|
+
.m-10 {
|
|
101
|
+
margin: 20rpx;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// singleLine - 单行格式
|
|
105
|
+
.m-10 { margin: 20rpx; }
|
|
106
|
+
|
|
107
|
+
// compressed - 压缩格式
|
|
108
|
+
.m-10{margin:20rpx}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
#### baseUnit
|
|
112
|
+
|
|
113
|
+
**类型**: `string`
|
|
114
|
+
**默认值**: `'rpx'`
|
|
115
|
+
**常用值**: `'rpx'` | `'px'` | `'rem'` | `'em'`
|
|
116
|
+
|
|
117
|
+
基础单位,用于没有指定单位的数值。
|
|
118
|
+
|
|
119
|
+
```javascript
|
|
120
|
+
baseUnit: 'rpx'
|
|
121
|
+
|
|
122
|
+
// 使用效果
|
|
123
|
+
// class="m-10" → margin: 20rpx;
|
|
124
|
+
// class="m-10px" → margin: 10px;
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
#### unitConversion
|
|
128
|
+
|
|
129
|
+
**类型**: `number`
|
|
130
|
+
**默认值**: `2`
|
|
131
|
+
|
|
132
|
+
单位转换比例。生成的样式单位 = 设置单位 × 比例。
|
|
133
|
+
|
|
134
|
+
```javascript
|
|
135
|
+
unitConversion: 2
|
|
136
|
+
|
|
137
|
+
// 使用效果
|
|
138
|
+
// class="m-10" → margin: 20rpx; (10 × 2)
|
|
139
|
+
// class="w-100" → width: 200rpx; (100 × 2)
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
**常见场景**:
|
|
143
|
+
- 设计稿 750px,小程序 750rpx → `unitConversion: 2`
|
|
144
|
+
- 设计稿 375px,小程序 750rpx → `unitConversion: 2`
|
|
145
|
+
- 1:1 转换 → `unitConversion: 1`
|
|
146
|
+
|
|
147
|
+
#### compression
|
|
148
|
+
|
|
149
|
+
**类型**: `boolean`
|
|
150
|
+
**默认值**: `true`
|
|
151
|
+
|
|
152
|
+
是否压缩 CSS 输出。
|
|
153
|
+
|
|
154
|
+
```javascript
|
|
155
|
+
compression: true // 推荐生产环境使用
|
|
156
|
+
compression: false // 推荐开发环境使用
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
#### sortClasses
|
|
160
|
+
|
|
161
|
+
**类型**: `boolean`
|
|
162
|
+
**默认值**: `true`
|
|
163
|
+
|
|
164
|
+
是否对生成的 CSS 类进行字母排序。
|
|
165
|
+
|
|
166
|
+
```javascript
|
|
167
|
+
sortClasses: true
|
|
168
|
+
|
|
169
|
+
// 生成的 CSS 会按字母顺序排列
|
|
170
|
+
.h-100 { ... }
|
|
171
|
+
.m-10 { ... }
|
|
172
|
+
.p-20 { ... }
|
|
173
|
+
.w-100 { ... }
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
#### unitStrategy
|
|
177
|
+
|
|
178
|
+
**类型**: `object`
|
|
179
|
+
|
|
180
|
+
智能单位处理策略配置。
|
|
181
|
+
|
|
182
|
+
##### autoDetect
|
|
183
|
+
|
|
184
|
+
**类型**: `boolean`
|
|
185
|
+
**默认值**: `true`
|
|
186
|
+
|
|
187
|
+
是否自动检测用户提供的单位。
|
|
188
|
+
|
|
189
|
+
```javascript
|
|
190
|
+
autoDetect: true
|
|
191
|
+
|
|
192
|
+
// 效果
|
|
193
|
+
// class="m-10" → margin: 20rpx; (使用默认单位)
|
|
194
|
+
// class="m-10px" → margin: 10px; (保持用户单位)
|
|
195
|
+
// class="m-10em" → margin: 10em; (保持用户单位)
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
##### propertyUnits
|
|
199
|
+
|
|
200
|
+
**类型**: `object`
|
|
201
|
+
|
|
202
|
+
为不同 CSS 属性配置默认单位。
|
|
203
|
+
|
|
204
|
+
```javascript
|
|
205
|
+
propertyUnits: {
|
|
206
|
+
'font-size': 'rpx', // 字体大小使用 rpx
|
|
207
|
+
'width|height': 'rpx', // 宽高使用 rpx (支持管道符)
|
|
208
|
+
'opacity': '', // 透明度无单位
|
|
209
|
+
'z-index': '', // 层级无单位
|
|
210
|
+
'line-height': '', // 行高可以无单位
|
|
211
|
+
'border-radius': 'rpx' // 圆角使用 rpx
|
|
212
|
+
}
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
**支持的格式**:
|
|
216
|
+
- 单个属性: `'font-size': 'rpx'`
|
|
217
|
+
- 多个属性: `'width|height': 'rpx'`
|
|
218
|
+
- 无单位: `'opacity': ''`
|
|
219
|
+
|
|
220
|
+
## 输出配置 (output)
|
|
221
|
+
|
|
222
|
+
单文件输出配置。
|
|
223
|
+
|
|
224
|
+
```javascript
|
|
225
|
+
output: {
|
|
226
|
+
path: './dist',
|
|
227
|
+
fileName: 'styles.wxss',
|
|
228
|
+
commonCssPath: './common.css'
|
|
229
|
+
}
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
### 配置项说明
|
|
233
|
+
|
|
234
|
+
#### path
|
|
235
|
+
|
|
236
|
+
**类型**: `string`
|
|
237
|
+
**必需**: 是
|
|
238
|
+
|
|
239
|
+
CSS 文件输出目录。
|
|
240
|
+
|
|
241
|
+
```javascript
|
|
242
|
+
path: './dist' // 相对路径
|
|
243
|
+
path: 'D:/project/dist' // 绝对路径
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
#### fileName
|
|
247
|
+
|
|
248
|
+
**类型**: `string`
|
|
249
|
+
**必需**: 是
|
|
250
|
+
|
|
251
|
+
输出文件名(包含扩展名)。
|
|
252
|
+
|
|
253
|
+
```javascript
|
|
254
|
+
fileName: 'styles.wxss' // 微信小程序
|
|
255
|
+
fileName: 'styles.css' // 普通 CSS
|
|
256
|
+
fileName: 'common.acss' // 支付宝小程序
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
#### commonCssPath
|
|
260
|
+
|
|
261
|
+
**类型**: `string`
|
|
262
|
+
**可选**: 是
|
|
263
|
+
|
|
264
|
+
共用 CSS 文件路径,会在生成的 CSS 文件开头引入。
|
|
265
|
+
|
|
266
|
+
```javascript
|
|
267
|
+
commonCssPath: './common.css'
|
|
268
|
+
|
|
269
|
+
// 生成的 CSS 文件会包含
|
|
270
|
+
@import './common.css';
|
|
271
|
+
|
|
272
|
+
.m-10 { margin: 20rpx; }
|
|
273
|
+
// ...
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
## 多文件配置 (multiFile)
|
|
277
|
+
|
|
278
|
+
多文件监听和输出配置。
|
|
279
|
+
|
|
280
|
+
```javascript
|
|
281
|
+
multiFile: {
|
|
282
|
+
entry: {
|
|
283
|
+
// 单入口
|
|
284
|
+
// path: './src',
|
|
285
|
+
// 多入口(目录/文件可混用)
|
|
286
|
+
path: ['./src', './subpackages', './pages/index.wxml'],
|
|
287
|
+
fileName: ['index.wxml', 'detail.wxml'],
|
|
288
|
+
fileType: ['html', 'wxml']
|
|
289
|
+
},
|
|
290
|
+
output: {
|
|
291
|
+
cssOutType: 'uniFile',
|
|
292
|
+
path: './dist',
|
|
293
|
+
fileName: 'common.wxss',
|
|
294
|
+
fileType: 'wxss'
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
### entry 配置
|
|
300
|
+
|
|
301
|
+
#### path
|
|
302
|
+
|
|
303
|
+
**类型**: `string | Array<string>`
|
|
304
|
+
**必需**: 是
|
|
305
|
+
|
|
306
|
+
扫描/监听入口路径。
|
|
307
|
+
|
|
308
|
+
- 当为 `string`:表示单目录或单文件
|
|
309
|
+
- 当为 `Array<string>`:表示多目录/多文件(目录与文件可混用)
|
|
310
|
+
|
|
311
|
+
```javascript
|
|
312
|
+
path: './src'
|
|
313
|
+
path: 'D:/project/src'
|
|
314
|
+
path: ['./src', './packages', './pages/index.wxml']
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
#### fileName
|
|
318
|
+
|
|
319
|
+
**类型**: `Array<string>`
|
|
320
|
+
**可选**: 是
|
|
321
|
+
|
|
322
|
+
需要监听的特定文件名列表。如果不指定,则监听所有符合 `fileType` 的文件。
|
|
323
|
+
|
|
324
|
+
```javascript
|
|
325
|
+
fileName: ['index.wxml', 'detail.wxml']
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
#### fileType
|
|
329
|
+
|
|
330
|
+
**类型**: `Array<string>`
|
|
331
|
+
**必需**: 是
|
|
332
|
+
|
|
333
|
+
需要监听的文件扩展名(不含点号)。
|
|
334
|
+
|
|
335
|
+
```javascript
|
|
336
|
+
fileType: ['html', 'wxml'] // 微信小程序
|
|
337
|
+
fileType: ['html', 'axml'] // 支付宝小程序
|
|
338
|
+
fileType: ['html', 'vue'] // Vue 项目
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
### output 配置
|
|
342
|
+
|
|
343
|
+
#### cssOutType
|
|
344
|
+
|
|
345
|
+
**类型**: `string`
|
|
346
|
+
**必需**: 是
|
|
347
|
+
**可选值**: `'filePath'` | `'uniFile'`
|
|
348
|
+
|
|
349
|
+
CSS 输出模式。
|
|
350
|
+
|
|
351
|
+
```javascript
|
|
352
|
+
// filePath - 每个源文件生成对应的 CSS 文件
|
|
353
|
+
cssOutType: 'filePath'
|
|
354
|
+
// src/pages/index.wxml → dist/pages/index.wxss
|
|
355
|
+
// src/pages/detail.wxml → dist/pages/detail.wxss
|
|
356
|
+
|
|
357
|
+
// uniFile - 所有样式合并到一个文件
|
|
358
|
+
cssOutType: 'uniFile'
|
|
359
|
+
// 所有文件 → dist/common.wxss
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
**对比**:
|
|
363
|
+
|
|
364
|
+
| 特性 | filePath | uniFile |
|
|
365
|
+
|------|----------|---------|
|
|
366
|
+
| 文件数量 | 多个 | 单个 |
|
|
367
|
+
| 更新方式 | 增量更新 | 全量更新 |
|
|
368
|
+
| 适用场景 | 组件化开发 | 统一样式管理 |
|
|
369
|
+
| 性能 | 快速 | 防抖优化 |
|
|
370
|
+
|
|
371
|
+
#### path
|
|
372
|
+
|
|
373
|
+
**类型**: `string`
|
|
374
|
+
**必需**: 是
|
|
375
|
+
|
|
376
|
+
CSS 文件输出目录。
|
|
377
|
+
|
|
378
|
+
```javascript
|
|
379
|
+
path: './dist'
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
#### fileName
|
|
383
|
+
|
|
384
|
+
**类型**: `string`
|
|
385
|
+
**必需**: 当 `cssOutType` 为 `'uniFile'` 时必需
|
|
386
|
+
|
|
387
|
+
统一输出文件名。
|
|
388
|
+
|
|
389
|
+
```javascript
|
|
390
|
+
fileName: 'common.wxss' // 仅在 uniFile 模式下使用
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
#### fileType
|
|
394
|
+
|
|
395
|
+
**类型**: `string`
|
|
396
|
+
**必需**: 当 `cssOutType` 为 `'filePath'` 时必需
|
|
397
|
+
|
|
398
|
+
输出文件的扩展名(不含点号)。
|
|
399
|
+
|
|
400
|
+
```javascript
|
|
401
|
+
fileType: 'wxss' // 仅在 filePath 模式下使用
|
|
402
|
+
```
|
|
403
|
+
|
|
404
|
+
## 原子化规则 (atomicRules)
|
|
405
|
+
|
|
406
|
+
定义原子化 CSS 类的生成规则。
|
|
407
|
+
|
|
408
|
+
```javascript
|
|
409
|
+
atomicRules: {
|
|
410
|
+
spacing: { /* 间距规则 */ },
|
|
411
|
+
sizing: { /* 尺寸规则 */ },
|
|
412
|
+
typography: { /* 字体规则 */ },
|
|
413
|
+
positioning: { /* 定位规则 */ },
|
|
414
|
+
borders: { /* 边框规则 */ },
|
|
415
|
+
effects: { /* 效果规则 */ }
|
|
416
|
+
}
|
|
417
|
+
```
|
|
418
|
+
|
|
419
|
+
### 规则格式
|
|
420
|
+
|
|
421
|
+
```javascript
|
|
422
|
+
atomicRules: {
|
|
423
|
+
spacing: {
|
|
424
|
+
m: {
|
|
425
|
+
properties: ['margin'],
|
|
426
|
+
defaultUnit: 'rpx'
|
|
427
|
+
},
|
|
428
|
+
mt: {
|
|
429
|
+
properties: ['margin-top'],
|
|
430
|
+
defaultUnit: 'rpx'
|
|
431
|
+
},
|
|
432
|
+
mx: {
|
|
433
|
+
properties: ['margin-left', 'margin-right'],
|
|
434
|
+
defaultUnit: 'rpx'
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
```
|
|
439
|
+
|
|
440
|
+
### 配置项说明
|
|
441
|
+
|
|
442
|
+
#### properties
|
|
443
|
+
|
|
444
|
+
**类型**: `Array<string>`
|
|
445
|
+
**必需**: 是
|
|
446
|
+
|
|
447
|
+
CSS 属性列表。支持单个或多个属性。
|
|
448
|
+
|
|
449
|
+
```javascript
|
|
450
|
+
// 单个属性
|
|
451
|
+
properties: ['margin']
|
|
452
|
+
|
|
453
|
+
// 多个属性
|
|
454
|
+
properties: ['margin-left', 'margin-right']
|
|
455
|
+
```
|
|
456
|
+
|
|
457
|
+
#### defaultUnit
|
|
458
|
+
|
|
459
|
+
**类型**: `string`
|
|
460
|
+
**必需**: 是
|
|
461
|
+
|
|
462
|
+
该规则的默认单位。
|
|
463
|
+
|
|
464
|
+
```javascript
|
|
465
|
+
defaultUnit: 'rpx' // 使用 rpx
|
|
466
|
+
defaultUnit: 'px' // 使用 px
|
|
467
|
+
defaultUnit: '' // 无单位
|
|
468
|
+
```
|
|
469
|
+
|
|
470
|
+
#### skipConversion
|
|
471
|
+
|
|
472
|
+
**类型**: `boolean`
|
|
473
|
+
**默认值**: `false`
|
|
474
|
+
|
|
475
|
+
是否跳过单位转换。
|
|
476
|
+
|
|
477
|
+
```javascript
|
|
478
|
+
{
|
|
479
|
+
properties: ['transition'],
|
|
480
|
+
defaultUnit: 'ms',
|
|
481
|
+
skipConversion: true // 不进行单位转换
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
// class="transition-300" → transition: 300ms; (不乘以 unitConversion)
|
|
485
|
+
```
|
|
486
|
+
|
|
487
|
+
### 内置规则类别
|
|
488
|
+
|
|
489
|
+
#### spacing - 间距系统
|
|
490
|
+
|
|
491
|
+
```javascript
|
|
492
|
+
spacing: {
|
|
493
|
+
// Margin
|
|
494
|
+
m: { properties: ['margin'], defaultUnit: 'rpx' },
|
|
495
|
+
mt: { properties: ['margin-top'], defaultUnit: 'rpx' },
|
|
496
|
+
mr: { properties: ['margin-right'], defaultUnit: 'rpx' },
|
|
497
|
+
mb: { properties: ['margin-bottom'], defaultUnit: 'rpx' },
|
|
498
|
+
ml: { properties: ['margin-left'], defaultUnit: 'rpx' },
|
|
499
|
+
mx: { properties: ['margin-left', 'margin-right'], defaultUnit: 'rpx' },
|
|
500
|
+
my: { properties: ['margin-top', 'margin-bottom'], defaultUnit: 'rpx' },
|
|
501
|
+
|
|
502
|
+
// Padding
|
|
503
|
+
p: { properties: ['padding'], defaultUnit: 'rpx' },
|
|
504
|
+
pt: { properties: ['padding-top'], defaultUnit: 'rpx' },
|
|
505
|
+
pr: { properties: ['padding-right'], defaultUnit: 'rpx' },
|
|
506
|
+
pb: { properties: ['padding-bottom'], defaultUnit: 'rpx' },
|
|
507
|
+
pl: { properties: ['padding-left'], defaultUnit: 'rpx' },
|
|
508
|
+
px: { properties: ['padding-left', 'padding-right'], defaultUnit: 'rpx' },
|
|
509
|
+
py: { properties: ['padding-top', 'padding-bottom'], defaultUnit: 'rpx' },
|
|
510
|
+
|
|
511
|
+
// Gap
|
|
512
|
+
gap: { properties: ['gap'], defaultUnit: 'rpx' }
|
|
513
|
+
}
|
|
514
|
+
```
|
|
515
|
+
|
|
516
|
+
#### sizing - 尺寸系统
|
|
517
|
+
|
|
518
|
+
```javascript
|
|
519
|
+
sizing: {
|
|
520
|
+
w: { properties: ['width'], defaultUnit: 'rpx' },
|
|
521
|
+
h: { properties: ['height'], defaultUnit: 'rpx' },
|
|
522
|
+
'max-w': { properties: ['max-width'], defaultUnit: 'rpx' },
|
|
523
|
+
'max-h': { properties: ['max-height'], defaultUnit: 'rpx' },
|
|
524
|
+
'min-w': { properties: ['min-width'], defaultUnit: 'rpx' },
|
|
525
|
+
'min-h': { properties: ['min-height'], defaultUnit: 'rpx' },
|
|
526
|
+
size: { properties: ['width', 'height'], defaultUnit: 'rpx' }
|
|
527
|
+
}
|
|
528
|
+
```
|
|
529
|
+
|
|
530
|
+
#### typography - 字体系统
|
|
531
|
+
|
|
532
|
+
```javascript
|
|
533
|
+
typography: {
|
|
534
|
+
'text-size': { properties: ['font-size'], defaultUnit: 'rpx' },
|
|
535
|
+
text: { properties: ['font-size'], defaultUnit: 'rpx' },
|
|
536
|
+
font: { properties: ['font-weight'], defaultUnit: '' },
|
|
537
|
+
leading: { properties: ['line-height'], defaultUnit: '' },
|
|
538
|
+
tracking: { properties: ['letter-spacing'], defaultUnit: 'rpx' }
|
|
539
|
+
}
|
|
540
|
+
```
|
|
541
|
+
|
|
542
|
+
#### positioning - 定位系统
|
|
543
|
+
|
|
544
|
+
```javascript
|
|
545
|
+
positioning: {
|
|
546
|
+
top: { properties: ['top'], defaultUnit: 'rpx' },
|
|
547
|
+
right: { properties: ['right'], defaultUnit: 'rpx' },
|
|
548
|
+
bottom: { properties: ['bottom'], defaultUnit: 'rpx' },
|
|
549
|
+
left: { properties: ['left'], defaultUnit: 'rpx' },
|
|
550
|
+
inset: { properties: ['top', 'right', 'bottom', 'left'], defaultUnit: 'rpx' },
|
|
551
|
+
'inset-x': { properties: ['left', 'right'], defaultUnit: 'rpx' },
|
|
552
|
+
'inset-y': { properties: ['top', 'bottom'], defaultUnit: 'rpx' }
|
|
553
|
+
}
|
|
554
|
+
```
|
|
555
|
+
|
|
556
|
+
#### borders - 边框系统
|
|
557
|
+
|
|
558
|
+
```javascript
|
|
559
|
+
borders: {
|
|
560
|
+
rounded: { properties: ['border-radius'], defaultUnit: 'rpx' },
|
|
561
|
+
border: { properties: ['border-width'], defaultUnit: 'rpx' },
|
|
562
|
+
bordert: { properties: ['border-top-width'], defaultUnit: 'rpx' },
|
|
563
|
+
borderr: { properties: ['border-right-width'], defaultUnit: 'rpx' },
|
|
564
|
+
borderb: { properties: ['border-bottom-width'], defaultUnit: 'rpx' },
|
|
565
|
+
borderl: { properties: ['border-left-width'], defaultUnit: 'rpx' }
|
|
566
|
+
}
|
|
567
|
+
```
|
|
568
|
+
|
|
569
|
+
#### effects - 效果系统
|
|
570
|
+
|
|
571
|
+
```javascript
|
|
572
|
+
effects: {
|
|
573
|
+
opacity: { properties: ['opacity'], defaultUnit: '' },
|
|
574
|
+
z: { properties: ['z-index'], defaultUnit: '' },
|
|
575
|
+
transition: { properties: ['transition'], defaultUnit: 'ms', skipConversion: true }
|
|
576
|
+
}
|
|
577
|
+
```
|
|
578
|
+
|
|
579
|
+
## 基础类配置 (baseClassName)
|
|
580
|
+
|
|
581
|
+
定义静态 CSS 类。
|
|
582
|
+
|
|
583
|
+
### 基本格式
|
|
584
|
+
|
|
585
|
+
```javascript
|
|
586
|
+
baseClassName: {
|
|
587
|
+
// 简单类
|
|
588
|
+
'flex': 'display: flex;',
|
|
589
|
+
'hidden': 'display: none;',
|
|
590
|
+
|
|
591
|
+
// 多属性类
|
|
592
|
+
'flex-center': 'display: flex; justify-content: center; align-items: center;',
|
|
593
|
+
|
|
594
|
+
// 颜色缩写配置
|
|
595
|
+
color: {
|
|
596
|
+
ABBR: 'color'
|
|
597
|
+
},
|
|
598
|
+
bg: {
|
|
599
|
+
ABBR: 'background-color'
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
```
|
|
603
|
+
|
|
604
|
+
### 使用示例
|
|
605
|
+
|
|
606
|
+
```html
|
|
607
|
+
<!-- 简单类 -->
|
|
608
|
+
<view class="flex hidden">...</view>
|
|
609
|
+
|
|
610
|
+
<!-- 多属性类 -->
|
|
611
|
+
<view class="flex-center">...</view>
|
|
612
|
+
|
|
613
|
+
<!-- 颜色类(需配合颜色配置) -->
|
|
614
|
+
<text class="color-red bg-blue">...</text>
|
|
615
|
+
```
|
|
616
|
+
|
|
617
|
+
### 颜色配置
|
|
618
|
+
|
|
619
|
+
使用 `ABBR` 字段配合颜色定义:
|
|
620
|
+
|
|
621
|
+
```javascript
|
|
622
|
+
const baseColor = {
|
|
623
|
+
white: '#ffffff',
|
|
624
|
+
black: '#000000',
|
|
625
|
+
red: '#ef4444',
|
|
626
|
+
blue: '#3b82f6'
|
|
627
|
+
};
|
|
628
|
+
|
|
629
|
+
const config = {
|
|
630
|
+
baseClassName: {
|
|
631
|
+
color: { ABBR: 'color' },
|
|
632
|
+
bg: { ABBR: 'background-color' }
|
|
633
|
+
}
|
|
634
|
+
};
|
|
635
|
+
|
|
636
|
+
function getConfig() {
|
|
637
|
+
const handleClass = config.baseClassName;
|
|
638
|
+
Object.values(handleClass).forEach((item) => {
|
|
639
|
+
if (item.ABBR) {
|
|
640
|
+
Object.assign(item, baseColor);
|
|
641
|
+
}
|
|
642
|
+
});
|
|
643
|
+
return config;
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
module.exports = getConfig();
|
|
647
|
+
```
|
|
648
|
+
|
|
649
|
+
生成效果:
|
|
650
|
+
|
|
651
|
+
```css
|
|
652
|
+
.color-white { color: #ffffff; }
|
|
653
|
+
.color-black { color: #000000; }
|
|
654
|
+
.color-red { color: #ef4444; }
|
|
655
|
+
.bg-white { background-color: #ffffff; }
|
|
656
|
+
.bg-blue { background-color: #3b82f6; }
|
|
657
|
+
```
|
|
658
|
+
|
|
659
|
+
### 常用静态类
|
|
660
|
+
|
|
661
|
+
#### 布局类
|
|
662
|
+
|
|
663
|
+
```javascript
|
|
664
|
+
baseClassName: {
|
|
665
|
+
// Display
|
|
666
|
+
'block': 'display: block;',
|
|
667
|
+
'inline': 'display: inline;',
|
|
668
|
+
'inline-block': 'display: inline-block;',
|
|
669
|
+
'flex': 'display: flex;',
|
|
670
|
+
'inline-flex': 'display: inline-flex;',
|
|
671
|
+
'grid': 'display: grid;',
|
|
672
|
+
'hidden': 'display: none;',
|
|
673
|
+
|
|
674
|
+
// Flexbox
|
|
675
|
+
'flex-row': 'flex-direction: row;',
|
|
676
|
+
'flex-col': 'flex-direction: column;',
|
|
677
|
+
'flex-wrap': 'flex-wrap: wrap;',
|
|
678
|
+
'items-center': 'align-items: center;',
|
|
679
|
+
'justify-center': 'justify-content: center;',
|
|
680
|
+
'justify-between': 'justify-content: space-between;',
|
|
681
|
+
|
|
682
|
+
// Position
|
|
683
|
+
'relative': 'position: relative;',
|
|
684
|
+
'absolute': 'position: absolute;',
|
|
685
|
+
'fixed': 'position: fixed;',
|
|
686
|
+
'sticky': 'position: sticky;'
|
|
687
|
+
}
|
|
688
|
+
```
|
|
689
|
+
|
|
690
|
+
## Important 标识 (importantFlags)
|
|
691
|
+
|
|
692
|
+
配置 `!important` 标识符。
|
|
693
|
+
|
|
694
|
+
```javascript
|
|
695
|
+
importantFlags: {
|
|
696
|
+
prefix: ['!', '$$'],
|
|
697
|
+
suffix: ['-i', '_i', '__imp'],
|
|
698
|
+
custom: ['--important', '##IMP##']
|
|
699
|
+
}
|
|
700
|
+
```
|
|
701
|
+
|
|
702
|
+
### 配置项说明
|
|
703
|
+
|
|
704
|
+
#### prefix
|
|
705
|
+
|
|
706
|
+
**类型**: `Array<string>`
|
|
707
|
+
|
|
708
|
+
前缀标识符。
|
|
709
|
+
|
|
710
|
+
```javascript
|
|
711
|
+
prefix: ['!', '$$']
|
|
712
|
+
|
|
713
|
+
// 使用
|
|
714
|
+
// class="!m-10" → margin: 20rpx !important;
|
|
715
|
+
// class="$$w-100" → width: 200rpx !important;
|
|
716
|
+
```
|
|
717
|
+
|
|
718
|
+
#### suffix
|
|
719
|
+
|
|
720
|
+
**类型**: `Array<string>`
|
|
721
|
+
|
|
722
|
+
后缀标识符。
|
|
723
|
+
|
|
724
|
+
```javascript
|
|
725
|
+
suffix: ['-i', '_i', '__imp']
|
|
726
|
+
|
|
727
|
+
// 使用
|
|
728
|
+
// class="m-10-i" → margin: 20rpx !important;
|
|
729
|
+
// class="w-100_i" → width: 200rpx !important;
|
|
730
|
+
// class="p-20__imp" → padding: 40rpx !important;
|
|
731
|
+
```
|
|
732
|
+
|
|
733
|
+
#### custom
|
|
734
|
+
|
|
735
|
+
**类型**: `Array<string>`
|
|
736
|
+
|
|
737
|
+
自定义标识符。
|
|
738
|
+
|
|
739
|
+
```javascript
|
|
740
|
+
custom: ['--important', '##IMP##']
|
|
741
|
+
|
|
742
|
+
// 使用
|
|
743
|
+
// class="m--important-10" → margin: 20rpx !important;
|
|
744
|
+
// class="w##IMP##-100" → width: 200rpx !important;
|
|
745
|
+
```
|
|
746
|
+
|
|
747
|
+
## 变体规则 (variants)
|
|
748
|
+
|
|
749
|
+
配置响应式和状态变体。
|
|
750
|
+
|
|
751
|
+
```javascript
|
|
752
|
+
variants: {
|
|
753
|
+
responsive: ['sm', 'md', 'lg', 'xl', '2xl'],
|
|
754
|
+
states: ['hover', 'focus', 'active', 'disabled'],
|
|
755
|
+
darkMode: ['dark']
|
|
756
|
+
}
|
|
757
|
+
```
|
|
758
|
+
|
|
759
|
+
### 响应式变体 (responsive)
|
|
760
|
+
|
|
761
|
+
响应式变体允许你根据屏幕尺寸应用不同的样式。使用 Tailwind 风格的语法:`sm:`, `md:`, `lg:`, `xl:`, `2xl:`。
|
|
762
|
+
|
|
763
|
+
#### 使用示例
|
|
764
|
+
|
|
765
|
+
```html
|
|
766
|
+
<!-- 基础样式 + 响应式样式 -->
|
|
767
|
+
<view class="w-full sm:w-100 md:w-200 lg:w-300">
|
|
768
|
+
<!-- 默认宽度 100%,小屏 100rpx,中屏 200rpx,大屏 300rpx -->
|
|
769
|
+
</view>
|
|
770
|
+
|
|
771
|
+
<!-- 响应式静态类 -->
|
|
772
|
+
<view class="hidden sm:flex">
|
|
773
|
+
<!-- 默认隐藏,小屏及以上显示为 flex -->
|
|
774
|
+
</view>
|
|
775
|
+
|
|
776
|
+
<!-- 响应式颜色 -->
|
|
777
|
+
<text class="color-red sm:color-blue md:color-green">
|
|
778
|
+
<!-- 默认红色,小屏蓝色,中屏绿色 -->
|
|
779
|
+
</text>
|
|
780
|
+
```
|
|
781
|
+
|
|
782
|
+
#### 断点配置 (breakpoints)
|
|
783
|
+
|
|
784
|
+
默认使用 Tailwind 标准断点值,可在配置中自定义:
|
|
785
|
+
|
|
786
|
+
```javascript
|
|
787
|
+
breakpoints: {
|
|
788
|
+
sm: '640px', // 小屏幕(手机横屏)
|
|
789
|
+
md: '768px', // 中等屏幕(平板)
|
|
790
|
+
lg: '1024px', // 大屏幕(笔记本)
|
|
791
|
+
xl: '1280px', // 超大屏幕(桌面)
|
|
792
|
+
'2xl': '1536px' // 超超大屏幕(大桌面)
|
|
793
|
+
}
|
|
794
|
+
```
|
|
795
|
+
|
|
796
|
+
**自定义断点**:
|
|
797
|
+
|
|
798
|
+
```javascript
|
|
799
|
+
breakpoints: {
|
|
800
|
+
sm: '480px', // 自定义小屏断点
|
|
801
|
+
md: '768px',
|
|
802
|
+
lg: '1024px',
|
|
803
|
+
xl: '1440px', // 自定义超大屏断点
|
|
804
|
+
'2xl': '1920px'
|
|
805
|
+
}
|
|
806
|
+
```
|
|
807
|
+
|
|
808
|
+
**注意**:
|
|
809
|
+
- 断点值必须包含单位(如 `px`, `em`, `rem`)
|
|
810
|
+
- 如果配置中未指定 `breakpoints`,将使用默认 Tailwind 值
|
|
811
|
+
- 响应式变体使用 `min-width` 媒体查询(移动优先)
|
|
812
|
+
|
|
813
|
+
#### 生成的 CSS 示例
|
|
814
|
+
|
|
815
|
+
```css
|
|
816
|
+
/* 基础样式 */
|
|
817
|
+
.w-full { width: 100%; }
|
|
818
|
+
|
|
819
|
+
/* 响应式样式 */
|
|
820
|
+
@media (min-width: 640px) {
|
|
821
|
+
.sm\:w-100 { width: 200rpx; }
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
@media (min-width: 768px) {
|
|
825
|
+
.md\:w-200 { width: 400rpx; }
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
@media (min-width: 1024px) {
|
|
829
|
+
.lg\:w-300 { width: 600rpx; }
|
|
830
|
+
}
|
|
831
|
+
```
|
|
832
|
+
|
|
833
|
+
### 状态变体 (states)
|
|
834
|
+
|
|
835
|
+
状态变体配置(计划中的功能,当前仅配置名称)。
|
|
836
|
+
|
|
837
|
+
```javascript
|
|
838
|
+
states: ['hover', 'focus', 'active', 'disabled', 'first', 'last', 'odd', 'even']
|
|
839
|
+
```
|
|
840
|
+
|
|
841
|
+
### 暗色模式 (darkMode)
|
|
842
|
+
|
|
843
|
+
暗色模式变体配置(计划中的功能,当前仅配置名称)。
|
|
844
|
+
|
|
845
|
+
```javascript
|
|
846
|
+
darkMode: ['dark']
|
|
847
|
+
```
|
|
848
|
+
|
|
849
|
+
## 配置示例
|
|
850
|
+
|
|
851
|
+
### 微信小程序完整配置
|
|
852
|
+
|
|
853
|
+
```javascript
|
|
854
|
+
module.exports = {
|
|
855
|
+
system: {
|
|
856
|
+
cssFormat: 'compressed',
|
|
857
|
+
baseUnit: 'rpx',
|
|
858
|
+
unitConversion: 2,
|
|
859
|
+
compression: true,
|
|
860
|
+
sortClasses: true,
|
|
861
|
+
unitStrategy: {
|
|
862
|
+
autoDetect: true,
|
|
863
|
+
propertyUnits: {
|
|
864
|
+
'font-size': 'rpx',
|
|
865
|
+
'width|height': 'rpx',
|
|
866
|
+
'opacity': '',
|
|
867
|
+
'z-index': ''
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
},
|
|
871
|
+
|
|
872
|
+
multiFile: {
|
|
873
|
+
entry: {
|
|
874
|
+
path: './pages',
|
|
875
|
+
fileType: ['wxml']
|
|
876
|
+
},
|
|
877
|
+
output: {
|
|
878
|
+
cssOutType: 'uniFile',
|
|
879
|
+
path: './styles',
|
|
880
|
+
fileName: 'common.wxss'
|
|
881
|
+
}
|
|
882
|
+
},
|
|
883
|
+
|
|
884
|
+
atomicRules: {
|
|
885
|
+
spacing: {
|
|
886
|
+
m: { properties: ['margin'], defaultUnit: 'rpx' },
|
|
887
|
+
p: { properties: ['padding'], defaultUnit: 'rpx' }
|
|
888
|
+
},
|
|
889
|
+
sizing: {
|
|
890
|
+
w: { properties: ['width'], defaultUnit: 'rpx' },
|
|
891
|
+
h: { properties: ['height'], defaultUnit: 'rpx' }
|
|
892
|
+
}
|
|
893
|
+
},
|
|
894
|
+
|
|
895
|
+
baseClassName: {
|
|
896
|
+
'flex': 'display: flex;',
|
|
897
|
+
'flex-center': 'display: flex; justify-content: center; align-items: center;'
|
|
898
|
+
},
|
|
899
|
+
|
|
900
|
+
importantFlags: {
|
|
901
|
+
suffix: ['-i']
|
|
902
|
+
}
|
|
903
|
+
};
|
|
904
|
+
```
|
|
905
|
+
|
|
906
|
+
### Web 项目配置
|
|
907
|
+
|
|
908
|
+
```javascript
|
|
909
|
+
module.exports = {
|
|
910
|
+
system: {
|
|
911
|
+
cssFormat: 'multiLine',
|
|
912
|
+
baseUnit: 'px',
|
|
913
|
+
unitConversion: 1,
|
|
914
|
+
compression: false,
|
|
915
|
+
sortClasses: true
|
|
916
|
+
},
|
|
917
|
+
|
|
918
|
+
multiFile: {
|
|
919
|
+
entry: {
|
|
920
|
+
path: './src',
|
|
921
|
+
fileType: ['html', 'vue']
|
|
922
|
+
},
|
|
923
|
+
output: {
|
|
924
|
+
cssOutType: 'filePath',
|
|
925
|
+
path: './dist/css',
|
|
926
|
+
fileType: 'css'
|
|
927
|
+
}
|
|
928
|
+
},
|
|
929
|
+
|
|
930
|
+
atomicRules: {
|
|
931
|
+
spacing: {
|
|
932
|
+
m: { properties: ['margin'], defaultUnit: 'px' },
|
|
933
|
+
p: { properties: ['padding'], defaultUnit: 'px' }
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
};
|
|
937
|
+
```
|
|
938
|
+
|
|
939
|
+
## 最佳实践
|
|
940
|
+
|
|
941
|
+
### 1. 模块化配置
|
|
942
|
+
|
|
943
|
+
将大型配置拆分为多个模块:
|
|
944
|
+
|
|
945
|
+
```javascript
|
|
946
|
+
// configs/spacing.config.js
|
|
947
|
+
module.exports = {
|
|
948
|
+
m: { properties: ['margin'], defaultUnit: 'rpx' },
|
|
949
|
+
p: { properties: ['padding'], defaultUnit: 'rpx' }
|
|
950
|
+
};
|
|
951
|
+
|
|
952
|
+
// class2css.config.js
|
|
953
|
+
const spacing = require('./configs/spacing.config');
|
|
954
|
+
|
|
955
|
+
module.exports = {
|
|
956
|
+
atomicRules: {
|
|
957
|
+
spacing: spacing
|
|
958
|
+
}
|
|
959
|
+
};
|
|
960
|
+
```
|
|
961
|
+
|
|
962
|
+
### 2. 环境区分
|
|
963
|
+
|
|
964
|
+
根据环境使用不同配置:
|
|
965
|
+
|
|
966
|
+
```javascript
|
|
967
|
+
const isDev = process.env.NODE_ENV === 'development';
|
|
968
|
+
|
|
969
|
+
module.exports = {
|
|
970
|
+
system: {
|
|
971
|
+
cssFormat: isDev ? 'multiLine' : 'compressed',
|
|
972
|
+
compression: !isDev
|
|
973
|
+
}
|
|
974
|
+
};
|
|
975
|
+
```
|
|
976
|
+
|
|
977
|
+
### 3. 性能优化
|
|
978
|
+
|
|
979
|
+
```javascript
|
|
980
|
+
system: {
|
|
981
|
+
compression: true, // 启用压缩
|
|
982
|
+
sortClasses: true, // 启用排序
|
|
983
|
+
unitStrategy: {
|
|
984
|
+
autoDetect: true // 启用智能单位检测
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
```
|
|
988
|
+
|
|
989
|
+
### 4. 命名规范
|
|
990
|
+
|
|
991
|
+
保持类名简洁且语义化:
|
|
992
|
+
|
|
993
|
+
```javascript
|
|
994
|
+
atomicRules: {
|
|
995
|
+
spacing: {
|
|
996
|
+
m: { /* margin */ },
|
|
997
|
+
mt: { /* margin-top */ },
|
|
998
|
+
mx: { /* margin-left + margin-right */ }
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
```
|
|
1002
|
+
|
|
1003
|
+
### 5. 单位策略
|
|
1004
|
+
|
|
1005
|
+
合理配置单位策略:
|
|
1006
|
+
|
|
1007
|
+
```javascript
|
|
1008
|
+
unitStrategy: {
|
|
1009
|
+
autoDetect: true,
|
|
1010
|
+
propertyUnits: {
|
|
1011
|
+
'font-size': 'rpx', // 字体使用 rpx
|
|
1012
|
+
'opacity': '', // 透明度无单位
|
|
1013
|
+
'z-index': '', // 层级无单位
|
|
1014
|
+
'line-height': '' // 行高可以无单位
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
```
|
|
1018
|
+
|
|
1019
|
+
## 配置验证
|
|
1020
|
+
|
|
1021
|
+
使用内置验证工具检查配置:
|
|
1022
|
+
|
|
1023
|
+
```javascript
|
|
1024
|
+
const { ConfigValidator } = require('class2css');
|
|
1025
|
+
|
|
1026
|
+
const validator = new ConfigValidator(eventBus);
|
|
1027
|
+
const result = validator.validateConfig(config);
|
|
1028
|
+
|
|
1029
|
+
if (!result.isValid) {
|
|
1030
|
+
console.log('配置错误:', result.errors);
|
|
1031
|
+
console.log('警告:', result.warnings);
|
|
1032
|
+
}
|
|
1033
|
+
```
|
|
1034
|
+
|
|
1035
|
+
## 配置诊断
|
|
1036
|
+
|
|
1037
|
+
运行配置诊断获取优化建议:
|
|
1038
|
+
|
|
1039
|
+
```javascript
|
|
1040
|
+
const { ConfigDiagnostics } = require('class2css');
|
|
1041
|
+
|
|
1042
|
+
const diagnostics = new ConfigDiagnostics(eventBus, configManager);
|
|
1043
|
+
const results = await diagnostics.runFullDiagnostics();
|
|
1044
|
+
|
|
1045
|
+
console.log(diagnostics.generateReport());
|
|
1046
|
+
```
|
|
1047
|
+
|
|
1048
|
+
## 常见问题
|
|
1049
|
+
|
|
1050
|
+
### Q: 如何禁用单位转换?
|
|
1051
|
+
|
|
1052
|
+
```javascript
|
|
1053
|
+
system: {
|
|
1054
|
+
unitConversion: 1 // 设置为 1 表示不转换
|
|
1055
|
+
}
|
|
1056
|
+
```
|
|
1057
|
+
|
|
1058
|
+
### Q: 如何支持多种单位?
|
|
1059
|
+
|
|
1060
|
+
```javascript
|
|
1061
|
+
system: {
|
|
1062
|
+
unitStrategy: {
|
|
1063
|
+
autoDetect: true // 启用自动检测,用户可以自由使用任何单位
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
```
|
|
1067
|
+
|
|
1068
|
+
### Q: 如何配置无单位属性?
|
|
1069
|
+
|
|
1070
|
+
```javascript
|
|
1071
|
+
system: {
|
|
1072
|
+
unitStrategy: {
|
|
1073
|
+
propertyUnits: {
|
|
1074
|
+
'opacity': '',
|
|
1075
|
+
'z-index': '',
|
|
1076
|
+
'line-height': ''
|
|
1077
|
+
}
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
```
|
|
1081
|
+
|
|
1082
|
+
### Q: 如何使用统一文件模式?
|
|
1083
|
+
|
|
1084
|
+
```javascript
|
|
1085
|
+
multiFile: {
|
|
1086
|
+
output: {
|
|
1087
|
+
cssOutType: 'uniFile',
|
|
1088
|
+
fileName: 'common.wxss'
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
```
|
|
1092
|
+
|
|
1093
|
+
---
|
|
1094
|
+
|
|
1095
|
+
> 💡 更多配置示例请参考项目中的 `class2css.config.js` 文件。
|
|
1096
|
+
|