-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
207 lines (179 loc) · 6.16 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
/* @flow */
const { join } = require('path');
const syntax = require('@babel/plugin-syntax-jsx').default;
/*::
type State = {
required: boolean;
items: Array<any>
}
*/
/* ::
type Options = {
target?: 'linaria' | 'styled-components' | 'auto' | 'none',
}
*/
module.exports = function(
babel /*: any */,
{ target = 'auto' } /*: Options */
) {
const { types: t } = babel;
return {
inherits: syntax,
visitor: {
Program: {
enter(path /*: any */, state /*: State */) {
// Whether we've inserted the require statement
state.required = false;
// Nodes to insert
state.items = [];
},
exit(path /*: any */, state /*: State */) {
// Add the new nodes to the end of the program
path.node.body.push(...state.items);
},
},
JSXAttribute(path /*: any */, state /*: State */) {
if (path.node.name.name !== 'css') {
// Don't do anything if we didn't find an attribute named 'css'
return;
}
// Get all the bindings in the program scope
const { bindings } = path.findParent(p => p.type === 'Program').scope;
if (!state.required) {
// If we haven't inserted a require statement, now is the time
if (!bindings.styled) {
// The binding doesn't exist, we need to insert the require
let library;
if (target === 'auto') {
const message =
"Please specify the name of one of these libraries in the 'target' option: 'linaria', 'styled-components'.\n" +
"If you don't want to insert 'require' statement from 'styled', set the target to 'none'.";
let dependencies;
try {
// Read the package.json to determine the library
/* $FlowFixMe */
dependencies = require(join(process.cwd(), 'package.json'))
.dependencies;
} catch (e) {
throw path.buildCodeFrameError(
"Couldn't read 'package.json' to determine the CSS in JS library.\n" +
message
);
}
if (dependencies && dependencies.linaria) {
library = 'linaria';
} else if (dependencies && dependencies['styled-components']) {
library = 'styled-components';
} else {
throw path.buildCodeFrameError(
"Couldn't find 'linaria' or 'styled-components' in the 'package.json'.\n" +
message
);
}
} else {
library = target;
}
if (library === 'linaria') {
// Insert `var styled = require('linaria/react').styled`
state.items.push(
t.variableDeclaration('var', [
t.variableDeclarator(
t.identifier('styled'),
t.memberExpression(
t.callExpression(t.identifier('require'), [
t.stringLiteral('linaria/react'),
]),
t.identifier('styled')
)
),
])
);
} else if (library === 'styled-components') {
// Insert `var styled = require('styled-components')`
state.items.push(
t.variableDeclaration('var', [
t.variableDeclarator(
t.identifier('styled'),
t.callExpression(t.identifier('require'), [
t.stringLiteral('styled-components'),
])
),
])
);
}
}
state.required = true;
}
const elem = path.parentPath;
const id = path.scope.generateUidIdentifier(
'CSS' +
elem.node.name.name.replace(/^([a-z])/, (match, p1) =>
p1.toUpperCase()
)
);
const tag = elem.node.name.name;
const styled = t.callExpression(t.identifier('styled'), [
/^[a-z]/.test(tag) ? t.stringLiteral(tag) : t.identifier(tag),
]);
let css;
if (t.isStringLiteral(path.node.value)) {
css = t.templateLiteral(
[t.templateElement({ raw: path.node.value.value }, true)],
[]
);
} else if (t.isJSXExpressionContainer(path.node.value)) {
if (t.isTemplateLiteral(path.node.value.expression)) {
css = path.node.value.expression;
} else {
css = t.templateLiteral(
[
t.templateElement({ raw: '' }, false),
t.templateElement({ raw: '' }, true),
],
[path.node.value.expression]
);
}
}
if (!css) {
return;
}
elem.node.attributes = elem.node.attributes.filter(
attr => attr !== path.node
);
elem.node.name.name = id.name;
if (elem.parentPath.node.closingElement) {
elem.parentPath.node.closingElement.name.name = id.name;
}
css.expressions = css.expressions.reduce((acc, ex) => {
if (
Object.entries(bindings).some(([, b] /*: any */) =>
b.referencePaths.find(p => p.node === ex)
) ||
t.isFunctionExpression(ex) ||
t.isArrowFunctionExpression(ex)
) {
acc.push(ex);
} else {
const name = path.scope.generateUidIdentifier(`_$p_`);
const p = t.identifier('p');
elem.node.attributes.push(
t.jSXAttribute(
t.jSXIdentifier(name.name),
t.jSXExpressionContainer(ex)
)
);
acc.push(
t.arrowFunctionExpression([p], t.memberExpression(p, name))
);
}
return acc;
}, []);
state.items.push(
t.variableDeclaration('var', [
t.variableDeclarator(id, t.taggedTemplateExpression(styled, css)),
])
);
},
},
};
};