1 |
|
2 | import { list } from 'postcss';
|
3 | import getClosestVariable from './get-closest-variable';
|
4 | import getReplacedString from './get-replaced-string';
|
5 | import manageUnresolved from './manage-unresolved';
|
6 | import setVariable from './set-variable';
|
7 | import transformNode from './transform-node';
|
8 |
|
9 |
|
10 | export default function transformIncludeAtrule(rule, opts) {
|
11 |
|
12 | if (opts.transform.includes('@include')) {
|
13 |
|
14 | const { name, args } = getIncludeOpts(rule);
|
15 |
|
16 |
|
17 | const mixin = getClosestVariable(`@mixin ${name}`, rule.parent, opts);
|
18 |
|
19 |
|
20 | if (mixin) {
|
21 |
|
22 | mixin.params.forEach(
|
23 | (param, index) => {
|
24 | const arg = index in args
|
25 | ? getReplacedString(args[index], rule, opts)
|
26 | : param.value;
|
27 |
|
28 | setVariable(rule, param.name, arg, opts);
|
29 | }
|
30 | );
|
31 |
|
32 |
|
33 | const clone = mixin.rule.clone({
|
34 | original: rule,
|
35 | parent: rule.parent,
|
36 | variables: rule.variables
|
37 | });
|
38 |
|
39 |
|
40 | transformNode(clone, opts);
|
41 |
|
42 |
|
43 | rule.parent.insertBefore(rule, clone.nodes);
|
44 |
|
45 | rule.remove();
|
46 | } else {
|
47 |
|
48 | manageUnresolved(rule, opts, name, `Could not resolve the mixin for "${name}"`);
|
49 | }
|
50 | }
|
51 | }
|
52 |
|
53 |
|
54 | const getIncludeOpts = node => {
|
55 |
|
56 | const [ name, sourceArgs ] = node.params.split(matchOpeningParen, 2);
|
57 | const args = sourceArgs
|
58 | ? list.comma(sourceArgs.slice(0, -1))
|
59 | : [];
|
60 |
|
61 | return { name, args };
|
62 | };
|
63 |
|
64 |
|
65 | const matchOpeningParen = '(';
|