@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,310 @@
|
|
|
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
|
+
|
|
15
|
+
function transform(source, opts = {}) {
|
|
16
|
+
return transformSync(source, {
|
|
17
|
+
filename: opts.filename,
|
|
18
|
+
parserOpts: {
|
|
19
|
+
flow: {
|
|
20
|
+
all: true,
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
plugins: [[stylexPlugin, opts]],
|
|
24
|
+
}).code;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
describe('@stylexjs/babel-plugin', () => {
|
|
28
|
+
describe('[transform] stylex imports', () => {
|
|
29
|
+
test('ignores valid imports', () => {
|
|
30
|
+
expect(
|
|
31
|
+
transform(`
|
|
32
|
+
import stylex from 'stylex';
|
|
33
|
+
import {foo, bar} from 'other';
|
|
34
|
+
`)
|
|
35
|
+
).toMatchInlineSnapshot(`
|
|
36
|
+
"import stylex from 'stylex';
|
|
37
|
+
import { foo, bar } from 'other';"
|
|
38
|
+
`);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test('ignores valid requires', () => {
|
|
42
|
+
expect(
|
|
43
|
+
transform(`
|
|
44
|
+
const stylex = require('stylex');
|
|
45
|
+
const {foo, bar} = require('other');
|
|
46
|
+
`)
|
|
47
|
+
).toMatchInlineSnapshot(`
|
|
48
|
+
"const stylex = require('stylex');
|
|
49
|
+
const {
|
|
50
|
+
foo,
|
|
51
|
+
bar
|
|
52
|
+
} = require('other');"
|
|
53
|
+
`);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test('named declaration export', () => {
|
|
57
|
+
expect(
|
|
58
|
+
transform(`
|
|
59
|
+
import stylex from 'stylex';
|
|
60
|
+
export const styles = stylex.create({
|
|
61
|
+
foo: {
|
|
62
|
+
color: 'red'
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
`)
|
|
66
|
+
).toMatchInlineSnapshot(`
|
|
67
|
+
"import stylex from 'stylex';
|
|
68
|
+
stylex.inject(".x1e2nbdu{color:red}", 1);
|
|
69
|
+
export const styles = {
|
|
70
|
+
foo: {
|
|
71
|
+
color: "x1e2nbdu"
|
|
72
|
+
}
|
|
73
|
+
};"
|
|
74
|
+
`);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test('Does nothing when stylex not imported', () => {
|
|
78
|
+
expect(
|
|
79
|
+
transform(`
|
|
80
|
+
export const styles = stylex.create({
|
|
81
|
+
foo: {
|
|
82
|
+
color: 'red'
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
`)
|
|
86
|
+
).toMatchInlineSnapshot(`
|
|
87
|
+
"export const styles = stylex.create({
|
|
88
|
+
foo: {
|
|
89
|
+
color: 'red'
|
|
90
|
+
}
|
|
91
|
+
});"
|
|
92
|
+
`);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test('named property export', () => {
|
|
96
|
+
expect(
|
|
97
|
+
transform(`
|
|
98
|
+
import stylex from 'stylex';
|
|
99
|
+
const styles = stylex.create({
|
|
100
|
+
foo: {
|
|
101
|
+
color: 'red'
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
export {styles}
|
|
105
|
+
`)
|
|
106
|
+
).toMatchInlineSnapshot(`
|
|
107
|
+
"import stylex from 'stylex';
|
|
108
|
+
stylex.inject(".x1e2nbdu{color:red}", 1);
|
|
109
|
+
const styles = {
|
|
110
|
+
foo: {
|
|
111
|
+
color: "x1e2nbdu"
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
export { styles };"
|
|
115
|
+
`);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test('default export', () => {
|
|
119
|
+
expect(
|
|
120
|
+
transform(`
|
|
121
|
+
import stylex from 'stylex';
|
|
122
|
+
export default (stylex.create({
|
|
123
|
+
foo: {
|
|
124
|
+
color: 'red'
|
|
125
|
+
},
|
|
126
|
+
}));
|
|
127
|
+
`)
|
|
128
|
+
).toMatchInlineSnapshot(`
|
|
129
|
+
"import stylex from 'stylex';
|
|
130
|
+
stylex.inject(".x1e2nbdu{color:red}", 1);
|
|
131
|
+
export default {
|
|
132
|
+
foo: {
|
|
133
|
+
color: "x1e2nbdu"
|
|
134
|
+
}
|
|
135
|
+
};"
|
|
136
|
+
`);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
test('module.export', () => {
|
|
140
|
+
expect(
|
|
141
|
+
transform(`
|
|
142
|
+
import stylex from 'stylex';
|
|
143
|
+
const styles = stylex.create({
|
|
144
|
+
foo: {
|
|
145
|
+
color: 'red'
|
|
146
|
+
},
|
|
147
|
+
});
|
|
148
|
+
module.export = styles;
|
|
149
|
+
`)
|
|
150
|
+
).toMatchInlineSnapshot(`
|
|
151
|
+
"import stylex from 'stylex';
|
|
152
|
+
stylex.inject(".x1e2nbdu{color:red}", 1);
|
|
153
|
+
const styles = {
|
|
154
|
+
foo: {
|
|
155
|
+
color: "x1e2nbdu"
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
module.export = styles;"
|
|
159
|
+
`);
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
describe('[transform] import aliases', () => {
|
|
164
|
+
test('can import with a different name ', () => {
|
|
165
|
+
expect(
|
|
166
|
+
transform(`
|
|
167
|
+
import foobar from 'stylex';
|
|
168
|
+
|
|
169
|
+
const styles = foobar.create({
|
|
170
|
+
default: {
|
|
171
|
+
backgroundColor: 'red',
|
|
172
|
+
color: 'blue',
|
|
173
|
+
padding: 5
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
styles;
|
|
177
|
+
`)
|
|
178
|
+
).toMatchInlineSnapshot(`
|
|
179
|
+
"import foobar from 'stylex';
|
|
180
|
+
foobar.inject(".xrkmrrc{background-color:red}", 1);
|
|
181
|
+
foobar.inject(".xju2f9n{color:blue}", 1);
|
|
182
|
+
foobar.inject(".x123j3cw{padding-top:5px}", 1);
|
|
183
|
+
foobar.inject(".x1mpkggp{padding-right:5px}", 1, ".x1mpkggp{padding-left:5px}");
|
|
184
|
+
foobar.inject(".xs9asl8{padding-bottom:5px}", 1);
|
|
185
|
+
foobar.inject(".x1t2a60a{padding-left:5px}", 1, ".x1t2a60a{padding-right:5px}");
|
|
186
|
+
const styles = {
|
|
187
|
+
default: {
|
|
188
|
+
backgroundColor: "xrkmrrc",
|
|
189
|
+
color: "xju2f9n",
|
|
190
|
+
paddingTop: "x123j3cw",
|
|
191
|
+
paddingEnd: "x1mpkggp",
|
|
192
|
+
paddingBottom: "xs9asl8",
|
|
193
|
+
paddingStart: "x1t2a60a"
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
styles;"
|
|
197
|
+
`);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
test('can import wildcard', () => {
|
|
201
|
+
expect(
|
|
202
|
+
transform(`
|
|
203
|
+
import * as foobar from 'stylex';
|
|
204
|
+
|
|
205
|
+
const styles = foobar.create({
|
|
206
|
+
default: {
|
|
207
|
+
backgroundColor: 'red',
|
|
208
|
+
color: 'blue',
|
|
209
|
+
padding: 5
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
styles;
|
|
213
|
+
`)
|
|
214
|
+
).toMatchInlineSnapshot(`
|
|
215
|
+
"import * as foobar from 'stylex';
|
|
216
|
+
foobar.inject(".xrkmrrc{background-color:red}", 1);
|
|
217
|
+
foobar.inject(".xju2f9n{color:blue}", 1);
|
|
218
|
+
foobar.inject(".x123j3cw{padding-top:5px}", 1);
|
|
219
|
+
foobar.inject(".x1mpkggp{padding-right:5px}", 1, ".x1mpkggp{padding-left:5px}");
|
|
220
|
+
foobar.inject(".xs9asl8{padding-bottom:5px}", 1);
|
|
221
|
+
foobar.inject(".x1t2a60a{padding-left:5px}", 1, ".x1t2a60a{padding-right:5px}");
|
|
222
|
+
const styles = {
|
|
223
|
+
default: {
|
|
224
|
+
backgroundColor: "xrkmrrc",
|
|
225
|
+
color: "xju2f9n",
|
|
226
|
+
paddingTop: "x123j3cw",
|
|
227
|
+
paddingEnd: "x1mpkggp",
|
|
228
|
+
paddingBottom: "xs9asl8",
|
|
229
|
+
paddingStart: "x1t2a60a"
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
styles;"
|
|
233
|
+
`);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
test('can import just {create}', () => {
|
|
237
|
+
expect(
|
|
238
|
+
transform(`
|
|
239
|
+
import {create} from 'stylex';
|
|
240
|
+
|
|
241
|
+
const styles = create({
|
|
242
|
+
default: {
|
|
243
|
+
backgroundColor: 'red',
|
|
244
|
+
color: 'blue',
|
|
245
|
+
padding: 5
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
styles;
|
|
249
|
+
`)
|
|
250
|
+
).toMatchInlineSnapshot(`
|
|
251
|
+
"import { create } from 'stylex';
|
|
252
|
+
import __stylex__ from "stylex";
|
|
253
|
+
__stylex__.inject(".xrkmrrc{background-color:red}", 1);
|
|
254
|
+
__stylex__.inject(".xju2f9n{color:blue}", 1);
|
|
255
|
+
__stylex__.inject(".x123j3cw{padding-top:5px}", 1);
|
|
256
|
+
__stylex__.inject(".x1mpkggp{padding-right:5px}", 1, ".x1mpkggp{padding-left:5px}");
|
|
257
|
+
__stylex__.inject(".xs9asl8{padding-bottom:5px}", 1);
|
|
258
|
+
__stylex__.inject(".x1t2a60a{padding-left:5px}", 1, ".x1t2a60a{padding-right:5px}");
|
|
259
|
+
const styles = {
|
|
260
|
+
default: {
|
|
261
|
+
backgroundColor: "xrkmrrc",
|
|
262
|
+
color: "xju2f9n",
|
|
263
|
+
paddingTop: "x123j3cw",
|
|
264
|
+
paddingEnd: "x1mpkggp",
|
|
265
|
+
paddingBottom: "xs9asl8",
|
|
266
|
+
paddingStart: "x1t2a60a"
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
styles;"
|
|
270
|
+
`);
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
test('can import just {create} with alias', () => {
|
|
274
|
+
expect(
|
|
275
|
+
transform(`
|
|
276
|
+
import {create as css} from 'stylex';
|
|
277
|
+
|
|
278
|
+
const styles = css({
|
|
279
|
+
default: {
|
|
280
|
+
backgroundColor: 'red',
|
|
281
|
+
color: 'blue',
|
|
282
|
+
padding: 5
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
styles;
|
|
286
|
+
`)
|
|
287
|
+
).toMatchInlineSnapshot(`
|
|
288
|
+
"import { create as css } from 'stylex';
|
|
289
|
+
import __stylex__ from "stylex";
|
|
290
|
+
__stylex__.inject(".xrkmrrc{background-color:red}", 1);
|
|
291
|
+
__stylex__.inject(".xju2f9n{color:blue}", 1);
|
|
292
|
+
__stylex__.inject(".x123j3cw{padding-top:5px}", 1);
|
|
293
|
+
__stylex__.inject(".x1mpkggp{padding-right:5px}", 1, ".x1mpkggp{padding-left:5px}");
|
|
294
|
+
__stylex__.inject(".xs9asl8{padding-bottom:5px}", 1);
|
|
295
|
+
__stylex__.inject(".x1t2a60a{padding-left:5px}", 1, ".x1t2a60a{padding-right:5px}");
|
|
296
|
+
const styles = {
|
|
297
|
+
default: {
|
|
298
|
+
backgroundColor: "xrkmrrc",
|
|
299
|
+
color: "xju2f9n",
|
|
300
|
+
paddingTop: "x123j3cw",
|
|
301
|
+
paddingEnd: "x1mpkggp",
|
|
302
|
+
paddingBottom: "xs9asl8",
|
|
303
|
+
paddingStart: "x1t2a60a"
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
styles;"
|
|
307
|
+
`);
|
|
308
|
+
});
|
|
309
|
+
});
|
|
310
|
+
});
|
|
@@ -0,0 +1,135 @@
|
|
|
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
|
+
|
|
15
|
+
function transform(source, opts = {}) {
|
|
16
|
+
return transformSync(source, {
|
|
17
|
+
filename: opts.filename,
|
|
18
|
+
parserOpts: {
|
|
19
|
+
flow: {
|
|
20
|
+
all: true,
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
plugins: [[stylexPlugin, opts]],
|
|
24
|
+
}).code;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
describe('@stylexjs/babel-plugin', () => {
|
|
28
|
+
describe('[transform] CSS keyframes', () => {
|
|
29
|
+
test('converts keyframes to CSS', () => {
|
|
30
|
+
expect(
|
|
31
|
+
transform(`
|
|
32
|
+
import stylex from 'stylex';
|
|
33
|
+
const name = stylex.keyframes({
|
|
34
|
+
from: {
|
|
35
|
+
backgroundColor: 'red',
|
|
36
|
+
},
|
|
37
|
+
|
|
38
|
+
to: {
|
|
39
|
+
backgroundColor: 'blue',
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
`)
|
|
43
|
+
).toMatchInlineSnapshot(`
|
|
44
|
+
"import stylex from 'stylex';
|
|
45
|
+
stylex.inject("@keyframes xbopttm-B{from{background-color:red;}to{background-color:blue;}}", 1);
|
|
46
|
+
const name = "xbopttm-B";"
|
|
47
|
+
`);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test('allows template literal references to keyframes', () => {
|
|
51
|
+
expect(
|
|
52
|
+
transform(`
|
|
53
|
+
import stylex from 'stylex';
|
|
54
|
+
const name = stylex.keyframes({
|
|
55
|
+
from: {
|
|
56
|
+
backgroundColor: 'blue',
|
|
57
|
+
},
|
|
58
|
+
to: {
|
|
59
|
+
backgroundColor: 'red',
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
const styles = stylex.create({
|
|
64
|
+
default: {
|
|
65
|
+
animation: \`3s \${name}\`,
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
`)
|
|
69
|
+
).toMatchInlineSnapshot(`
|
|
70
|
+
"import stylex from 'stylex';
|
|
71
|
+
stylex.inject("@keyframes x3zqmp-B{from{background-color:blue;}to{background-color:red;}}", 1);
|
|
72
|
+
const name = "x3zqmp-B";
|
|
73
|
+
stylex.inject(".x1qs41r0{animation:3s x3zqmp-B}", 1);"
|
|
74
|
+
`);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test('allows inline references to keyframes', () => {
|
|
78
|
+
expect(
|
|
79
|
+
transform(`
|
|
80
|
+
import stylex from 'stylex';
|
|
81
|
+
|
|
82
|
+
const styles = stylex.create({
|
|
83
|
+
default: {
|
|
84
|
+
animationName: stylex.keyframes({
|
|
85
|
+
from: {
|
|
86
|
+
backgroundColor: 'blue',
|
|
87
|
+
},
|
|
88
|
+
to: {
|
|
89
|
+
backgroundColor: 'red',
|
|
90
|
+
},
|
|
91
|
+
}),
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
`)
|
|
95
|
+
).toMatchInlineSnapshot(`
|
|
96
|
+
"import stylex from 'stylex';
|
|
97
|
+
stylex.inject("@keyframes x3zqmp-B{from{background-color:blue;}to{background-color:red;}}", 1);
|
|
98
|
+
stylex.inject(".xcoz2pf{animation-name:x3zqmp-B}", 1);"
|
|
99
|
+
`);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test('generates RTL-specific keyframes', () => {
|
|
103
|
+
expect(
|
|
104
|
+
transform(`
|
|
105
|
+
import stylex from 'stylex';
|
|
106
|
+
const name = stylex.keyframes({
|
|
107
|
+
from: {
|
|
108
|
+
start: 0,
|
|
109
|
+
},
|
|
110
|
+
|
|
111
|
+
to: {
|
|
112
|
+
start: 500,
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
export const styles = stylex.create({
|
|
117
|
+
root: {
|
|
118
|
+
animationName: name,
|
|
119
|
+
},
|
|
120
|
+
});
|
|
121
|
+
`)
|
|
122
|
+
).toMatchInlineSnapshot(`
|
|
123
|
+
"import stylex from 'stylex';
|
|
124
|
+
stylex.inject("@keyframes x1lvx8r0-B{from{left:0;}to{left:500px;}}", 1, "@keyframes x1lvx8r0-B{from{right:0;}to{right:500px;}}");
|
|
125
|
+
const name = "x1lvx8r0-B";
|
|
126
|
+
stylex.inject(".x1ugarde{animation-name:x1lvx8r0-B}", 1);
|
|
127
|
+
export const styles = {
|
|
128
|
+
root: {
|
|
129
|
+
animationName: "x1ugarde"
|
|
130
|
+
}
|
|
131
|
+
};"
|
|
132
|
+
`);
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
});
|