UNPKG

1.06 kBJavaScriptView Raw
1// tooling
2import manageUnresolved from './manage-unresolved';
3import transformNode from './transform-node';
4
5// transform @content at-rules
6export default function transformContentAtrule(rule, opts) {
7 // if @content is supported
8 if (opts.transform.includes('@content')) {
9 // the closest @mixin at-rule
10 const mixin = getClosestMixin(rule);
11
12 // if the @mixin at-rule exists
13 if (mixin) {
14 // clone the @mixin at-rule
15 const clone = mixin.original.clone({
16 parent: rule.parent,
17 variables: rule.variables
18 });
19
20 // transform the clone children
21 transformNode(clone, opts);
22
23 // replace the @content at-rule with the clone children
24 rule.parent.insertBefore(rule, clone.nodes);
25
26 rule.remove();
27 } else {
28 // otherwise, if the @mixin at-rule does not exist
29 manageUnresolved(rule, opts, '@content', 'Could not resolve the mixin for @content');
30 }
31 }
32}
33
34// return the closest @mixin at-rule
35const getClosestMixin = node => 'atrule' === node.type && 'mixin' === node.name
36 ? node
37: node.parent && getClosestMixin(node.parent);