-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
index.js
206 lines (178 loc) · 5.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
const IGNORE = Symbol('ignore')
function definition (variables, node, opts) {
let name = node.prop.slice(1)
variables[name] = transformBackslashSequences(node.value)
if (!opts.keep) {
node.remove()
}
}
function variable (variables, node, str, name, opts, result) {
if (isIgnore(node, name)) return str
if (opts.only) {
if (typeof opts.only[name] !== 'undefined') {
return opts.only[name]
}
return str
}
if (typeof variables[name] !== 'undefined') {
return variables[name]
}
if (opts.silent) {
return str
}
let fix = opts.unknown(node, name, result)
if (fix) {
return fix
}
return str
}
const UNICODE_8_CODE = /(?<=[^\\]|^)\\U([0-9abcdefABCDEF]{8})/g
const UNICODE_4_CODE = /(?<=[^\\]|^)\\u([0-9abcdefABCDEF]{4})/g
function transformBackslashSequences(value) {
if (typeof value !== 'string') return value
if (!value.includes('\\')) return value
return value
.replace(UNICODE_8_CODE, (_, cp) => String.fromCodePoint(`0x${cp}`))
.replace(UNICODE_4_CODE, (_, cp) => String.fromCodePoint(`0x${cp}`))
.replace(/\\\\/g, '\\')
}
function simpleSyntax (variables, node, str, opts, result) {
return str.replace(/(^|[^\w])\$([\w\d-_]+)/g, (_, bef, name) => {
return bef + variable(variables, node, '$' + name, name, opts, result)
})
}
function inStringSyntax (variables, node, str, opts, result) {
return str.replace(/\$\(\s*([\w\d-_]+)\s*\)/g, (all, name) => {
return variable(variables, node, all, name, opts, result)
})
}
function bothSyntaxes (variables, node, str, opts, result) {
str = simpleSyntax(variables, node, str, opts, result)
str = inStringSyntax(variables, node, str, opts, result)
return str
}
function repeat (value, callback) {
let oldValue
let newValue = value
do {
oldValue = newValue
newValue = callback(oldValue)
} while (newValue !== oldValue && newValue.includes('$'))
return newValue
}
function declValue (variables, node, opts, result) {
node.value = repeat(node.value, value => {
return bothSyntaxes(variables, node, value, opts, result)
})
}
function declProp (variables, node, opts, result) {
node.prop = repeat(node.prop, value => {
return inStringSyntax(variables, node, value, opts, result)
})
}
function ruleSelector (variables, node, opts, result) {
node.selector = repeat(node.selector, value => {
return bothSyntaxes(variables, node, value, opts, result)
})
}
function atruleParams (variables, node, opts, result) {
node.params = repeat(node.params, value => {
return bothSyntaxes(variables, node, value, opts, result)
})
}
function comment (variables, node, opts, result) {
node.text = node.text.replace(/<<\$\(\s*(\w+)\s*\)>>/g, (all, name) => {
return variable(variables, node, all, name, opts, result)
})
}
function mixin (helpers, node) {
let name = node.params.split(/\s/, 1)[0]
let vars = node.params.slice(name.length).trim()
if (vars.length) {
node[IGNORE] = helpers.list.comma(vars).map(str => {
let arg = str.split(':', 1)[0]
return arg.slice(1).trim()
})
}
}
function isIgnore (node, value) {
if (node[IGNORE] && node[IGNORE].includes(value)) {
return true
} else if (node.parent) {
return isIgnore(node.parent, value)
} else {
return false
}
}
module.exports = (opts = {}) => {
if (!opts.unknown) {
opts.unknown = (node, name) => {
throw node.error('Undefined variable $' + name)
}
}
if (typeof opts.keep === 'undefined') {
opts.keep = false
}
return {
postcssPlugin: 'postcss-simple-vars',
prepare () {
let variables = {}
if (typeof opts.variables === 'function') {
variables = opts.variables()
} else if (typeof opts.variables === 'object') {
variables = { ...opts.variables }
}
for (let name in variables) {
if (name[0] === '$') {
name = name.slice(1)
variables[name] = variables[`$${name}`]
delete variables[`$${name}`]
}
variables[name] = transformBackslashSequences(variables[name])
}
return {
AtRule (node, helpers) {
if (node.name === 'define-mixin') {
mixin(helpers, node)
} else if (node.params && node.params.includes('$')) {
atruleParams(variables, node, opts, helpers.result)
}
},
Comment (node, { result }) {
if (node.text.includes('$')) {
comment(variables, node, opts, result)
}
},
Declaration (node, { result }) {
if (node.value.includes('$')) {
declValue(variables, node, opts, result)
}
if (node.prop[0] === '$' && node.prop[1] !== '(') {
if (!opts.only) definition(variables, node, opts)
} else if (node.prop.includes('$(')) {
declProp(variables, node, opts, result)
}
},
OnceExit (_, { result }) {
Object.keys(variables).forEach(key => {
result.messages.push({
name: key,
plugin: 'postcss-simple-vars',
type: 'variable',
value: variables[key]
})
})
if (opts.onVariables) {
opts.onVariables(variables)
}
},
Rule (node, { result }) {
if (node.selector.includes('$')) {
ruleSelector(variables, node, opts, result)
}
}
}
}
}
}
module.exports.postcss = true