1 |
|
2 | import { list } from 'postcss';
|
3 | import setVariable from './set-variable';
|
4 |
|
5 |
|
6 | export default function transformMixinAtrule(rule, opts) {
|
7 |
|
8 | if (opts.transform.includes('@mixin')) {
|
9 |
|
10 | const { name, params } = getMixinOpts(rule);
|
11 |
|
12 |
|
13 | setVariable(rule.parent, `@mixin ${name}`, { params, rule }, opts);
|
14 |
|
15 |
|
16 | rule.remove();
|
17 | }
|
18 | }
|
19 |
|
20 |
|
21 | const getMixinOpts = node => {
|
22 |
|
23 | const [ name, sourceParams ] = node.params.split(matchOpeningParen, 2);
|
24 | const params = sourceParams && sourceParams.slice(0, -1).trim()
|
25 | ? list.comma(sourceParams.slice(0, -1).trim()).map(
|
26 | param => {
|
27 | const parts = list.split(param, ':');
|
28 | const paramName = parts[0].slice(1);
|
29 | const paramValue = parts.length > 1 ? parts.slice(1).join(':') : undefined;
|
30 |
|
31 | return { name: paramName, value: paramValue };
|
32 | }
|
33 | )
|
34 | : [];
|
35 |
|
36 | return { name, params };
|
37 | };
|
38 |
|
39 |
|
40 | const matchOpeningParen = '(';
|