forked from postcss/postcss-custom-properties
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·308 lines (270 loc) · 8.31 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import postcss from "postcss"
import balanced from "balanced-match"
const VAR_PROP_IDENTIFIER = "--"
const VAR_FUNC_IDENTIFIER = "var"
const VAR_FUNC_REGEX = /(^|[^\w-])var\(/
// matches `name[, fallback]`, captures "name" and "fallback"
const RE_VAR = /[\f\n\r\t ]*([\w-]+)(?:[\f\n\r\t ]*,[\f\n\r\t ]*([\W\w]+))?/
/**
* Module variables
*/
let globalOpts
/**
* Resolve CSS variables.
*
* The second argument to the CSS variable function, var(name[, fallback]),
* is used in the event that first argument cannot be resolved.
*
* @param {String} value May contain the CSS variable function
* @param {Object} variables A map of variable names and values
* @param {Object} result The PostCSS result object
* @param {Object} decl The declaration containing the rule
* @return {String} A property value with all CSS variables substituted.
*/
function resolveValue(value, variables, result, decl) {
const results = []
const hasVarFunction = VAR_FUNC_REGEX.test(value)
if (!hasVarFunction) {
return [value]
}
const match = value.match(VAR_FUNC_REGEX)
const start = match.index + match[1].length
const matches = balanced("(", ")", value.substring(start))
if (!matches) {
throw decl.error(`missing closing ')' in the value '${value}'`)
}
if (matches.body === "") {
throw decl.error("var() must contain a non-whitespace string")
}
matches.body.replace(RE_VAR, function(_, name, fallback) {
const variable = variables[name]
let post
// undefined and without fallback, just keep original value
if (!variable && !fallback) {
if (globalOpts.warnings) {
const errorStr =
`variable '${name}' is undefined and used without a fallback`
if (globalOpts.noValueNotifications === "error") {
throw decl.error(errorStr, {word: name})
}
else {
result.warn(errorStr, {node: decl})
}
}
post = matches.post
? resolveValue(matches.post, variables, result, decl)
: [""]
// resolve the end of the expression
post.forEach(function(afterValue) {
results.push(
value.slice(0, start) +
VAR_FUNC_IDENTIFIER + "(" + name + ")" + afterValue
)
})
return
}
// prepend with fallbacks
if (fallback) {
// resolve fallback values
fallback = resolveValue(fallback, variables, result, decl)
// resolve the end of the expression before the rest
post = matches.post
? resolveValue(matches.post, variables, result, decl)
: [""]
fallback.forEach((fbValue) => {
post.forEach((afterValue) => {
results.push(value.slice(0, start) + fbValue + afterValue)
})
})
}
if (!variable) {
return
}
// replace with computed custom properties
if (!variable.resolved) {
// circular reference encountered
if (variable.deps.indexOf(name) !== -1) {
if (!fallback) {
if (globalOpts.warnings) {
result.warn("Circular variable reference: " + name, {node: decl})
}
variable.value = [variable.value]
variable.circular = true
}
else {
variable.value = fallback
return
}
}
else {
variable.deps.push(name)
variable.value = resolveValue(variable.value, variables, result, decl)
}
variable.resolved = true
}
if (variable.circular && fallback) {
return
}
// resolve the end of the expression
post = matches.post
? resolveValue(matches.post, variables, result, decl)
: [""]
variable.value.forEach((replacementValue) => {
post.forEach((afterValue) => {
results.push(value.slice(0, start) + replacementValue + afterValue)
})
})
})
return results
}
function prefixVariables(variables) {
const prefixedVariables = {}
if (!variables) {
return prefixedVariables
}
Object.keys(variables).forEach((name) => {
const val = variables[name]
if (name.slice(0, 2) !== "--") {
name = "--" + name
}
prefixedVariables[name] = String(val)
})
return prefixedVariables
}
/**
* Module export.
*/
export default postcss.plugin("postcss-custom-properties", (options = {}) => {
function setVariables(variables) {
options.variables = prefixVariables(variables)
}
function plugin(style, result) {
const variables = prefixVariables(options.variables)
const strict = "strict" in options ? Boolean(options.strict) : true
const appendVariables = "appendVariables" in options
? Boolean(options.appendVariables) : false
const preserve = "preserve" in options ? options.preserve : true
const map = {}
const importantMap = {}
globalOpts = {
warnings: "warnings" in options ? Boolean(options.warnings) : false,
noValueNotifications: "noValueNotifications" in options
? String(options.noValueNotifications) : "warning",
}
// define variables
style.walkRules((rule) => {
const toRemove = []
// only variables declared for `:root` are supported for now
if (
rule.selectors.length !== 1 ||
rule.selectors[0] !== ":root" ||
rule.parent.type !== "root"
) {
rule.each((decl) => {
const prop = decl.prop
if (
globalOpts.warnings &&
prop &&
prop.indexOf(VAR_PROP_IDENTIFIER) === 0
) {
result.warn(
"Custom property ignored: not scoped to the top-level :root " +
`element (${rule.selectors} { ... ${prop}: ... })` +
(rule.parent.type !== "root" ? ", in " + rule.parent.type : ""),
{node: decl}
)
}
})
return
}
rule.each((decl, index) => {
const prop = decl.prop
if (prop && prop.indexOf(VAR_PROP_IDENTIFIER) === 0) {
if (!map[prop] || !importantMap[prop] || decl.important) {
map[prop] = {
value: decl.value,
deps: [],
circular: false,
resolved: false,
}
importantMap[prop] = decl.important
}
toRemove.push(index)
}
})
// optionally remove `--*` properties from the rule
if (!preserve) {
for (let i = toRemove.length - 1; i >= 0; i--) {
rule.nodes.splice(toRemove[i], 1)
}
// remove empty :root {}
if (rule.nodes.length === 0) {
rule.remove()
}
}
})
// apply js-defined custom properties
Object.keys(variables).forEach((variable) => {
map[variable] = {
value: variables[variable],
deps: [],
circular: false,
resolved: false,
}
})
if (preserve) {
Object.keys(map).forEach((name) => {
const variable = map[name]
if (!variable.resolved) {
variable.value = resolveValue(variable.value, map, result)
variable.resolved = true
}
})
}
// resolve variables
style.walkDecls((decl) => {
const value = decl.raws.value ? decl.raws.value.raw : decl.value
// skip values that don’t contain variable functions
if (!value || value.indexOf(VAR_FUNC_IDENTIFIER + "(") === -1) {
return
}
let resolved = resolveValue(value, map, result, decl)
if (!strict) {
resolved = [resolved.pop()]
}
resolved.forEach((resolvedValue) => {
const clone = decl.cloneBefore()
clone.value = resolvedValue
})
if (!preserve || preserve === "computed") {
decl.remove()
}
})
if (preserve && variables && appendVariables) {
const names = Object.keys(map)
if (names.length) {
const container = postcss.rule({
selector: ":root",
raws: {semicolon: true},
})
names
.filter((name) => variables.hasOwnProperty(name))
.forEach((name) => {
const variable = map[name]
let val = variable.value
if (variable.resolved) {
val = val[val.length - 1]
}
const decl = postcss.decl({
prop: name,
value: val,
})
container.append(decl)
})
style.append(container)
}
}
}
plugin.setVariables = setVariables
return plugin
})