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
package/docs/index.md CHANGED
@@ -1,68 +1,71 @@
1
- ---
2
- home: true
3
- title: Class2CSS
4
- titleTemplate: false
5
-
6
- hero:
7
- name: Class2CSS
8
- text: 面向微信小程序的原子化 CSS 生成器
9
- tagline: 扫描 WXML/HTML 类名 → 生成 wxss/css。内置单位策略、配置校验、缓存与增量模式,让“写类名”变成稳定的工程能力。
10
- actions:
11
- - theme: brand
12
- text: 开始使用
13
- link: /guide/getting-started
14
- - theme: alt
15
- text: 查看配置
16
- link: /guide/config
17
-
18
- features:
19
- - title: 类名即规则
20
- details: 用 `m-10`、`w-100` 这类表达直接生成样式,避免手写重复 CSS,保持风格一致。
21
- - title: 智能单位策略
22
- details: 支持 rpx/px 等单位;可自动检测用户单位,并按属性设置默认单位与转换比例。
23
- - title: 增量只增不删
24
- details: 统一文件模式支持“只增不删”与 `appendDelta` 追加写入,兼顾性能与可控性。
25
- - title: 配置诊断与兼容
26
- details: 新旧配置无缝兼容;冲突检测、健康检查与建议,减少“配置写错但没发现”的成本。
27
- ---
28
-
29
- ## Quick start
30
-
31
- 安装:
32
-
33
- ```bash
34
- npm install css2class --save-dev
35
- ```
36
-
37
- 创建 `class2css.config.js`(最小可用示例):
38
-
39
- ```js
40
- module.exports = {
41
- system: { baseUnit: 'rpx', unitConversion: 2, cssFormat: 'compressed' },
42
- output: { path: '../dist', fileName: 'styles.wxss' },
43
- cssName: {
44
- m: { classArr: ['margin'], unit: 'rpx' },
45
- w: { classArr: ['width'], unit: 'rpx' },
46
- h: { classArr: ['height'], unit: 'rpx' },
47
- },
48
- };
49
- ```
50
-
51
- 启动(监听模式):
52
-
53
- ```bash
54
- npm run start
55
- ```
56
-
57
- 在模板里使用类名:
58
-
59
- ```html
60
- <view class="m-10 w-100 h-200"></view>
61
- ```
62
-
63
- ## 下一步
64
-
65
- - **从零跑通一次**:阅读 [快速开始](./guide/getting-started.md)
66
- - **了解命令**:查看 [CLI](./guide/cli.md)
67
- - **把配置写“正确且可维护”**:阅读 [配置指南](./guide/config.md)
68
- - **大型项目优化**:了解 [增量模式(只增不删)](./guide/incremental.md)
1
+ ---
2
+ home: true
3
+ title: Class2CSS
4
+ titleTemplate: false
5
+
6
+ hero:
7
+ name: Class2CSS
8
+ text: 面向微信小程序的原子化 CSS 生成器
9
+ tagline: 扫描 WXML/HTML 类名 → 生成 wxss/css。内置单位策略、配置校验、缓存与增量模式,让“写类名”变成稳定的工程能力。
10
+ actions:
11
+ - theme: brand
12
+ text: 开始使用
13
+ link: /guide/getting-started
14
+ - theme: alt
15
+ text: 查看配置
16
+ link: /guide/config
17
+
18
+ features:
19
+ - title: 类名即规则
20
+ details: 用 `m-10`、`w-100` 这类表达直接生成样式,避免手写重复 CSS,保持风格一致。
21
+ - title: 智能单位策略
22
+ details: 支持 rpx/px 等单位;可自动检测用户单位,并按属性设置默认单位与转换比例。
23
+ - title: 直接颜色值
24
+ details: 支持 `bg-hex-fff`、`color-rgb-255-0-0` 等直接颜色值写法,无需预先配置颜色映射即可使用。
25
+ - title: 增量只增不删
26
+ details: 统一文件模式支持“只增不删”与 `appendDelta` 追加写入,兼顾性能与可控性。
27
+ - title: 配置诊断与兼容
28
+ details: 新旧配置无缝兼容;冲突检测、健康检查与建议,减少“配置写错但没发现”的成本。
29
+ ---
30
+
31
+ ## Quick start
32
+
33
+ 安装:
34
+
35
+ ```bash
36
+ npm install css2class --save-dev
37
+ ```
38
+
39
+ 复制示例配置(推荐):
40
+
41
+ - **小程序(wxss / rpx)**:`examples/weapp/class2css.config.js` + `examples/weapp/styles.config.js`
42
+ - **Web(css / px)**:`examples/web/class2css.config.js` + `examples/web/styles.config.js`
43
+
44
+ 你可以直接用 `-c` 指定示例配置启动:
45
+
46
+ ```bash
47
+ class2css -c ./examples/weapp/class2css.config.js
48
+ # 或
49
+ class2css -c ./examples/web/class2css.config.js
50
+ ```
51
+
52
+ 启动(监听模式):
53
+
54
+ ```bash
55
+ npm run start # 默认使用根目录配置:./class2css.config.js
56
+ npm run example:web # 显式跑 web 示例
57
+ npm run example:weapp # 显式跑 weapp 示例
58
+ ```
59
+
60
+ 在模板里使用类名:
61
+
62
+ ```html
63
+ <view class="m-10 w-100 h-200 bg-hex-fff color-rgb-0-0-0"></view>
64
+ ```
65
+
66
+ ## 下一步
67
+
68
+ - **从零跑通一次**:阅读 [快速开始](./guide/getting-started.md)
69
+ - **了解命令**:查看 [CLI](./guide/cli.md)
70
+ - **把配置写“正确且可维护”**:阅读 [配置指南](./guide/config.md)
71
+ - **大型项目优化**:了解 [增量模式(只增不删)](./guide/incremental.md)
@@ -0,0 +1,15 @@
1
+ # 小程序示例(配置模板)
2
+
3
+ 该目录主要用于提供可复制的配置模板。
4
+
5
+ ## 运行(一次)
6
+
7
+ 在仓库根目录执行:
8
+
9
+ ```bash
10
+ node bin/class2css.js --no-watch --config ./examples/weapp/class2css.config.js
11
+ ```
12
+
13
+ 生成结果:`examples/weapp/dist/styles.wxss`
14
+
15
+ > 你需要把 `multiFile.entry.path` 改成你的小程序项目源码目录(wxml 所在目录),才会扫描到真实的 class。
@@ -0,0 +1,70 @@
1
+ // ========== 工具配置(小程序示例)==========
2
+ // 用法:
3
+ // 1) 推荐直接复制 `examples/weapp` 目录到你的项目根目录
4
+ // 2) 然后执行:`class2css -c ./examples/weapp/class2css.config.js`
5
+ // 3) 或者把本文件重命名为根目录的 `class2css.config.js`,即可用默认配置启动
6
+
7
+ const path = require('path');
8
+ const stylesConfig = require('./styles.config.js');
9
+
10
+ module.exports = {
11
+ system: {
12
+ cssFormat: 'compressed', // 'multiLine' | 'singleLine' | 'compressed'
13
+ baseUnit: 'rpx',
14
+ unitConversion: 2,
15
+ compression: true,
16
+ sortClasses: true,
17
+ unitStrategy: {
18
+ autoDetect: true,
19
+ propertyUnits: {
20
+ 'font-size': 'rpx',
21
+ 'width|height': 'rpx',
22
+ opacity: '',
23
+ 'z-index': '',
24
+ 'line-height': '',
25
+ 'border-radius': 'rpx',
26
+ },
27
+ },
28
+ },
29
+
30
+ // 单文件输出(非 multiFile 模式时使用;当前版本运行时通常会走 multiFile)
31
+ output: {
32
+ path: path.join(__dirname, 'dist'),
33
+ fileName: 'styles.wxss',
34
+ // commonCssPath: './common.css', // 可选:公共基础样式(相对路径基于配置文件所在目录)
35
+ },
36
+
37
+ importantFlags: stylesConfig.importantFlags,
38
+
39
+ // 当前版本建议启用 multiFile(用于扫描/监听入口 + 输出策略)
40
+ multiFile: {
41
+ entry: {
42
+ // 扫描/监听入口:string | string[]
43
+ // 注意:工具内部会直接按“运行目录”解释相对路径,因此示例这里显式转绝对路径,保证开箱即用
44
+ path: path.join(__dirname, 'src'),
45
+ // 小程序:wxml;同时支持 html(便于混合模板或组件库)
46
+ fileType: ['wxml', 'html'],
47
+ },
48
+ output: {
49
+ // uniFile:统一输出单文件;filePath:输出到每个文件同目录(同名)
50
+ cssOutType: 'uniFile',
51
+ path: path.join(__dirname, 'dist'),
52
+ fileName: 'styles.wxss',
53
+ fileType: 'wxss', // cssOutType=filePath 时生效
54
+
55
+ // 增量模式(可选):只增不删(大型项目可开启)
56
+ incrementalOnlyAdd: false,
57
+ incrementalBaseline: 'fromOutputFile',
58
+ rebuildOnStart: true,
59
+ unusedReportLimit: 200,
60
+
61
+ // uniFile 写入策略(可选):'rewrite' | 'appendDelta'
62
+ uniFileWriteMode: 'rewrite',
63
+ },
64
+ },
65
+
66
+ atomicRules: stylesConfig.atomicRules,
67
+ baseClassName: stylesConfig.baseClassName,
68
+ variants: stylesConfig.variants,
69
+ breakpoints: stylesConfig.breakpoints,
70
+ };
@@ -0,0 +1,201 @@
1
+ // ========== 样式规则配置(小程序示例)==========
2
+ // 说明:
3
+ // - 该示例更偏向小程序(rpx),你可以直接复制到项目根目录使用
4
+ // - 如需 Web 示例(px),请参考 examples/web/styles.config.js
5
+
6
+ const DEFAULT_UNIT = 'rpx';
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();
@@ -0,0 +1,25 @@
1
+ # Web 示例(可直接看效果)
2
+
3
+ ## 1) 生成 CSS(一次)
4
+
5
+ 在仓库根目录执行:
6
+
7
+ ```bash
8
+ node bin/class2css.js --no-watch --config ./examples/web/class2css.config.js
9
+ ```
10
+
11
+ 生成结果:`examples/web/dist/styles.css`
12
+
13
+ ## 2) 打开示例页面
14
+
15
+ 直接用浏览器打开:`examples/web/demo.html`
16
+
17
+ 你会看到页面里使用的 class(例如 `p-24` / `text-20` / `bg-red`)已经生效。
18
+
19
+ ## 3) 开发模式(推荐)
20
+
21
+ ```bash
22
+ node bin/class2css.js --config ./examples/web/class2css.config.js
23
+ ```
24
+
25
+ 然后修改 `demo.html` 里的 class,刷新页面即可看到效果更新。
@@ -0,0 +1,70 @@
1
+ // ========== 工具配置(Web 示例)==========
2
+ // 用法:
3
+ // 1) 推荐直接复制 `examples/web` 目录到你的项目根目录
4
+ // 2) 然后执行:`class2css -c ./examples/web/class2css.config.js`
5
+ // 3) 或者把本文件重命名为根目录的 `class2css.config.js`,即可用默认配置启动
6
+ //
7
+ // 注意:
8
+ // - 当前解析器默认只提取 `class="..."`(适用于 HTML / Vue 模板里的 class)
9
+ // - React/JSX 常见的 `className="..."` 当前不会被提取(如需支持可扩展解析器/正则)
10
+
11
+ const path = require('path');
12
+ const stylesConfig = require('./styles.config.js');
13
+
14
+ module.exports = {
15
+ system: {
16
+ cssFormat: 'compressed',
17
+ baseUnit: 'px',
18
+ unitConversion: 1,
19
+ compression: true,
20
+ sortClasses: true,
21
+ unitStrategy: {
22
+ autoDetect: true,
23
+ propertyUnits: {
24
+ 'font-size': 'px',
25
+ 'width|height': 'px',
26
+ opacity: '',
27
+ 'z-index': '',
28
+ 'line-height': '',
29
+ 'border-radius': 'px',
30
+ },
31
+ },
32
+ },
33
+
34
+ output: {
35
+ path: path.join(__dirname, 'dist'),
36
+ fileName: 'styles.css',
37
+ // commonCssPath: './common.css', // 可选:公共基础样式(相对路径基于配置文件所在目录)
38
+ },
39
+
40
+ importantFlags: stylesConfig.importantFlags,
41
+
42
+ multiFile: {
43
+ entry: {
44
+ // 注意:工具内部会直接按“运行目录”解释相对路径,因此示例这里显式转绝对路径,保证开箱即用
45
+ // - 扫描 demo.html(可直接在浏览器看到效果)
46
+ // - 同时扫描 src 目录(给你放更多示例文件用)
47
+ path: [path.join(__dirname, 'demo.html'), path.join(__dirname, 'src')],
48
+ // Web:html/vue(你也可以追加 svelte、astro 等模板文件扩展名)
49
+ fileType: ['html', 'vue'],
50
+ },
51
+ output: {
52
+ cssOutType: 'uniFile',
53
+ path: path.join(__dirname, 'dist'),
54
+ fileName: 'styles.css',
55
+ fileType: 'css',
56
+
57
+ incrementalOnlyAdd: false,
58
+ incrementalBaseline: 'fromOutputFile',
59
+ rebuildOnStart: true,
60
+ unusedReportLimit: 200,
61
+
62
+ uniFileWriteMode: 'rewrite',
63
+ },
64
+ },
65
+
66
+ atomicRules: stylesConfig.atomicRules,
67
+ baseClassName: stylesConfig.baseClassName,
68
+ variants: stylesConfig.variants,
69
+ breakpoints: stylesConfig.breakpoints,
70
+ };
@@ -0,0 +1,105 @@
1
+ <!doctype html>
2
+ <html lang="zh-CN">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
+ <title>css2class Web Demo</title>
7
+ <!-- 由 class2css 生成:examples/web/dist/styles.css -->
8
+ <link rel="stylesheet" href="./dist/styles.css" />
9
+ <style>
10
+ /* 仅用于 demo 的基础页面样式(与工具无关) */
11
+ body {
12
+ font-family: system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, "PingFang SC",
13
+ "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
14
+ background: #f6f7fb;
15
+ margin: 0;
16
+ }
17
+ .container {
18
+ max-width: 980px;
19
+ margin: 0 auto;
20
+ padding: 24px;
21
+ }
22
+ .card {
23
+ background: white;
24
+ border-radius: 16px;
25
+ box-shadow: 0 6px 20px rgba(16, 24, 40, 0.08);
26
+ }
27
+ .row {
28
+ display: flex;
29
+ gap: 12px;
30
+ flex-wrap: wrap;
31
+ }
32
+ .chip {
33
+ border: 1px solid #e6e8f0;
34
+ border-radius: 999px;
35
+ }
36
+ .muted {
37
+ color: #667085;
38
+ }
39
+ </style>
40
+ </head>
41
+ <body>
42
+ <div class="container">
43
+ <div class="card p-24">
44
+ <div class="row items-center justify-between">
45
+ <div>
46
+ <div class="text-20 font-700">css2class - Web 示例页面</div>
47
+ <div class="text-14 muted mt-8">
48
+ 这个页面里的 class 会被工具扫描并生成到 <code>dist/styles.css</code>,打开页面即可看到效果。
49
+ </div>
50
+ </div>
51
+ <div class="row">
52
+ <div class="chip px-12 py-8 text-14">m / p</div>
53
+ <div class="chip px-12 py-8 text-14">w / h</div>
54
+ <div class="chip px-12 py-8 text-14">rounded / border</div>
55
+ </div>
56
+ </div>
57
+
58
+ <div class="mt-24">
59
+ <div class="text-16 font-600 mb-12">间距(spacing)</div>
60
+ <div class="row">
61
+ <div class="p-12 bg-sky color-white rounded-12">p-12</div>
62
+ <div class="p-16 bg-violet color-white rounded-12">p-16</div>
63
+ <div class="p-20 bg-green color-white rounded-12">p-20</div>
64
+ </div>
65
+ </div>
66
+
67
+ <div class="mt-24">
68
+ <div class="text-16 font-600 mb-12">尺寸(sizing)</div>
69
+ <div class="row">
70
+ <div class="w-120 h-60 bg-gray rounded-12"></div>
71
+ <div class="w-160 h-60 bg-gray rounded-12"></div>
72
+ <div class="w-200 h-60 bg-gray rounded-12"></div>
73
+ <div class="w-full h-60 bg-gray rounded-12"></div>
74
+ </div>
75
+
76
+ <!-- 响应式示例:确保 @media 在 base 规则之后输出,避免被覆盖导致“看起来失效” -->
77
+ <div class="mt-8 muted text-12">响应式:w-120 + sm:w-200(≥640px 时应变宽)</div>
78
+ <div class="w-120 sm:w-200 h-60 bg-sky rounded-12 lg:w-333 w-222"></div>
79
+ </div>
80
+
81
+ <div class="mt-24">
82
+ <div class="text-16 font-600 mb-12">文字(typography)</div>
83
+ <div class="text-12 muted">text-12</div>
84
+ <div class="text-14 mt-8">text-14</div>
85
+ <div class="text-18 mt-8 font-700">text-18 font-700</div>
86
+ </div>
87
+
88
+ <div class="mt-24">
89
+ <div class="text-16 font-600 mb-12">颜色族(color / bg / bcolor)</div>
90
+ <div class="row">
91
+ <div class="px-16 py-12 bg-red color-white rounded-12">bg-red + color-white</div>
92
+ <div class="px-16 py-12 bg-black color-white rounded-12">bg-black + color-white</div>
93
+ <div class="px-16 py-12 bg-transparent bcolor-gray border-1 rounded-12">
94
+ bg-transparent + bcolor-gray + border-1
95
+ </div>
96
+ </div>
97
+ </div>
98
+ </div>
99
+
100
+ <div class="mt-16 muted text-12">
101
+ 提示:修改本文件里的 class 后,重新运行/保持运行 class2css,即可看到 CSS 更新。
102
+ </div>
103
+ </div>
104
+ </body>
105
+ </html>
@@ -0,0 +1,5 @@
1
+ <!--
2
+ 这个文件仅用于保证 examples/web 的默认 entry.path(./src)有内容可扫描。
3
+ 实际可视化 demo 请打开 examples/web/demo.html
4
+ -->
5
+ <div class="p-1"></div>