1 |
|
2 | import { list } from 'postcss';
|
3 | import getClosestVariable from './get-closest-variable';
|
4 | import getReplacedString from './get-replaced-string';
|
5 | import setVariable from './set-variable';
|
6 | import transformNode from './transform-node';
|
7 |
|
8 |
|
9 | export default function transformIncludeAtrule(atrule, result, opts) {
|
10 |
|
11 | const [ name, sourceArgs ] = atrule.params.split(matchOpeningParen, 2);
|
12 | const args = sourceArgs
|
13 | ? list.comma(sourceArgs.slice(0, -1))
|
14 | : [];
|
15 |
|
16 |
|
17 | const mixin = getClosestVariable(name, atrule.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], atrule, result, opts)
|
26 | : param.value;
|
27 |
|
28 | setVariable(atrule, param.name, arg, opts);
|
29 | }
|
30 | );
|
31 |
|
32 |
|
33 | const clone = mixin.atrule.clone({
|
34 | original: atrule,
|
35 | parent: atrule.parent,
|
36 | variables: atrule.variables
|
37 | });
|
38 |
|
39 |
|
40 | transformNode(clone, result, opts);
|
41 |
|
42 |
|
43 | atrule.parent.insertBefore(atrule, clone.nodes);
|
44 | } else {
|
45 |
|
46 | const unresolvedOption = getUnresolvedOption(opts);
|
47 | const message = `Could not resolve the mixin "${name}"`;
|
48 |
|
49 |
|
50 | if ('throw' === unresolvedOption) {
|
51 | throw atrule.error(message, { word: name });
|
52 | } else if ('warn' === unresolvedOption) {
|
53 | atrule.warn(result, message, { word: name });
|
54 | }
|
55 | }
|
56 |
|
57 |
|
58 | atrule.remove();
|
59 | }
|
60 |
|
61 |
|
62 | const matchOpeningParen = '(';
|
63 |
|
64 |
|
65 | const getUnresolvedOption = opts => matchUnresolvedOptions.test(Object(opts).unresolved) ? String(opts.unresolved).toLowerCase() : 'throw'
|
66 |
|
67 |
|
68 | const matchUnresolvedOptions = /^(ignore|throw|warn)$/i;
|