UNPKG

2.24 kBJavaScriptView Raw
1// tooling
2import transformDecl from './transform-decl';
3import transformAtrule from './transform-atrule';
4import transformEachAtrule from './transform-each-atrule';
5import transformIfAtrule from './transform-if-atrule';
6import transformImportAtrule from './transform-import-atrule';
7import transformIncludeAtrule from './transform-include-atrule';
8import transformForAtrule from './transform-for-atrule';
9import transformMixinAtrule from './transform-mixin-atrule';
10import transformRule from './transform-rule';
11import transformContentAtrule from './transform-content-atrule';
12import waterfall from './waterfall';
13
14export default function transformNode(node, opts) {
15 return waterfall(
16 getNodesArray(node),
17 child => transformRuleOrDecl(child, opts).then(() => {
18 // conditionally walk the children of the attached child
19 if (child.parent) {
20 return transformNode(child, opts);
21 }
22 })
23 );
24}
25
26function transformRuleOrDecl(child, opts) {
27 return Promise.resolve().then(() => {
28 const type = child.type;
29
30 if ('atrule' === type) {
31 const name = child.name.toLowerCase();
32
33 if ('content' === name) {
34 // transform @content at-rules
35 return transformContentAtrule(child, opts);
36 } else if ('each' === name) {
37 // transform @each at-rules
38 return transformEachAtrule(child, opts);
39 } else if ('if' === name) {
40 // transform @if at-rules
41 return transformIfAtrule(child, opts);
42 } else if ('import' === name) {
43 return transformImportAtrule(child, opts);
44 } else if ('include' === name) {
45 // transform @include at-rules
46 return transformIncludeAtrule(child, opts);
47 } else if ('for' === name) {
48 // transform @for at-rules
49 return transformForAtrule(child, opts);
50 } else if ('mixin' === name) {
51 // transform mixin at-rules
52 return transformMixinAtrule(child, opts);
53 } else {
54 // transform all other at-rules
55 return transformAtrule(child, opts);
56 }
57 } else if ('decl' === type) {
58 // transform declarations
59 return transformDecl(child, opts);
60 } else if ('rule' === type) {
61 // transform rule
62 return transformRule(child, opts);
63 }
64 })
65}
66
67// return the children of a node as an array
68const getNodesArray = node => Array.from(Object(node).nodes || []);