-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
43 lines (39 loc) · 1.3 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
const REGEXP_OVER = /z-over\((?<param>.*)\)/
const REGEXP_UNDER = /z-under\((?<param>.*)\)/
const REGEXP_STACK = /z-stack\((?<value>.*)\)/
module.exports = () => {
return {
postcssPlugin: 'postcss-easy-z',
prepare() {
let stackedVariable
return {
Declaration(decl) {
switch (true) {
case decl.value.startsWith('z-over('): {
decl.value = decl.value.replace(REGEXP_OVER, 'calc($<param> + 1)')
return
}
case decl.value.startsWith('z-under('): {
decl.value = decl.value.replace(REGEXP_UNDER, 'calc($<param> - 1)')
return
}
case decl.variable && decl.value.startsWith('z-stack('): {
const explicitValue = decl.value.match(REGEXP_STACK).groups.value
if (explicitValue && stackedVariable) {
throw decl.error('z-stack with starting value should be first')
}
const startingZ = explicitValue || '1'
decl.value = stackedVariable ? `calc(var(${stackedVariable}) + 1)` : startingZ
stackedVariable = decl.prop
return
}
}
},
RuleExit() {
stackedVariable = undefined
}
}
},
}
}
module.exports.postcss = true