UNPKG

878 BJavaScriptView Raw
1// tooling
2import { list } from 'postcss';
3import setVariable from './set-variable';
4
5// transform @mixin at-rules
6export default function transformMixinAtrule(atrule, result, opts) {
7 // @mixin name and default params ([{ name, value }, ...])
8 const [ name, sourceParams ] = atrule.params.split(matchOpeningParen, 2);
9 const params = sourceParams
10 ? list.comma(sourceParams.slice(0, -1)).map(
11 param => {
12 const parts = list.split(param, ':');
13 const paramName = parts[0].slice(1);
14 const paramValue = parts.length > 1 ? parts.slice(1).join(':') : undefined;
15
16 return { name: paramName, value: paramValue };
17 }
18 )
19 : [];
20
21 // set the mixin as a variable on the parent of the atrule
22 setVariable(atrule.parent, name, { params, atrule }, opts);
23
24 // remove the @mixin at-rule
25 atrule.remove();
26}
27
28// match an opening parenthesis
29const matchOpeningParen = '(';