-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSSXDefinition.js
178 lines (157 loc) · 5.3 KB
/
CSSXDefinition.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
var isArray = require('../helpers/isArray');
var injectAt = require('../helpers/injectAt');
var getID = require('../helpers/randomId');
var t = require('babel-types');
var isIgnored = require('../core/traverse').isIgnored;
var updateStyleSheet = function (node, stylesheetId, options) {
var key, i;
if (
node && node.type === 'CallExpression' &&
node.callee && node.callee.type === 'MemberExpression' &&
node.callee.object && node.callee.object.name === 'cssx'
) {
node.callee.object.name = stylesheetId;
} else if (
node && node.type === 'AssignmentExpression' &&
node.left && node.left.type === 'MemberExpression' &&
node.left.object && node.left.object.name === 'cssx'
) {
node.left.object.name = stylesheetId;
} else {
if (isArray(node)) {
for (i = 0; i < node.length; i++) {
updateStyleSheet(node[i], stylesheetId, options);
}
} else {
for (key in node) {
if (!isIgnored(key)) {
if (typeof node[key] === 'object') {
updateStyleSheet(node[key], stylesheetId, options);
}
}
}
}
}
return node;
};
var checkForStyleDefinition = function (node) {
return node.body &&
node.body.length === 1 &&
node.body[0].type === 'CallExpression' &&
node.body[0].callee && node.body[0].callee.name === 'cssx' &&
node.body[0].arguments && node.body[0].arguments.length === 1 &&
node.body[0].arguments[0].type === 'Identifier';
};
var funcLines, variables, objectLiterals, stylesheetId;
module.exports = {
enter: function (node, parent, index, context) {
funcLines = [];
objectLiterals = [];
variables = [];
stylesheetId = getID();
context.addToCSSXSelfInvoke = function (item, parent) {
if (item.type === 'VariableDeclaration') {
objectLiterals.push({
selector: parent.selector.value ? t.stringLiteral(parent.selector.value) : parent.selector,
rulesObjVar: item.declarations[0].id.name
});
variables.push(item);
} else {
funcLines.push(item);
}
};
},
exit: function (node, parent, index, context) {
var rulesRegistration;
var createSelfInvoke;
var funcExpr;
var funcCallExpr;
var result;
var options = this.options;
var rulesBuffer;
var isASingleObject = objectLiterals.length >= 1 &&
(typeof objectLiterals[0].selector === 'object' ?
objectLiterals[0].selector.value === '' :
objectLiterals[0].selector === '');
var applyResult = function (r) {
if (isArray(parent)) {
injectAt(parent, index, r);
} else {
parent[index] = r;
}
};
delete context.addToCSSXSelfInvoke;
if (node.body.length === 0) {
delete parent[index];
return;
}
// make sure that we keep the stylesheet definitions
if (checkForStyleDefinition(node)) {
applyResult(node.body[0]);
return;
}
if (!isASingleObject) {
variables = [t.variableDeclaration(
'var',
[
t.variableDeclarator(
t.identifier(stylesheetId),
t.objectExpression([])
)
]
)].concat(variables);
}
rulesRegistration = node.body.map(function (line) {
line = updateStyleSheet(line, stylesheetId, options);
if (line.type === 'CallExpression') {
line = t.expressionStatement(updateStyleSheet(line, stylesheetId, options));
}
return line;
});
// checking for duplicated selectors (like multiple @font-face for example)
rulesBuffer = {};
rulesRegistration = rulesRegistration.reduce(function (result, current) {
var name;
if (current.expression && current.expression.left) {
name = current.expression.left.property.value;
if (name && rulesBuffer[name]) {
if (result[rulesBuffer[name].index].expression.right.type === 'ArrayExpression') {
result[rulesBuffer[name].index].expression.right.elements.push(
t.identifier(current.expression.right.name)
);
} else {
result[rulesBuffer[name].index].expression.right = t.arrayExpression([
t.identifier(rulesBuffer[name].rule.expression.right.name),
t.identifier(current.expression.right.name)
]);
}
} else {
rulesBuffer[name] = { index: result.length, rule: current };
result.push(current);
}
}
return result;
}, []);
// putting the variables in one line
variables = t.variableDeclaration('var', variables.map(function (v) {
return v.declarations[0];
}));
funcLines = [variables].concat(funcLines);
// styles for only one rule
if (isASingleObject) {
funcLines.push(t.returnStatement(t.identifier(objectLiterals[0].rulesObjVar)));
} else {
funcLines = funcLines.concat(rulesRegistration);
funcLines.push(t.returnStatement(t.identifier(stylesheetId)));
}
createSelfInvoke = function (expr) { return t.parenthesizedExpression(expr); };
// wrapping up the self-invoke function
funcExpr = t.functionExpression(null, [], t.blockStatement(funcLines));
funcCallExpr = t.callExpression(
t.memberExpression(funcExpr, t.identifier('apply')),
[t.thisExpression()]
);
result = createSelfInvoke(funcCallExpr);
applyResult(result);
}
};