Skip to content

Commit cb8cf47

Browse files
committed
feat: add meta generation, update changelog and version
1 parent 72797cc commit cb8cf47

7 files changed

+885
-3
lines changed

.npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
*.config.js
33
src
44
test
5+
build

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.1.3
2+
3+
- Fix colors for translucent buttons.
4+
- Add `variables.json` to export meta.
5+
16
## 0.1.2
27

38
- Allow CLI usage with `--dls`.

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ less
2929

3030
### Import from stylesheets
3131

32+
With [less-loader](https://github.com/webpack-contrib/less-loader):
33+
3234
```less
3335
@import "~less-plugin-dls/tokens/index.less";
3436

build/vars.js

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import fs from 'fs'
2+
import path from 'path'
3+
import less from 'less'
4+
import { parse } from 'postcss-values-parser'
5+
import dls from '../dist'
6+
7+
class VariablesOutputVisitor {
8+
constructor () {
9+
this._visitor = new less.visitors.Visitor(this)
10+
}
11+
12+
run (root) {
13+
this.variables = Object.keys(root.variables())
14+
return this._visitor.visit(root)
15+
}
16+
}
17+
18+
const SELECTOR = 'DLS_VARS'
19+
const visitor = new VariablesOutputVisitor()
20+
21+
less
22+
.render(fs.readFileSync('./tokens/index.less', 'utf-8'), {
23+
plugins: [
24+
dls(),
25+
{
26+
install (less, pluginManager) {
27+
pluginManager.addVisitor(visitor)
28+
}
29+
}
30+
],
31+
paths: ['tokens']
32+
})
33+
.then(() => {
34+
let src = [
35+
`${SELECTOR}{`,
36+
visitor.variables.map(v => `${v.slice(1)}: ${v}`).join(';'),
37+
'}'
38+
].join('')
39+
return less.render(src, {
40+
plugins: [dls()]
41+
})
42+
})
43+
.then(({ css }) => {
44+
fs.writeFileSync(
45+
path.resolve(__dirname, '..', 'variables.json'),
46+
JSON.stringify(
47+
css
48+
.replace(new RegExp(`^[\\s\\S]*${SELECTOR}[\\s\\n]*{[\\s\\n]*`), '')
49+
.replace(/}[\n\s]*$/, '')
50+
.split(/;[\n\s]*/)
51+
.filter(v => v)
52+
.map(decl => {
53+
let [key, value] = decl.split(/:\s*/)
54+
return {
55+
[key]: {
56+
value,
57+
type: getType(value)
58+
}
59+
}
60+
})
61+
.reduce(
62+
(acc, cur) => ({
63+
...acc,
64+
...cur
65+
}),
66+
{}
67+
),
68+
null,
69+
' '
70+
)
71+
)
72+
})
73+
74+
// https://code.visualstudio.com/api/references/vscode-api#CompletionItemKind
75+
function getType (value) {
76+
if (['inherit', 'initial', 'unset', 'revert'].includes(value)) {
77+
return 'keyword'
78+
}
79+
80+
const root = parse(value)
81+
82+
if (root.nodes.length > 1) {
83+
return 'complex'
84+
}
85+
86+
const node = root.nodes[0]
87+
88+
if (!node) {
89+
return 'unknown'
90+
}
91+
92+
if (node.type === 'numeric') {
93+
if (node.unit) {
94+
return 'length'
95+
}
96+
return 'numeric'
97+
}
98+
99+
if (node.type === 'quoted') {
100+
return 'string'
101+
}
102+
103+
if (value === 'transparent' || value === 'currentColor' || node.isColor) {
104+
return 'color'
105+
}
106+
107+
if (node.type === 'function') {
108+
return 'function'
109+
}
110+
111+
return 'unknown'
112+
}

package-lock.json

+59-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "less-plugin-dls",
3-
"version": "0.1.2",
3+
"version": "0.1.3",
44
"description": "Less plugin for Baidu DLS.",
55
"main": "dist/index.js",
66
"scripts": {
@@ -30,6 +30,7 @@
3030
"esm": "^3.2.25",
3131
"kolor": "^1.1.9",
3232
"less": "^3.9.0",
33+
"postcss-values-parser": "^3.0.4",
3334
"prettier": "^1.17.1",
3435
"rollup": "^1.12.4",
3536
"rollup-plugin-commonjs": "^10.0.0",
@@ -38,5 +39,6 @@
3839
"stylelint": "^10.0.1",
3940
"stylelint-config-prettier": "^5.2.0",
4041
"stylelint-config-standard": "^18.3.0"
41-
}
42+
},
43+
"dependencies": {}
4244
}

0 commit comments

Comments
 (0)