-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathtransform-mixin-atrule.js
43 lines (36 loc) · 1.29 KB
/
transform-mixin-atrule.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
// tooling
import { list } from 'postcss';
import getReplacedString from './get-replaced-string';
import setVariable from './set-variable';
// transform @mixin at-rules
export default function transformMixinAtrule(rule, opts) {
// if @mixin is supported
if (opts.transform.includes('@mixin')) {
// @mixin options
const { name, params } = getMixinOpts(rule, opts);
// set the mixin as a variable on the parent of the @mixin at-rule
setVariable(rule.parent, `@mixin ${name}`, { params, rule }, opts);
// remove the @mixin at-rule
rule.remove();
}
}
// return the @mixin statement options (@mixin NAME, @mixin NAME(PARAMS))
const getMixinOpts = (node, opts) => {
// @mixin name and default params ([{ name, value }, ...])
const [ name, sourceParams ] = node.params.split(matchOpeningParen, 2);
const params = sourceParams && sourceParams.slice(0, -1).trim()
? list.comma(sourceParams.slice(0, -1).trim()).map(
param => {
const parts = list.split(param, ':');
const paramName = parts[0].slice(1);
const paramValue = parts.length > 1
? getReplacedString(parts.slice(1).join(':'), node, opts)
: undefined;
return { name: paramName, value: paramValue };
}
)
: [];
return { name, params };
};
// match an opening parenthesis
const matchOpeningParen = /(?<!var)\(/;