-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
381 lines (339 loc) · 10.5 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/**
* @copyright 2016-present, React CSS Components team
* @flow
*/
import {createHash} from 'crypto';
import invariant from 'invariant';
import * as LoaderUtils from 'loader-utils';
import {identifier, stringLiteral, program} from 'babel-types';
import generate from 'babel-generator';
import traverse from 'babel-traverse';
import * as types from 'babel-types';
import {expr, stmt} from 'babel-plugin-ast-literal/api';
import {parse} from 'babylon';
import * as postcss from 'postcss';
import createSelectorParser, {className} from 'postcss-selector-parser';
import HTMLTagList from './HTMLTagList';
import CSSPseudoClassList from './CSSPseudoClassList';
import * as ComponentRef from './ComponentRef';
type RenderConfig = {
loadCSS: string;
};
type JSNode = Object;
type CSSNode = Object;
type VariantSpec = {
componentName: string;
variantName: string;
expression: ?JSNode;
};
type ComponentSpec = {
base: ?JSNode;
variants: {[name: string]: {expression: ?JSNode}};
};
type ComponentSpecCollection = {
[name: string]: ComponentSpec;
};
const LOADER = require.resolve('../webpack');
const COMPONENT_RE = /^[A-Z][a-zA-Z_0-9]*$/;
const PROP_VARIANT_NAME = ':prop';
function hash(value) {
let hasher = createHash('md5');
hasher.update(value);
return hasher.digest('hex');
}
function dashToCamelCase(name: string) {
return name.replace(/-([a-z])/g, word => word[1].toUpperCase());
}
function parseSelector(selector) {
let parser = createSelectorParser();
return parser.process(selector).res;
}
function isPropReference(path) {
if (!types.isIdentifier(path.node)) {
return false;
}
if (path.node.__seen) {
return false;
}
if (path.scope.parent !== undefined) {
return false;
}
if (types.isMemberExpression(path.parentPath.node)) {
while (types.isMemberExpression(path.parentPath.node)) {
if (path.node === path.parentPath.node.property) {
return false;
}
path = path.parentPath;
}
}
return true;
}
function parsePropVariantExpression(expression) {
let node = parse(expression);
traverse(node, {
enter(path) {
if (isPropReference(path)) {
let nextNode = expr`props.${path.node}`;
nextNode.object.__seen = true;
path.replaceWith(nextNode);
}
}
});
node = node.program.body[0].expression;
return node;
}
function findComponentNames(node: CSSNode): Array<string> {
let componentNames = [];
let selector = parseSelector(node.selector);
selector.eachTag(selector => {
if (COMPONENT_RE.exec(selector.value)) {
componentNames.push(selector.value);
}
});
return componentNames;
}
function findVariants(node: CSSNode): Array<VariantSpec> {
let variantNames = [];
let selector = parseSelector(node.selector);
selector.eachPseudo(selector => {
let expression = null;
let variantName = selector.value.slice(1);
if (selector.value === PROP_VARIANT_NAME) {
expression = node.selector.slice(
selector.source.start.column + PROP_VARIANT_NAME.length,
selector.source.end.column - 1
);
variantName = variantName + '__' + hash(expression).slice(0, 6);
expression = parsePropVariantExpression(expression);
}
let idx = selector.parent.nodes.indexOf(selector);
let prev = selector.parent.nodes[idx - 1];
if (prev && prev.type === 'tag' && COMPONENT_RE.exec(prev.value)) {
variantNames.push({
componentName: prev.value,
variantName,
expression,
});
}
});
return variantNames;
}
function isPrimaryComponent(node: CSSNode): boolean {
let selector = parseSelector(node.selector);
return (
selector.nodes.length === 1 &&
selector.nodes[0].type === 'selector' &&
selector.nodes[0].nodes.length === 1 &&
selector.nodes[0].nodes[0].type === 'tag' &&
COMPONENT_RE.exec(selector.nodes[0].nodes[0].value)
);
}
function renderToCSS(source: string): string {
let root = postcss.parse(source);
root.walkRules(node => {
removeBaseDeclaration(node);
localizeComponentRule(node);
});
return root.toString();
}
function removeBaseDeclaration(node) {
node.walkDecls(node => {
if (node.prop === 'base') {
node.remove();
}
});
}
function localizeComponentRule(node) {
let componentNames = findComponentNames(node);
if (componentNames.length > 0) {
let toClassify = [];
let toPseudoClassify = [];
let selector = parseSelector(node.selector);
selector.eachTag(selector => {
if (componentNames.indexOf(selector.value) > -1) {
toClassify.push(selector);
}
});
selector.eachPseudo(selector => {
let idx = selector.parent.nodes.indexOf(selector);
let prev = selector.parent.nodes[idx - 1];
if (prev && prev.type === 'tag' && componentNames.indexOf(prev.value) > -1) {
let componentName = prev.value;
let variantName = dashToCamelCase(selector.value.slice(1));
if (CSSPseudoClassList[variantName]) {
selector.parent.parent.append(className({value: componentName + '__' + variantName}));
} else {
toPseudoClassify.push(selector);
}
}
});
toPseudoClassify.forEach(selector => {
let parent = selector.parent;
let idx = parent.nodes.indexOf(selector);
let prev = parent.nodes[idx - 1];
let componentName = prev.value;
let variantName = dashToCamelCase(selector.value.slice(1));
if (selector.value === PROP_VARIANT_NAME) {
let expression = node.selector.slice(
selector.source.start.column + PROP_VARIANT_NAME.length,
selector.source.end.column - 1
);
variantName = variantName + '__' + hash(expression).slice(0, 6);
}
let nextSelector = className({value: componentName + '__' + variantName});
prev.removeSelf();
selector.replaceWith(nextSelector);
});
toClassify.forEach(selector => {
let nextSelector = className({value: selector.value});
selector.replaceWith(nextSelector);
});
let nextNode = node.clone();
nextNode.selector = selector.toString();
node.replaceWith(nextNode);
}
}
function renderToJS(source: string, config: RenderConfig): string {
let root = postcss.parse(source);
let imports = stmt`
var React = require("react");
var styles = require("${config.loadCSS}");
`;
let statements = [];
let components: ComponentSpecCollection = {};
statements.push(stmt`
function reconcileProps(props, className) {
var nextProps = {};
for (var k in props) {
if (k === 'variant') {
continue;
}
if (props.hasOwnProperty(k)) {
nextProps[k] = props[k];
}
}
nextProps.className = className;
return nextProps;
}
`);
function registerComponent(componentName) {
if (components[componentName] === undefined) {
components[componentName] = {
base: stringLiteral('div'),
variants: {},
};
}
}
function registerComponentVariants({componentName, variantName, expression}) {
invariant(
components[componentName],
'Trying to configure base for an unknown component %s', componentName
);
components[componentName].variants[variantName] = {expression};
}
function configureComponentBase(componentName, base) {
invariant(
components[componentName],
'Trying to configure base for an unknown component %s', componentName
);
if (HTMLTagList[base]) {
base = stringLiteral(base);
} else {
let ref = ComponentRef.parse(base);
invariant(
ref != null,
'Found invalid component ref: %s', base
);
base = identifier(componentName + '__Base');
imports.push(stmt`
var ${base} = require("${ref.source}").${identifier(ref.name)};
`);
}
components[componentName].base = base;
}
// walk CSS AST and register all component configurations
root.walkRules(node => {
let componentNames = findComponentNames(node);
if (componentNames.length === 0) {
return;
}
componentNames.forEach(componentName => {
registerComponent(componentName);
});
if (isPrimaryComponent(node)) {
let componentName = componentNames[0];
node.walkDecls(decl => {
if (decl.prop === 'base') {
configureComponentBase(componentName, decl.value);
}
});
}
let variants = findVariants(node);
for (let i = 0; i < variants.length; i++) {
let variant = variants[i];
registerComponentVariants(variant);
}
});
// generate JS code from component configurations
for (let componentName in components) {
let component = components[componentName];
if (components.hasOwnProperty(componentName)) {
let className = expr`styles.${identifier(componentName)}`;
for (let variantName in component.variants) {
let variant = component.variants[variantName];
variantName = dashToCamelCase(variantName);
if (variant.expression) {
className = expr`
${className} + (${variant.expression}
? ' ' + styles.${identifier(componentName + '__' + variantName)}
: '')
`;
} else {
className = expr`
${className} + (variant.${identifier(variantName)}
? ' ' + styles.${identifier(componentName + '__' + variantName)}
: '')
`;
}
}
statements.push(stmt`
module.exports.${identifier(componentName)} = function ${identifier(componentName)}(props) {
var variant = props.variant || {};
var className = ${className};
return React.createElement(
${component.base},
reconcileProps(props, className)
);
}
`);
}
}
return generate(program(imports.concat(statements))).code;
}
/**
* Webpack loader for React CSS component modules.
*/
export function loader(source: string): string {
this.cacheable();
let query = LoaderUtils.parseQuery(this.query);
if (query.css) {
let result = renderToCSS(source);
return result;
} else {
let loadCSS = query.loadCSS
? query.loadCSS
: ['style-loader', 'css-loader?modules'];
let result = renderToJS(source, {
loadCSS: `!!${loadCSS.join('!')}!${LOADER}?css!${this.resource}`,
});
return result;
}
}
/**
* Render React CSS component module into JS and CSS sources.
*/
export function render(source: string, config: RenderConfig): {js: string; css: string} {
let js = renderToJS(source, {loadCSS: config.loadCSS});
let css = renderToCSS(source);
return {js, css};
}