-
Notifications
You must be signed in to change notification settings - Fork 32
/
csjs.js
78 lines (68 loc) · 2.27 KB
/
csjs.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
'use strict';
var extractExtends = require('./css-extract-extends');
var isComposition = require('./composition').isComposition;
var buildExports = require('./build-exports');
var scopify = require('./scopeify');
var cssKey = require('./css-key');
module.exports = function csjsHandler(strings) {
// Fast path to prevent arguments deopt
var values = Array(arguments.length - 1);
for (var i = 1; i < arguments.length; i++) {
values[i - 1] = arguments[i];
}
var css = joiner(strings, values.map(selectorize));
var ignores = values.reduce(function(acc, val) {
if (isComposition(val)) {
val.classNames.forEach(function(name, i) {
acc[name] = val.unscoped[i];
});
}
return acc;
}, {});
var scoped = scopify(css, ignores);
var hashes = Object.assign({}, scoped.classes, scoped.keyframes);
var extracted = extractExtends(scoped.css, hashes);
var localClasses = without(scoped.classes, ignores);
var localKeyframes = without(scoped.keyframes, ignores);
var compositions = extracted.compositions;
var exports = buildExports(localClasses, localKeyframes, compositions);
return Object.defineProperty(exports, cssKey, {
enumerable: false,
configurable: false,
writeable: false,
value: extracted.css
});
};
/**
* Replaces class compositions with comma seperated class selectors
* @param value - the potential class composition
* @return - the original value or the selectorized class composition
*/
function selectorize(value) {
return isComposition(value) ? value.selector : value;
}
/**
* Joins template string literals and values
* @param {array} strings - array of strings
* @param {array} values - array of values
* @return {string} - strings and values joined
*/
function joiner(strings, values) {
return strings.map(function(str, i) {
return (i !== values.length) ? str + values[i] : str;
}).join('');
}
/**
* Returns first object without keys of second
* @param {object} obj - source object
* @param {object} unwanted - object with unwanted keys
* @return {object} - first object without unwanted keys
*/
function without(obj, unwanted) {
return Object.keys(obj).reduce(function(acc, key) {
if (!unwanted[key]) {
acc[key] = obj[key];
}
return acc;
}, {});
}