@stylexjs/babel-plugin 0.1.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.babelrc +16 -0
- package/__tests__/stylex-evaluation-test.js +164 -0
- package/__tests__/stylex-metadata-test.js +103 -0
- package/__tests__/stylex-transform-call-test.js +775 -0
- package/__tests__/stylex-transform-create-test.js +551 -0
- package/__tests__/stylex-transform-import-test.js +310 -0
- package/__tests__/stylex-transform-keyframes-test.js +135 -0
- package/__tests__/stylex-transform-logical-properties-test.js +736 -0
- package/__tests__/stylex-transform-logical-values-test.js +440 -0
- package/__tests__/stylex-transform-polyfills-test.js +65 -0
- package/__tests__/stylex-transform-value-normalize-test.js +287 -0
- package/__tests__/stylex-transform-variable-removal-test.js +112 -0
- package/__tests__/stylex-validation-create-test.ts +310 -0
- package/__tests__/stylex-validation-custom-properties-test.ts +122 -0
- package/__tests__/stylex-validation-declarations-test.ts +129 -0
- package/__tests__/stylex-validation-import-test.ts +57 -0
- package/__tests__/stylex-validation-keyframes-test.ts +109 -0
- package/jest.config.ts +20 -0
- package/package.json +26 -0
- package/rollup.config.mjs +30 -0
- package/tsconfig.json +103 -0
|
@@ -0,0 +1,775 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
'use strict';
|
|
9
|
+
|
|
10
|
+
jest.autoMockOff();
|
|
11
|
+
|
|
12
|
+
const { transformSync } = require('@babel/core');
|
|
13
|
+
const stylexPlugin = require('../src/index');
|
|
14
|
+
const jsx = require('@babel/plugin-syntax-jsx');
|
|
15
|
+
|
|
16
|
+
function transform(source, opts = {}) {
|
|
17
|
+
return transformSync(source, {
|
|
18
|
+
filename: opts.filename,
|
|
19
|
+
parserOpts: {
|
|
20
|
+
flow: {
|
|
21
|
+
all: true,
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
plugins: [jsx, [stylexPlugin, opts]],
|
|
25
|
+
}).code;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
describe('@stylexjs/babel-plugin', () => {
|
|
29
|
+
describe('[transform] stylex() call', () => {
|
|
30
|
+
test('empty stylex call', () => {
|
|
31
|
+
expect(
|
|
32
|
+
transform(`
|
|
33
|
+
import stylex from 'stylex';
|
|
34
|
+
stylex();
|
|
35
|
+
`)
|
|
36
|
+
).toMatchInlineSnapshot(`
|
|
37
|
+
"import stylex from 'stylex';
|
|
38
|
+
"";"
|
|
39
|
+
`);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test('basic stylex call', () => {
|
|
43
|
+
expect(
|
|
44
|
+
transform(`
|
|
45
|
+
import stylex from 'stylex';
|
|
46
|
+
const styles = stylex.create({
|
|
47
|
+
red: {
|
|
48
|
+
color: 'red',
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
stylex(styles.red);
|
|
52
|
+
`)
|
|
53
|
+
).toMatchInlineSnapshot(`
|
|
54
|
+
"import stylex from 'stylex';
|
|
55
|
+
stylex.inject(".x1e2nbdu{color:red}", 1);
|
|
56
|
+
"x1e2nbdu";"
|
|
57
|
+
`);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test('stylex call with number', () => {
|
|
61
|
+
expect(
|
|
62
|
+
transform(`
|
|
63
|
+
import stylex from 'stylex';
|
|
64
|
+
const styles = stylex.create({
|
|
65
|
+
0: {
|
|
66
|
+
color: 'red',
|
|
67
|
+
},
|
|
68
|
+
1: {
|
|
69
|
+
backgroundColor: 'blue',
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
stylex(styles[0], styles[1]);
|
|
73
|
+
`)
|
|
74
|
+
).toMatchInlineSnapshot(`
|
|
75
|
+
"import stylex from 'stylex';
|
|
76
|
+
stylex.inject(".x1e2nbdu{color:red}", 1);
|
|
77
|
+
stylex.inject(".x1t391ir{background-color:blue}", 1);
|
|
78
|
+
"x1e2nbdu x1t391ir";"
|
|
79
|
+
`);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test('stylex call with computed number', () => {
|
|
83
|
+
expect(
|
|
84
|
+
transform(`
|
|
85
|
+
import stylex from 'stylex';
|
|
86
|
+
const styles = stylex.create({
|
|
87
|
+
[0]: {
|
|
88
|
+
color: 'red',
|
|
89
|
+
},
|
|
90
|
+
[1]: {
|
|
91
|
+
backgroundColor: 'blue',
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
stylex(styles[0], styles[1]);
|
|
95
|
+
`)
|
|
96
|
+
).toMatchInlineSnapshot(`
|
|
97
|
+
"import stylex from 'stylex';
|
|
98
|
+
stylex.inject(".x1e2nbdu{color:red}", 1);
|
|
99
|
+
stylex.inject(".x1t391ir{background-color:blue}", 1);
|
|
100
|
+
"x1e2nbdu x1t391ir";"
|
|
101
|
+
`);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test('stylex call with computed string', () => {
|
|
105
|
+
expect(
|
|
106
|
+
transform(`
|
|
107
|
+
import stylex from 'stylex';
|
|
108
|
+
const styles = stylex.create({
|
|
109
|
+
'default': {
|
|
110
|
+
color: 'red',
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
stylex(styles['default']);
|
|
114
|
+
`)
|
|
115
|
+
).toMatchInlineSnapshot(`
|
|
116
|
+
"import stylex from 'stylex';
|
|
117
|
+
stylex.inject(".x1e2nbdu{color:red}", 1);
|
|
118
|
+
"x1e2nbdu";"
|
|
119
|
+
`);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
test('stylex call with multiple namespaces', () => {
|
|
123
|
+
expect(
|
|
124
|
+
transform(`
|
|
125
|
+
import stylex from 'stylex';
|
|
126
|
+
const styles = stylex.create({
|
|
127
|
+
default: {
|
|
128
|
+
color: 'red',
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
const otherStyles = stylex.create({
|
|
132
|
+
default: {
|
|
133
|
+
backgroundColor: 'blue',
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
stylex(styles.default, otherStyles.default);
|
|
137
|
+
`)
|
|
138
|
+
).toMatchInlineSnapshot(`
|
|
139
|
+
"import stylex from 'stylex';
|
|
140
|
+
stylex.inject(".x1e2nbdu{color:red}", 1);
|
|
141
|
+
stylex.inject(".x1t391ir{background-color:blue}", 1);
|
|
142
|
+
"x1e2nbdu x1t391ir";"
|
|
143
|
+
`);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
test('stylex call within variable declarations', () => {
|
|
147
|
+
expect(
|
|
148
|
+
transform(`
|
|
149
|
+
import stylex from 'stylex';
|
|
150
|
+
const styles = stylex.create({
|
|
151
|
+
foo: { color: 'red' }
|
|
152
|
+
});
|
|
153
|
+
const a = function() {
|
|
154
|
+
return stylex(styles.foo);
|
|
155
|
+
}
|
|
156
|
+
`)
|
|
157
|
+
).toMatchInlineSnapshot(`
|
|
158
|
+
"import stylex from 'stylex';
|
|
159
|
+
stylex.inject(".x1e2nbdu{color:red}", 1);
|
|
160
|
+
const a = function () {
|
|
161
|
+
return "x1e2nbdu";
|
|
162
|
+
};"
|
|
163
|
+
`);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
test('stylex call with styles variable assignment', () => {
|
|
167
|
+
expect(
|
|
168
|
+
transform(`
|
|
169
|
+
import stylex from 'stylex';
|
|
170
|
+
const styles = stylex.create({
|
|
171
|
+
foo: {
|
|
172
|
+
color: 'red',
|
|
173
|
+
},
|
|
174
|
+
bar: {
|
|
175
|
+
backgroundColor: 'blue',
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
stylex(styles.foo, styles.bar);
|
|
179
|
+
const foo = styles;
|
|
180
|
+
`)
|
|
181
|
+
).toMatchInlineSnapshot(`
|
|
182
|
+
"import stylex from 'stylex';
|
|
183
|
+
stylex.inject(".x1e2nbdu{color:red}", 1);
|
|
184
|
+
stylex.inject(".x1t391ir{background-color:blue}", 1);
|
|
185
|
+
const styles = {
|
|
186
|
+
foo: {
|
|
187
|
+
color: "x1e2nbdu"
|
|
188
|
+
},
|
|
189
|
+
bar: {
|
|
190
|
+
backgroundColor: "x1t391ir"
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
"x1e2nbdu x1t391ir";
|
|
194
|
+
const foo = styles;"
|
|
195
|
+
`);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
test('stylex call within export declarations', () => {
|
|
199
|
+
expect(
|
|
200
|
+
transform(`
|
|
201
|
+
import stylex from 'stylex';
|
|
202
|
+
const styles = stylex.create({
|
|
203
|
+
foo: { color: 'red' }
|
|
204
|
+
});
|
|
205
|
+
export default function MyExportDefault() {
|
|
206
|
+
return stylex(styles.foo);
|
|
207
|
+
}
|
|
208
|
+
export function MyExport() {
|
|
209
|
+
return stylex(styles.foo);
|
|
210
|
+
}
|
|
211
|
+
`)
|
|
212
|
+
).toMatchInlineSnapshot(`
|
|
213
|
+
"import stylex from 'stylex';
|
|
214
|
+
stylex.inject(".x1e2nbdu{color:red}", 1);
|
|
215
|
+
export default function MyExportDefault() {
|
|
216
|
+
return "x1e2nbdu";
|
|
217
|
+
}
|
|
218
|
+
export function MyExport() {
|
|
219
|
+
return "x1e2nbdu";
|
|
220
|
+
}"
|
|
221
|
+
`);
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
test('stylex call with short-form properties', () => {
|
|
225
|
+
expect(
|
|
226
|
+
transform(`
|
|
227
|
+
import stylex from 'stylex';
|
|
228
|
+
const styles = stylex.create({
|
|
229
|
+
foo: {
|
|
230
|
+
padding: 5
|
|
231
|
+
}
|
|
232
|
+
});
|
|
233
|
+
stylex(styles.foo);
|
|
234
|
+
`)
|
|
235
|
+
).toMatchInlineSnapshot(`
|
|
236
|
+
"import stylex from 'stylex';
|
|
237
|
+
stylex.inject(".x123j3cw{padding-top:5px}", 1);
|
|
238
|
+
stylex.inject(".x1mpkggp{padding-right:5px}", 1, ".x1mpkggp{padding-left:5px}");
|
|
239
|
+
stylex.inject(".xs9asl8{padding-bottom:5px}", 1);
|
|
240
|
+
stylex.inject(".x1t2a60a{padding-left:5px}", 1, ".x1t2a60a{padding-right:5px}");
|
|
241
|
+
"x123j3cw x1mpkggp xs9asl8 x1t2a60a";"
|
|
242
|
+
`);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
test('stylex call with exported short-form properties', () => {
|
|
246
|
+
expect(
|
|
247
|
+
transform(`
|
|
248
|
+
import stylex from 'stylex';
|
|
249
|
+
export const styles = stylex.create({
|
|
250
|
+
foo: {
|
|
251
|
+
padding: 5
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
stylex(styles.foo);
|
|
255
|
+
`)
|
|
256
|
+
).toMatchInlineSnapshot(`
|
|
257
|
+
"import stylex from 'stylex';
|
|
258
|
+
stylex.inject(".x123j3cw{padding-top:5px}", 1);
|
|
259
|
+
stylex.inject(".x1mpkggp{padding-right:5px}", 1, ".x1mpkggp{padding-left:5px}");
|
|
260
|
+
stylex.inject(".xs9asl8{padding-bottom:5px}", 1);
|
|
261
|
+
stylex.inject(".x1t2a60a{padding-left:5px}", 1, ".x1t2a60a{padding-right:5px}");
|
|
262
|
+
export const styles = {
|
|
263
|
+
foo: {
|
|
264
|
+
paddingTop: "x123j3cw",
|
|
265
|
+
paddingEnd: "x1mpkggp",
|
|
266
|
+
paddingBottom: "xs9asl8",
|
|
267
|
+
paddingStart: "x1t2a60a"
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
"x123j3cw x1mpkggp xs9asl8 x1t2a60a";"
|
|
271
|
+
`);
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
test('stylex call using styles with pseudo selectors', () => {
|
|
275
|
+
expect(
|
|
276
|
+
transform(`
|
|
277
|
+
import stylex from 'stylex';
|
|
278
|
+
const styles = stylex.create({
|
|
279
|
+
default: {
|
|
280
|
+
color: 'red',
|
|
281
|
+
':hover': {
|
|
282
|
+
color: 'blue',
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
});
|
|
286
|
+
stylex(styles.default);
|
|
287
|
+
`)
|
|
288
|
+
).toMatchInlineSnapshot(`
|
|
289
|
+
"import stylex from 'stylex';
|
|
290
|
+
stylex.inject(".x1e2nbdu{color:red}", 1);
|
|
291
|
+
stylex.inject(".x17z2mba:hover{color:blue}", 8);
|
|
292
|
+
"x1e2nbdu x17z2mba";"
|
|
293
|
+
`);
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
test('stylex call using styles with Media Queries', () => {
|
|
297
|
+
expect(
|
|
298
|
+
transform(`
|
|
299
|
+
import stylex from 'stylex';
|
|
300
|
+
const styles = stylex.create({
|
|
301
|
+
default: {
|
|
302
|
+
backgroundColor: 'red',
|
|
303
|
+
'@media (min-width: 1000px)': {
|
|
304
|
+
backgroundColor: 'blue',
|
|
305
|
+
},
|
|
306
|
+
'@media (min-width: 2000px)': {
|
|
307
|
+
backgroundColor: 'purple',
|
|
308
|
+
},
|
|
309
|
+
},
|
|
310
|
+
});
|
|
311
|
+
stylex(styles.default);
|
|
312
|
+
`)
|
|
313
|
+
).toMatchInlineSnapshot(`
|
|
314
|
+
"import stylex from 'stylex';
|
|
315
|
+
stylex.inject(".xrkmrrc{background-color:red}", 1);
|
|
316
|
+
stylex.inject("@media (min-width: 1000px){.xc445zv.xc445zv{background-color:blue}}", 2);
|
|
317
|
+
stylex.inject("@media (min-width: 2000px){.x1ssfqz5.x1ssfqz5{background-color:purple}}", 2);
|
|
318
|
+
"xrkmrrc xc445zv x1ssfqz5";"
|
|
319
|
+
`);
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
test('stylex call using styles with Support Queries', () => {
|
|
323
|
+
expect(
|
|
324
|
+
transform(`
|
|
325
|
+
import stylex from 'stylex';
|
|
326
|
+
const styles = stylex.create({
|
|
327
|
+
default: {
|
|
328
|
+
backgroundColor: 'red',
|
|
329
|
+
'@supports (hover: hover)': {
|
|
330
|
+
backgroundColor: 'blue',
|
|
331
|
+
},
|
|
332
|
+
'@supports not (hover: hover)': {
|
|
333
|
+
backgroundColor: 'purple',
|
|
334
|
+
},
|
|
335
|
+
},
|
|
336
|
+
});
|
|
337
|
+
stylex(styles.default);
|
|
338
|
+
`)
|
|
339
|
+
).toMatchInlineSnapshot(`
|
|
340
|
+
"import stylex from 'stylex';
|
|
341
|
+
stylex.inject(".xrkmrrc{background-color:red}", 1);
|
|
342
|
+
stylex.inject("@supports (hover: hover){.x6m3b6q.x6m3b6q{background-color:blue}}", 2);
|
|
343
|
+
stylex.inject("@supports not (hover: hover){.x6um648.x6um648{background-color:purple}}", 2);
|
|
344
|
+
"xrkmrrc x6m3b6q x6um648";"
|
|
345
|
+
`);
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
describe('with conditional styles and collisions', () => {
|
|
349
|
+
test('stylex call with conditions', () => {
|
|
350
|
+
expect(
|
|
351
|
+
transform(`
|
|
352
|
+
import stylex from 'stylex';
|
|
353
|
+
const styles = stylex.create({
|
|
354
|
+
default: {
|
|
355
|
+
backgroundColor: 'red',
|
|
356
|
+
},
|
|
357
|
+
active: {
|
|
358
|
+
color: 'blue',
|
|
359
|
+
}
|
|
360
|
+
});
|
|
361
|
+
stylex(styles.default, isActive && styles.active);
|
|
362
|
+
`)
|
|
363
|
+
).toMatchInlineSnapshot(`
|
|
364
|
+
"import stylex from 'stylex';
|
|
365
|
+
stylex.inject(".xrkmrrc{background-color:red}", 1);
|
|
366
|
+
stylex.inject(".xju2f9n{color:blue}", 1);
|
|
367
|
+
"xrkmrrc" + (isActive ? " xju2f9n" : "");"
|
|
368
|
+
`);
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
test('stylex call with property collisions', () => {
|
|
372
|
+
expect(
|
|
373
|
+
transform(`
|
|
374
|
+
import stylex from 'stylex';
|
|
375
|
+
const styles = stylex.create({
|
|
376
|
+
red: {
|
|
377
|
+
color: 'red',
|
|
378
|
+
},
|
|
379
|
+
blue: {
|
|
380
|
+
color: 'blue',
|
|
381
|
+
}
|
|
382
|
+
});
|
|
383
|
+
stylex(styles.red, styles.blue);
|
|
384
|
+
stylex(styles.blue, styles.red);
|
|
385
|
+
`)
|
|
386
|
+
).toMatchInlineSnapshot(`
|
|
387
|
+
"import stylex from 'stylex';
|
|
388
|
+
stylex.inject(".x1e2nbdu{color:red}", 1);
|
|
389
|
+
stylex.inject(".xju2f9n{color:blue}", 1);
|
|
390
|
+
"xju2f9n";
|
|
391
|
+
"x1e2nbdu";"
|
|
392
|
+
`);
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
test('stylex call with short-form property collisions', () => {
|
|
396
|
+
expect(
|
|
397
|
+
transform(`
|
|
398
|
+
import stylex from 'stylex';
|
|
399
|
+
const styles = stylex.create({
|
|
400
|
+
foo: {
|
|
401
|
+
padding: 5,
|
|
402
|
+
paddingEnd: 10,
|
|
403
|
+
},
|
|
404
|
+
|
|
405
|
+
bar: {
|
|
406
|
+
padding: 2,
|
|
407
|
+
paddingStart: 10,
|
|
408
|
+
},
|
|
409
|
+
});
|
|
410
|
+
stylex(styles.foo, styles.bar);
|
|
411
|
+
`)
|
|
412
|
+
).toMatchInlineSnapshot(`
|
|
413
|
+
"import stylex from 'stylex';
|
|
414
|
+
stylex.inject(".x123j3cw{padding-top:5px}", 1);
|
|
415
|
+
stylex.inject(".x1iji9kk{padding-right:10px}", 1, ".x1iji9kk{padding-left:10px}");
|
|
416
|
+
stylex.inject(".xs9asl8{padding-bottom:5px}", 1);
|
|
417
|
+
stylex.inject(".x1t2a60a{padding-left:5px}", 1, ".x1t2a60a{padding-right:5px}");
|
|
418
|
+
stylex.inject(".x1nn3v0j{padding-top:2px}", 1);
|
|
419
|
+
stylex.inject(".xg83lxy{padding-right:2px}", 1, ".xg83lxy{padding-left:2px}");
|
|
420
|
+
stylex.inject(".x1120s5i{padding-bottom:2px}", 1);
|
|
421
|
+
stylex.inject(".x1sln4lm{padding-left:10px}", 1, ".x1sln4lm{padding-right:10px}");
|
|
422
|
+
"x1nn3v0j xg83lxy x1120s5i x1sln4lm";"
|
|
423
|
+
`);
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
test('stylex call with conditions and collisions', () => {
|
|
427
|
+
expect(
|
|
428
|
+
transform(`
|
|
429
|
+
import stylex from 'stylex';
|
|
430
|
+
const styles = stylex.create({
|
|
431
|
+
red: {
|
|
432
|
+
color: 'red',
|
|
433
|
+
},
|
|
434
|
+
blue: {
|
|
435
|
+
color: 'blue',
|
|
436
|
+
}
|
|
437
|
+
});
|
|
438
|
+
stylex(styles.red, isActive && styles.blue);
|
|
439
|
+
`)
|
|
440
|
+
).toMatchInlineSnapshot(`
|
|
441
|
+
"import stylex from 'stylex';
|
|
442
|
+
stylex.inject(".x1e2nbdu{color:red}", 1);
|
|
443
|
+
stylex.inject(".xju2f9n{color:blue}", 1);
|
|
444
|
+
"" + (isActive ? " xju2f9n" : " x1e2nbdu");"
|
|
445
|
+
`);
|
|
446
|
+
});
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
// COMPOSITION
|
|
450
|
+
describe('with plugin options', () => {
|
|
451
|
+
test('stylex call produces dev class names', () => {
|
|
452
|
+
const options = {
|
|
453
|
+
filename: '/html/js/FooBar.react.js',
|
|
454
|
+
dev: true,
|
|
455
|
+
};
|
|
456
|
+
expect(
|
|
457
|
+
transform(
|
|
458
|
+
`
|
|
459
|
+
import stylex from 'stylex';
|
|
460
|
+
const styles = stylex.create({
|
|
461
|
+
default: {
|
|
462
|
+
color: 'red',
|
|
463
|
+
},
|
|
464
|
+
});
|
|
465
|
+
stylex(styles.default);
|
|
466
|
+
`,
|
|
467
|
+
options
|
|
468
|
+
)
|
|
469
|
+
).toMatchInlineSnapshot(`
|
|
470
|
+
"import stylex from 'stylex';
|
|
471
|
+
stylex.inject(".x1e2nbdu{color:red}", 1);
|
|
472
|
+
"FooBar__styles.default x1e2nbdu";"
|
|
473
|
+
`);
|
|
474
|
+
});
|
|
475
|
+
|
|
476
|
+
test('stylex call produces dev class name with conditions', () => {
|
|
477
|
+
const options = {
|
|
478
|
+
filename: '/html/js/FooBar.react.js',
|
|
479
|
+
dev: true,
|
|
480
|
+
};
|
|
481
|
+
expect(
|
|
482
|
+
transform(
|
|
483
|
+
`
|
|
484
|
+
import stylex from 'stylex';
|
|
485
|
+
const styles = stylex.create({
|
|
486
|
+
default: {
|
|
487
|
+
color: 'red',
|
|
488
|
+
},
|
|
489
|
+
});
|
|
490
|
+
const otherStyles = stylex.create({
|
|
491
|
+
default: {
|
|
492
|
+
backgroundColor: 'blue',
|
|
493
|
+
}
|
|
494
|
+
});
|
|
495
|
+
stylex(styles.default, isActive && otherStyles.default);
|
|
496
|
+
`,
|
|
497
|
+
options
|
|
498
|
+
)
|
|
499
|
+
).toMatchInlineSnapshot(`
|
|
500
|
+
"import stylex from 'stylex';
|
|
501
|
+
stylex.inject(".x1e2nbdu{color:red}", 1);
|
|
502
|
+
stylex.inject(".x1t391ir{background-color:blue}", 1);
|
|
503
|
+
"FooBar__styles.default x1e2nbdu" + (isActive ? " FooBar__otherStyles.default x1t391ir" : "");"
|
|
504
|
+
`);
|
|
505
|
+
});
|
|
506
|
+
|
|
507
|
+
test('stylex call produces dev class name with collisions', () => {
|
|
508
|
+
const options = {
|
|
509
|
+
filename: '/html/js/FooBar.react.js',
|
|
510
|
+
dev: true,
|
|
511
|
+
};
|
|
512
|
+
|
|
513
|
+
expect(
|
|
514
|
+
transform(
|
|
515
|
+
`
|
|
516
|
+
import stylex from 'stylex';
|
|
517
|
+
const styles = stylex.create({
|
|
518
|
+
default: {
|
|
519
|
+
color: 'red',
|
|
520
|
+
},
|
|
521
|
+
active: {
|
|
522
|
+
color: 'blue',
|
|
523
|
+
}
|
|
524
|
+
});
|
|
525
|
+
stylex(styles.default, isActive && styles.active);
|
|
526
|
+
`,
|
|
527
|
+
options
|
|
528
|
+
)
|
|
529
|
+
).toMatchInlineSnapshot(`
|
|
530
|
+
"import stylex from 'stylex';
|
|
531
|
+
stylex.inject(".x1e2nbdu{color:red}", 1);
|
|
532
|
+
stylex.inject(".xju2f9n{color:blue}", 1);
|
|
533
|
+
"FooBar__styles.default" + (isActive ? " xju2f9n FooBar__styles.active" : " x1e2nbdu");"
|
|
534
|
+
`);
|
|
535
|
+
});
|
|
536
|
+
});
|
|
537
|
+
});
|
|
538
|
+
describe('Keep stylex.create when needed', () => {
|
|
539
|
+
test('stylex call with computed key access', () => {
|
|
540
|
+
expect(
|
|
541
|
+
transform(`
|
|
542
|
+
import stylex from 'stylex';
|
|
543
|
+
const styles = stylex.create({
|
|
544
|
+
[0]: {
|
|
545
|
+
color: 'red',
|
|
546
|
+
},
|
|
547
|
+
[1]: {
|
|
548
|
+
backgroundColor: 'blue',
|
|
549
|
+
}
|
|
550
|
+
});
|
|
551
|
+
stylex(styles[variant]);
|
|
552
|
+
`)
|
|
553
|
+
).toMatchInlineSnapshot(`
|
|
554
|
+
"import stylex from 'stylex';
|
|
555
|
+
stylex.inject(".x1e2nbdu{color:red}", 1);
|
|
556
|
+
stylex.inject(".x1t391ir{background-color:blue}", 1);
|
|
557
|
+
const styles = {
|
|
558
|
+
"0": {
|
|
559
|
+
color: "x1e2nbdu"
|
|
560
|
+
},
|
|
561
|
+
"1": {
|
|
562
|
+
backgroundColor: "x1t391ir"
|
|
563
|
+
}
|
|
564
|
+
};
|
|
565
|
+
stylex(styles[variant]);"
|
|
566
|
+
`);
|
|
567
|
+
});
|
|
568
|
+
test('stylex call with composition of external styles', () => {
|
|
569
|
+
expect(
|
|
570
|
+
transform(`
|
|
571
|
+
import stylex from 'stylex';
|
|
572
|
+
const styles = stylex.create({
|
|
573
|
+
default: {
|
|
574
|
+
color: 'red',
|
|
575
|
+
},
|
|
576
|
+
});
|
|
577
|
+
stylex(styles.default, props);
|
|
578
|
+
`)
|
|
579
|
+
).toMatchInlineSnapshot(`
|
|
580
|
+
"import stylex from 'stylex';
|
|
581
|
+
stylex.inject(".x1e2nbdu{color:red}", 1);
|
|
582
|
+
const styles = {
|
|
583
|
+
default: {
|
|
584
|
+
color: "x1e2nbdu"
|
|
585
|
+
}
|
|
586
|
+
};
|
|
587
|
+
stylex(styles.default, props);"
|
|
588
|
+
`);
|
|
589
|
+
});
|
|
590
|
+
|
|
591
|
+
test('stylex call using exported styles with pseudo selectors, and queries', () => {
|
|
592
|
+
expect(
|
|
593
|
+
transform(`
|
|
594
|
+
import stylex from 'stylex';
|
|
595
|
+
export const styles = stylex.create({
|
|
596
|
+
default: {
|
|
597
|
+
':hover': {
|
|
598
|
+
color: 'blue',
|
|
599
|
+
},
|
|
600
|
+
'@media (min-width: 1000px)': {
|
|
601
|
+
backgroundColor: 'blue',
|
|
602
|
+
},
|
|
603
|
+
}
|
|
604
|
+
});
|
|
605
|
+
stylex(styles.default);
|
|
606
|
+
`)
|
|
607
|
+
).toMatchInlineSnapshot(`
|
|
608
|
+
"import stylex from 'stylex';
|
|
609
|
+
stylex.inject(".x17z2mba:hover{color:blue}", 8);
|
|
610
|
+
stylex.inject("@media (min-width: 1000px){.xc445zv.xc445zv{background-color:blue}}", 2);
|
|
611
|
+
export const styles = {
|
|
612
|
+
default: {
|
|
613
|
+
":hover_color": "x17z2mba",
|
|
614
|
+
"@media (min-width: 1000px)_backgroundColor": "xc445zv"
|
|
615
|
+
}
|
|
616
|
+
};
|
|
617
|
+
"x17z2mba xc445zv";"
|
|
618
|
+
`);
|
|
619
|
+
});
|
|
620
|
+
|
|
621
|
+
describe('even when stylex calls come first', () => {
|
|
622
|
+
test('stylex call with computed key access', () => {
|
|
623
|
+
expect(
|
|
624
|
+
transform(`
|
|
625
|
+
import stylex from 'stylex';
|
|
626
|
+
stylex(styles[variant]);
|
|
627
|
+
const styles = stylex.create({
|
|
628
|
+
[0]: {
|
|
629
|
+
color: 'red',
|
|
630
|
+
},
|
|
631
|
+
[1]: {
|
|
632
|
+
backgroundColor: 'blue',
|
|
633
|
+
}
|
|
634
|
+
});
|
|
635
|
+
`)
|
|
636
|
+
).toMatchInlineSnapshot(`
|
|
637
|
+
"import stylex from 'stylex';
|
|
638
|
+
stylex(styles[variant]);
|
|
639
|
+
stylex.inject(".x1e2nbdu{color:red}", 1);
|
|
640
|
+
stylex.inject(".x1t391ir{background-color:blue}", 1);
|
|
641
|
+
const styles = {
|
|
642
|
+
"0": {
|
|
643
|
+
color: "x1e2nbdu"
|
|
644
|
+
},
|
|
645
|
+
"1": {
|
|
646
|
+
backgroundColor: "x1t391ir"
|
|
647
|
+
}
|
|
648
|
+
};"
|
|
649
|
+
`);
|
|
650
|
+
});
|
|
651
|
+
|
|
652
|
+
test('stylex call with mixed access', () => {
|
|
653
|
+
expect(
|
|
654
|
+
transform(`
|
|
655
|
+
import stylex from 'stylex';
|
|
656
|
+
|
|
657
|
+
function MyComponent() {
|
|
658
|
+
return (
|
|
659
|
+
<>
|
|
660
|
+
<div className={stylex(styles.foo)} />
|
|
661
|
+
<div className={stylex(styles.bar)} />
|
|
662
|
+
<CustomComponent xstyle={styles.foo} />
|
|
663
|
+
<div className={stylex(styles.foo, styles.bar)} />
|
|
664
|
+
</>
|
|
665
|
+
);
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
const styles = stylex.create({
|
|
669
|
+
foo: {
|
|
670
|
+
color: 'red',
|
|
671
|
+
},
|
|
672
|
+
bar: {
|
|
673
|
+
backgroundColor: 'blue',
|
|
674
|
+
}
|
|
675
|
+
});
|
|
676
|
+
`)
|
|
677
|
+
).toMatchInlineSnapshot(`
|
|
678
|
+
"import stylex from 'stylex';
|
|
679
|
+
function MyComponent() {
|
|
680
|
+
return <>
|
|
681
|
+
<div className={"x1e2nbdu"} />
|
|
682
|
+
<div className={"x1t391ir"} />
|
|
683
|
+
<CustomComponent xstyle={styles.foo} />
|
|
684
|
+
<div className={"x1e2nbdu x1t391ir"} />
|
|
685
|
+
</>;
|
|
686
|
+
}
|
|
687
|
+
stylex.inject(".x1e2nbdu{color:red}", 1);
|
|
688
|
+
stylex.inject(".x1t391ir{background-color:blue}", 1);
|
|
689
|
+
const styles = {
|
|
690
|
+
foo: {
|
|
691
|
+
color: "x1e2nbdu"
|
|
692
|
+
},
|
|
693
|
+
bar: {
|
|
694
|
+
backgroundColor: "x1t391ir"
|
|
695
|
+
}
|
|
696
|
+
};"
|
|
697
|
+
`);
|
|
698
|
+
});
|
|
699
|
+
test('stylex call with composition of external styles', () => {
|
|
700
|
+
expect(
|
|
701
|
+
transform(`
|
|
702
|
+
import stylex from 'stylex';
|
|
703
|
+
stylex(styles.default, props);
|
|
704
|
+
const styles = stylex.create({
|
|
705
|
+
default: {
|
|
706
|
+
color: 'red',
|
|
707
|
+
},
|
|
708
|
+
});
|
|
709
|
+
`)
|
|
710
|
+
).toMatchInlineSnapshot(`
|
|
711
|
+
"import stylex from 'stylex';
|
|
712
|
+
stylex(styles.default, props);
|
|
713
|
+
stylex.inject(".x1e2nbdu{color:red}", 1);
|
|
714
|
+
const styles = {
|
|
715
|
+
default: {
|
|
716
|
+
color: "x1e2nbdu"
|
|
717
|
+
}
|
|
718
|
+
};"
|
|
719
|
+
`);
|
|
720
|
+
});
|
|
721
|
+
|
|
722
|
+
test('stylex call using exported styles with pseudo selectors, and queries', () => {
|
|
723
|
+
expect(
|
|
724
|
+
transform(`
|
|
725
|
+
import stylex from 'stylex';
|
|
726
|
+
stylex(styles.default);
|
|
727
|
+
export const styles = stylex.create({
|
|
728
|
+
default: {
|
|
729
|
+
':hover': {
|
|
730
|
+
color: 'blue',
|
|
731
|
+
},
|
|
732
|
+
'@media (min-width: 1000px)': {
|
|
733
|
+
backgroundColor: 'blue',
|
|
734
|
+
},
|
|
735
|
+
}
|
|
736
|
+
});
|
|
737
|
+
`)
|
|
738
|
+
).toMatchInlineSnapshot(`
|
|
739
|
+
"import stylex from 'stylex';
|
|
740
|
+
"x17z2mba xc445zv";
|
|
741
|
+
stylex.inject(".x17z2mba:hover{color:blue}", 8);
|
|
742
|
+
stylex.inject("@media (min-width: 1000px){.xc445zv.xc445zv{background-color:blue}}", 2);
|
|
743
|
+
export const styles = {
|
|
744
|
+
default: {
|
|
745
|
+
":hover_color": "x17z2mba",
|
|
746
|
+
"@media (min-width: 1000px)_backgroundColor": "xc445zv"
|
|
747
|
+
}
|
|
748
|
+
};"
|
|
749
|
+
`);
|
|
750
|
+
});
|
|
751
|
+
});
|
|
752
|
+
});
|
|
753
|
+
describe('Setting custom import paths', () => {
|
|
754
|
+
test('Basic stylex call', () => {
|
|
755
|
+
expect(
|
|
756
|
+
transform(
|
|
757
|
+
`
|
|
758
|
+
import stylex from 'custom-stylex-path';
|
|
759
|
+
const styles = stylex.create({
|
|
760
|
+
red: {
|
|
761
|
+
color: 'red',
|
|
762
|
+
}
|
|
763
|
+
});
|
|
764
|
+
stylex(styles.red);
|
|
765
|
+
`,
|
|
766
|
+
{ importSources: ['custom-stylex-path'] }
|
|
767
|
+
)
|
|
768
|
+
).toMatchInlineSnapshot(`
|
|
769
|
+
"import stylex from 'custom-stylex-path';
|
|
770
|
+
stylex.inject(".x1e2nbdu{color:red}", 1);
|
|
771
|
+
"x1e2nbdu";"
|
|
772
|
+
`);
|
|
773
|
+
});
|
|
774
|
+
});
|
|
775
|
+
});
|