-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
252 lines (216 loc) · 8.21 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
var postcss = require('postcss');
var extend = require('util')._extend;
var config = {
suit: {
separators: {
namespace: '-',
descendent: '-',
modifier: '--',
state: '.is-'
}
},
bem: {
separators: {
namespace: '--',
descendent: '__',
modifier: '_'
}
},
shortcuts: {}
};
// combines a list of base names (eg. a Component name) with a list of child
// names (eg. a Modifier name)
function combineNames(parentNames, childNames, separator) {
var classNames = [];
childNames.forEach(function (child) {
parentNames.forEach(function (parent) {
classNames.push(parent.trim() + separator + child.trim());
});
});
return classNames;
}
module.exports = postcss.plugin('postcss-bem', function (opts) {
opts = opts || {};
if (!opts.style) {
opts.style = 'suit';
}
if (opts.style !== 'suit' && opts.style !== 'bem') {
throw new Error('postcss-bem: opts.style may only be "suit" or "bem"');
}
opts.shortcuts = extend(config.shortcuts, opts.shortcuts);
var currentConfig = config[opts.style];
if (opts.separators) {
for (var customSeparator in opts.separators) {
if (!opts.separators.hasOwnProperty(customSeparator)) continue;
var separatorValue = opts.separators[customSeparator];
if (typeof separatorValue === 'string') {
currentConfig.separators[customSeparator] = separatorValue;
} else {
throw new Error('postcss-bem: opts.separators.' + customSeparator + ' must be a string');
}
}
}
function checkRuleMatches(name, rule) {
return rule.name === name || !!opts.shortcuts[name] && rule.name === opts.shortcuts[name];
}
function processModifierOrDescendent(baseNames, rule, container, after) {
var separator;
var last;
var newRule;
var newNames;
if (checkRuleMatches('modifier', rule)) {
separator = currentConfig.separators.modifier;
} else if (checkRuleMatches('descendent', rule)) {
separator = currentConfig.separators.descendent;
}
if(separator) {
newNames = combineNames(baseNames, rule.params.split(','), separator);
newRule = postcss.rule({
// selectors: component names with `.`s in front
selector: newNames.map(function (name) {
return '.' + name;
}).join(', '),
source: rule.source
});
container.insertAfter(after, newRule);
last = newRule;
rule.each(function (node) {
var subrule = false;
if (node.type === 'atrule') {
subrule = processModifierOrDescendent(newNames, node, container, last);
}
if (subrule) {
last = subrule;
} else {
newRule.append(node); //node.moveTo(newRule);
}
});
rule.remove();
return last;
}
return false;
}
function processComponent (component, namespace) {
var name = component.params;
if (namespace) {
name = namespace + currentConfig.separators.namespace + name;
}
var last = component;
var newComponent = postcss.rule({
selector: '.' + name,
source: component.source
});
component.each(function (rule) {
var newRule = false;
if (rule.type === 'atrule') {
newRule = processModifierOrDescendent([name], rule, component.parent, last);
}
if (newRule) {
last = newRule;
} else {
newComponent.append(rule);//rule.moveTo(newComponent);
}
});
component.replaceWith(newComponent);
}
return function (css, result) {
var namespaces = {};
if (opts.style === 'suit') {
css.walkAtRules(function (utility) {
if (!checkRuleMatches('utility', utility)) return;
if (!utility.params) {
throw utility.error('No names supplied to @utility');
}
var utilityNames = postcss.list.comma(utility.params);
var selector = utilityNames.map(function (params) {
params = postcss.list.space(params);
var variant;
var name;
if (params.length > 2) {
result.warn('Too many parameters for @utility', {
node: utility
});
}
name = 'u-';
if (params.length > 1) {
variant = params[1];
if (variant === 'small') {
name += 'sm';
} else if (variant === 'medium') {
name += 'md';
} else if (variant === 'large') {
name += 'lg';
} else {
result.warn('Unknown variant: ' + variant, {
node: utility
});
}
name += '-';
}
name += params[0];
return '.' + name;
}).join(', ');
var newUtility = postcss.rule({
selector: selector,
source: utility.source
});
utility.each(function (node) {
newUtility.append(node);//node.moveTo(newUtility);
});
utility.replaceWith(newUtility);
});
}
css.walkAtRules(function (namespace) {
if ( !checkRuleMatches('component-namespace', namespace) ) return;
var name = namespace.params;
if (!namespace.nodes) {
namespaces[namespace.source.input.file || namespace.source.input.id] = name;
namespace.remove();
return;
}
namespace.walkAtRules(function (component) {
if ( !checkRuleMatches('component', component) ) return;
processComponent(component, name);
});
var node = namespace.last;
while (node) {
namespace.after(node);//node.moveAfter(namespace);
node = namespace.last;
}
namespace.remove();
});
css.walkAtRules(function (component) {
if ( !checkRuleMatches('component', component) ) return;
var namespace = opts.defaultNamespace;
var id = component.source.input.file || component.source.input.id;
if (id in namespaces) {
namespace = namespaces[id];
}
processComponent(component, namespace);
});
if (opts.style === 'suit') {
css.walkAtRules(function (when) {
if ( !checkRuleMatches('when', when)) return;
var parent = when.parent;
if (parent === css || parent.type !== 'rule') {
throw when.error('@when can only be used in rules which are not the root node');
}
var states = when.params;
var newSelector = postcss.list.comma(parent.selector).map(function (selector) {
return postcss.list.comma(states).map(function (state) {
return selector + currentConfig.separators.state + state;
}).join(', ');
}).join(', ');
var newWhen = postcss.rule({
selector: newSelector,
source: when.source
});
when.each(function (node) {
newWhen.append(node);//node.moveTo(newWhen);
});
parent.after(newWhen);//newWhen.moveAfter(parent);
when.remove();
});
}
};
});