styimat 1.9.0 → 1.11.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.
- package/README.md +35 -15
- package/convert-styimat.js +157 -60
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Styimat
|
|
2
2
|
[](https://gitee.com/wxy6987/styimat/stargazers)
|
|
3
3
|
[](https://gitee.com/wxy6987/styimat/members)
|
|
4
4
|
[](https://www.npmjs.com/package/styimat)
|
|
5
5
|
[](LICENSE)
|
|
6
6
|
[](https://www.npmjs.com/package/styimat)
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
让CSS变量拥有预处理能力:嵌套语法、Lab/LCH色彩空间、Display P3广色域、智能数学计算,一份代码适配所有现代浏览器。
|
|
9
9
|
|
|
10
10
|
## 安装方式
|
|
11
11
|
|
|
@@ -43,9 +43,9 @@ npm install styimat
|
|
|
43
43
|
- **Lab/LCH 颜色支持** - 将 Lab 和 LCH 颜色转换为 CSS 兼容的 RGB
|
|
44
44
|
- **广色域显示** - 自动检测并支持 Display P3 广色域显示器
|
|
45
45
|
- **变量系统** - 类似 Sass 的变量系统,支持作用域变量
|
|
46
|
-
|
|
46
|
+
- **嵌套规则** - 支持 Sass 风格的嵌套选择器
|
|
47
47
|
- **十六进制语法** - 支持 `lab#LLAABB` 和 `lch#LLCCHHH` 简洁语法
|
|
48
|
-
- **增强数学计算** - 支持 `math()
|
|
48
|
+
- **增强数学计算** - 支持 `math()`` 函数和复杂数学表达式
|
|
49
49
|
- **轻量高效** - 无依赖,压缩后仅约 20KB
|
|
50
50
|
- **多种使用方式** - 浏览器、Node.js、命令行均可使用
|
|
51
51
|
- **灵活API** - 支持函数调用、模板标签、对象配置等多种用法
|
|
@@ -124,6 +124,10 @@ npx styimat
|
|
|
124
124
|
# 全局安装后使用
|
|
125
125
|
npm install -g styimat
|
|
126
126
|
styimat input.css output.css
|
|
127
|
+
|
|
128
|
+
# 监听
|
|
129
|
+
npx styimat --watch input.css output.css
|
|
130
|
+
npx styimat -w input.css output.css
|
|
127
131
|
```
|
|
128
132
|
|
|
129
133
|
## 高级用法
|
|
@@ -226,7 +230,6 @@ ele.cssVar[prop] = value; // 设置prop变量为value
|
|
|
226
230
|
ele.cssVar(prop); // 获取prop变量的值
|
|
227
231
|
ele.cssVar.prop; // 获取prop变量的值
|
|
228
232
|
ele.cssVar[prop] // 获取prop变量的值
|
|
229
|
-
|
|
230
233
|
```
|
|
231
234
|
|
|
232
235
|
### 4. 混合使用示例
|
|
@@ -307,7 +310,7 @@ body {
|
|
|
307
310
|
|
|
308
311
|
### 配置头语法
|
|
309
312
|
|
|
310
|
-
您可以在CSS文件的开头使用配置头来设置预处理选项,配置行以
|
|
313
|
+
您可以在CSS文件的开头使用配置头来设置预处理选项,配置行以 `#`` 开头:
|
|
311
314
|
|
|
312
315
|
```css
|
|
313
316
|
#indentSize 4
|
|
@@ -333,6 +336,8 @@ body {
|
|
|
333
336
|
}
|
|
334
337
|
```
|
|
335
338
|
|
|
339
|
+
**注意**:配置头既支持驼峰命名也支持连字符命名,例如 `indent-size` 和 `indentSize` 效果相同。
|
|
340
|
+
|
|
336
341
|
### Math 计算语法
|
|
337
342
|
|
|
338
343
|
```css
|
|
@@ -384,7 +389,7 @@ body {
|
|
|
384
389
|
## 十六进制颜色语法详解
|
|
385
390
|
|
|
386
391
|
### Lab 十六进制格式:`lab#LLAABB`
|
|
387
|
-
Lab 颜色使用 `lab
|
|
392
|
+
Lab 颜色使用 `lab#`` 前缀,后跟 6 个十六进制字符:
|
|
388
393
|
- 前 2 位:L(明度),范围 00-FF,映射到 0-100
|
|
389
394
|
- 中间 2 位:A(红绿轴),范围 00-FF,映射到 -192 到 192
|
|
390
395
|
- 后 2 位:B(黄蓝轴),范围 00-FF,映射到 -192 到 192
|
|
@@ -395,7 +400,7 @@ Lab 颜色使用 `lab#` 前缀,后跟 6 个十六进制字符:
|
|
|
395
400
|
- B = 0x80 = 128 → (128-128) × 1.5 = 0
|
|
396
401
|
|
|
397
402
|
### LCH 十六进制格式:`lch#LLCCHHH`
|
|
398
|
-
LCH 颜色使用 `lch
|
|
403
|
+
LCH 颜色使用 `lch#`` 前缀,后跟 6 个字符(最后1-3位是十进制色相值):
|
|
399
404
|
- 前 2 位:L(明度),范围 00-FF,映射到 0-100
|
|
400
405
|
- 中间 2 位:C(色度),范围 00-FF,映射到 0-150
|
|
401
406
|
- 后 1-3 位:H(色相),范围 0-100,映射到 0-360度
|
|
@@ -471,7 +476,13 @@ styimat.config({
|
|
|
471
476
|
enableMath: true,
|
|
472
477
|
mathPrecision: 6,
|
|
473
478
|
indentSize: 2,
|
|
474
|
-
enableNesting: true
|
|
479
|
+
enableNesting: true,
|
|
480
|
+
importBaseUrl: '', // 导入基础URL
|
|
481
|
+
importCache: true, // 缓存导入文件
|
|
482
|
+
importTimeout: 5000, // 导入超时时间
|
|
483
|
+
autoProcessStyleTags: true,
|
|
484
|
+
styleTagAttribute: 'e',
|
|
485
|
+
preserveOriginal: false
|
|
475
486
|
});
|
|
476
487
|
|
|
477
488
|
// 数学工具
|
|
@@ -485,24 +496,33 @@ const lab = styimat.colorUtils.lchToLab(54.7, 78.9, 38.5);
|
|
|
485
496
|
|
|
486
497
|
// 解析颜色
|
|
487
498
|
const colorInfo = styimat.colorUtils.parseColor('lab(54.7 77.9 80.1 / 0.8)');
|
|
499
|
+
|
|
500
|
+
// 导入工具
|
|
501
|
+
styimat.imports.clearCache(); // 清除导入缓存
|
|
502
|
+
styimat.imports.setBaseUrl('/css/'); // 设置导入基础URL
|
|
503
|
+
styimat.imports.setCacheEnabled(false); // 禁用导入缓存
|
|
504
|
+
styimat.imports.setTimeout(10000); // 设置导入超时时间
|
|
488
505
|
```
|
|
489
506
|
|
|
490
507
|
### 配置选项
|
|
491
508
|
|
|
492
509
|
| 选项 | 类型 | 默认值 | 描述 |
|
|
493
510
|
|------|------|--------|------|
|
|
494
|
-
| `rootSelector` | string |
|
|
495
|
-
| `variablePrefix` | string |
|
|
496
|
-
| `preserveOriginal` | boolean | `false` | 是否保留原始 `<style e
|
|
497
|
-
| `indentSize` | number | `
|
|
511
|
+
| `rootSelector` | string | `:root` | CSS 变量定义的根选择器 |
|
|
512
|
+
| `variablePrefix` | string | `--`` | CSS 变量前缀 |
|
|
513
|
+
| `preserveOriginal` | boolean | `false` | 是否保留原始 `<style e>`` 标签 |
|
|
514
|
+
| `indentSize` | number | `4` | 嵌套规则的缩进大小 |
|
|
498
515
|
| `enableNesting` | boolean | `true` | 是否启用嵌套规则 |
|
|
499
516
|
| `autoProcessStyleTags` | boolean | `true` | 是否自动处理页面中的 style 标签 |
|
|
500
|
-
| `styleTagAttribute` | string | `
|
|
517
|
+
| `styleTagAttribute` | string | `e` | 标识需要处理的 style 标签属性 |
|
|
501
518
|
| `convertLabToRGB` | boolean | `true` | 是否转换 Lab 颜色为 RGB |
|
|
502
519
|
| `convertLchToRGB` | boolean | `true` | 是否转换 LCH 颜色为 RGB |
|
|
503
520
|
| `enableP3` | boolean | `true` | 是否启用 Display P3 广色域支持 |
|
|
504
|
-
| `enableMath` | boolean | `true` | 是否启用 `math()
|
|
521
|
+
| `enableMath` | boolean | `true` | 是否启用 `math()`` 函数增强 |
|
|
505
522
|
| `mathPrecision` | number | `6` | 数学计算的精度 |
|
|
523
|
+
| `importBaseUrl` | string | `` | 导入的基础URL |
|
|
524
|
+
| `importCache` | boolean | `true` | 是否缓存导入的文件 |
|
|
525
|
+
| `importTimeout` | number | `5000` | 导入超时时间(毫秒) |
|
|
506
526
|
|
|
507
527
|
## 示例
|
|
508
528
|
|
package/convert-styimat.js
CHANGED
|
@@ -1,89 +1,186 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
// 1. 只做一件事,做好
|
|
4
|
-
// 2. 输入 stdin,输出 stdout
|
|
5
|
-
// 3. 错误输出到 stderr
|
|
6
|
-
// 4. 安静模式可用
|
|
7
|
-
// 5. 返回正确的退出码
|
|
8
|
-
|
|
9
3
|
const fs = require('fs');
|
|
10
4
|
const path = require('path');
|
|
5
|
+
const chokidar = require('chokidar');
|
|
11
6
|
|
|
12
7
|
async function main() {
|
|
13
8
|
const args = process.argv.slice(2);
|
|
14
9
|
|
|
10
|
+
// 解析参数
|
|
15
11
|
let inputContent = '';
|
|
16
12
|
let outputFile = null;
|
|
13
|
+
let watchMode = false;
|
|
14
|
+
let inputFile = null;
|
|
17
15
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
16
|
+
for (let i = 0; i < args.length; i++) {
|
|
17
|
+
const arg = args[i];
|
|
18
|
+
|
|
19
|
+
if (arg === '--watch' || arg === '-w') {
|
|
20
|
+
watchMode = true;
|
|
21
|
+
} else if (arg === '--help' || arg === '-h') {
|
|
22
|
+
showHelp();
|
|
23
|
+
process.exit(0);
|
|
24
|
+
} else if (arg.startsWith('-')) {
|
|
25
|
+
console.error(`错误: 未知选项 ${arg}`);
|
|
26
|
+
showHelp();
|
|
27
|
+
process.exit(1);
|
|
28
|
+
} else if (!inputFile) {
|
|
29
|
+
inputFile = arg;
|
|
30
|
+
} else if (!outputFile) {
|
|
31
|
+
outputFile = arg;
|
|
32
|
+
} else {
|
|
33
|
+
console.error('错误: 参数过多');
|
|
34
|
+
showHelp();
|
|
35
|
+
process.exit(1);
|
|
36
|
+
}
|
|
38
37
|
}
|
|
39
38
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
39
|
+
function showHelp() {
|
|
40
|
+
console.log(`
|
|
41
|
+
用法: styimat [选项] [输入文件] [输出文件]
|
|
42
|
+
|
|
43
|
+
选项:
|
|
44
|
+
-w, --watch 监控输入文件变化并自动转换
|
|
45
|
+
-h, --help 显示帮助信息
|
|
46
|
+
|
|
47
|
+
示例:
|
|
48
|
+
styimat # 从 stdin 读取,输出到 stdout
|
|
49
|
+
styimat input.css # 从文件读取,输出到 stdout
|
|
50
|
+
styimat input.css output.css # 从文件读取,输出到文件
|
|
51
|
+
styimat -w input.css output.css # 监控并自动转换
|
|
52
|
+
`);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// 如果没有输入文件,从 stdin 读取
|
|
56
|
+
if (!inputFile) {
|
|
57
|
+
if (watchMode) {
|
|
58
|
+
console.error('错误: 监控模式需要指定输入文件');
|
|
59
|
+
process.exit(1);
|
|
59
60
|
}
|
|
60
61
|
|
|
61
|
-
|
|
62
|
-
|
|
62
|
+
inputContent = fs.readFileSync(0, 'utf8');
|
|
63
|
+
await convertAndOutput(inputContent, null);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// 检查输入文件是否存在
|
|
68
|
+
if (!fs.existsSync(inputFile)) {
|
|
69
|
+
console.error(`错误: 输入文件 "${inputFile}" 不存在`);
|
|
70
|
+
process.exit(1);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// 如果是监控模式
|
|
74
|
+
if (watchMode) {
|
|
75
|
+
if (!outputFile) {
|
|
76
|
+
console.error('错误: 监控模式需要指定输出文件');
|
|
77
|
+
process.exit(1);
|
|
63
78
|
}
|
|
64
79
|
|
|
65
|
-
|
|
66
|
-
|
|
80
|
+
console.log(`监控模式已启动: ${inputFile} -> ${outputFile}`);
|
|
81
|
+
console.log('按 Ctrl+C 退出');
|
|
67
82
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
83
|
+
// 初始转换
|
|
84
|
+
await convertFile(inputFile, outputFile);
|
|
85
|
+
|
|
86
|
+
// 监控文件变化
|
|
87
|
+
const watcher = chokidar.watch(inputFile, {
|
|
88
|
+
persistent: true,
|
|
89
|
+
ignoreInitial: true,
|
|
90
|
+
awaitWriteFinish: {
|
|
91
|
+
stabilityThreshold: 200,
|
|
92
|
+
pollInterval: 100
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
watcher
|
|
97
|
+
.on('change', async (filePath) => {
|
|
98
|
+
console.log(`${new Date().toLocaleTimeString()} - 检测到变化: ${filePath}`);
|
|
99
|
+
await convertFile(filePath, outputFile);
|
|
100
|
+
})
|
|
101
|
+
.on('error', (error) => {
|
|
102
|
+
console.error(`监控错误: ${error.message}`);
|
|
103
|
+
});
|
|
71
104
|
|
|
72
|
-
//
|
|
73
|
-
|
|
105
|
+
// 处理进程终止
|
|
106
|
+
process.on('SIGINT', () => {
|
|
107
|
+
console.log('
|
|
108
|
+
停止监控...');
|
|
109
|
+
watcher.close();
|
|
110
|
+
process.exit(0);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
} else {
|
|
114
|
+
// 非监控模式
|
|
115
|
+
inputContent = fs.readFileSync(inputFile, 'utf8');
|
|
116
|
+
await convertAndOutput(inputContent, outputFile);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async function convertFile(inputFilePath, outputFilePath) {
|
|
121
|
+
try {
|
|
122
|
+
const inputContent = fs.readFileSync(inputFilePath, 'utf8');
|
|
123
|
+
const outputContent = await convertStyimat(inputContent);
|
|
124
|
+
fs.writeFileSync(outputFilePath, outputContent, 'utf8');
|
|
125
|
+
console.log(`${new Date().toLocaleTimeString()} - 转换完成: ${inputFilePath} -> ${outputFilePath}`);
|
|
126
|
+
} catch (error) {
|
|
127
|
+
console.error(`${new Date().toLocaleTimeString()} - 转换错误: ${error.message}`);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async function convertAndOutput(inputContent, outputFile) {
|
|
132
|
+
try {
|
|
133
|
+
const outputContent = await convertStyimat(inputContent);
|
|
74
134
|
|
|
75
|
-
// 输出结果
|
|
76
135
|
if (outputFile) {
|
|
77
136
|
fs.writeFileSync(outputFile, outputContent, 'utf8');
|
|
78
137
|
} else {
|
|
79
|
-
process.stdout.write(outputContent + '
|
|
138
|
+
process.stdout.write(outputContent + '
|
|
139
|
+
');
|
|
80
140
|
}
|
|
81
|
-
|
|
82
141
|
} catch (error) {
|
|
83
|
-
console.error('
|
|
142
|
+
console.error('转换错误:', error.message);
|
|
84
143
|
process.exit(1);
|
|
85
144
|
}
|
|
86
145
|
}
|
|
87
146
|
|
|
88
|
-
|
|
89
|
-
|
|
147
|
+
async function convertStyimat(inputContent) {
|
|
148
|
+
// 尝试从多个位置加载 styimat.js
|
|
149
|
+
const possiblePaths = [
|
|
150
|
+
path.join(__dirname, 'styimat.js'),
|
|
151
|
+
path.join(process.cwd(), 'styimat.js'),
|
|
152
|
+
path.join(process.cwd(), 'node_modules', 'styimat', 'styimat.js')
|
|
153
|
+
];
|
|
154
|
+
|
|
155
|
+
let styimatModule = null;
|
|
156
|
+
for (const modulePath of possiblePaths) {
|
|
157
|
+
try {
|
|
158
|
+
if (fs.existsSync(modulePath)) {
|
|
159
|
+
styimatModule = require(modulePath);
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
} catch (e) {
|
|
163
|
+
// 继续尝试下一个路径
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (!styimatModule) {
|
|
168
|
+
throw new Error('找不到 styimat.js 模块');
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// 获取转换函数
|
|
172
|
+
const styimat = styimatModule.styimat || styimatModule;
|
|
173
|
+
|
|
174
|
+
if (typeof styimat.convert !== 'function') {
|
|
175
|
+
throw new Error('styimat.convert 不是函数');
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// 执行转换
|
|
179
|
+
return await styimat.convert(inputContent);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// 处理 promise 错误
|
|
183
|
+
main().catch(error => {
|
|
184
|
+
console.error('程序错误:', error.message);
|
|
185
|
+
process.exit(1);
|
|
186
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "styimat",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.11.1",
|
|
4
4
|
"description": "一个高效的CSS变量预处理库,支持Lab/LCH颜色空间自动转换、嵌套选择器和Display P3广色域,让现代CSS开发更简洁强大。",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"css",
|
|
@@ -53,6 +53,7 @@
|
|
|
53
53
|
"git:auto-tag": "git tag -d v$(node -p \"require('./package.json').version\") 2>/dev/null || true && git tag -a v$(node -p \"require('./package.json').version\") -m 'Version $(node -p \"require('./package.json').version\")' && git push && git push --tags"
|
|
54
54
|
},
|
|
55
55
|
"dependencies": {
|
|
56
|
+
"chokidar": "^5.0.0",
|
|
56
57
|
"terser": "^5.44.1"
|
|
57
58
|
}
|
|
58
59
|
}
|