@stylexjs/babel-plugin 0.1.0-beta.3 → 0.1.0-beta.4
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/lib/index.js +2968 -0
- package/package.json +8 -5
- package/.babelrc +0 -16
- package/__tests__/stylex-evaluation-test.js +0 -164
- package/__tests__/stylex-metadata-test.js +0 -103
- package/__tests__/stylex-transform-call-test.js +0 -775
- package/__tests__/stylex-transform-create-test.js +0 -551
- package/__tests__/stylex-transform-import-test.js +0 -310
- package/__tests__/stylex-transform-keyframes-test.js +0 -135
- package/__tests__/stylex-transform-logical-properties-test.js +0 -736
- package/__tests__/stylex-transform-logical-values-test.js +0 -440
- package/__tests__/stylex-transform-polyfills-test.js +0 -65
- package/__tests__/stylex-transform-value-normalize-test.js +0 -287
- package/__tests__/stylex-transform-variable-removal-test.js +0 -112
- package/__tests__/stylex-validation-create-test.ts +0 -310
- package/__tests__/stylex-validation-custom-properties-test.ts +0 -122
- package/__tests__/stylex-validation-declarations-test.ts +0 -129
- package/__tests__/stylex-validation-import-test.ts +0 -57
- package/__tests__/stylex-validation-keyframes-test.ts +0 -109
- package/jest.config.ts +0 -20
- package/rollup.config.mjs +0 -30
- package/tsconfig.json +0 -103
package/lib/index.js
ADDED
|
@@ -0,0 +1,2968 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var t = require('@babel/types');
|
|
6
|
+
var require$$0 = require('postcss-value-parser');
|
|
7
|
+
var path = require('path');
|
|
8
|
+
|
|
9
|
+
function _interopNamespaceDefault(e) {
|
|
10
|
+
var n = Object.create(null);
|
|
11
|
+
if (e) {
|
|
12
|
+
Object.keys(e).forEach(function (k) {
|
|
13
|
+
if (k !== 'default') {
|
|
14
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
15
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
get: function () { return e[k]; }
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
n.default = e;
|
|
23
|
+
return Object.freeze(n);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
var t__namespace = /*#__PURE__*/_interopNamespaceDefault(t);
|
|
27
|
+
|
|
28
|
+
function _defineProperty(obj, key, value) {
|
|
29
|
+
if (key in obj) {
|
|
30
|
+
Object.defineProperty(obj, key, {
|
|
31
|
+
value: value,
|
|
32
|
+
enumerable: true,
|
|
33
|
+
configurable: true,
|
|
34
|
+
writable: true
|
|
35
|
+
});
|
|
36
|
+
} else {
|
|
37
|
+
obj[key] = value;
|
|
38
|
+
}
|
|
39
|
+
return obj;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
var name = "@stylexjs/stylex";
|
|
43
|
+
|
|
44
|
+
class StateManager {
|
|
45
|
+
// Imports
|
|
46
|
+
|
|
47
|
+
// `stylex.create` calls
|
|
48
|
+
|
|
49
|
+
// resuls of `stylex.create` calls that should be kept
|
|
50
|
+
|
|
51
|
+
constructor(state) {
|
|
52
|
+
_defineProperty(this, "_state", void 0);
|
|
53
|
+
_defineProperty(this, "stylexImport", new Set());
|
|
54
|
+
_defineProperty(this, "stylexCreateImport", new Set());
|
|
55
|
+
_defineProperty(this, "stylexIncludeImport", new Set());
|
|
56
|
+
_defineProperty(this, "stylexFirstThatWorksImport", new Set());
|
|
57
|
+
_defineProperty(this, "stylexKeyframesImport", new Set());
|
|
58
|
+
_defineProperty(this, "styleMap", new Map());
|
|
59
|
+
_defineProperty(this, "styleVars", new Map());
|
|
60
|
+
_defineProperty(this, "styleVarsToKeep", new Set());
|
|
61
|
+
this._state = state;
|
|
62
|
+
state.file.metadata.stylex = [];
|
|
63
|
+
}
|
|
64
|
+
get options() {
|
|
65
|
+
const options = this._state.opts || {};
|
|
66
|
+
this._state.opts = {
|
|
67
|
+
...options,
|
|
68
|
+
dev: !!options.dev,
|
|
69
|
+
test: !!options.test,
|
|
70
|
+
stylexSheetName: options.stylexSheetName ?? undefined,
|
|
71
|
+
classNamePrefix: options.classNamePrefix ?? 'x',
|
|
72
|
+
importSources: options.importSources ?? [name, 'stylex'],
|
|
73
|
+
definedStylexCSSVariables: options.definedStylexCSSVariables ?? {}
|
|
74
|
+
};
|
|
75
|
+
return this._state.opts;
|
|
76
|
+
}
|
|
77
|
+
get metadata() {
|
|
78
|
+
return this._state.file.metadata;
|
|
79
|
+
}
|
|
80
|
+
get stylexSheetName() {
|
|
81
|
+
return this.options.stylexSheetName ?? undefined;
|
|
82
|
+
}
|
|
83
|
+
get isDev() {
|
|
84
|
+
return !!this.options.dev;
|
|
85
|
+
}
|
|
86
|
+
get isTest() {
|
|
87
|
+
return !!this.options.test;
|
|
88
|
+
}
|
|
89
|
+
get filename() {
|
|
90
|
+
return this._state.filename;
|
|
91
|
+
}
|
|
92
|
+
get cssVars() {
|
|
93
|
+
return this.options.definedStylexCSSVariables;
|
|
94
|
+
}
|
|
95
|
+
addStyle(style) {
|
|
96
|
+
this.metadata.stylex.push(style);
|
|
97
|
+
}
|
|
98
|
+
markComposedNamespace(memberExpression) {
|
|
99
|
+
this.styleVarsToKeep.add(memberExpression);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
105
|
+
*
|
|
106
|
+
* This source code is licensed under the MIT license found in the
|
|
107
|
+
* LICENSE file in the root directory of this source tree.
|
|
108
|
+
* @flow strict
|
|
109
|
+
*/
|
|
110
|
+
|
|
111
|
+
// Read imports of react and remember the name of the local varsiables for later
|
|
112
|
+
function readImportDeclarations(path, state) {
|
|
113
|
+
const {
|
|
114
|
+
node
|
|
115
|
+
} = path;
|
|
116
|
+
if ((node === null || node === void 0 ? void 0 : node.importKind) === 'type' || (node === null || node === void 0 ? void 0 : node.importKind) === 'typeof') {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
if (state.options.importSources.includes(node.source.value)) {
|
|
120
|
+
for (const specifier of node.specifiers) {
|
|
121
|
+
if (specifier.type === 'ImportDefaultSpecifier') {
|
|
122
|
+
state.stylexImport.add(specifier.local.name);
|
|
123
|
+
}
|
|
124
|
+
if (specifier.type === 'ImportNamespaceSpecifier') {
|
|
125
|
+
state.stylexImport.add(specifier.local.name);
|
|
126
|
+
}
|
|
127
|
+
if (specifier.type === 'ImportSpecifier') {
|
|
128
|
+
if (specifier.imported.type === 'Identifier') {
|
|
129
|
+
if (specifier.imported.name === 'create') {
|
|
130
|
+
state.stylexCreateImport.add(specifier.local.name);
|
|
131
|
+
}
|
|
132
|
+
if (specifier.imported.name === 'keyframes') {
|
|
133
|
+
state.stylexKeyframesImport.add(specifier.local.name);
|
|
134
|
+
}
|
|
135
|
+
if (specifier.imported.name === 'include') {
|
|
136
|
+
state.stylexIncludeImport.add(specifier.local.name);
|
|
137
|
+
}
|
|
138
|
+
if (specifier.imported.name === 'firstThatWorks') {
|
|
139
|
+
state.stylexFirstThatWorksImport.add(specifier.local.name);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
if (specifier.imported.type === 'StringLiteral') {
|
|
143
|
+
if (specifier.imported.value === 'create') {
|
|
144
|
+
state.stylexCreateImport.add(specifier.local.name);
|
|
145
|
+
}
|
|
146
|
+
if (specifier.imported.value === 'keyframes') {
|
|
147
|
+
state.stylexKeyframesImport.add(specifier.local.name);
|
|
148
|
+
}
|
|
149
|
+
if (specifier.imported.value === 'include') {
|
|
150
|
+
state.stylexIncludeImport.add(specifier.local.name);
|
|
151
|
+
}
|
|
152
|
+
if (specifier.imported.value === 'firstThatWorks') {
|
|
153
|
+
state.stylexFirstThatWorksImport.add(specifier.local.name);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Read require calls and remember the names of the variables for later
|
|
162
|
+
function readRequires(path, state) {
|
|
163
|
+
var _node$init, _node$init2, _node$init2$callee, _node$init3, _node$init3$callee, _node$init4, _node$init4$arguments, _node$init5, _node$init5$arguments, _node$init6, _node$init6$arguments, _node$init7, _node$init7$arguments;
|
|
164
|
+
const {
|
|
165
|
+
node
|
|
166
|
+
} = path;
|
|
167
|
+
if (((_node$init = node.init) === null || _node$init === void 0 ? void 0 : _node$init.type) === 'CallExpression' && ((_node$init2 = node.init) === null || _node$init2 === void 0 ? void 0 : (_node$init2$callee = _node$init2.callee) === null || _node$init2$callee === void 0 ? void 0 : _node$init2$callee.type) === 'Identifier' && ((_node$init3 = node.init) === null || _node$init3 === void 0 ? void 0 : (_node$init3$callee = _node$init3.callee) === null || _node$init3$callee === void 0 ? void 0 : _node$init3$callee.name) === 'require' && ((_node$init4 = node.init) === null || _node$init4 === void 0 ? void 0 : (_node$init4$arguments = _node$init4.arguments) === null || _node$init4$arguments === void 0 ? void 0 : _node$init4$arguments.length) === 1 && ((_node$init5 = node.init) === null || _node$init5 === void 0 ? void 0 : (_node$init5$arguments = _node$init5.arguments) === null || _node$init5$arguments === void 0 ? void 0 : _node$init5$arguments[0].type) === 'StringLiteral' && (((_node$init6 = node.init) === null || _node$init6 === void 0 ? void 0 : (_node$init6$arguments = _node$init6.arguments) === null || _node$init6$arguments === void 0 ? void 0 : _node$init6$arguments[0].value) === 'stylex' || state.options.importSources.includes((_node$init7 = node.init) === null || _node$init7 === void 0 ? void 0 : (_node$init7$arguments = _node$init7.arguments) === null || _node$init7$arguments === void 0 ? void 0 : _node$init7$arguments[0].value))) {
|
|
168
|
+
if (node.id.type === 'Identifier') {
|
|
169
|
+
state.stylexImport.add(node.id.name);
|
|
170
|
+
}
|
|
171
|
+
if (node.id.type === 'ObjectPattern') {
|
|
172
|
+
for (const prop of node.id.properties) {
|
|
173
|
+
if (prop.type === 'ObjectProperty' && prop.key.type === 'Identifier' && prop.value.type === 'Identifier') {
|
|
174
|
+
if (prop.key.name === 'create') {
|
|
175
|
+
state.stylexCreateImport.add(prop.value.name);
|
|
176
|
+
}
|
|
177
|
+
if (prop.key.name === 'keyframes') {
|
|
178
|
+
state.stylexKeyframesImport.add(prop.value.name);
|
|
179
|
+
}
|
|
180
|
+
if (prop.key.name === 'include') {
|
|
181
|
+
state.stylexIncludeImport.add(prop.value.name);
|
|
182
|
+
}
|
|
183
|
+
if (prop.key.name === 'firstThatWorks') {
|
|
184
|
+
state.stylexFirstThatWorksImport.add(prop.value.name);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
var lib = {};
|
|
193
|
+
|
|
194
|
+
var stylexCreate = {};
|
|
195
|
+
|
|
196
|
+
var convertToClassName$1 = {};
|
|
197
|
+
|
|
198
|
+
var hash$1 = {};
|
|
199
|
+
|
|
200
|
+
Object.defineProperty(hash$1, "__esModule", {
|
|
201
|
+
value: true
|
|
202
|
+
});
|
|
203
|
+
hash$1.default = void 0;
|
|
204
|
+
/**
|
|
205
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
206
|
+
*
|
|
207
|
+
* This source code is licensed under the MIT license found in the
|
|
208
|
+
* LICENSE file in the root directory of this source tree.
|
|
209
|
+
*
|
|
210
|
+
*
|
|
211
|
+
*/
|
|
212
|
+
|
|
213
|
+
/* eslint-disable default-case */
|
|
214
|
+
/* eslint-disable no-fallthrough */
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* JS Implementation of MurmurHash2
|
|
218
|
+
*
|
|
219
|
+
* @author <a href="mailto:gary.court@gmail.com">Gary Court</a>
|
|
220
|
+
* @see http://github.com/garycourt/murmurhash-js
|
|
221
|
+
* @author <a href="mailto:aappleby@gmail.com">Austin Appleby</a>
|
|
222
|
+
* @see http://sites.google.com/site/murmurhash/
|
|
223
|
+
*
|
|
224
|
+
* @param {string} str ASCII only
|
|
225
|
+
* @param {number} seed Positive integer only
|
|
226
|
+
* @return {number} 32-bit positive integer hash
|
|
227
|
+
*/
|
|
228
|
+
function murmurhash2_32_gc(str, seed) {
|
|
229
|
+
let l = str.length,
|
|
230
|
+
h = seed ^ l,
|
|
231
|
+
i = 0,
|
|
232
|
+
k;
|
|
233
|
+
while (l >= 4) {
|
|
234
|
+
k = str.charCodeAt(i) & 0xff | (str.charCodeAt(++i) & 0xff) << 8 | (str.charCodeAt(++i) & 0xff) << 16 | (str.charCodeAt(++i) & 0xff) << 24;
|
|
235
|
+
k = (k & 0xffff) * 0x5bd1e995 + (((k >>> 16) * 0x5bd1e995 & 0xffff) << 16);
|
|
236
|
+
k ^= k >>> 24;
|
|
237
|
+
k = (k & 0xffff) * 0x5bd1e995 + (((k >>> 16) * 0x5bd1e995 & 0xffff) << 16);
|
|
238
|
+
h = (h & 0xffff) * 0x5bd1e995 + (((h >>> 16) * 0x5bd1e995 & 0xffff) << 16) ^ k;
|
|
239
|
+
l -= 4;
|
|
240
|
+
++i;
|
|
241
|
+
}
|
|
242
|
+
switch (l) {
|
|
243
|
+
case 3:
|
|
244
|
+
h ^= (str.charCodeAt(i + 2) & 0xff) << 16;
|
|
245
|
+
case 2:
|
|
246
|
+
h ^= (str.charCodeAt(i + 1) & 0xff) << 8;
|
|
247
|
+
case 1:
|
|
248
|
+
h ^= str.charCodeAt(i) & 0xff;
|
|
249
|
+
h = (h & 0xffff) * 0x5bd1e995 + (((h >>> 16) * 0x5bd1e995 & 0xffff) << 16);
|
|
250
|
+
}
|
|
251
|
+
h ^= h >>> 13;
|
|
252
|
+
h = (h & 0xffff) * 0x5bd1e995 + (((h >>> 16) * 0x5bd1e995 & 0xffff) << 16);
|
|
253
|
+
h ^= h >>> 15;
|
|
254
|
+
return h >>> 0;
|
|
255
|
+
}
|
|
256
|
+
const hash = str => murmurhash2_32_gc(str, 1).toString(36);
|
|
257
|
+
var _default = hash;
|
|
258
|
+
hash$1.default = _default;
|
|
259
|
+
|
|
260
|
+
var dashify$1 = {};
|
|
261
|
+
|
|
262
|
+
Object.defineProperty(dashify$1, "__esModule", {
|
|
263
|
+
value: true
|
|
264
|
+
});
|
|
265
|
+
dashify$1.default = dashify;
|
|
266
|
+
/**
|
|
267
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
268
|
+
*
|
|
269
|
+
* This source code is licensed under the MIT license found in the
|
|
270
|
+
* LICENSE file in the root directory of this source tree.
|
|
271
|
+
*
|
|
272
|
+
*/
|
|
273
|
+
|
|
274
|
+
function dashify(str) {
|
|
275
|
+
return str.replace(/(^|[a-z])([A-Z])/g, '$1-$2').toLowerCase();
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
var transformValue$1 = {};
|
|
279
|
+
|
|
280
|
+
var normalizeValue$1 = {};
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
284
|
+
*
|
|
285
|
+
* This source code is licensed under the MIT license found in the
|
|
286
|
+
* LICENSE file in the root directory of this source tree.
|
|
287
|
+
*
|
|
288
|
+
*
|
|
289
|
+
*/
|
|
290
|
+
|
|
291
|
+
const parser = require$$0;
|
|
292
|
+
const ROOT_FONT_SIZE = 16;
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Convert font sizes from absolute unit `px` to relative unit `rem`.
|
|
296
|
+
* This will allow developers to continue thinking and using what's familiar
|
|
297
|
+
* while we output font sizes that are adjustable
|
|
298
|
+
*/
|
|
299
|
+
var fontSizePxToRem = function convertFontSizeToRem(ast, key) {
|
|
300
|
+
if (key !== 'fontSize') {
|
|
301
|
+
return ast;
|
|
302
|
+
}
|
|
303
|
+
ast.walk(node => {
|
|
304
|
+
if (node.type !== 'word') {
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
const dimension = parser.unit(node.value);
|
|
308
|
+
if (dimension && dimension.unit === 'px') {
|
|
309
|
+
node.value = `${parseFloat(dimension.number) / ROOT_FONT_SIZE}rem`;
|
|
310
|
+
}
|
|
311
|
+
});
|
|
312
|
+
return ast;
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
var leadingZero = {};
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
319
|
+
*
|
|
320
|
+
* This source code is licensed under the MIT license found in the
|
|
321
|
+
* LICENSE file in the root directory of this source tree.
|
|
322
|
+
*
|
|
323
|
+
*
|
|
324
|
+
*/
|
|
325
|
+
|
|
326
|
+
Object.defineProperty(leadingZero, "__esModule", {
|
|
327
|
+
value: true
|
|
328
|
+
});
|
|
329
|
+
leadingZero.default = normalizeLeadingZero;
|
|
330
|
+
var _postcssValueParser$5 = _interopRequireDefault$b(require$$0);
|
|
331
|
+
function _interopRequireDefault$b(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
332
|
+
/**
|
|
333
|
+
* Remove leading zeros from numbers
|
|
334
|
+
*/
|
|
335
|
+
function normalizeLeadingZero(ast, _) {
|
|
336
|
+
ast.walk(node => {
|
|
337
|
+
if (node.type !== 'word') {
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
const value = Number.parseFloat(node.value);
|
|
341
|
+
if (Number.isNaN(value)) {
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
const dimension = _postcssValueParser$5.default.unit(node.value);
|
|
345
|
+
if (value < 1 && value >= 0) {
|
|
346
|
+
node.value = value.toString().replace('0.', '.') + (dimension ? dimension.unit : '');
|
|
347
|
+
}
|
|
348
|
+
});
|
|
349
|
+
return ast;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
var quotes = {};
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
356
|
+
*
|
|
357
|
+
* This source code is licensed under the MIT license found in the
|
|
358
|
+
* LICENSE file in the root directory of this source tree.
|
|
359
|
+
*
|
|
360
|
+
*
|
|
361
|
+
*
|
|
362
|
+
*/
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Make empty strings use consistent double quotes
|
|
366
|
+
*/
|
|
367
|
+
Object.defineProperty(quotes, "__esModule", {
|
|
368
|
+
value: true
|
|
369
|
+
});
|
|
370
|
+
quotes.default = normalizeQuotes;
|
|
371
|
+
function normalizeQuotes(ast, _) {
|
|
372
|
+
ast.walk(node => {
|
|
373
|
+
if (node.type !== 'string') {
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
if (node.value === '') {
|
|
377
|
+
node.quote = '"';
|
|
378
|
+
}
|
|
379
|
+
});
|
|
380
|
+
return ast;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
var timings$1 = {};
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
387
|
+
*
|
|
388
|
+
* This source code is licensed under the MIT license found in the
|
|
389
|
+
* LICENSE file in the root directory of this source tree.
|
|
390
|
+
*
|
|
391
|
+
*
|
|
392
|
+
*
|
|
393
|
+
*/
|
|
394
|
+
|
|
395
|
+
Object.defineProperty(timings$1, "__esModule", {
|
|
396
|
+
value: true
|
|
397
|
+
});
|
|
398
|
+
timings$1.default = normalizeTimings;
|
|
399
|
+
var _postcssValueParser$4 = _interopRequireDefault$a(require$$0);
|
|
400
|
+
function _interopRequireDefault$a(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
401
|
+
/**
|
|
402
|
+
* Turn millisecond values to seconds (shorter), except when < 10ms
|
|
403
|
+
*/
|
|
404
|
+
|
|
405
|
+
function normalizeTimings(ast, _) {
|
|
406
|
+
ast.walk(node => {
|
|
407
|
+
if (node.type !== 'word') {
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
const value = Number.parseFloat(node.value);
|
|
411
|
+
if (Number.isNaN(value)) {
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
const dimension = _postcssValueParser$4.default.unit(node.value);
|
|
415
|
+
if (!dimension || dimension.unit !== 'ms' || value < 10) {
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
node.value = value / 1000 + 's';
|
|
419
|
+
});
|
|
420
|
+
return ast;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
var whitespace = {};
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
427
|
+
*
|
|
428
|
+
* This source code is licensed under the MIT license found in the
|
|
429
|
+
* LICENSE file in the root directory of this source tree.
|
|
430
|
+
*
|
|
431
|
+
*
|
|
432
|
+
*
|
|
433
|
+
*/
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* Use single spaces and remove spaces when not needed: around functions,
|
|
437
|
+
* commas. But preserve spece around + and - as they are required in calc()
|
|
438
|
+
*/
|
|
439
|
+
Object.defineProperty(whitespace, "__esModule", {
|
|
440
|
+
value: true
|
|
441
|
+
});
|
|
442
|
+
whitespace.default = normalizeWhitespace;
|
|
443
|
+
function normalizeWhitespace(ast, _) {
|
|
444
|
+
// trim
|
|
445
|
+
if (ast.nodes[0].type === 'space') {
|
|
446
|
+
ast.nodes.shift();
|
|
447
|
+
}
|
|
448
|
+
if (ast.nodes[ast.nodes.length - 1].type === 'space') {
|
|
449
|
+
ast.nodes.pop();
|
|
450
|
+
}
|
|
451
|
+
ast.walk((node, idx) => {
|
|
452
|
+
switch (node.type) {
|
|
453
|
+
case 'space':
|
|
454
|
+
{
|
|
455
|
+
node.value = ' ';
|
|
456
|
+
break;
|
|
457
|
+
}
|
|
458
|
+
case 'div':
|
|
459
|
+
case 'function':
|
|
460
|
+
{
|
|
461
|
+
node.before = '';
|
|
462
|
+
node.after = '';
|
|
463
|
+
break;
|
|
464
|
+
}
|
|
465
|
+
case 'word':
|
|
466
|
+
{
|
|
467
|
+
if (node.value === '!important') {
|
|
468
|
+
if (ast.nodes[idx - 1] && ast.nodes[idx - 1].type === 'space') {
|
|
469
|
+
ast.nodes.splice(idx - 1, 1);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
break;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
});
|
|
476
|
+
return ast;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
var zeroDimensions = {};
|
|
480
|
+
|
|
481
|
+
/**
|
|
482
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
483
|
+
*
|
|
484
|
+
* This source code is licensed under the MIT license found in the
|
|
485
|
+
* LICENSE file in the root directory of this source tree.
|
|
486
|
+
*
|
|
487
|
+
*
|
|
488
|
+
*/
|
|
489
|
+
|
|
490
|
+
Object.defineProperty(zeroDimensions, "__esModule", {
|
|
491
|
+
value: true
|
|
492
|
+
});
|
|
493
|
+
zeroDimensions.default = normalizeZeroDimensions;
|
|
494
|
+
var _postcssValueParser$3 = _interopRequireDefault$9(require$$0);
|
|
495
|
+
function _interopRequireDefault$9(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
496
|
+
const angles = ['deg', 'grad', 'turn', 'rad'];
|
|
497
|
+
const timings = ['ms', 's'];
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* Remove units in zero values, except when required: in angles and timings,
|
|
501
|
+
* in which case make them consistent 0deg and 0s.
|
|
502
|
+
*/
|
|
503
|
+
|
|
504
|
+
function normalizeZeroDimensions(ast, _) {
|
|
505
|
+
ast.walk(node => {
|
|
506
|
+
if (node.type !== 'word') {
|
|
507
|
+
return;
|
|
508
|
+
}
|
|
509
|
+
const dimension = _postcssValueParser$3.default.unit(node.value);
|
|
510
|
+
if (!dimension || dimension.number !== '0') {
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
if (angles.indexOf(dimension.unit) !== -1) {
|
|
514
|
+
node.value = '0deg';
|
|
515
|
+
} else if (timings.indexOf(dimension.unit) !== -1) {
|
|
516
|
+
node.value = '0s';
|
|
517
|
+
} else {
|
|
518
|
+
node.value = '0';
|
|
519
|
+
}
|
|
520
|
+
});
|
|
521
|
+
return ast;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
var detectUnclosedFns$1 = {};
|
|
525
|
+
|
|
526
|
+
var messages$4 = {};
|
|
527
|
+
|
|
528
|
+
/**
|
|
529
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
530
|
+
*
|
|
531
|
+
* This source code is licensed under the MIT license found in the
|
|
532
|
+
* LICENSE file in the root directory of this source tree.
|
|
533
|
+
*
|
|
534
|
+
*/
|
|
535
|
+
|
|
536
|
+
// This file contains constants to be used within Error messages.
|
|
537
|
+
// The URLs within will eventually be replaced by links to the documenation website for Stylex.
|
|
538
|
+
Object.defineProperty(messages$4, "__esModule", {
|
|
539
|
+
value: true
|
|
540
|
+
});
|
|
541
|
+
messages$4.UNKNOWN_PROP_KEY = messages$4.UNKNOWN_NAMESPACE = messages$4.UNEXPECTED_ARGUMENT = messages$4.UNBOUND_STYLEX_CALL_VALUE = messages$4.ONLY_TOP_LEVEL = messages$4.NO_PARENT_PATH = messages$4.NON_STATIC_VALUE = messages$4.NON_OBJECT_FOR_STYLEX_CALL = messages$4.LOCAL_ONLY = messages$4.LINT_UNCLOSED_FUNCTION = messages$4.INVALID_SPREAD = messages$4.INVALID_PSEUDO = messages$4.ILLEGAL_PROP_VALUE = messages$4.ILLEGAL_PROP_ARRAY_VALUE = messages$4.ILLEGAL_NESTED_PSEUDO = messages$4.ILLEGAL_NAMESPACE_VALUE = messages$4.ILLEGAL_NAMESPACE_TYPE = messages$4.ILLEGAL_ARGUMENT_LENGTH = messages$4.EXPECTED_FUNCTION_CALL = messages$4.ESCAPED_STYLEX_VALUE = void 0;
|
|
542
|
+
const ILLEGAL_ARGUMENT_LENGTH = 'stylex() should have 1 argument.';
|
|
543
|
+
messages$4.ILLEGAL_ARGUMENT_LENGTH = ILLEGAL_ARGUMENT_LENGTH;
|
|
544
|
+
const NON_STATIC_VALUE = 'Only static values are allowed inside of a stylex.create() call.';
|
|
545
|
+
messages$4.NON_STATIC_VALUE = NON_STATIC_VALUE;
|
|
546
|
+
const ESCAPED_STYLEX_VALUE = 'Escaping a stylex.create() value is not allowed.';
|
|
547
|
+
messages$4.ESCAPED_STYLEX_VALUE = ESCAPED_STYLEX_VALUE;
|
|
548
|
+
const UNBOUND_STYLEX_CALL_VALUE = 'stylex.create calls must be bound to a bare variable.';
|
|
549
|
+
messages$4.UNBOUND_STYLEX_CALL_VALUE = UNBOUND_STYLEX_CALL_VALUE;
|
|
550
|
+
const ONLY_TOP_LEVEL = 'stylex.create() is only allowed at the root of a program.';
|
|
551
|
+
messages$4.ONLY_TOP_LEVEL = ONLY_TOP_LEVEL;
|
|
552
|
+
const NON_OBJECT_FOR_STYLEX_CALL = 'stylex.create() can only accept a style object.';
|
|
553
|
+
messages$4.NON_OBJECT_FOR_STYLEX_CALL = NON_OBJECT_FOR_STYLEX_CALL;
|
|
554
|
+
const UNKNOWN_PROP_KEY = 'Unknown property key';
|
|
555
|
+
messages$4.UNKNOWN_PROP_KEY = UNKNOWN_PROP_KEY;
|
|
556
|
+
const INVALID_PSEUDO = 'Invalid pseudo selector, not on the whitelist.';
|
|
557
|
+
messages$4.INVALID_PSEUDO = INVALID_PSEUDO;
|
|
558
|
+
const ILLEGAL_NAMESPACE_TYPE = 'Only a string literal namespace is allowed here.';
|
|
559
|
+
messages$4.ILLEGAL_NAMESPACE_TYPE = ILLEGAL_NAMESPACE_TYPE;
|
|
560
|
+
const UNKNOWN_NAMESPACE = 'Unknown namespace';
|
|
561
|
+
messages$4.UNKNOWN_NAMESPACE = UNKNOWN_NAMESPACE;
|
|
562
|
+
const ILLEGAL_NESTED_PSEUDO = "Pseudo objects can't be nested.";
|
|
563
|
+
messages$4.ILLEGAL_NESTED_PSEUDO = ILLEGAL_NESTED_PSEUDO;
|
|
564
|
+
const ILLEGAL_PROP_VALUE = 'A style value can only contain an array, string or number.';
|
|
565
|
+
messages$4.ILLEGAL_PROP_VALUE = ILLEGAL_PROP_VALUE;
|
|
566
|
+
const ILLEGAL_PROP_ARRAY_VALUE = 'A style array value can only contain strings or numbers.';
|
|
567
|
+
messages$4.ILLEGAL_PROP_ARRAY_VALUE = ILLEGAL_PROP_ARRAY_VALUE;
|
|
568
|
+
const ILLEGAL_NAMESPACE_VALUE = 'A stylex namespace must be an object.';
|
|
569
|
+
messages$4.ILLEGAL_NAMESPACE_VALUE = ILLEGAL_NAMESPACE_VALUE;
|
|
570
|
+
const INVALID_SPREAD = 'Imported styles spread with a stylex.create call must be type cast as `XStyle<>` to verify their type.';
|
|
571
|
+
messages$4.INVALID_SPREAD = INVALID_SPREAD;
|
|
572
|
+
const LINT_UNCLOSED_FUNCTION = 'Rule contains an unclosed function';
|
|
573
|
+
messages$4.LINT_UNCLOSED_FUNCTION = LINT_UNCLOSED_FUNCTION;
|
|
574
|
+
const LOCAL_ONLY = 'The return value of stylex.create() should not be exported.';
|
|
575
|
+
messages$4.LOCAL_ONLY = LOCAL_ONLY;
|
|
576
|
+
const UNEXPECTED_ARGUMENT = 'Unexpected argument passed to the stylex() function.';
|
|
577
|
+
messages$4.UNEXPECTED_ARGUMENT = UNEXPECTED_ARGUMENT;
|
|
578
|
+
const EXPECTED_FUNCTION_CALL = 'Expected a simple function call but found something else.';
|
|
579
|
+
messages$4.EXPECTED_FUNCTION_CALL = EXPECTED_FUNCTION_CALL;
|
|
580
|
+
const NO_PARENT_PATH = 'Unexpected AST node without a parent path.';
|
|
581
|
+
messages$4.NO_PARENT_PATH = NO_PARENT_PATH;
|
|
582
|
+
|
|
583
|
+
/**
|
|
584
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
585
|
+
*
|
|
586
|
+
* This source code is licensed under the MIT license found in the
|
|
587
|
+
* LICENSE file in the root directory of this source tree.
|
|
588
|
+
*
|
|
589
|
+
*
|
|
590
|
+
*/
|
|
591
|
+
|
|
592
|
+
Object.defineProperty(detectUnclosedFns$1, "__esModule", {
|
|
593
|
+
value: true
|
|
594
|
+
});
|
|
595
|
+
detectUnclosedFns$1.default = detectUnclosedFns;
|
|
596
|
+
var messages$3 = _interopRequireWildcard$3(messages$4);
|
|
597
|
+
function _getRequireWildcardCache$3(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache$3 = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
598
|
+
function _interopRequireWildcard$3(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache$3(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
599
|
+
/**
|
|
600
|
+
* Remove leading zeros from numbers
|
|
601
|
+
*/
|
|
602
|
+
function detectUnclosedFns(ast, _) {
|
|
603
|
+
ast.walk(node => {
|
|
604
|
+
if (node.type === 'function' && node.unclosed) {
|
|
605
|
+
throw new Error(messages$3.LINT_UNCLOSED_FUNCTION);
|
|
606
|
+
}
|
|
607
|
+
});
|
|
608
|
+
return ast;
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
/**
|
|
612
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
613
|
+
*
|
|
614
|
+
* This source code is licensed under the MIT license found in the
|
|
615
|
+
* LICENSE file in the root directory of this source tree.
|
|
616
|
+
*
|
|
617
|
+
*
|
|
618
|
+
*
|
|
619
|
+
*/
|
|
620
|
+
|
|
621
|
+
Object.defineProperty(normalizeValue$1, "__esModule", {
|
|
622
|
+
value: true
|
|
623
|
+
});
|
|
624
|
+
normalizeValue$1.default = normalizeValue;
|
|
625
|
+
var _fontSizePxToRem = _interopRequireDefault$8(fontSizePxToRem);
|
|
626
|
+
var _leadingZero = _interopRequireDefault$8(leadingZero);
|
|
627
|
+
var _quotes = _interopRequireDefault$8(quotes);
|
|
628
|
+
var _timings = _interopRequireDefault$8(timings$1);
|
|
629
|
+
var _whitespace = _interopRequireDefault$8(whitespace);
|
|
630
|
+
var _zeroDimensions = _interopRequireDefault$8(zeroDimensions);
|
|
631
|
+
var _detectUnclosedFns = _interopRequireDefault$8(detectUnclosedFns$1);
|
|
632
|
+
var _postcssValueParser$2 = _interopRequireDefault$8(require$$0);
|
|
633
|
+
function _interopRequireDefault$8(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
634
|
+
// `Timings` should be before `LeadingZero`, because it
|
|
635
|
+
// changes 500ms to 0.5s, then `LeadingZero` makes it .5s
|
|
636
|
+
const normalizers = [_detectUnclosedFns.default, _whitespace.default, _timings.default, _zeroDimensions.default, _leadingZero.default, _quotes.default, _fontSizePxToRem.default];
|
|
637
|
+
function normalizeValue(value, key) {
|
|
638
|
+
const parsedAST = (0, _postcssValueParser$2.default)(value);
|
|
639
|
+
return normalizers.reduce((ast, fn) => fn(ast, key), parsedAST).toString();
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
Object.defineProperty(transformValue$1, "__esModule", {
|
|
643
|
+
value: true
|
|
644
|
+
});
|
|
645
|
+
transformValue$1.default = transformValue;
|
|
646
|
+
var _normalizeValue = _interopRequireDefault$7(normalizeValue$1);
|
|
647
|
+
function _interopRequireDefault$7(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
648
|
+
/**
|
|
649
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
650
|
+
*
|
|
651
|
+
* This source code is licensed under the MIT license found in the
|
|
652
|
+
* LICENSE file in the root directory of this source tree.
|
|
653
|
+
*
|
|
654
|
+
*/
|
|
655
|
+
|
|
656
|
+
/**
|
|
657
|
+
* Convert a CSS value in JS to the final CSS string value
|
|
658
|
+
*/
|
|
659
|
+
function transformValue(key, rawValue) {
|
|
660
|
+
const value = typeof rawValue === 'number' ? String(Math.round(rawValue * 10000) / 10000) + getNumberSuffix(key) : rawValue;
|
|
661
|
+
|
|
662
|
+
// content is one of the values that needs to wrapped in quotes.
|
|
663
|
+
// Users may write `''` without thinking about it, so we fix that.
|
|
664
|
+
if (key === 'content' && typeof value === 'string') {
|
|
665
|
+
const val = value.trim();
|
|
666
|
+
if (val.match(/^attr\([a-zA-Z0-9-]+\)$/)) {
|
|
667
|
+
return val;
|
|
668
|
+
}
|
|
669
|
+
if (!(val.startsWith('"') && val.endsWith('"') || val.startsWith("'") && val.endsWith("'"))) {
|
|
670
|
+
return `"${val}"`;
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
return (0, _normalizeValue.default)(value, key);
|
|
674
|
+
}
|
|
675
|
+
function getNumberSuffix(key) {
|
|
676
|
+
if (unitlessNumberProperties.has(key)) {
|
|
677
|
+
return '';
|
|
678
|
+
}
|
|
679
|
+
const suffix = numberPropertySuffixes[key];
|
|
680
|
+
if (suffix == null) {
|
|
681
|
+
return 'px';
|
|
682
|
+
} else {
|
|
683
|
+
return suffix;
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
const unitlessNumberProperties = new Set(['animationIterationCount', 'borderImageOutset', 'borderImageSlice', 'borderImageWidth', 'columnCount', 'flex',
|
|
687
|
+
// Unsupported
|
|
688
|
+
'flexGrow', 'flexPositive', 'flexShrink', 'flexOrder', 'gridRow', 'gridColumn', 'fontWeight', 'lineClamp', 'lineHeight', 'opacity', 'order', 'orphans', 'tabSize', 'widows', 'zIndex', 'fillOpacity', 'floodOpacity', 'stopOpacity', 'strokeDasharray', 'strokeDashoffset', 'strokeMiterlimit', 'strokeOpacity', 'strokeWidth']);
|
|
689
|
+
|
|
690
|
+
// List of properties that have custom suffixes for numbers
|
|
691
|
+
const numberPropertySuffixes = {
|
|
692
|
+
animationDelay: 'ms',
|
|
693
|
+
animationDuration: 'ms',
|
|
694
|
+
transitionDelay: 'ms',
|
|
695
|
+
transitionDuration: 'ms',
|
|
696
|
+
voiceDuration: 'ms'
|
|
697
|
+
};
|
|
698
|
+
|
|
699
|
+
var generateCssRule = {};
|
|
700
|
+
|
|
701
|
+
var generateLtr = {};
|
|
702
|
+
|
|
703
|
+
Object.defineProperty(generateLtr, "__esModule", {
|
|
704
|
+
value: true
|
|
705
|
+
});
|
|
706
|
+
generateLtr.default = generateLTR;
|
|
707
|
+
/**
|
|
708
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
709
|
+
*
|
|
710
|
+
* This source code is licensed under the MIT license found in the
|
|
711
|
+
* LICENSE file in the root directory of this source tree.
|
|
712
|
+
*
|
|
713
|
+
*
|
|
714
|
+
*
|
|
715
|
+
*/
|
|
716
|
+
|
|
717
|
+
const logicalToPhysical$1 = {
|
|
718
|
+
start: 'left',
|
|
719
|
+
end: 'right'
|
|
720
|
+
};
|
|
721
|
+
const propertyToLTR = {
|
|
722
|
+
'margin-start': _ref => {
|
|
723
|
+
let [key, val] = _ref;
|
|
724
|
+
return ['margin-left', val];
|
|
725
|
+
},
|
|
726
|
+
// 'margin-inline-start': ([key, val]) => ['margin-left', val],
|
|
727
|
+
'margin-end': _ref2 => {
|
|
728
|
+
let [key, val] = _ref2;
|
|
729
|
+
return ['margin-right', val];
|
|
730
|
+
},
|
|
731
|
+
// 'margin-inline-end': ([key, val]: [string, string]) => ['margin-right', val],
|
|
732
|
+
'padding-start': _ref3 => {
|
|
733
|
+
let [key, val] = _ref3;
|
|
734
|
+
return ['padding-left', val];
|
|
735
|
+
},
|
|
736
|
+
// 'padding-inline-start': ([key, val]: [string, string]) => ['padding-left', val],
|
|
737
|
+
'padding-end': _ref4 => {
|
|
738
|
+
let [key, val] = _ref4;
|
|
739
|
+
return ['padding-right', val];
|
|
740
|
+
},
|
|
741
|
+
// 'padding-inline-end': ([key, val]: [string, string]) => ['padding-right', val],
|
|
742
|
+
'border-start': _ref5 => {
|
|
743
|
+
let [key, val] = _ref5;
|
|
744
|
+
return ['border-left', val];
|
|
745
|
+
},
|
|
746
|
+
// 'border-inline-start': ([key, val]: [string, string]) => ['border-left', val],
|
|
747
|
+
'border-end': _ref6 => {
|
|
748
|
+
let [key, val] = _ref6;
|
|
749
|
+
return ['border-right', val];
|
|
750
|
+
},
|
|
751
|
+
// 'border-inline-end': ([key, val]: [string, string]) => ['border-right', val],
|
|
752
|
+
'border-start-width': _ref7 => {
|
|
753
|
+
let [key, val] = _ref7;
|
|
754
|
+
return ['border-left-width', val];
|
|
755
|
+
},
|
|
756
|
+
// 'border-inline-start-width': ([key, val]: [string, string]) => ['border-left-width', val],
|
|
757
|
+
'border-end-width': _ref8 => {
|
|
758
|
+
let [key, val] = _ref8;
|
|
759
|
+
return ['border-right-width', val];
|
|
760
|
+
},
|
|
761
|
+
// 'border-inline-end-width': ([key, val]: [string, string]) => ['border-right-width', val],
|
|
762
|
+
'border-start-color': _ref9 => {
|
|
763
|
+
let [key, val] = _ref9;
|
|
764
|
+
return ['border-left-color', val];
|
|
765
|
+
},
|
|
766
|
+
// 'border-inline-start-color': ([key, val]: [string, string]) => ['border-left-color', val],
|
|
767
|
+
'border-end-color': _ref10 => {
|
|
768
|
+
let [key, val] = _ref10;
|
|
769
|
+
return ['border-right-color', val];
|
|
770
|
+
},
|
|
771
|
+
// 'border-inline-end-color': ([key, val]: [string, string]) => ['border-right-color', val],
|
|
772
|
+
'border-start-style': _ref11 => {
|
|
773
|
+
let [key, val] = _ref11;
|
|
774
|
+
return ['border-left-style', val];
|
|
775
|
+
},
|
|
776
|
+
// 'border-inline-start-style': ([key, val]: [string, string]) => ['border-left-style', val],
|
|
777
|
+
'border-end-style': _ref12 => {
|
|
778
|
+
let [key, val] = _ref12;
|
|
779
|
+
return ['border-right-style', val];
|
|
780
|
+
},
|
|
781
|
+
// 'border-inline-end-style': ([key, val]: [string, string]) => ['border-right-style', val],
|
|
782
|
+
'border-top-start-radius': _ref13 => {
|
|
783
|
+
let [key, val] = _ref13;
|
|
784
|
+
return ['border-top-left-radius', val];
|
|
785
|
+
},
|
|
786
|
+
// 'border-start-start-radius': ([key, val]: [string, string]) => ['border-top-left-radius', val],
|
|
787
|
+
'border-bottom-start-radius': _ref14 => {
|
|
788
|
+
let [key, val] = _ref14;
|
|
789
|
+
return ['border-bottom-left-radius', val];
|
|
790
|
+
},
|
|
791
|
+
// 'border-end-start-radius': ([key, val]: [string, string]) => ['border-bottom-left-radius', val],
|
|
792
|
+
'border-top-end-radius': _ref15 => {
|
|
793
|
+
let [key, val] = _ref15;
|
|
794
|
+
return ['border-top-right-radius', val];
|
|
795
|
+
},
|
|
796
|
+
// 'border-start-end-radius': ([key, val]: [string, string]) => ['border-top-right-radius', val],
|
|
797
|
+
'border-bottom-end-radius': _ref16 => {
|
|
798
|
+
let [key, val] = _ref16;
|
|
799
|
+
return ['border-bottom-right-radius', val];
|
|
800
|
+
},
|
|
801
|
+
// 'border-end-end-radius': ([key, val]: [string, string]) => ['border-bottom-right-radius', val],
|
|
802
|
+
'text-align': _ref17 => {
|
|
803
|
+
let [key, val] = _ref17;
|
|
804
|
+
return [key, logicalToPhysical$1[val] ?? val];
|
|
805
|
+
},
|
|
806
|
+
float: _ref18 => {
|
|
807
|
+
let [key, val] = _ref18;
|
|
808
|
+
return [key, logicalToPhysical$1[val] ?? val];
|
|
809
|
+
},
|
|
810
|
+
clear: _ref19 => {
|
|
811
|
+
let [key, val] = _ref19;
|
|
812
|
+
return [key, logicalToPhysical$1[val] ?? val];
|
|
813
|
+
},
|
|
814
|
+
start: _ref20 => {
|
|
815
|
+
let [key, val] = _ref20;
|
|
816
|
+
return ['left', val];
|
|
817
|
+
},
|
|
818
|
+
// 'inset-inline-start': ([key, val]: [string, string]) => ['left', val],
|
|
819
|
+
end: _ref21 => {
|
|
820
|
+
let [key, val] = _ref21;
|
|
821
|
+
return ['right', val];
|
|
822
|
+
},
|
|
823
|
+
// 'inset-inline-end': ([key, val]) => ['right', val],
|
|
824
|
+
'background-position': _ref22 => {
|
|
825
|
+
let [key, val] = _ref22;
|
|
826
|
+
return [key, val.split(' ').map(word => word === 'start' ? 'left' : word === 'end' ? 'right' : word).join(' ')];
|
|
827
|
+
}
|
|
828
|
+
};
|
|
829
|
+
function generateLTR(pair) {
|
|
830
|
+
const [key] = pair;
|
|
831
|
+
if (propertyToLTR[key]) {
|
|
832
|
+
return propertyToLTR[key](pair);
|
|
833
|
+
}
|
|
834
|
+
return pair;
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
var generateRtl = {};
|
|
838
|
+
|
|
839
|
+
Object.defineProperty(generateRtl, "__esModule", {
|
|
840
|
+
value: true
|
|
841
|
+
});
|
|
842
|
+
generateRtl.default = generateRTL;
|
|
843
|
+
var _postcssValueParser$1 = _interopRequireDefault$6(require$$0);
|
|
844
|
+
function _interopRequireDefault$6(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
845
|
+
/**
|
|
846
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
847
|
+
*
|
|
848
|
+
* This source code is licensed under the MIT license found in the
|
|
849
|
+
* LICENSE file in the root directory of this source tree.
|
|
850
|
+
*
|
|
851
|
+
*
|
|
852
|
+
*
|
|
853
|
+
*/
|
|
854
|
+
|
|
855
|
+
const cursorFlip = {
|
|
856
|
+
'e-resize': 'w-resize',
|
|
857
|
+
'w-resize': 'e-resize',
|
|
858
|
+
'ne-resize': 'nw-resize',
|
|
859
|
+
'nesw-resize': 'nwse-resize',
|
|
860
|
+
'nw-resize': 'ne-resize',
|
|
861
|
+
'nwse-resize': 'nesw-resize',
|
|
862
|
+
'se-resize': 'sw-resize',
|
|
863
|
+
'sw-resize': 'se-resize'
|
|
864
|
+
};
|
|
865
|
+
function splitByDivisor(value) {
|
|
866
|
+
const ast = (0, _postcssValueParser$1.default)(value);
|
|
867
|
+
const groups = [];
|
|
868
|
+
let currGroup = [];
|
|
869
|
+
function push() {
|
|
870
|
+
if (currGroup.length === 0) {
|
|
871
|
+
return;
|
|
872
|
+
}
|
|
873
|
+
groups.push(_postcssValueParser$1.default.stringify(currGroup));
|
|
874
|
+
currGroup = [];
|
|
875
|
+
}
|
|
876
|
+
for (const node of ast.nodes) {
|
|
877
|
+
if (node.type === 'div') {
|
|
878
|
+
push();
|
|
879
|
+
} else {
|
|
880
|
+
currGroup.push(node);
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
push();
|
|
884
|
+
return groups;
|
|
885
|
+
}
|
|
886
|
+
function flipSign(value) {
|
|
887
|
+
if (value === '0') {
|
|
888
|
+
return value;
|
|
889
|
+
} else {
|
|
890
|
+
return value[0] === '-' ? value.slice(1) : '-' + value;
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
function flipShadow(value) {
|
|
894
|
+
const defs = splitByDivisor(value);
|
|
895
|
+
const builtDefs = [];
|
|
896
|
+
for (const def of defs) {
|
|
897
|
+
const parts = def.split(' ');
|
|
898
|
+
const index = _postcssValueParser$1.default.unit(parts[0]) === false ? 1 : 0;
|
|
899
|
+
if (index < parts.length) {
|
|
900
|
+
parts[index] = flipSign(parts[index]);
|
|
901
|
+
}
|
|
902
|
+
builtDefs.push(parts.join(' '));
|
|
903
|
+
}
|
|
904
|
+
const rtl = builtDefs.join(', ');
|
|
905
|
+
if (rtl !== value) {
|
|
906
|
+
return rtl;
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
// Should we be flipping shadows at all?
|
|
911
|
+
// I think the better approach would be to let engineers use
|
|
912
|
+
// CSS-vars directly.
|
|
913
|
+
const shadowsFlip = {
|
|
914
|
+
'box-shadow': _ref => {
|
|
915
|
+
let [key, val] = _ref;
|
|
916
|
+
const rtlVal = flipShadow(val);
|
|
917
|
+
return rtlVal ? [key, rtlVal] : null;
|
|
918
|
+
},
|
|
919
|
+
'text-shadow': _ref2 => {
|
|
920
|
+
let [key, val] = _ref2;
|
|
921
|
+
const rtlVal = flipShadow(val);
|
|
922
|
+
return rtlVal ? [key, rtlVal] : null;
|
|
923
|
+
}
|
|
924
|
+
};
|
|
925
|
+
const logicalToPhysical = {
|
|
926
|
+
start: 'right',
|
|
927
|
+
end: 'left'
|
|
928
|
+
};
|
|
929
|
+
const propertyToRTL = {
|
|
930
|
+
'margin-start': _ref3 => {
|
|
931
|
+
let [key, val] = _ref3;
|
|
932
|
+
return ['margin-right', val];
|
|
933
|
+
},
|
|
934
|
+
// 'margin-inline-start': ([key, val]: [string, string]) => ['margin-right', val],
|
|
935
|
+
'margin-end': _ref4 => {
|
|
936
|
+
let [key, val] = _ref4;
|
|
937
|
+
return ['margin-left', val];
|
|
938
|
+
},
|
|
939
|
+
// 'margin-inline-end': ([key, val]: [string, string]) => ['margin-left', val],
|
|
940
|
+
'padding-start': _ref5 => {
|
|
941
|
+
let [key, val] = _ref5;
|
|
942
|
+
return ['padding-right', val];
|
|
943
|
+
},
|
|
944
|
+
// 'padding-inline-start': ([key, val]: [string, string]) => ['padding-right', val],
|
|
945
|
+
'padding-end': _ref6 => {
|
|
946
|
+
let [key, val] = _ref6;
|
|
947
|
+
return ['padding-left', val];
|
|
948
|
+
},
|
|
949
|
+
// 'padding-inline-end': ([key, val]: [string, string]) => ['padding-left', val],
|
|
950
|
+
'border-start': _ref7 => {
|
|
951
|
+
let [key, val] = _ref7;
|
|
952
|
+
return ['border-right', val];
|
|
953
|
+
},
|
|
954
|
+
// 'border-inline-start': ([key, val]: [string, string]) => ['border-right', val],
|
|
955
|
+
'border-end': _ref8 => {
|
|
956
|
+
let [key, val] = _ref8;
|
|
957
|
+
return ['border-left', val];
|
|
958
|
+
},
|
|
959
|
+
// 'border-inline-end': ([key, val]: [string, string]) => ['border-left', val],
|
|
960
|
+
'border-start-width': _ref9 => {
|
|
961
|
+
let [key, val] = _ref9;
|
|
962
|
+
return ['border-right-width', val];
|
|
963
|
+
},
|
|
964
|
+
// 'border-inline-start-width': ([key, val]: [string, string]) => ['border-right-width', val],
|
|
965
|
+
'border-end-width': _ref10 => {
|
|
966
|
+
let [key, val] = _ref10;
|
|
967
|
+
return ['border-left-width', val];
|
|
968
|
+
},
|
|
969
|
+
// 'border-inline-end-width': ([key, val]: [string, string]) => ['border-left-width', val],
|
|
970
|
+
'border-start-color': _ref11 => {
|
|
971
|
+
let [key, val] = _ref11;
|
|
972
|
+
return ['border-right-color', val];
|
|
973
|
+
},
|
|
974
|
+
// 'border-inline-start-color': ([key, val]: [string, string]) => ['border-right-color', val],
|
|
975
|
+
'border-end-color': _ref12 => {
|
|
976
|
+
let [key, val] = _ref12;
|
|
977
|
+
return ['border-left-color', val];
|
|
978
|
+
},
|
|
979
|
+
// 'border-inline-end-color': ([key, val]: [string, string]) => ['border-left-color', val],
|
|
980
|
+
'border-start-style': _ref13 => {
|
|
981
|
+
let [key, val] = _ref13;
|
|
982
|
+
return ['border-right-style', val];
|
|
983
|
+
},
|
|
984
|
+
// 'border-inline-start-style': ([key, val]: [string, string]) => ['border-right-style', val],
|
|
985
|
+
'border-end-style': _ref14 => {
|
|
986
|
+
let [key, val] = _ref14;
|
|
987
|
+
return ['border-left-style', val];
|
|
988
|
+
},
|
|
989
|
+
// 'border-inline-end-style': ([key, val]: [string, string]) => ['border-left-style', val],
|
|
990
|
+
'border-top-start-radius': _ref15 => {
|
|
991
|
+
let [key, val] = _ref15;
|
|
992
|
+
return ['border-top-right-radius', val];
|
|
993
|
+
},
|
|
994
|
+
// 'border-start-start-radius': ([key, val]: [string, string]) => ['border-top-right-radius', val],
|
|
995
|
+
'border-bottom-start-radius': _ref16 => {
|
|
996
|
+
let [key, val] = _ref16;
|
|
997
|
+
return ['border-bottom-right-radius', val];
|
|
998
|
+
},
|
|
999
|
+
// 'border-end-start-radius': ([key, val]: [string, string]) => ['border-bottom-right-radius', val],
|
|
1000
|
+
'border-top-end-radius': _ref17 => {
|
|
1001
|
+
let [key, val] = _ref17;
|
|
1002
|
+
return ['border-top-left-radius', val];
|
|
1003
|
+
},
|
|
1004
|
+
// 'border-start-end-radius': ([key, val]: [string, string]) => ['border-top-left-radius', val],
|
|
1005
|
+
'border-bottom-end-radius': _ref18 => {
|
|
1006
|
+
let [key, val] = _ref18;
|
|
1007
|
+
return ['border-bottom-left-radius', val];
|
|
1008
|
+
},
|
|
1009
|
+
// 'border-end-end-radius': ([key, val]: [string, string]) => ['border-bottom-left-radius', val],
|
|
1010
|
+
'text-align': _ref19 => {
|
|
1011
|
+
let [key, val] = _ref19;
|
|
1012
|
+
return logicalToPhysical[val] != null ? [key, logicalToPhysical[val]] : null;
|
|
1013
|
+
},
|
|
1014
|
+
float: _ref20 => {
|
|
1015
|
+
let [key, val] = _ref20;
|
|
1016
|
+
return logicalToPhysical[val] != null ? [key, logicalToPhysical[val]] : null;
|
|
1017
|
+
},
|
|
1018
|
+
clear: _ref21 => {
|
|
1019
|
+
let [key, val] = _ref21;
|
|
1020
|
+
return logicalToPhysical[val] != null ? [key, logicalToPhysical[val]] : null;
|
|
1021
|
+
},
|
|
1022
|
+
start: _ref22 => {
|
|
1023
|
+
let [key, val] = _ref22;
|
|
1024
|
+
return ['right', val];
|
|
1025
|
+
},
|
|
1026
|
+
// 'inset-inline-start': ([key, val]: [string, string]) => ['right', val],
|
|
1027
|
+
end: _ref23 => {
|
|
1028
|
+
let [key, val] = _ref23;
|
|
1029
|
+
return ['left', val];
|
|
1030
|
+
},
|
|
1031
|
+
// 'inset-inline-end': ([key, val]: [string, string]) => ['left', val],
|
|
1032
|
+
'background-position': _ref24 => {
|
|
1033
|
+
let [key, val] = _ref24;
|
|
1034
|
+
const words = val.split(' ');
|
|
1035
|
+
if (!words.includes('start') && !words.includes('end')) {
|
|
1036
|
+
return null;
|
|
1037
|
+
}
|
|
1038
|
+
return [key, words.map(word => word === 'start' ? 'right' : word === 'end' ? 'left' : word).join(' ')];
|
|
1039
|
+
},
|
|
1040
|
+
cursor: _ref25 => {
|
|
1041
|
+
let [key, val] = _ref25;
|
|
1042
|
+
return cursorFlip[val] != null ? [key, cursorFlip[val]] : null;
|
|
1043
|
+
},
|
|
1044
|
+
...shadowsFlip
|
|
1045
|
+
};
|
|
1046
|
+
function generateRTL(_ref26) {
|
|
1047
|
+
let [key, value] = _ref26;
|
|
1048
|
+
if (propertyToRTL[key]) {
|
|
1049
|
+
return propertyToRTL[key]([key, value]);
|
|
1050
|
+
}
|
|
1051
|
+
return null;
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
var genCSSRule = {};
|
|
1055
|
+
|
|
1056
|
+
/**
|
|
1057
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
1058
|
+
*
|
|
1059
|
+
* This source code is licensed under the MIT license found in the
|
|
1060
|
+
* LICENSE file in the root directory of this source tree.
|
|
1061
|
+
*
|
|
1062
|
+
*
|
|
1063
|
+
*
|
|
1064
|
+
*/
|
|
1065
|
+
|
|
1066
|
+
Object.defineProperty(genCSSRule, "__esModule", {
|
|
1067
|
+
value: true
|
|
1068
|
+
});
|
|
1069
|
+
genCSSRule.default = generateCSSRule$1;
|
|
1070
|
+
function generateCSSRule$1(className, decls, pseudo) {
|
|
1071
|
+
if (pseudo === '::thumb') {
|
|
1072
|
+
const selector = THUMB_VARIANTS.map(suffix => '.' + className + suffix).join(', ');
|
|
1073
|
+
return `${selector}{${decls}}`;
|
|
1074
|
+
}
|
|
1075
|
+
return pseudo != null && pseudo[0] === '@' ? `${pseudo}{.${className}.${className}{${decls}}}` : pseudo != null && pseudo[0] === ':' ? `.${className}${pseudo}{${decls}}` : `.${className}{${decls}}`;
|
|
1076
|
+
}
|
|
1077
|
+
const THUMB_VARIANTS = ['::-webkit-slider-thumb', '::-moz-range-thumb', '::-ms-thumb'];
|
|
1078
|
+
|
|
1079
|
+
Object.defineProperty(generateCssRule, "__esModule", {
|
|
1080
|
+
value: true
|
|
1081
|
+
});
|
|
1082
|
+
generateCssRule.default = generateCSSRule;
|
|
1083
|
+
var _generateLtr$1 = _interopRequireDefault$5(generateLtr);
|
|
1084
|
+
var _generateRtl$1 = _interopRequireDefault$5(generateRtl);
|
|
1085
|
+
var _genCSSRule = _interopRequireDefault$5(genCSSRule);
|
|
1086
|
+
function _interopRequireDefault$5(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
1087
|
+
/**
|
|
1088
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
1089
|
+
*
|
|
1090
|
+
* This source code is licensed under the MIT license found in the
|
|
1091
|
+
* LICENSE file in the root directory of this source tree.
|
|
1092
|
+
*
|
|
1093
|
+
*
|
|
1094
|
+
*/
|
|
1095
|
+
|
|
1096
|
+
function generateCSSRule(className, key, value, pseudo) {
|
|
1097
|
+
const pairs = Array.isArray(value) ? value.map(eachValue => [key, eachValue]) : [[key, value]];
|
|
1098
|
+
const ltrPairs = pairs.map(_generateLtr$1.default);
|
|
1099
|
+
const ltrDecls = ltrPairs.map(pair => pair.join(':')).join(';');
|
|
1100
|
+
const rtlDecls = pairs.map(_generateRtl$1.default).filter(Boolean).map(pair => pair.join(':')).join(';');
|
|
1101
|
+
const ltrRule = (0, _genCSSRule.default)(className, ltrDecls, pseudo);
|
|
1102
|
+
const rtlRule = !rtlDecls ? null : (0, _genCSSRule.default)(className, rtlDecls, pseudo);
|
|
1103
|
+
let priority = 1;
|
|
1104
|
+
if (pseudo != null) {
|
|
1105
|
+
if (pseudo[0] === '@') {
|
|
1106
|
+
priority = 2;
|
|
1107
|
+
} else if (pseudo[0] === ':') {
|
|
1108
|
+
priority = pseudoPriorities[pseudo] ?? 2;
|
|
1109
|
+
if (pseudo.startsWith(':nth-child')) {
|
|
1110
|
+
priority = 6;
|
|
1111
|
+
}
|
|
1112
|
+
if (pseudo.startsWith(':nth-of-type')) {
|
|
1113
|
+
priority = 7;
|
|
1114
|
+
}
|
|
1115
|
+
}
|
|
1116
|
+
}
|
|
1117
|
+
if (key.toLowerCase().includes('left') || key.toLowerCase().includes('right')) {
|
|
1118
|
+
// Bump priority for physical left/right values.
|
|
1119
|
+
priority += 0.1;
|
|
1120
|
+
}
|
|
1121
|
+
return {
|
|
1122
|
+
priority,
|
|
1123
|
+
ltr: ltrRule,
|
|
1124
|
+
rtl: rtlRule
|
|
1125
|
+
};
|
|
1126
|
+
}
|
|
1127
|
+
const pseudoPriorities = {
|
|
1128
|
+
// Might become unsupported:
|
|
1129
|
+
':first-child': 3,
|
|
1130
|
+
':last-child': 4,
|
|
1131
|
+
':only-child': 5,
|
|
1132
|
+
':nth-child': 6,
|
|
1133
|
+
':nth-of-type': 7,
|
|
1134
|
+
':hover': 8,
|
|
1135
|
+
':focus': 9,
|
|
1136
|
+
':active': 10,
|
|
1137
|
+
':disabled': 11,
|
|
1138
|
+
'::placeholder': 12,
|
|
1139
|
+
'::thumb': 13
|
|
1140
|
+
};
|
|
1141
|
+
|
|
1142
|
+
Object.defineProperty(convertToClassName$1, "__esModule", {
|
|
1143
|
+
value: true
|
|
1144
|
+
});
|
|
1145
|
+
convertToClassName$1.default = convertToClassName;
|
|
1146
|
+
var _hash$1 = _interopRequireDefault$4(hash$1);
|
|
1147
|
+
var _dashify$1 = _interopRequireDefault$4(dashify$1);
|
|
1148
|
+
var _transformValue$1 = _interopRequireDefault$4(transformValue$1);
|
|
1149
|
+
var _generateCssRule = _interopRequireDefault$4(generateCssRule);
|
|
1150
|
+
function _interopRequireDefault$4(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
1151
|
+
/**
|
|
1152
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
1153
|
+
*
|
|
1154
|
+
* This source code is licensed under the MIT license found in the
|
|
1155
|
+
* LICENSE file in the root directory of this source tree.
|
|
1156
|
+
*
|
|
1157
|
+
*
|
|
1158
|
+
*/
|
|
1159
|
+
|
|
1160
|
+
// This function takes a single style rule and transforms it into a CSS rule.
|
|
1161
|
+
// [color: 'red'] => ['color', 'classname-for-color-red', CSSRULE{ltr, rtl, priority}]
|
|
1162
|
+
//
|
|
1163
|
+
// It converts the camelCased style key to a dash-separated key.
|
|
1164
|
+
// Handles RTL-flipping
|
|
1165
|
+
// Hashes to get a className
|
|
1166
|
+
// Returns the final key, className a CSS Rule
|
|
1167
|
+
function convertToClassName(objEntry, pseudo) {
|
|
1168
|
+
let {
|
|
1169
|
+
stylexSheetName = '<>',
|
|
1170
|
+
classNamePrefix = 'x'
|
|
1171
|
+
} = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
1172
|
+
const [key, rawValue] = objEntry;
|
|
1173
|
+
const dashedKey = (0, _dashify$1.default)(key);
|
|
1174
|
+
const value = Array.isArray(rawValue) ? rawValue.map(eachValue => (0, _transformValue$1.default)(key, eachValue)) : (0, _transformValue$1.default)(key, rawValue);
|
|
1175
|
+
const stringToHash = Array.isArray(value) ? dashedKey + value.join(', ') + (pseudo ?? 'null') : dashedKey + value + (pseudo ?? 'null');
|
|
1176
|
+
const className = classNamePrefix + (0, _hash$1.default)(stylexSheetName + stringToHash);
|
|
1177
|
+
const cssRules = (0, _generateCssRule.default)(className, dashedKey, value, pseudo);
|
|
1178
|
+
return [key, className, cssRules];
|
|
1179
|
+
}
|
|
1180
|
+
|
|
1181
|
+
var expandShorthands = {};
|
|
1182
|
+
|
|
1183
|
+
Object.defineProperty(expandShorthands, "__esModule", {
|
|
1184
|
+
value: true
|
|
1185
|
+
});
|
|
1186
|
+
expandShorthands.default = flatMapExpandedShorthands;
|
|
1187
|
+
var _postcssValueParser = _interopRequireDefault$3(require$$0);
|
|
1188
|
+
function _interopRequireDefault$3(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
1189
|
+
/**
|
|
1190
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
1191
|
+
*
|
|
1192
|
+
* This source code is licensed under the MIT license found in the
|
|
1193
|
+
* LICENSE file in the root directory of this source tree.
|
|
1194
|
+
*
|
|
1195
|
+
*
|
|
1196
|
+
*/
|
|
1197
|
+
|
|
1198
|
+
function printNode(node) {
|
|
1199
|
+
switch (node.type) {
|
|
1200
|
+
case 'word':
|
|
1201
|
+
case 'string':
|
|
1202
|
+
return `${node.value}`;
|
|
1203
|
+
case 'function':
|
|
1204
|
+
return `${node.value}(${node.nodes.map(printNode).join('')})`;
|
|
1205
|
+
default:
|
|
1206
|
+
return node.value;
|
|
1207
|
+
}
|
|
1208
|
+
}
|
|
1209
|
+
|
|
1210
|
+
// Using split(' ') Isn't enough bcause of values like calc.
|
|
1211
|
+
function splitValue(str) {
|
|
1212
|
+
if (Array.isArray(str)) {
|
|
1213
|
+
return str;
|
|
1214
|
+
}
|
|
1215
|
+
const parsed = (0, _postcssValueParser.default)(str.trim());
|
|
1216
|
+
const nodes = parsed.nodes.filter(node => node.type !== 'space' && node.type !== 'div').map(printNode);
|
|
1217
|
+
if (nodes.length > 1 && nodes[nodes.length - 1].toLowerCase() === '!important') {
|
|
1218
|
+
return nodes.slice(0, nodes.length - 1).map(node => node + ' !important');
|
|
1219
|
+
}
|
|
1220
|
+
return nodes;
|
|
1221
|
+
}
|
|
1222
|
+
|
|
1223
|
+
// TODO: to be added later.
|
|
1224
|
+
// const aliases = {
|
|
1225
|
+
// marginInlineStart: (rawValue) => [['marginStart', rawValue]],
|
|
1226
|
+
// marginInlineEnd: (rawValue) => [['marginEnd', rawValue]],
|
|
1227
|
+
// marginInline: (rawValue) => [
|
|
1228
|
+
// ['marginStart', rawValue],
|
|
1229
|
+
// ['marginEnd', rawValue],
|
|
1230
|
+
// ],
|
|
1231
|
+
// paddingInlineStart: (rawValue) => [['paddingStart', rawValue]],
|
|
1232
|
+
// paddingInlineEnd: (rawValue) => [['paddingEnd', rawValue]],
|
|
1233
|
+
// paddingInline: (rawValue) => [
|
|
1234
|
+
// ['paddingStart', rawValue],
|
|
1235
|
+
// ['paddingEnd', rawValue],
|
|
1236
|
+
// ],
|
|
1237
|
+
// // 'borderInlineStart': (rawValue) => [['borderStart', rawValue]],
|
|
1238
|
+
// // 'borderInlineEnd': (rawValue) => [['borderEnd', rawValue]],
|
|
1239
|
+
// // // This will need to change.
|
|
1240
|
+
// // 'borderInline': (rawValue) => [
|
|
1241
|
+
// // ['borderStart', rawValue],
|
|
1242
|
+
// // ['borderEnd', rawValue],
|
|
1243
|
+
// // ],
|
|
1244
|
+
// };
|
|
1245
|
+
|
|
1246
|
+
/**
|
|
1247
|
+
* Shorthand properties:
|
|
1248
|
+
* - [x] all - Should be banned
|
|
1249
|
+
* - [ ] animation
|
|
1250
|
+
* - [ ] background
|
|
1251
|
+
* - [-] border
|
|
1252
|
+
* - [x] border-block-end
|
|
1253
|
+
* - [x] border-block-start
|
|
1254
|
+
* - [ ] border-bottom
|
|
1255
|
+
* - [x] border-color
|
|
1256
|
+
* - [x] border-image
|
|
1257
|
+
* - [x] border-inline-end
|
|
1258
|
+
* - [x] border-inline-start
|
|
1259
|
+
* - [ ] border-left
|
|
1260
|
+
* - [x] border-radius
|
|
1261
|
+
* - [ ] border-right
|
|
1262
|
+
* - [x] border-style
|
|
1263
|
+
* - [ ] border-top
|
|
1264
|
+
* - [x] border-width
|
|
1265
|
+
* - [ ] column-rule
|
|
1266
|
+
* - [ ] columns
|
|
1267
|
+
* - [ ] flex
|
|
1268
|
+
* - [ ] flex-flow
|
|
1269
|
+
* - [ ] font
|
|
1270
|
+
* - [ ] gap
|
|
1271
|
+
* - [ ] grid
|
|
1272
|
+
* - [ ] grid-area
|
|
1273
|
+
* - [ ] grid-column
|
|
1274
|
+
* - [ ] grid-row
|
|
1275
|
+
* - [ ] grid-template
|
|
1276
|
+
* - [ ] list-style
|
|
1277
|
+
* - [x] margin
|
|
1278
|
+
* - [ ] mask
|
|
1279
|
+
* - [ ] offset
|
|
1280
|
+
* - [ ] outline
|
|
1281
|
+
* - [x] overflow
|
|
1282
|
+
* - [x] padding
|
|
1283
|
+
* - [ ] place-content
|
|
1284
|
+
* - [ ] place-items
|
|
1285
|
+
* - [ ] place-self
|
|
1286
|
+
* - [ ] scroll-margin
|
|
1287
|
+
* - [ ] scroll-padding
|
|
1288
|
+
* - [ ] text-decoration
|
|
1289
|
+
* - [ ] text-emphasis
|
|
1290
|
+
* - [ ] transition
|
|
1291
|
+
*/
|
|
1292
|
+
|
|
1293
|
+
const expansions = {
|
|
1294
|
+
// ...aliases,
|
|
1295
|
+
border: rawValue => {
|
|
1296
|
+
return [['borderTop', rawValue], ['borderEnd', rawValue], ['borderBottom', rawValue], ['borderStart', rawValue]];
|
|
1297
|
+
},
|
|
1298
|
+
/*
|
|
1299
|
+
// Add this later, as this will be a breaking change
|
|
1300
|
+
border: (rawValue: string) => {
|
|
1301
|
+
if (typeof rawValue === 'number') {
|
|
1302
|
+
return expansions.borderWidth(rawValue);
|
|
1303
|
+
}
|
|
1304
|
+
const [width, style, color] = splitValue(rawValue);
|
|
1305
|
+
return [
|
|
1306
|
+
...expansions.borderWidth(width),
|
|
1307
|
+
...expansions.borderStyle(style),
|
|
1308
|
+
...expansions.borderColor(color),
|
|
1309
|
+
];
|
|
1310
|
+
}
|
|
1311
|
+
*/
|
|
1312
|
+
borderColor: rawValue => {
|
|
1313
|
+
const [top, right = top, bottom = top, left = right] = splitValue(rawValue);
|
|
1314
|
+
return [['borderTopColor', top], ['borderEndColor', right], ['borderBottomColor', bottom], ['borderStartColor', left]];
|
|
1315
|
+
},
|
|
1316
|
+
borderHorizontal: rawValue => {
|
|
1317
|
+
return [['borderStart', rawValue], ['borderEnd', rawValue]];
|
|
1318
|
+
},
|
|
1319
|
+
borderStyle: rawValue => {
|
|
1320
|
+
const [top, right = top, bottom = top, left = right] = splitValue(rawValue);
|
|
1321
|
+
return [['borderTopStyle', top], ['borderEndStyle', right], ['borderBottomStyle', bottom], ['borderStartStyle', left]];
|
|
1322
|
+
},
|
|
1323
|
+
borderVertical: rawValue => {
|
|
1324
|
+
return [['borderTop', rawValue], ['borderBottom', rawValue]];
|
|
1325
|
+
},
|
|
1326
|
+
borderWidth: rawValue => {
|
|
1327
|
+
const [top, right = top, bottom = top, left = right] = typeof rawValue === 'number' ? [rawValue] : splitValue(rawValue);
|
|
1328
|
+
return [['borderTopWidth', top], ['borderEndWidth', right], ['borderBottomWidth', bottom], ['borderStartWidth', left]];
|
|
1329
|
+
},
|
|
1330
|
+
borderRadius: rawValue => {
|
|
1331
|
+
const [top, right = top, bottom = top, left = right] = typeof rawValue === 'string' ? splitValue(rawValue) : typeof rawValue === 'number' ? [rawValue] : rawValue; // remove
|
|
1332
|
+
|
|
1333
|
+
return [['borderTopStartRadius', top], ['borderTopEndRadius', right], ['borderBottomEndRadius', bottom], ['borderBottomStartRadius', left]];
|
|
1334
|
+
},
|
|
1335
|
+
margin: rawValue => {
|
|
1336
|
+
const [top, right = top, bottom = top, left = right] = typeof rawValue === 'number' ? [rawValue] : splitValue(rawValue);
|
|
1337
|
+
return [['marginTop', top], ['marginEnd', right], ['marginBottom', bottom], ['marginStart', left]];
|
|
1338
|
+
},
|
|
1339
|
+
marginHorizontal: rawValue => {
|
|
1340
|
+
return [['marginStart', rawValue], ['marginEnd', rawValue]];
|
|
1341
|
+
},
|
|
1342
|
+
marginVertical: rawValue => {
|
|
1343
|
+
return [['marginTop', rawValue], ['marginBottom', rawValue]];
|
|
1344
|
+
},
|
|
1345
|
+
overflow: rawValue => {
|
|
1346
|
+
const [x, y = x] = splitValue(rawValue);
|
|
1347
|
+
return [['overflowX', x], ['overflowY', y]];
|
|
1348
|
+
},
|
|
1349
|
+
padding: rawValue => {
|
|
1350
|
+
const [top, right = top, bottom = top, left = right] = typeof rawValue === 'number' ? [rawValue] : splitValue(rawValue);
|
|
1351
|
+
return [['paddingTop', top], ['paddingEnd', right], ['paddingBottom', bottom], ['paddingStart', left]];
|
|
1352
|
+
},
|
|
1353
|
+
paddingHorizontal: rawValue => {
|
|
1354
|
+
return [['paddingStart', rawValue], ['paddingEnd', rawValue]];
|
|
1355
|
+
},
|
|
1356
|
+
paddingVertical: rawValue => {
|
|
1357
|
+
return [['paddingTop', rawValue], ['paddingBottom', rawValue]];
|
|
1358
|
+
}
|
|
1359
|
+
};
|
|
1360
|
+
function flatMapExpandedShorthands(objEntry) {
|
|
1361
|
+
const [key, value] = objEntry;
|
|
1362
|
+
const expansion = expansions[key];
|
|
1363
|
+
if (expansion) {
|
|
1364
|
+
if (Array.isArray(value)) {
|
|
1365
|
+
throw new Error('Cannot use fallbacks for shorthands. Use the expansion instead.');
|
|
1366
|
+
}
|
|
1367
|
+
return expansion(value);
|
|
1368
|
+
}
|
|
1369
|
+
return [objEntry];
|
|
1370
|
+
}
|
|
1371
|
+
|
|
1372
|
+
var objectUtils = {};
|
|
1373
|
+
|
|
1374
|
+
var stylexInclude$1 = {};
|
|
1375
|
+
|
|
1376
|
+
Object.defineProperty(stylexInclude$1, "__esModule", {
|
|
1377
|
+
value: true
|
|
1378
|
+
});
|
|
1379
|
+
stylexInclude$1.IncludedStyles = void 0;
|
|
1380
|
+
stylexInclude$1.default = stylexInclude;
|
|
1381
|
+
var messages$2 = _interopRequireWildcard$2(messages$4);
|
|
1382
|
+
function _getRequireWildcardCache$2(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache$2 = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
1383
|
+
function _interopRequireWildcard$2(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache$2(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
1384
|
+
/**
|
|
1385
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
1386
|
+
*
|
|
1387
|
+
* This source code is licensed under the MIT license found in the
|
|
1388
|
+
* LICENSE file in the root directory of this source tree.
|
|
1389
|
+
*
|
|
1390
|
+
*
|
|
1391
|
+
*
|
|
1392
|
+
*/
|
|
1393
|
+
|
|
1394
|
+
let number = 0;
|
|
1395
|
+
function uuid() {
|
|
1396
|
+
return `__included_${++number}__`;
|
|
1397
|
+
}
|
|
1398
|
+
let IncludedStyles$1 = class IncludedStyles {
|
|
1399
|
+
constructor(astNode) {
|
|
1400
|
+
this.astNode = astNode;
|
|
1401
|
+
}
|
|
1402
|
+
};
|
|
1403
|
+
stylexInclude$1.IncludedStyles = IncludedStyles$1;
|
|
1404
|
+
function stylexInclude(firstArg) {
|
|
1405
|
+
if ((arguments.length <= 1 ? 0 : arguments.length - 1) > 0) {
|
|
1406
|
+
throw new Error(messages$2.ILLEGAL_ARGUMENT_LENGTH);
|
|
1407
|
+
}
|
|
1408
|
+
return {
|
|
1409
|
+
[uuid()]: new IncludedStyles$1(firstArg.node)
|
|
1410
|
+
};
|
|
1411
|
+
}
|
|
1412
|
+
|
|
1413
|
+
Object.defineProperty(objectUtils, "__esModule", {
|
|
1414
|
+
value: true
|
|
1415
|
+
});
|
|
1416
|
+
objectUtils.Pipe = void 0;
|
|
1417
|
+
objectUtils.flattenObject = flattenObject$1;
|
|
1418
|
+
objectUtils.objEntries = objEntries;
|
|
1419
|
+
objectUtils.objFromEntries = objFromEntries;
|
|
1420
|
+
objectUtils.objMap = objMap;
|
|
1421
|
+
objectUtils.objMapEntry = objMapEntry;
|
|
1422
|
+
objectUtils.objMapKeys = objMapKeys;
|
|
1423
|
+
objectUtils.objValues = objValues;
|
|
1424
|
+
var _stylexInclude$2 = stylexInclude$1;
|
|
1425
|
+
/**
|
|
1426
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
1427
|
+
*
|
|
1428
|
+
* This source code is licensed under the MIT license found in the
|
|
1429
|
+
* LICENSE file in the root directory of this source tree.
|
|
1430
|
+
*
|
|
1431
|
+
*
|
|
1432
|
+
*/
|
|
1433
|
+
|
|
1434
|
+
// A bunch of object utils with better Flow types
|
|
1435
|
+
|
|
1436
|
+
function flattenObject$1(obj) {
|
|
1437
|
+
const result = {};
|
|
1438
|
+
for (const [key, value] of objEntries(obj)) {
|
|
1439
|
+
if (typeof value === 'string') {
|
|
1440
|
+
result[key] = value;
|
|
1441
|
+
} else if (value instanceof _stylexInclude$2.IncludedStyles) {
|
|
1442
|
+
result[key] = value;
|
|
1443
|
+
} else {
|
|
1444
|
+
for (const [subKey, subValue] of objEntries(value)) {
|
|
1445
|
+
result[`${key}_${subKey}`] = subValue;
|
|
1446
|
+
}
|
|
1447
|
+
}
|
|
1448
|
+
}
|
|
1449
|
+
return result;
|
|
1450
|
+
}
|
|
1451
|
+
function objEntries(obj) {
|
|
1452
|
+
const retVal = [];
|
|
1453
|
+
for (const key of Object.keys(obj)) {
|
|
1454
|
+
retVal.push([key, obj[key]]);
|
|
1455
|
+
}
|
|
1456
|
+
return retVal;
|
|
1457
|
+
}
|
|
1458
|
+
function objValues(obj) {
|
|
1459
|
+
const retVal = [];
|
|
1460
|
+
for (const key of Object.keys(obj)) {
|
|
1461
|
+
retVal.push(obj[key]);
|
|
1462
|
+
}
|
|
1463
|
+
return retVal;
|
|
1464
|
+
}
|
|
1465
|
+
function objFromEntries(entries) {
|
|
1466
|
+
const retVal = {};
|
|
1467
|
+
for (const [key, value] of entries) {
|
|
1468
|
+
retVal[key] = value;
|
|
1469
|
+
}
|
|
1470
|
+
return retVal;
|
|
1471
|
+
}
|
|
1472
|
+
function objMapKeys(obj, mapper) {
|
|
1473
|
+
return objFromEntries(objEntries(obj).map(_ref => {
|
|
1474
|
+
let [key, value] = _ref;
|
|
1475
|
+
return [mapper(key), value];
|
|
1476
|
+
}));
|
|
1477
|
+
}
|
|
1478
|
+
function objMapEntry(obj, mapper) {
|
|
1479
|
+
return objFromEntries(objEntries(obj).map(_ref2 => {
|
|
1480
|
+
let [key, value] = _ref2;
|
|
1481
|
+
return mapper([key, value]);
|
|
1482
|
+
}));
|
|
1483
|
+
}
|
|
1484
|
+
function objMap(obj, mapper) {
|
|
1485
|
+
return objFromEntries(objEntries(obj).map(_ref3 => {
|
|
1486
|
+
let [key, value] = _ref3;
|
|
1487
|
+
return [key, mapper(value, key)];
|
|
1488
|
+
}));
|
|
1489
|
+
}
|
|
1490
|
+
class Pipe {
|
|
1491
|
+
constructor(val) {
|
|
1492
|
+
this.value = val;
|
|
1493
|
+
}
|
|
1494
|
+
pipe(mapper) {
|
|
1495
|
+
return new Pipe(mapper(this.value));
|
|
1496
|
+
}
|
|
1497
|
+
done() {
|
|
1498
|
+
return this.value;
|
|
1499
|
+
}
|
|
1500
|
+
static create(val) {
|
|
1501
|
+
return new Pipe(val);
|
|
1502
|
+
}
|
|
1503
|
+
}
|
|
1504
|
+
objectUtils.Pipe = Pipe;
|
|
1505
|
+
|
|
1506
|
+
Object.defineProperty(stylexCreate, "__esModule", {
|
|
1507
|
+
value: true
|
|
1508
|
+
});
|
|
1509
|
+
stylexCreate.default = styleXCreateSet;
|
|
1510
|
+
var _convertToClassName = _interopRequireDefault$2(convertToClassName$1);
|
|
1511
|
+
var _expandShorthands$1 = _interopRequireDefault$2(expandShorthands);
|
|
1512
|
+
var _objectUtils$1 = objectUtils;
|
|
1513
|
+
var messages$1 = _interopRequireWildcard$1(messages$4);
|
|
1514
|
+
var _stylexInclude$1 = stylexInclude$1;
|
|
1515
|
+
function _getRequireWildcardCache$1(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache$1 = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
1516
|
+
function _interopRequireWildcard$1(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache$1(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
1517
|
+
function _interopRequireDefault$2(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
1518
|
+
/**
|
|
1519
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
1520
|
+
*
|
|
1521
|
+
* This source code is licensed under the MIT license found in the
|
|
1522
|
+
* LICENSE file in the root directory of this source tree.
|
|
1523
|
+
*
|
|
1524
|
+
*
|
|
1525
|
+
*/
|
|
1526
|
+
|
|
1527
|
+
// This takes the object of styles passed to `stylex.create` and transforms it.
|
|
1528
|
+
// The transformation replaces style values with classNames.
|
|
1529
|
+
//
|
|
1530
|
+
// It also collects all injected styles along the way.
|
|
1531
|
+
// It then returns a tuple of the transformed style Object and an object of injected styles.
|
|
1532
|
+
//
|
|
1533
|
+
// This function does some basic validation, and then uses `styleXCreateNamespace` to transform
|
|
1534
|
+
// each namespace within,
|
|
1535
|
+
//
|
|
1536
|
+
// Before returning, it ensures that there are no duplicate styles being injected.
|
|
1537
|
+
function styleXCreateSet(namespaces) {
|
|
1538
|
+
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
1539
|
+
const resolvedNamespaces = {};
|
|
1540
|
+
const injectedStyles = {};
|
|
1541
|
+
for (const namespaceName of Object.keys(namespaces)) {
|
|
1542
|
+
const namespace = namespaces[namespaceName];
|
|
1543
|
+
if (typeof namespace !== 'object' || Array.isArray(namespace)) {
|
|
1544
|
+
throw new Error(messages$1.ILLEGAL_NAMESPACE_VALUE);
|
|
1545
|
+
}
|
|
1546
|
+
const [resolvedNamespace, injected] = styleXCreateNamespace(namespace, options);
|
|
1547
|
+
resolvedNamespaces[namespaceName] = (0, _objectUtils$1.flattenObject)(resolvedNamespace);
|
|
1548
|
+
for (const cn of Object.keys(injected)) {
|
|
1549
|
+
if (injectedStyles[cn] == null) {
|
|
1550
|
+
injectedStyles[cn] = injected[cn];
|
|
1551
|
+
}
|
|
1552
|
+
}
|
|
1553
|
+
}
|
|
1554
|
+
return [resolvedNamespaces, injectedStyles];
|
|
1555
|
+
}
|
|
1556
|
+
|
|
1557
|
+
// Transforms a single style namespace.
|
|
1558
|
+
// e.g. Something along the lines of:
|
|
1559
|
+
// {color: 'red', margin: '10px'} =>
|
|
1560
|
+
// {
|
|
1561
|
+
// color: 'color-red',
|
|
1562
|
+
// marginTop: 'margin-top-10px',
|
|
1563
|
+
// marginBottom: 'margin-bottom-10px',
|
|
1564
|
+
// marginStart: 'margin-start-10px',
|
|
1565
|
+
// marginEnd: 'margin-end-10px'
|
|
1566
|
+
// }
|
|
1567
|
+
//
|
|
1568
|
+
// First, it expands shorthand properties. (margin => marginTop, marginBottom, marginStart, marginEnd)
|
|
1569
|
+
// Then, it converts each style value to a className.
|
|
1570
|
+
// Then, it returns the transformed style Object and an object of injected styles.
|
|
1571
|
+
function styleXCreateNamespace(style, options) {
|
|
1572
|
+
const namespaceEntries = (0, _objectUtils$1.objEntries)(style);
|
|
1573
|
+
|
|
1574
|
+
// First the shorthand properties are expanded.
|
|
1575
|
+
// e.g. `margin` gets expanded to `marginTop`, `marginBottom`, `marginStart`, `marginEnd`.
|
|
1576
|
+
// `entries` is an array of [key, value] pairs.
|
|
1577
|
+
const entries = namespaceEntries.flatMap(_ref => {
|
|
1578
|
+
let [key, value] = _ref;
|
|
1579
|
+
if (value instanceof _stylexInclude$1.IncludedStyles) {
|
|
1580
|
+
return [[key, value]];
|
|
1581
|
+
}
|
|
1582
|
+
if (value != null && typeof value === 'object' && !Array.isArray(value)) {
|
|
1583
|
+
if (!key.startsWith(':') && !key.startsWith('@')) {
|
|
1584
|
+
throw new Error(messages$1.INVALID_PSEUDO);
|
|
1585
|
+
}
|
|
1586
|
+
return [[key, (0, _objectUtils$1.objFromEntries)((0, _objectUtils$1.objEntries)(value).flatMap(_ref2 => {
|
|
1587
|
+
let [innerKey, innerValue] = _ref2;
|
|
1588
|
+
if (innerValue != null && typeof innerValue === 'object' && !Array.isArray(innerValue)) {
|
|
1589
|
+
throw new Error(messages$1.ILLEGAL_NESTED_PSEUDO);
|
|
1590
|
+
}
|
|
1591
|
+
return (0, _expandShorthands$1.default)([innerKey, innerValue]);
|
|
1592
|
+
}))]];
|
|
1593
|
+
} else {
|
|
1594
|
+
if (typeof value !== 'string' && typeof value !== 'number' && !Array.isArray(value)) {
|
|
1595
|
+
throw new Error(messages$1.ILLEGAL_PROP_VALUE);
|
|
1596
|
+
}
|
|
1597
|
+
if (Array.isArray(value) && value.some(val => typeof val === 'object')) {
|
|
1598
|
+
throw new Error(messages$1.ILLEGAL_PROP_ARRAY_VALUE);
|
|
1599
|
+
}
|
|
1600
|
+
return (0, _expandShorthands$1.default)([key, value]);
|
|
1601
|
+
}
|
|
1602
|
+
});
|
|
1603
|
+
|
|
1604
|
+
// Now each [key, value] pair is considered a single atomic style.
|
|
1605
|
+
// This atomic style is converted to a className by hashing
|
|
1606
|
+
//
|
|
1607
|
+
// The [key, className] pair is then added to the output Object: `resolvedNamespace`.
|
|
1608
|
+
// While hashing, the CSS rule that the className is generated from is also added to the output Object: `injectedStyles`.
|
|
1609
|
+
const resolvedNamespace = {};
|
|
1610
|
+
const injectedStyles = {};
|
|
1611
|
+
for (const [key, val] of entries) {
|
|
1612
|
+
if (val instanceof _stylexInclude$1.IncludedStyles) {
|
|
1613
|
+
resolvedNamespace[key] = val;
|
|
1614
|
+
} else if (val != null && typeof val === 'object' && !Array.isArray(val)) {
|
|
1615
|
+
const pseudo = key;
|
|
1616
|
+
const innerObj = {};
|
|
1617
|
+
for (const [innerKey, innerVal] of (0, _objectUtils$1.objEntries)(val)) {
|
|
1618
|
+
const [updatedKey, className, cssRule] = (0, _convertToClassName.default)([innerKey, innerVal], pseudo, options);
|
|
1619
|
+
innerObj[updatedKey] = className;
|
|
1620
|
+
injectedStyles[updatedKey + pseudo] = [className, cssRule];
|
|
1621
|
+
}
|
|
1622
|
+
resolvedNamespace[key] = innerObj;
|
|
1623
|
+
} else {
|
|
1624
|
+
const [updatedKey, className, cssRule] = (0, _convertToClassName.default)([key, val], undefined, options);
|
|
1625
|
+
resolvedNamespace[updatedKey] = className;
|
|
1626
|
+
injectedStyles[updatedKey] = [className, cssRule];
|
|
1627
|
+
}
|
|
1628
|
+
}
|
|
1629
|
+
const finalInjectedStyles = (0, _objectUtils$1.objFromEntries)((0, _objectUtils$1.objValues)(injectedStyles));
|
|
1630
|
+
return [resolvedNamespace, finalInjectedStyles];
|
|
1631
|
+
}
|
|
1632
|
+
|
|
1633
|
+
var stylexKeyframes = {};
|
|
1634
|
+
|
|
1635
|
+
Object.defineProperty(stylexKeyframes, "__esModule", {
|
|
1636
|
+
value: true
|
|
1637
|
+
});
|
|
1638
|
+
stylexKeyframes.default = styleXKeyframes;
|
|
1639
|
+
var _hash = _interopRequireDefault$1(hash$1);
|
|
1640
|
+
var _expandShorthands = _interopRequireDefault$1(expandShorthands);
|
|
1641
|
+
var _generateLtr = _interopRequireDefault$1(generateLtr);
|
|
1642
|
+
var _generateRtl = _interopRequireDefault$1(generateRtl);
|
|
1643
|
+
var _transformValue = _interopRequireDefault$1(transformValue$1);
|
|
1644
|
+
var _dashify = _interopRequireDefault$1(dashify$1);
|
|
1645
|
+
var _objectUtils = objectUtils;
|
|
1646
|
+
function _interopRequireDefault$1(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
1647
|
+
/**
|
|
1648
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
1649
|
+
*
|
|
1650
|
+
* This source code is licensed under the MIT license found in the
|
|
1651
|
+
* LICENSE file in the root directory of this source tree.
|
|
1652
|
+
*
|
|
1653
|
+
*
|
|
1654
|
+
*/
|
|
1655
|
+
|
|
1656
|
+
// Similar to `stylex.create` it takes an object of keyframes
|
|
1657
|
+
// and returns a string after hashing it.
|
|
1658
|
+
//
|
|
1659
|
+
// It also expands shorthand properties to maintain parity with
|
|
1660
|
+
// `stylex.create`.
|
|
1661
|
+
function styleXKeyframes(frames) {
|
|
1662
|
+
let {
|
|
1663
|
+
stylexSheetName = '<>',
|
|
1664
|
+
classNamePrefix = 'x'
|
|
1665
|
+
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
1666
|
+
const expandedObject = (0, _objectUtils.objMap)(frames, frame => _objectUtils.Pipe.create(frame).pipe(expandFrameShorthands).pipe(x => (0, _objectUtils.objMapKeys)(x, _dashify.default)).pipe(x => (0, _objectUtils.objMap)(x, (value, key) => (0, _transformValue.default)(key, value))).done());
|
|
1667
|
+
const ltrStyles = (0, _objectUtils.objMap)(expandedObject, frame => (0, _objectUtils.objMapEntry)(frame, _generateLtr.default));
|
|
1668
|
+
const rtlStyles = (0, _objectUtils.objMap)(expandedObject, frame => (0, _objectUtils.objMapEntry)(frame, entry => (0, _generateRtl.default)(entry) ?? entry));
|
|
1669
|
+
const ltrString = constructKeyframesObj(ltrStyles);
|
|
1670
|
+
const rtlString = constructKeyframesObj(rtlStyles);
|
|
1671
|
+
|
|
1672
|
+
// This extra `-B` is kept for some idiosyncratic legacy compatibility for now.
|
|
1673
|
+
const animationName = classNamePrefix + (0, _hash.default)(stylexSheetName + ltrString) + '-B';
|
|
1674
|
+
const ltr = `@keyframes ${animationName}{${ltrString}}`;
|
|
1675
|
+
const rtl = ltrString === rtlString ? null : `@keyframes ${animationName}{${rtlString}}`;
|
|
1676
|
+
return [animationName, {
|
|
1677
|
+
ltr,
|
|
1678
|
+
rtl,
|
|
1679
|
+
priority: 1
|
|
1680
|
+
}];
|
|
1681
|
+
}
|
|
1682
|
+
function expandFrameShorthands(frame) {
|
|
1683
|
+
return (0, _objectUtils.objFromEntries)((0, _objectUtils.objEntries)(frame).flatMap(pair => (0, _expandShorthands.default)(pair)));
|
|
1684
|
+
}
|
|
1685
|
+
|
|
1686
|
+
// Create t
|
|
1687
|
+
function constructKeyframesObj(frames) {
|
|
1688
|
+
return (0, _objectUtils.objEntries)(frames).map(_ref => {
|
|
1689
|
+
let [key, value] = _ref;
|
|
1690
|
+
return `${key}{${(0, _objectUtils.objEntries)(value).map(_ref2 => {
|
|
1691
|
+
let [k, v] = _ref2;
|
|
1692
|
+
return `${k}:${v};`;
|
|
1693
|
+
}).join('')}}`;
|
|
1694
|
+
}).join('');
|
|
1695
|
+
}
|
|
1696
|
+
|
|
1697
|
+
var stylexFirstThatWorks$1 = {};
|
|
1698
|
+
|
|
1699
|
+
Object.defineProperty(stylexFirstThatWorks$1, "__esModule", {
|
|
1700
|
+
value: true
|
|
1701
|
+
});
|
|
1702
|
+
stylexFirstThatWorks$1.default = stylexFirstThatWorks;
|
|
1703
|
+
/**
|
|
1704
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
1705
|
+
*
|
|
1706
|
+
* This source code is licensed under the MIT license found in the
|
|
1707
|
+
* LICENSE file in the root directory of this source tree.
|
|
1708
|
+
*
|
|
1709
|
+
*
|
|
1710
|
+
*
|
|
1711
|
+
*/
|
|
1712
|
+
|
|
1713
|
+
function stylexFirstThatWorks() {
|
|
1714
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
1715
|
+
args[_key] = arguments[_key];
|
|
1716
|
+
}
|
|
1717
|
+
return [...args].reverse();
|
|
1718
|
+
}
|
|
1719
|
+
|
|
1720
|
+
Object.defineProperty(lib, "__esModule", {
|
|
1721
|
+
value: true
|
|
1722
|
+
});
|
|
1723
|
+
var messages_1 = lib.messages = keyframes_1 = lib.keyframes = include_1 = lib.include = firstThatWorks_1 = lib.firstThatWorks = create_1 = lib.create = IncludedStyles_1 = lib.IncludedStyles = void 0;
|
|
1724
|
+
var _stylexCreate = _interopRequireDefault(stylexCreate);
|
|
1725
|
+
var _stylexKeyframes = _interopRequireDefault(stylexKeyframes);
|
|
1726
|
+
var _stylexInclude = _interopRequireWildcard(stylexInclude$1);
|
|
1727
|
+
var _stylexFirstThatWorks = _interopRequireDefault(stylexFirstThatWorks$1);
|
|
1728
|
+
var m = _interopRequireWildcard(messages$4);
|
|
1729
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
1730
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
1731
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
1732
|
+
/**
|
|
1733
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
1734
|
+
*
|
|
1735
|
+
* This source code is licensed under the MIT license found in the
|
|
1736
|
+
* LICENSE file in the root directory of this source tree.
|
|
1737
|
+
*
|
|
1738
|
+
*
|
|
1739
|
+
*/
|
|
1740
|
+
|
|
1741
|
+
// All functions exposed from `stylex` are defined in a way that can be run
|
|
1742
|
+
// entirely in the browser.
|
|
1743
|
+
|
|
1744
|
+
// These are the implementations of those functions.
|
|
1745
|
+
|
|
1746
|
+
const create = _stylexCreate.default;
|
|
1747
|
+
var create_1 = lib.create = create;
|
|
1748
|
+
const keyframes = _stylexKeyframes.default;
|
|
1749
|
+
var keyframes_1 = lib.keyframes = keyframes;
|
|
1750
|
+
const include = _stylexInclude.default;
|
|
1751
|
+
var include_1 = lib.include = include;
|
|
1752
|
+
const messages = m;
|
|
1753
|
+
messages_1 = lib.messages = messages;
|
|
1754
|
+
const IncludedStyles = _stylexInclude.IncludedStyles;
|
|
1755
|
+
var IncludedStyles_1 = lib.IncludedStyles = IncludedStyles;
|
|
1756
|
+
const firstThatWorks = _stylexFirstThatWorks.default;
|
|
1757
|
+
var firstThatWorks_1 = lib.firstThatWorks = firstThatWorks;
|
|
1758
|
+
|
|
1759
|
+
/**
|
|
1760
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
1761
|
+
*
|
|
1762
|
+
* This source code is licensed under the MIT license found in the
|
|
1763
|
+
* LICENSE file in the root directory of this source tree.
|
|
1764
|
+
*/
|
|
1765
|
+
// TODO: We will need to maintain the full path to the file eventually
|
|
1766
|
+
// Perhaps this can be an option that is passed in.
|
|
1767
|
+
function namespaceToDevClassName(namespace, varName, filename) {
|
|
1768
|
+
// Get the basename of the file without the extension
|
|
1769
|
+
const basename = path.basename(filename).split('.')[0];
|
|
1770
|
+
|
|
1771
|
+
// Build up the class name, and sanitize it of disallowed characters
|
|
1772
|
+
const className = `${basename}__${varName ? `${varName}.` : ''}${namespace}`;
|
|
1773
|
+
return className.replace(/[^.a-zA-Z0-9_-]/g, '');
|
|
1774
|
+
}
|
|
1775
|
+
function injectDevClassNames(obj, varName, state) {
|
|
1776
|
+
const result = {};
|
|
1777
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
1778
|
+
const devClassName = namespaceToDevClassName(key, varName, state.filename ?? 'UnkownFile');
|
|
1779
|
+
result[key] = {
|
|
1780
|
+
[devClassName]: devClassName,
|
|
1781
|
+
...value
|
|
1782
|
+
};
|
|
1783
|
+
}
|
|
1784
|
+
return result;
|
|
1785
|
+
}
|
|
1786
|
+
function convertToTestStyles(obj, varName, state) {
|
|
1787
|
+
const result = {};
|
|
1788
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
1789
|
+
const devClassName = namespaceToDevClassName(key, varName, state.filename ?? 'UnkownFile');
|
|
1790
|
+
result[key] = {
|
|
1791
|
+
[devClassName]: devClassName
|
|
1792
|
+
};
|
|
1793
|
+
}
|
|
1794
|
+
return result;
|
|
1795
|
+
}
|
|
1796
|
+
|
|
1797
|
+
/**
|
|
1798
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
1799
|
+
*
|
|
1800
|
+
* This source code is licensed under the MIT license found in the
|
|
1801
|
+
* LICENSE file in the root directory of this source tree.
|
|
1802
|
+
*/
|
|
1803
|
+
function convertObjectToAST(obj) {
|
|
1804
|
+
return t__namespace.objectExpression(Object.entries(obj).map(_ref => {
|
|
1805
|
+
let [key, value] = _ref;
|
|
1806
|
+
return value instanceof IncludedStyles_1 ? t__namespace.spreadElement(value.astNode) : t__namespace.objectProperty(canBeIdentifier(key) ? t__namespace.identifier(key) : t__namespace.stringLiteral(key), typeof value === 'string' ? t__namespace.stringLiteral(value) : convertObjectToAST(value));
|
|
1807
|
+
}));
|
|
1808
|
+
}
|
|
1809
|
+
function canBeIdentifier(str) {
|
|
1810
|
+
return str.match(/^[a-zA-Z_$][a-zA-Z0-9_$]*$/) != null;
|
|
1811
|
+
}
|
|
1812
|
+
|
|
1813
|
+
/**
|
|
1814
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
1815
|
+
*
|
|
1816
|
+
* This source code is licensed under the MIT license found in the
|
|
1817
|
+
* LICENSE file in the root directory of this source tree.
|
|
1818
|
+
*/
|
|
1819
|
+
|
|
1820
|
+
/**
|
|
1821
|
+
* This is a extended version of the path evaluation code from Babel.
|
|
1822
|
+
*
|
|
1823
|
+
* The original can be found at:
|
|
1824
|
+
* https://github.com/babel/babel/blob/main/packages/babel-traverse/src/path/evaluation.ts
|
|
1825
|
+
*
|
|
1826
|
+
* The following extensions were made:
|
|
1827
|
+
* - It can accept a mapping from variable names to functions
|
|
1828
|
+
* which when encountered will be evaluated instead of deopting.
|
|
1829
|
+
* - The functions can be configured to accept the raw path instead of
|
|
1830
|
+
* static values to handle dynamic values.
|
|
1831
|
+
* - It can handle object spreads when the spread value itself is statically evaluated.
|
|
1832
|
+
*/
|
|
1833
|
+
|
|
1834
|
+
// This file contains Babels metainterpreter that can evaluate static code.
|
|
1835
|
+
|
|
1836
|
+
const VALID_CALLEES = ['String', 'Number', 'Math'];
|
|
1837
|
+
const INVALID_METHODS = ['random'];
|
|
1838
|
+
function isValidCallee(val) {
|
|
1839
|
+
return VALID_CALLEES.includes(
|
|
1840
|
+
// @ts-expect-error
|
|
1841
|
+
val);
|
|
1842
|
+
}
|
|
1843
|
+
function isInvalidMethod(val) {
|
|
1844
|
+
return INVALID_METHODS.includes(
|
|
1845
|
+
// @ts-expect-error
|
|
1846
|
+
val);
|
|
1847
|
+
}
|
|
1848
|
+
/**
|
|
1849
|
+
* Deopts the evaluation
|
|
1850
|
+
*/
|
|
1851
|
+
function deopt(path, state) {
|
|
1852
|
+
if (!state.confident) return;
|
|
1853
|
+
state.deoptPath = path;
|
|
1854
|
+
state.confident = false;
|
|
1855
|
+
}
|
|
1856
|
+
|
|
1857
|
+
/**
|
|
1858
|
+
* We wrap the _evaluate method so we can track `seen` nodes, we push an item
|
|
1859
|
+
* to the map before we actually evaluate it so we can deopt on self recursive
|
|
1860
|
+
* nodes such as:
|
|
1861
|
+
*
|
|
1862
|
+
* var g = a ? 1 : 2,
|
|
1863
|
+
* a = g * this.foo
|
|
1864
|
+
*/
|
|
1865
|
+
function evaluateCached(path, state) {
|
|
1866
|
+
const {
|
|
1867
|
+
node
|
|
1868
|
+
} = path;
|
|
1869
|
+
const {
|
|
1870
|
+
seen
|
|
1871
|
+
} = state;
|
|
1872
|
+
if (seen.has(node)) {
|
|
1873
|
+
const existing = seen.get(node);
|
|
1874
|
+
if (existing.resolved) {
|
|
1875
|
+
return existing.value;
|
|
1876
|
+
} else {
|
|
1877
|
+
deopt(path, state);
|
|
1878
|
+
return;
|
|
1879
|
+
}
|
|
1880
|
+
} else {
|
|
1881
|
+
const item = {
|
|
1882
|
+
resolved: false
|
|
1883
|
+
};
|
|
1884
|
+
seen.set(node, item);
|
|
1885
|
+
const val = _evaluate(path, state);
|
|
1886
|
+
if (state.confident) {
|
|
1887
|
+
item.resolved = true;
|
|
1888
|
+
item.value = val;
|
|
1889
|
+
}
|
|
1890
|
+
return val;
|
|
1891
|
+
}
|
|
1892
|
+
}
|
|
1893
|
+
function _evaluate(path, state) {
|
|
1894
|
+
if (!state.confident) return;
|
|
1895
|
+
if (path.isSequenceExpression()) {
|
|
1896
|
+
const exprs = path.get('expressions');
|
|
1897
|
+
return evaluateCached(exprs[exprs.length - 1], state);
|
|
1898
|
+
}
|
|
1899
|
+
if (path.isStringLiteral() || path.isNumericLiteral() || path.isBooleanLiteral()) {
|
|
1900
|
+
return path.node.value;
|
|
1901
|
+
}
|
|
1902
|
+
if (path.isNullLiteral()) {
|
|
1903
|
+
return null;
|
|
1904
|
+
}
|
|
1905
|
+
if (path.isTemplateLiteral()) {
|
|
1906
|
+
return evaluateQuasis(path, path.node.quasis, state);
|
|
1907
|
+
}
|
|
1908
|
+
if (path.isTaggedTemplateExpression() && path.get('tag').isMemberExpression()) {
|
|
1909
|
+
const object = path.get('tag.object');
|
|
1910
|
+
const {
|
|
1911
|
+
// @ts-expect-error todo(flow->ts): possible bug, object is can be any expression and so name might be undefined
|
|
1912
|
+
node: {
|
|
1913
|
+
name
|
|
1914
|
+
}
|
|
1915
|
+
} = object;
|
|
1916
|
+
const property = path.get('tag.property');
|
|
1917
|
+
if (object.isIdentifier() && name === 'String' &&
|
|
1918
|
+
// todo(flow->ts): was changed from getBinding(name, true)
|
|
1919
|
+
// should this be hasBinding(name, true) as the binding is never used later?
|
|
1920
|
+
!path.scope.getBinding(name) && property.isIdentifier() && property.node.name === 'raw') {
|
|
1921
|
+
return evaluateQuasis(path, path.node.quasi.quasis, state, true);
|
|
1922
|
+
}
|
|
1923
|
+
}
|
|
1924
|
+
if (path.isConditionalExpression()) {
|
|
1925
|
+
const testResult = evaluateCached(path.get('test'), state);
|
|
1926
|
+
if (!state.confident) return;
|
|
1927
|
+
if (testResult) {
|
|
1928
|
+
return evaluateCached(path.get('consequent'), state);
|
|
1929
|
+
} else {
|
|
1930
|
+
return evaluateCached(path.get('alternate'), state);
|
|
1931
|
+
}
|
|
1932
|
+
}
|
|
1933
|
+
if (path.isExpressionWrapper()) {
|
|
1934
|
+
// TypeCastExpression, ExpressionStatement etc
|
|
1935
|
+
return evaluateCached(path.get('expression'), state);
|
|
1936
|
+
}
|
|
1937
|
+
|
|
1938
|
+
// "foo".length
|
|
1939
|
+
if (path.isMemberExpression() && !path.parentPath.isCallExpression({
|
|
1940
|
+
callee: path.node
|
|
1941
|
+
})) {
|
|
1942
|
+
const property = path.get('property');
|
|
1943
|
+
const object = path.get('object');
|
|
1944
|
+
if (object.isLiteral() && property.isIdentifier()) {
|
|
1945
|
+
// @ts-expect-error todo(flow->ts): instead of typeof - would it be better to check type of ast node?
|
|
1946
|
+
const value = object.node.value;
|
|
1947
|
+
const type = typeof value;
|
|
1948
|
+
if (type === 'number' || type === 'string') {
|
|
1949
|
+
return value[property.node.name];
|
|
1950
|
+
}
|
|
1951
|
+
}
|
|
1952
|
+
}
|
|
1953
|
+
if (path.isReferencedIdentifier()) {
|
|
1954
|
+
const binding = path.scope.getBinding(path.node.name);
|
|
1955
|
+
if (binding && binding.constantViolations.length > 0) {
|
|
1956
|
+
return deopt(binding.path, state);
|
|
1957
|
+
}
|
|
1958
|
+
if (binding && path.node.start < binding.path.node.end) {
|
|
1959
|
+
return deopt(binding.path, state);
|
|
1960
|
+
}
|
|
1961
|
+
if (binding !== null && binding !== void 0 && binding.hasValue) {
|
|
1962
|
+
return binding.value;
|
|
1963
|
+
} else {
|
|
1964
|
+
if (path.node.name === 'undefined') {
|
|
1965
|
+
return binding ? deopt(binding.path, state) : undefined;
|
|
1966
|
+
} else if (path.node.name === 'Infinity') {
|
|
1967
|
+
return binding ? deopt(binding.path, state) : Infinity;
|
|
1968
|
+
} else if (path.node.name === 'NaN') {
|
|
1969
|
+
return binding ? deopt(binding.path, state) : NaN;
|
|
1970
|
+
}
|
|
1971
|
+
const resolved = path.resolve();
|
|
1972
|
+
if (resolved === path) {
|
|
1973
|
+
return deopt(path, state);
|
|
1974
|
+
} else {
|
|
1975
|
+
return evaluateCached(resolved, state);
|
|
1976
|
+
}
|
|
1977
|
+
}
|
|
1978
|
+
}
|
|
1979
|
+
if (path.isUnaryExpression({
|
|
1980
|
+
prefix: true
|
|
1981
|
+
})) {
|
|
1982
|
+
if (path.node.operator === 'void') {
|
|
1983
|
+
// we don't need to evaluate the argument to know what this will return
|
|
1984
|
+
return undefined;
|
|
1985
|
+
}
|
|
1986
|
+
const argument = path.get('argument');
|
|
1987
|
+
if (path.node.operator === 'typeof' && (argument.isFunction() || argument.isClass())) {
|
|
1988
|
+
return 'function';
|
|
1989
|
+
}
|
|
1990
|
+
const arg = evaluateCached(argument, state);
|
|
1991
|
+
if (!state.confident) return;
|
|
1992
|
+
switch (path.node.operator) {
|
|
1993
|
+
case '!':
|
|
1994
|
+
return !arg;
|
|
1995
|
+
case '+':
|
|
1996
|
+
return +arg;
|
|
1997
|
+
case '-':
|
|
1998
|
+
return -arg;
|
|
1999
|
+
case '~':
|
|
2000
|
+
return ~arg;
|
|
2001
|
+
case 'typeof':
|
|
2002
|
+
return typeof arg;
|
|
2003
|
+
}
|
|
2004
|
+
}
|
|
2005
|
+
if (path.isArrayExpression()) {
|
|
2006
|
+
const arr = [];
|
|
2007
|
+
const elems = path.get('elements');
|
|
2008
|
+
for (const elem of elems) {
|
|
2009
|
+
const elemValue = evaluate(elem, state.functions);
|
|
2010
|
+
if (elemValue.confident) {
|
|
2011
|
+
arr.push(elemValue.value);
|
|
2012
|
+
} else {
|
|
2013
|
+
return deopt(elemValue.deopt, state);
|
|
2014
|
+
}
|
|
2015
|
+
}
|
|
2016
|
+
return arr;
|
|
2017
|
+
}
|
|
2018
|
+
if (path.isObjectExpression()) {
|
|
2019
|
+
const obj = {};
|
|
2020
|
+
const props = path.get('properties');
|
|
2021
|
+
for (const prop of props) {
|
|
2022
|
+
if (prop.isObjectMethod()) {
|
|
2023
|
+
return deopt(prop, state);
|
|
2024
|
+
}
|
|
2025
|
+
if (prop.isSpreadElement()) {
|
|
2026
|
+
const spreadExpression = evaluateCached(prop.get('argument'), state);
|
|
2027
|
+
if (!state.confident) {
|
|
2028
|
+
return deopt(prop, state);
|
|
2029
|
+
}
|
|
2030
|
+
Object.assign(obj, spreadExpression);
|
|
2031
|
+
continue;
|
|
2032
|
+
}
|
|
2033
|
+
const keyPath = prop.get('key');
|
|
2034
|
+
let key = keyPath;
|
|
2035
|
+
if (prop.node.computed) {
|
|
2036
|
+
key = evaluate(key);
|
|
2037
|
+
if (!key.confident) {
|
|
2038
|
+
return deopt(key.deopt, state);
|
|
2039
|
+
}
|
|
2040
|
+
key = key.value;
|
|
2041
|
+
} else if (key.isIdentifier()) {
|
|
2042
|
+
key = key.node.name;
|
|
2043
|
+
} else {
|
|
2044
|
+
key = key.node.value;
|
|
2045
|
+
}
|
|
2046
|
+
// todo(flow->ts): remove typecast
|
|
2047
|
+
const valuePath = prop.get('value');
|
|
2048
|
+
let value = evaluate(valuePath, state.functions);
|
|
2049
|
+
if (!value.confident) {
|
|
2050
|
+
return deopt(value.deopt, state);
|
|
2051
|
+
}
|
|
2052
|
+
value = value.value;
|
|
2053
|
+
// @ts-expect-error
|
|
2054
|
+
obj[key] = value;
|
|
2055
|
+
}
|
|
2056
|
+
return obj;
|
|
2057
|
+
}
|
|
2058
|
+
if (path.isLogicalExpression()) {
|
|
2059
|
+
// If we are confident that the left side of an && is false, or the left
|
|
2060
|
+
// side of an || is true, we can be confident about the entire expression
|
|
2061
|
+
const wasConfident = state.confident;
|
|
2062
|
+
const left = evaluateCached(path.get('left'), state);
|
|
2063
|
+
const leftConfident = state.confident;
|
|
2064
|
+
state.confident = wasConfident;
|
|
2065
|
+
const right = evaluateCached(path.get('right'), state);
|
|
2066
|
+
const rightConfident = state.confident;
|
|
2067
|
+
switch (path.node.operator) {
|
|
2068
|
+
case '||':
|
|
2069
|
+
// TODO consider having a "truthy type" that doesn't bail on
|
|
2070
|
+
// left uncertainty but can still evaluate to truthy.
|
|
2071
|
+
state.confident = leftConfident && (!!left || rightConfident);
|
|
2072
|
+
if (!state.confident) return;
|
|
2073
|
+
return left || right;
|
|
2074
|
+
case '&&':
|
|
2075
|
+
state.confident = leftConfident && (!left || rightConfident);
|
|
2076
|
+
if (!state.confident) return;
|
|
2077
|
+
return left && right;
|
|
2078
|
+
}
|
|
2079
|
+
}
|
|
2080
|
+
if (path.isBinaryExpression()) {
|
|
2081
|
+
const left = evaluateCached(path.get('left'), state);
|
|
2082
|
+
if (!state.confident) return;
|
|
2083
|
+
const right = evaluateCached(path.get('right'), state);
|
|
2084
|
+
if (!state.confident) return;
|
|
2085
|
+
switch (path.node.operator) {
|
|
2086
|
+
case '-':
|
|
2087
|
+
return left - right;
|
|
2088
|
+
case '+':
|
|
2089
|
+
return left + right;
|
|
2090
|
+
case '/':
|
|
2091
|
+
return left / right;
|
|
2092
|
+
case '*':
|
|
2093
|
+
return left * right;
|
|
2094
|
+
case '%':
|
|
2095
|
+
return left % right;
|
|
2096
|
+
case '**':
|
|
2097
|
+
return left ** right;
|
|
2098
|
+
case '<':
|
|
2099
|
+
return left < right;
|
|
2100
|
+
case '>':
|
|
2101
|
+
return left > right;
|
|
2102
|
+
case '<=':
|
|
2103
|
+
return left <= right;
|
|
2104
|
+
case '>=':
|
|
2105
|
+
return left >= right;
|
|
2106
|
+
case '==':
|
|
2107
|
+
return left == right;
|
|
2108
|
+
// eslint-disable-line eqeqeq
|
|
2109
|
+
case '!=':
|
|
2110
|
+
return left != right;
|
|
2111
|
+
case '===':
|
|
2112
|
+
return left === right;
|
|
2113
|
+
case '!==':
|
|
2114
|
+
return left !== right;
|
|
2115
|
+
case '|':
|
|
2116
|
+
return left | right;
|
|
2117
|
+
case '&':
|
|
2118
|
+
return left & right;
|
|
2119
|
+
case '^':
|
|
2120
|
+
return left ^ right;
|
|
2121
|
+
case '<<':
|
|
2122
|
+
return left << right;
|
|
2123
|
+
case '>>':
|
|
2124
|
+
return left >> right;
|
|
2125
|
+
case '>>>':
|
|
2126
|
+
return left >>> right;
|
|
2127
|
+
}
|
|
2128
|
+
}
|
|
2129
|
+
if (path.isCallExpression()) {
|
|
2130
|
+
const callee = path.get('callee');
|
|
2131
|
+
let context;
|
|
2132
|
+
let func;
|
|
2133
|
+
|
|
2134
|
+
// Number(1);
|
|
2135
|
+
if (callee.isIdentifier() && !path.scope.getBinding(callee.node.name) && isValidCallee(callee.node.name)) {
|
|
2136
|
+
func = global[callee.node.name];
|
|
2137
|
+
} else if (callee.isIdentifier() && state.functions.identifiers[callee.node.name]) {
|
|
2138
|
+
func = state.functions.identifiers[callee.node.name];
|
|
2139
|
+
}
|
|
2140
|
+
if (callee.isMemberExpression()) {
|
|
2141
|
+
const object = callee.get('object');
|
|
2142
|
+
const property = callee.get('property');
|
|
2143
|
+
|
|
2144
|
+
// Math.min(1, 2)
|
|
2145
|
+
if (object.isIdentifier() && property.isIdentifier()) {
|
|
2146
|
+
if (isValidCallee(object.node.name) && !isInvalidMethod(property.node.name)) {
|
|
2147
|
+
context = global[object.node.name];
|
|
2148
|
+
// @ts-expect-error property may not exist in context object
|
|
2149
|
+
func = context[property.node.name];
|
|
2150
|
+
} else if (state.functions.memberExpressions[object.node.name] && state.functions.memberExpressions[object.node.name][property.node.name]) {
|
|
2151
|
+
context = state.functions.memberExpressions[object.node.name];
|
|
2152
|
+
func = context[property.node.name];
|
|
2153
|
+
}
|
|
2154
|
+
}
|
|
2155
|
+
if (object.isIdentifier() && property.isStringLiteral() && state.functions.memberExpressions[object.node.name] && state.functions.memberExpressions[object.node.name][property.node.value]) {
|
|
2156
|
+
context = state.functions.memberExpressions[object.node.name];
|
|
2157
|
+
func = context[property.node.value];
|
|
2158
|
+
}
|
|
2159
|
+
|
|
2160
|
+
// "abc".charCodeAt(4)
|
|
2161
|
+
if (object.isLiteral() && property.isIdentifier()) {
|
|
2162
|
+
// @ts-expect-error todo(flow->ts): consider checking ast node type instead of value type (StringLiteral and NumberLiteral)
|
|
2163
|
+
const type = typeof object.node.value;
|
|
2164
|
+
if (type === 'string' || type === 'number') {
|
|
2165
|
+
// @ts-expect-error todo(flow->ts): consider checking ast node type instead of value type
|
|
2166
|
+
context = object.node.value;
|
|
2167
|
+
func = context[property.node.name];
|
|
2168
|
+
}
|
|
2169
|
+
}
|
|
2170
|
+
}
|
|
2171
|
+
if (func) {
|
|
2172
|
+
if (func.takesPath) {
|
|
2173
|
+
const args = path.get('arguments');
|
|
2174
|
+
return func.fn(...args);
|
|
2175
|
+
} else {
|
|
2176
|
+
const args = path.get('arguments').map(arg => evaluateCached(arg, state));
|
|
2177
|
+
if (!state.confident) return;
|
|
2178
|
+
if (func.fn) {
|
|
2179
|
+
return func.fn.apply(context, args);
|
|
2180
|
+
} else {
|
|
2181
|
+
return func.apply(context, args);
|
|
2182
|
+
}
|
|
2183
|
+
}
|
|
2184
|
+
}
|
|
2185
|
+
}
|
|
2186
|
+
deopt(path, state);
|
|
2187
|
+
}
|
|
2188
|
+
function evaluateQuasis(path, quasis, state) {
|
|
2189
|
+
let raw = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
|
|
2190
|
+
let str = '';
|
|
2191
|
+
let i = 0;
|
|
2192
|
+
const exprs = path.get('expressions');
|
|
2193
|
+
for (const elem of quasis) {
|
|
2194
|
+
// not confident, evaluated an expression we don't like
|
|
2195
|
+
if (!state.confident) break;
|
|
2196
|
+
|
|
2197
|
+
// add on element
|
|
2198
|
+
str += raw ? elem.value.raw : elem.value.cooked;
|
|
2199
|
+
|
|
2200
|
+
// add on interpolated expression if it's present
|
|
2201
|
+
const expr = exprs[i++];
|
|
2202
|
+
if (expr) str += String(evaluateCached(expr, state));
|
|
2203
|
+
}
|
|
2204
|
+
if (!state.confident) return;
|
|
2205
|
+
return str;
|
|
2206
|
+
}
|
|
2207
|
+
|
|
2208
|
+
/**
|
|
2209
|
+
* Walk the input `node` and statically evaluate it.
|
|
2210
|
+
*
|
|
2211
|
+
* Returns an object in the form `{ confident, value, deopt }`. `confident`
|
|
2212
|
+
* indicates whether or not we had to drop out of evaluating the expression
|
|
2213
|
+
* because of hitting an unknown node that we couldn't confidently find the
|
|
2214
|
+
* value of, in which case `deopt` is the path of said node.
|
|
2215
|
+
*
|
|
2216
|
+
* Example:
|
|
2217
|
+
*
|
|
2218
|
+
* evaluate(parse("5 + 5")) // { confident: true, value: 10 }
|
|
2219
|
+
* evaluate(parse("!true")) // { confident: true, value: false }
|
|
2220
|
+
* evaluate(parse("foo + foo")) // { confident: false, value: undefined, deopt: NodePath }
|
|
2221
|
+
*
|
|
2222
|
+
*/
|
|
2223
|
+
|
|
2224
|
+
function evaluate(path) {
|
|
2225
|
+
let functions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
|
|
2226
|
+
identifiers: {},
|
|
2227
|
+
memberExpressions: {}
|
|
2228
|
+
};
|
|
2229
|
+
const state = {
|
|
2230
|
+
confident: true,
|
|
2231
|
+
deoptPath: null,
|
|
2232
|
+
seen: new Map(),
|
|
2233
|
+
functions
|
|
2234
|
+
};
|
|
2235
|
+
let value = evaluateCached(path, state);
|
|
2236
|
+
if (!state.confident) value = undefined;
|
|
2237
|
+
return {
|
|
2238
|
+
confident: state.confident,
|
|
2239
|
+
deopt: state.deoptPath,
|
|
2240
|
+
value: value
|
|
2241
|
+
};
|
|
2242
|
+
}
|
|
2243
|
+
|
|
2244
|
+
/**
|
|
2245
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2246
|
+
*
|
|
2247
|
+
* This source code is licensed under the MIT license found in the
|
|
2248
|
+
* LICENSE file in the root directory of this source tree.
|
|
2249
|
+
*/
|
|
2250
|
+
|
|
2251
|
+
/// This function looks for `stylex.create` calls and transforms them.
|
|
2252
|
+
//. 1. It finds the first argument to `stylex.create` and validates it.
|
|
2253
|
+
/// 2. It pre-processes valid-dynamic parts of style object such as custom presets (spreads)
|
|
2254
|
+
/// 3. It envalues the style object to get a JS object. This also handles local constants automatically.
|
|
2255
|
+
/// 4. It uses the `stylexCreate` from `@stylexjs/shared` to transform the JS
|
|
2256
|
+
/// object and to get a list of injected styles.
|
|
2257
|
+
/// 5. It converts the resulting Object back into an AST and replaces the call with it.
|
|
2258
|
+
/// 6. It also inserts `stylex.inject` calls above the current statement as needed.
|
|
2259
|
+
function transformStyleXCreate(path, state) {
|
|
2260
|
+
const {
|
|
2261
|
+
node
|
|
2262
|
+
} = path;
|
|
2263
|
+
if (node.type !== 'CallExpression') {
|
|
2264
|
+
return;
|
|
2265
|
+
}
|
|
2266
|
+
if (node.callee.type === 'Identifier' && state.stylexCreateImport.has(node.callee.name) || node.callee.type === 'MemberExpression' && node.callee.object.type === 'Identifier' && node.callee.property.type === 'Identifier' && state.stylexImport.has(node.callee.object.name) && node.callee.property.name === 'create') {
|
|
2267
|
+
validateStyleXCreate(path);
|
|
2268
|
+
const args = path.get('arguments');
|
|
2269
|
+
const firstArg = args[0];
|
|
2270
|
+
if (firstArg == null || !firstArg.isObjectExpression()) {
|
|
2271
|
+
throw new Error(messages_1.ILLEGAL_ARGUMENT_LENGTH);
|
|
2272
|
+
}
|
|
2273
|
+
const processingState = preProcessStyleArg(firstArg);
|
|
2274
|
+
const injectedKeyframes = {};
|
|
2275
|
+
function keyframes(animation) {
|
|
2276
|
+
const [animationName, injectedStyle] = keyframes_1(animation, state.options);
|
|
2277
|
+
injectedKeyframes[animationName] = injectedStyle;
|
|
2278
|
+
return animationName;
|
|
2279
|
+
}
|
|
2280
|
+
const identifiers = {};
|
|
2281
|
+
const memberExpressions = {};
|
|
2282
|
+
state.stylexIncludeImport.forEach(name => {
|
|
2283
|
+
identifiers[name] = {
|
|
2284
|
+
fn: include_1,
|
|
2285
|
+
takesPath: true
|
|
2286
|
+
};
|
|
2287
|
+
});
|
|
2288
|
+
state.stylexFirstThatWorksImport.forEach(name => {
|
|
2289
|
+
identifiers[name] = {
|
|
2290
|
+
fn: firstThatWorks_1
|
|
2291
|
+
};
|
|
2292
|
+
});
|
|
2293
|
+
state.stylexKeyframesImport.forEach(name => {
|
|
2294
|
+
identifiers[name] = {
|
|
2295
|
+
fn: keyframes
|
|
2296
|
+
};
|
|
2297
|
+
});
|
|
2298
|
+
state.stylexImport.forEach(name => {
|
|
2299
|
+
if (memberExpressions[name] == null) {
|
|
2300
|
+
memberExpressions[name] = {};
|
|
2301
|
+
}
|
|
2302
|
+
memberExpressions[name].include = {
|
|
2303
|
+
fn: include_1,
|
|
2304
|
+
takesPath: true
|
|
2305
|
+
};
|
|
2306
|
+
memberExpressions[name].firstThatWorks = {
|
|
2307
|
+
fn: firstThatWorks_1
|
|
2308
|
+
};
|
|
2309
|
+
memberExpressions[name].keyframes = {
|
|
2310
|
+
fn: keyframes
|
|
2311
|
+
};
|
|
2312
|
+
});
|
|
2313
|
+
const {
|
|
2314
|
+
confident,
|
|
2315
|
+
value
|
|
2316
|
+
} = evaluate(firstArg, {
|
|
2317
|
+
identifiers,
|
|
2318
|
+
memberExpressions
|
|
2319
|
+
});
|
|
2320
|
+
if (!confident) {
|
|
2321
|
+
throw new Error(messages_1.NON_STATIC_VALUE);
|
|
2322
|
+
}
|
|
2323
|
+
const plainObject = value;
|
|
2324
|
+
let [compiledStyles, injectedStylesSansKeyframes] = create_1(plainObject, state.options);
|
|
2325
|
+
const injectedStyles = {
|
|
2326
|
+
...injectedKeyframes,
|
|
2327
|
+
...injectedStylesSansKeyframes
|
|
2328
|
+
};
|
|
2329
|
+
let varName = null;
|
|
2330
|
+
if (path.parentPath.isVariableDeclarator()) {
|
|
2331
|
+
const idNode = path.parentPath.node.id;
|
|
2332
|
+
if (idNode.type === 'Identifier') {
|
|
2333
|
+
varName = idNode.name;
|
|
2334
|
+
}
|
|
2335
|
+
}
|
|
2336
|
+
if (state.isTest) {
|
|
2337
|
+
compiledStyles = convertToTestStyles(compiledStyles, varName, state);
|
|
2338
|
+
} else if (state.isDev) {
|
|
2339
|
+
compiledStyles = injectDevClassNames(compiledStyles, varName, state);
|
|
2340
|
+
}
|
|
2341
|
+
if (varName != null) {
|
|
2342
|
+
state.styleMap.set(varName, compiledStyles);
|
|
2343
|
+
state.styleVars.set(varName, path.parentPath);
|
|
2344
|
+
}
|
|
2345
|
+
path.replaceWith(convertObjectToAST(compiledStyles));
|
|
2346
|
+
postProcessStyles(path, processingState);
|
|
2347
|
+
if (Object.keys(injectedStyles).length === 0) {
|
|
2348
|
+
return;
|
|
2349
|
+
}
|
|
2350
|
+
if (state.isDev || state.stylexSheetName == null) {
|
|
2351
|
+
const statementPath = findNearestStatementAncestor(path);
|
|
2352
|
+
let stylexName;
|
|
2353
|
+
state.stylexImport.forEach(importName => {
|
|
2354
|
+
stylexName = importName;
|
|
2355
|
+
});
|
|
2356
|
+
if (stylexName == null) {
|
|
2357
|
+
stylexName = '__stylex__';
|
|
2358
|
+
statementPath.insertBefore(t__namespace.importDeclaration([t__namespace.importDefaultSpecifier(t__namespace.identifier(stylexName))], t__namespace.stringLiteral('stylex')));
|
|
2359
|
+
}
|
|
2360
|
+
for (const [key, {
|
|
2361
|
+
ltr,
|
|
2362
|
+
priority,
|
|
2363
|
+
rtl
|
|
2364
|
+
}] of Object.entries(injectedStyles)) {
|
|
2365
|
+
statementPath.insertBefore(t__namespace.expressionStatement(t__namespace.callExpression(t__namespace.memberExpression(t__namespace.identifier(stylexName), t__namespace.identifier('inject')), [t__namespace.stringLiteral(ltr), t__namespace.numericLiteral(priority), ...(rtl != null ? [t__namespace.stringLiteral(rtl)] : [])])));
|
|
2366
|
+
}
|
|
2367
|
+
}
|
|
2368
|
+
for (const [key, {
|
|
2369
|
+
priority,
|
|
2370
|
+
...rest
|
|
2371
|
+
}] of Object.entries(injectedStyles)) {
|
|
2372
|
+
state.addStyle([key, rest, priority]);
|
|
2373
|
+
}
|
|
2374
|
+
}
|
|
2375
|
+
}
|
|
2376
|
+
|
|
2377
|
+
// Validates the first argument to `stylex.create`.
|
|
2378
|
+
function validateStyleXCreate(path) {
|
|
2379
|
+
if (path.parentPath == null || path.parentPath.isExpressionStatement()) {
|
|
2380
|
+
throw new Error(messages_1.UNBOUND_STYLEX_CALL_VALUE);
|
|
2381
|
+
}
|
|
2382
|
+
const nearestStatement = findNearestStatementAncestor(path);
|
|
2383
|
+
if (!nearestStatement.parentPath.isProgram() && !nearestStatement.parentPath.isExportNamedDeclaration()) {
|
|
2384
|
+
throw new Error(messages_1.ONLY_TOP_LEVEL);
|
|
2385
|
+
}
|
|
2386
|
+
if (path.node.arguments.length !== 1) {
|
|
2387
|
+
throw new Error(messages_1.ILLEGAL_ARGUMENT_LENGTH);
|
|
2388
|
+
}
|
|
2389
|
+
if (path.node.arguments[0].type !== 'ObjectExpression') {
|
|
2390
|
+
throw new Error(messages_1.NON_OBJECT_FOR_STYLEX_CALL);
|
|
2391
|
+
}
|
|
2392
|
+
}
|
|
2393
|
+
|
|
2394
|
+
// Find the nearest statement ancestor of a given path.
|
|
2395
|
+
function findNearestStatementAncestor(path) {
|
|
2396
|
+
if (path.isStatement()) {
|
|
2397
|
+
return path;
|
|
2398
|
+
}
|
|
2399
|
+
if (path.parentPath == null) {
|
|
2400
|
+
throw new Error('Unexpected Path found that is not part of the AST.');
|
|
2401
|
+
}
|
|
2402
|
+
return findNearestStatementAncestor(path.parentPath);
|
|
2403
|
+
}
|
|
2404
|
+
|
|
2405
|
+
// These functions are for special handling of dynamic parts of the style object
|
|
2406
|
+
// Currently they handle spreading pre-defined style objects with stylex.create calls.
|
|
2407
|
+
//
|
|
2408
|
+
// It converts the spreads into a special string and remembers the original value in an object...
|
|
2409
|
+
|
|
2410
|
+
function preProcessStyleArg(objPath) {
|
|
2411
|
+
const state = {};
|
|
2412
|
+
objPath.traverse({
|
|
2413
|
+
SpreadElement(path) {
|
|
2414
|
+
const argument = path.get('argument');
|
|
2415
|
+
if (!argument.isTypeCastExpression()) {
|
|
2416
|
+
return;
|
|
2417
|
+
}
|
|
2418
|
+
const expression = argument.get('expression');
|
|
2419
|
+
if (!expression.isIdentifier() && !expression.isMemberExpression()) {
|
|
2420
|
+
throw new Error(messages_1.ILLEGAL_NAMESPACE_VALUE);
|
|
2421
|
+
}
|
|
2422
|
+
if (!(path.parentPath.isObjectExpression() &&
|
|
2423
|
+
// namespaceObject
|
|
2424
|
+
path.parentPath.parentPath.isObjectProperty() &&
|
|
2425
|
+
// namespaceProperty
|
|
2426
|
+
path.parentPath.parentPath.parentPath.isObjectExpression() &&
|
|
2427
|
+
// stylex.create argument
|
|
2428
|
+
path.parentPath.parentPath.parentPath.parentPath.isCallExpression()
|
|
2429
|
+
// stylex.create
|
|
2430
|
+
)) {
|
|
2431
|
+
// Disallow spreads within pseudo or media query objects
|
|
2432
|
+
throw new Error(messages_1.ILLEGAL_NESTED_PSEUDO);
|
|
2433
|
+
}
|
|
2434
|
+
const key = `include(${toString(expression)})`;
|
|
2435
|
+
path.replaceWith(t__namespace.objectProperty(t__namespace.stringLiteral(key), t__namespace.stringLiteral(key)));
|
|
2436
|
+
state[key] = expression.node;
|
|
2437
|
+
}
|
|
2438
|
+
});
|
|
2439
|
+
return state;
|
|
2440
|
+
}
|
|
2441
|
+
|
|
2442
|
+
// Later, it finds those strings and replaces them back with their original values.
|
|
2443
|
+
function postProcessStyles(objPath, state) {
|
|
2444
|
+
objPath.traverse({
|
|
2445
|
+
ObjectProperty(path) {
|
|
2446
|
+
const node = path.node.key;
|
|
2447
|
+
if (node.type === 'StringLiteral' && state[node.value] != null) {
|
|
2448
|
+
path.replaceWith(t__namespace.spreadElement(state[node.value]));
|
|
2449
|
+
}
|
|
2450
|
+
}
|
|
2451
|
+
});
|
|
2452
|
+
}
|
|
2453
|
+
|
|
2454
|
+
// A function to deterministicly convert a spreadded expression to a string.
|
|
2455
|
+
function toString(path) {
|
|
2456
|
+
if (path.isIdentifier()) {
|
|
2457
|
+
return path.node.name;
|
|
2458
|
+
} else if (path.isStringLiteral() || path.isNumericLiteral()) {
|
|
2459
|
+
return String(path.node.value);
|
|
2460
|
+
} else if (path.isMemberExpression()) {
|
|
2461
|
+
return `${toString(path.get('object'))}.${toString(path.get('property'))}`;
|
|
2462
|
+
}
|
|
2463
|
+
throw new Error(path.node.type);
|
|
2464
|
+
}
|
|
2465
|
+
|
|
2466
|
+
/**
|
|
2467
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2468
|
+
*
|
|
2469
|
+
* This source code is licensed under the MIT license found in the
|
|
2470
|
+
* LICENSE file in the root directory of this source tree.
|
|
2471
|
+
*/
|
|
2472
|
+
|
|
2473
|
+
/// This function looks for `stylex.keyframes` calls and transforms them.
|
|
2474
|
+
//. 1. It finds the first argument to `stylex.keyframes` and validates it.
|
|
2475
|
+
/// 2. It envalues the style object to get a JS object. This also handles local constants automatically.
|
|
2476
|
+
/// 4. It uses the `stylexKeyframes` from `@stylexjs/shared` to transform the JS
|
|
2477
|
+
/// object and to get a list of injected styles.
|
|
2478
|
+
/// 5. It converts the resulting Object back into an AST and replaces the call with it.
|
|
2479
|
+
/// 6. It also inserts `stylex.inject` calls above the current statement as needed.
|
|
2480
|
+
function transformStyleXKeyframes(path, state) {
|
|
2481
|
+
var _node$init;
|
|
2482
|
+
const {
|
|
2483
|
+
node
|
|
2484
|
+
} = path;
|
|
2485
|
+
if (((_node$init = node.init) === null || _node$init === void 0 ? void 0 : _node$init.type) !== 'CallExpression') {
|
|
2486
|
+
return;
|
|
2487
|
+
}
|
|
2488
|
+
if (node.id.type !== 'Identifier') {
|
|
2489
|
+
return;
|
|
2490
|
+
}
|
|
2491
|
+
if (node.init.callee.type === 'Identifier' && state.stylexKeyframesImport.has(node.init.callee.name) || node.init.callee.type === 'MemberExpression' && node.init.callee.object.type === 'Identifier' && node.init.callee.property.type === 'Identifier' && state.stylexImport.has(node.init.callee.object.name) && node.init.callee.property.name === 'keyframes') {
|
|
2492
|
+
if (node.init.arguments.length !== 1) {
|
|
2493
|
+
throw new Error(messages_1.ILLEGAL_ARGUMENT_LENGTH);
|
|
2494
|
+
}
|
|
2495
|
+
if (node.init.arguments[0].type !== 'ObjectExpression') {
|
|
2496
|
+
throw new Error(messages_1.NON_OBJECT_FOR_STYLEX_CALL);
|
|
2497
|
+
}
|
|
2498
|
+
const init = path.get('init');
|
|
2499
|
+
const args = init.get('arguments');
|
|
2500
|
+
const firstArg = args[0];
|
|
2501
|
+
|
|
2502
|
+
// TODO: This doesn't support nested function calls.
|
|
2503
|
+
// So when we add those, we'll need to replace this with an
|
|
2504
|
+
// expanded fork of `evaluate` from `@babel/traverse.
|
|
2505
|
+
const {
|
|
2506
|
+
confident,
|
|
2507
|
+
value
|
|
2508
|
+
} = firstArg.evaluate();
|
|
2509
|
+
if (!confident) {
|
|
2510
|
+
throw new Error(messages_1.NON_STATIC_VALUE);
|
|
2511
|
+
}
|
|
2512
|
+
const plainObject = value;
|
|
2513
|
+
assertValidKeyframes(plainObject);
|
|
2514
|
+
let [animationName, injectedStyle] = keyframes_1(plainObject, state.options);
|
|
2515
|
+
|
|
2516
|
+
// This should be a string
|
|
2517
|
+
path.get('init').replaceWith(t__namespace.stringLiteral(animationName));
|
|
2518
|
+
const {
|
|
2519
|
+
ltr,
|
|
2520
|
+
priority,
|
|
2521
|
+
rtl
|
|
2522
|
+
} = injectedStyle;
|
|
2523
|
+
if (state.isDev || state.stylexSheetName == null) {
|
|
2524
|
+
// We know that the parent path is a variable declaration
|
|
2525
|
+
const statementPath = path.parentPath;
|
|
2526
|
+
let stylexName;
|
|
2527
|
+
state.stylexImport.forEach(importName => {
|
|
2528
|
+
stylexName = importName;
|
|
2529
|
+
});
|
|
2530
|
+
if (stylexName == null) {
|
|
2531
|
+
stylexName = '__stylex__';
|
|
2532
|
+
statementPath.insertBefore(t__namespace.importDeclaration([t__namespace.importDefaultSpecifier(t__namespace.identifier(stylexName))], t__namespace.stringLiteral('stylex')));
|
|
2533
|
+
}
|
|
2534
|
+
statementPath.insertBefore(t__namespace.expressionStatement(t__namespace.callExpression(t__namespace.memberExpression(t__namespace.identifier(stylexName), t__namespace.identifier('inject')), [t__namespace.stringLiteral(ltr), t__namespace.numericLiteral(priority), ...(rtl != null ? [t__namespace.stringLiteral(rtl)] : [])])));
|
|
2535
|
+
}
|
|
2536
|
+
state.addStyle([animationName, {
|
|
2537
|
+
ltr,
|
|
2538
|
+
rtl
|
|
2539
|
+
}, priority]);
|
|
2540
|
+
}
|
|
2541
|
+
}
|
|
2542
|
+
// Validation of `stylex.keyframes` function call.
|
|
2543
|
+
function assertValidKeyframes(obj) {
|
|
2544
|
+
if (typeof obj !== 'object' || Array.isArray(obj) || obj == null) {
|
|
2545
|
+
throw new Error(messages_1.NON_OBJECT_FOR_STYLEX_CALL);
|
|
2546
|
+
}
|
|
2547
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
2548
|
+
if (typeof value !== 'object' || Array.isArray(value)) {
|
|
2549
|
+
throw new Error(messages_1.ILLEGAL_NAMESPACE_VALUE);
|
|
2550
|
+
}
|
|
2551
|
+
}
|
|
2552
|
+
}
|
|
2553
|
+
|
|
2554
|
+
/**
|
|
2555
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2556
|
+
*
|
|
2557
|
+
* This source code is licensed under the MIT license found in the
|
|
2558
|
+
* LICENSE file in the root directory of this source tree.
|
|
2559
|
+
*/
|
|
2560
|
+
function skipStylexMergeChildren(path, state) {
|
|
2561
|
+
const {
|
|
2562
|
+
node
|
|
2563
|
+
} = path;
|
|
2564
|
+
if (node == null || node.callee.type !== 'Identifier' || !state.stylexImport.has(node.callee.name)) {
|
|
2565
|
+
return;
|
|
2566
|
+
}
|
|
2567
|
+
path.skip();
|
|
2568
|
+
}
|
|
2569
|
+
|
|
2570
|
+
// If a `stylex()` call uses styles that are all locally defined,
|
|
2571
|
+
// This function is able to pre-compute that into a single string or
|
|
2572
|
+
// a single expression of strings and ternary expressions.
|
|
2573
|
+
function transformStyleXMerge(path, state) {
|
|
2574
|
+
const {
|
|
2575
|
+
node
|
|
2576
|
+
} = path;
|
|
2577
|
+
if (node == null || node.callee.type !== 'Identifier' || !state.stylexImport.has(node.callee.name)) {
|
|
2578
|
+
return;
|
|
2579
|
+
}
|
|
2580
|
+
let bailOut = false;
|
|
2581
|
+
const resolvedStyles = {};
|
|
2582
|
+
for (const arg of node.arguments) {
|
|
2583
|
+
switch (arg.type) {
|
|
2584
|
+
case 'MemberExpression':
|
|
2585
|
+
const {
|
|
2586
|
+
property,
|
|
2587
|
+
object,
|
|
2588
|
+
computed
|
|
2589
|
+
} = arg;
|
|
2590
|
+
let objName = null;
|
|
2591
|
+
let propName = null;
|
|
2592
|
+
if (object.type === 'Identifier' && state.styleMap.has(object.name) && property.type === 'Identifier' && !computed) {
|
|
2593
|
+
objName = object.name;
|
|
2594
|
+
propName = property.name;
|
|
2595
|
+
}
|
|
2596
|
+
if (object.type === 'Identifier' && state.styleMap.has(object.name) && (property.type === 'StringLiteral' || property.type === 'NumericLiteral') && computed) {
|
|
2597
|
+
objName = object.name;
|
|
2598
|
+
propName = property.value;
|
|
2599
|
+
}
|
|
2600
|
+
if (objName != null && propName != null) {
|
|
2601
|
+
const style = state.styleMap.get(objName);
|
|
2602
|
+
if (style == null || style[propName] == null) {
|
|
2603
|
+
throw new Error(`Unknown style ${objName}.${propName}. The defined style ${objName}, contains the following keys: ${Object.keys(style ?? {}).join(', ')}`);
|
|
2604
|
+
}
|
|
2605
|
+
const namespace = flattenObject(style[propName]);
|
|
2606
|
+
Object.assign(resolvedStyles, namespace);
|
|
2607
|
+
} else {
|
|
2608
|
+
// Unknown style found. bail out.
|
|
2609
|
+
bailOut = true;
|
|
2610
|
+
}
|
|
2611
|
+
break;
|
|
2612
|
+
case 'ConditionalExpression':
|
|
2613
|
+
const {
|
|
2614
|
+
test,
|
|
2615
|
+
consequent,
|
|
2616
|
+
alternate
|
|
2617
|
+
} = arg;
|
|
2618
|
+
const primary = parseNullableStyle(consequent, state);
|
|
2619
|
+
const fallback = parseNullableStyle(alternate, state);
|
|
2620
|
+
if (primary === 'other' || fallback === 'other') {
|
|
2621
|
+
bailOut = true;
|
|
2622
|
+
break;
|
|
2623
|
+
}
|
|
2624
|
+
if (primary === null && fallback === null) {
|
|
2625
|
+
// A no-op
|
|
2626
|
+
break;
|
|
2627
|
+
}
|
|
2628
|
+
const allKeys = new Set([...Object.keys(primary ?? {}), ...Object.keys(fallback ?? {})]);
|
|
2629
|
+
for (const key of allKeys) {
|
|
2630
|
+
if (resolvedStyles[key] == null) {
|
|
2631
|
+
const primaryValue = (primary === null || primary === void 0 ? void 0 : primary[key]) ?? resolvedStyles[key] ?? '';
|
|
2632
|
+
const fallbackValue = (fallback === null || fallback === void 0 ? void 0 : fallback[key]) ?? resolvedStyles[key] ?? '';
|
|
2633
|
+
resolvedStyles[key] = [test, primaryValue, fallbackValue];
|
|
2634
|
+
}
|
|
2635
|
+
}
|
|
2636
|
+
break;
|
|
2637
|
+
case 'LogicalExpression':
|
|
2638
|
+
if (arg.operator !== '&&') {
|
|
2639
|
+
bailOut = true;
|
|
2640
|
+
break;
|
|
2641
|
+
}
|
|
2642
|
+
const {
|
|
2643
|
+
left,
|
|
2644
|
+
right
|
|
2645
|
+
} = arg;
|
|
2646
|
+
if (left.type === 'MemberExpression' && left.object.type === 'Identifier' && state.styleMap.has(left.object.name)) {
|
|
2647
|
+
// We don't support `a && b` in stylex calls where `a` is a style namespace.
|
|
2648
|
+
bailOut = true;
|
|
2649
|
+
break;
|
|
2650
|
+
}
|
|
2651
|
+
const value = parseNullableStyle(right, state);
|
|
2652
|
+
if (value === 'other') {
|
|
2653
|
+
bailOut = true;
|
|
2654
|
+
break;
|
|
2655
|
+
}
|
|
2656
|
+
if (value === null) {
|
|
2657
|
+
// A no-op??
|
|
2658
|
+
break;
|
|
2659
|
+
}
|
|
2660
|
+
for (const [key, val] of Object.entries(value)) {
|
|
2661
|
+
resolvedStyles[key] = [left, val, resolvedStyles[key] ?? ''];
|
|
2662
|
+
}
|
|
2663
|
+
break;
|
|
2664
|
+
default:
|
|
2665
|
+
bailOut = true;
|
|
2666
|
+
break;
|
|
2667
|
+
}
|
|
2668
|
+
}
|
|
2669
|
+
if (bailOut) {
|
|
2670
|
+
path.traverse({
|
|
2671
|
+
MemberExpression(path) {
|
|
2672
|
+
const object = path.get('object').node;
|
|
2673
|
+
const property = path.get('property').node;
|
|
2674
|
+
const computed = path.node.computed;
|
|
2675
|
+
let objName = null;
|
|
2676
|
+
let propName = null;
|
|
2677
|
+
if (object.type === 'Identifier' && state.styleMap.has(object.name)) {
|
|
2678
|
+
objName = object.name;
|
|
2679
|
+
if (property.type === 'Identifier' && !computed) {
|
|
2680
|
+
propName = property.name;
|
|
2681
|
+
}
|
|
2682
|
+
if ((property.type === 'StringLiteral' || property.type === 'NumericLiteral') && computed) {
|
|
2683
|
+
propName = property.value;
|
|
2684
|
+
}
|
|
2685
|
+
}
|
|
2686
|
+
if (objName != null) {
|
|
2687
|
+
state.styleVarsToKeep.add([objName, propName != null ? String(propName) : null]);
|
|
2688
|
+
}
|
|
2689
|
+
}
|
|
2690
|
+
});
|
|
2691
|
+
} else {
|
|
2692
|
+
path.skip();
|
|
2693
|
+
// convert resolvedStyles to a string + ternary expressions
|
|
2694
|
+
// We no longer need the keys, so we can just use the values.
|
|
2695
|
+
const stringExpression = makeStringExpression(Object.values(resolvedStyles));
|
|
2696
|
+
path.replaceWith(stringExpression);
|
|
2697
|
+
}
|
|
2698
|
+
}
|
|
2699
|
+
|
|
2700
|
+
// This function takes nested objects with styles collapses them to a single level deep.
|
|
2701
|
+
// `':hover': {color: 'red'}` becomes `':hover.color': 'red'`
|
|
2702
|
+
function flattenObject(object) {
|
|
2703
|
+
const result = {};
|
|
2704
|
+
for (const [key, value] of Object.entries(object)) {
|
|
2705
|
+
if (typeof value === 'string') {
|
|
2706
|
+
result[key] = value;
|
|
2707
|
+
} else if (typeof value === 'object') {
|
|
2708
|
+
for (const [subKey, subValue] of Object.entries(value)) {
|
|
2709
|
+
result[`${key}.${subKey}`] = subValue;
|
|
2710
|
+
}
|
|
2711
|
+
}
|
|
2712
|
+
}
|
|
2713
|
+
return result;
|
|
2714
|
+
}
|
|
2715
|
+
|
|
2716
|
+
// Looks for Null or locally defined style namespaces.
|
|
2717
|
+
// Otherwise it returns the string "other"
|
|
2718
|
+
// Which is used as an indicator to bail out of this optimization.
|
|
2719
|
+
function parseNullableStyle(node, state) {
|
|
2720
|
+
if (t__namespace.isNullLiteral(node) || t__namespace.isIdentifier(node) && node.name === 'undefined') {
|
|
2721
|
+
return null;
|
|
2722
|
+
}
|
|
2723
|
+
if (t__namespace.isMemberExpression(node)) {
|
|
2724
|
+
const {
|
|
2725
|
+
object: obj,
|
|
2726
|
+
property: prop,
|
|
2727
|
+
computed: computed
|
|
2728
|
+
} = node;
|
|
2729
|
+
if (obj.type !== 'Identifier' || !state.styleMap.has(obj.name) || prop.type !== 'Identifier' || computed) {
|
|
2730
|
+
return 'other';
|
|
2731
|
+
}
|
|
2732
|
+
const namespace1 = state.styleMap.get(obj.name);
|
|
2733
|
+
if (namespace1 == null) {
|
|
2734
|
+
return 'other';
|
|
2735
|
+
}
|
|
2736
|
+
const style = namespace1[prop.name];
|
|
2737
|
+
if (style == null) {
|
|
2738
|
+
return 'other';
|
|
2739
|
+
}
|
|
2740
|
+
return flattenObject(style);
|
|
2741
|
+
} else {
|
|
2742
|
+
return 'other';
|
|
2743
|
+
}
|
|
2744
|
+
}
|
|
2745
|
+
function makeStringExpression(values) {
|
|
2746
|
+
let inTernary = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
2747
|
+
// To start let's split the plain strings and everything else.
|
|
2748
|
+
let strings = values.filter(value => typeof value === 'string').join(' ').trim();
|
|
2749
|
+
if (inTernary && strings !== '') {
|
|
2750
|
+
strings = ' ' + strings;
|
|
2751
|
+
}
|
|
2752
|
+
const nonStrings = values.filter(value => typeof value !== 'string');
|
|
2753
|
+
const groupedByTest = groupBy(nonStrings, _ref => {
|
|
2754
|
+
let [test, _a, _b] = _ref;
|
|
2755
|
+
return test;
|
|
2756
|
+
});
|
|
2757
|
+
const eachTernary = [...groupedByTest.entries()].map(_ref2 => {
|
|
2758
|
+
let [test, value] = _ref2;
|
|
2759
|
+
const consequents = makeStringExpression(value.map(_ref3 => {
|
|
2760
|
+
let [_a, a, _b] = _ref3;
|
|
2761
|
+
return a;
|
|
2762
|
+
}), true);
|
|
2763
|
+
const fallbacks = makeStringExpression(value.map(_ref4 => {
|
|
2764
|
+
let [_a, _b, b] = _ref4;
|
|
2765
|
+
return b;
|
|
2766
|
+
}), true);
|
|
2767
|
+
return t__namespace.conditionalExpression(test, consequents, fallbacks);
|
|
2768
|
+
});
|
|
2769
|
+
return addAll([t__namespace.stringLiteral(strings), ...eachTernary]);
|
|
2770
|
+
}
|
|
2771
|
+
function addAll(_ref5) {
|
|
2772
|
+
let [first, ...nodes] = _ref5;
|
|
2773
|
+
if (nodes.length === 0) {
|
|
2774
|
+
return first;
|
|
2775
|
+
}
|
|
2776
|
+
return t__namespace.binaryExpression('+', first, addAll(nodes));
|
|
2777
|
+
}
|
|
2778
|
+
|
|
2779
|
+
// Array groupBy function
|
|
2780
|
+
function groupBy(array, key) {
|
|
2781
|
+
const result = new Map();
|
|
2782
|
+
for (const item of array) {
|
|
2783
|
+
const k = key(item);
|
|
2784
|
+
const array = result.get(k) ?? [];
|
|
2785
|
+
array.push(item);
|
|
2786
|
+
result.set(k, array);
|
|
2787
|
+
}
|
|
2788
|
+
return result;
|
|
2789
|
+
}
|
|
2790
|
+
|
|
2791
|
+
/**
|
|
2792
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2793
|
+
*
|
|
2794
|
+
* This source code is licensed under the MIT license found in the
|
|
2795
|
+
* LICENSE file in the root directory of this source tree.
|
|
2796
|
+
*/
|
|
2797
|
+
const NAME = 'stylex';
|
|
2798
|
+
|
|
2799
|
+
/**
|
|
2800
|
+
* Entry point for the StyleX babel plugin.
|
|
2801
|
+
*/
|
|
2802
|
+
function styleXTransform() {
|
|
2803
|
+
// To simplify state management, we use a StateManager object to abstract
|
|
2804
|
+
// away some of the details.
|
|
2805
|
+
let state;
|
|
2806
|
+
|
|
2807
|
+
/**
|
|
2808
|
+
* Babel plugins will run the `visitor` on "enter" by default.
|
|
2809
|
+
*
|
|
2810
|
+
* This visitor object can be read mostly top-down, except, Program.exit runs
|
|
2811
|
+
* after all other visitors.
|
|
2812
|
+
*/
|
|
2813
|
+
return {
|
|
2814
|
+
name: NAME,
|
|
2815
|
+
visitor: {
|
|
2816
|
+
Program: {
|
|
2817
|
+
// First we reads all relevant imports and requires.
|
|
2818
|
+
// Store them in the StateManager object.
|
|
2819
|
+
enter: (path, s) => {
|
|
2820
|
+
state = new StateManager(s);
|
|
2821
|
+
for (const block of path.get('body')) {
|
|
2822
|
+
if (block.isImportDeclaration()) {
|
|
2823
|
+
// Read and remember 'stylex' Imports
|
|
2824
|
+
// Consider user `path.referencesImport`
|
|
2825
|
+
// But what about `requires`?
|
|
2826
|
+
readImportDeclarations(block, state);
|
|
2827
|
+
}
|
|
2828
|
+
if (block.isVariableDeclaration()) {
|
|
2829
|
+
for (const decl of block.get('declarations')) {
|
|
2830
|
+
// Read and remember 'stylex' requires
|
|
2831
|
+
readRequires(decl, state);
|
|
2832
|
+
}
|
|
2833
|
+
}
|
|
2834
|
+
}
|
|
2835
|
+
path.traverse({
|
|
2836
|
+
// Look for stylex-related function calls and transform them.
|
|
2837
|
+
// e.g.
|
|
2838
|
+
// stylex.create(...)
|
|
2839
|
+
// stylex.keyframes(...)
|
|
2840
|
+
CallExpression(path) {
|
|
2841
|
+
if (path.parentPath.isVariableDeclarator()) {
|
|
2842
|
+
// # Look for `stylex.keyframes` calls
|
|
2843
|
+
// Needs to be handled *before* `stylex.create` as the `create` call
|
|
2844
|
+
// may use the generated animation name.
|
|
2845
|
+
transformStyleXKeyframes(path.parentPath, state);
|
|
2846
|
+
}
|
|
2847
|
+
transformStyleXCreate(path, state);
|
|
2848
|
+
}
|
|
2849
|
+
});
|
|
2850
|
+
},
|
|
2851
|
+
// After all other visitors are done, we can remove `styles=stylex.create(...)`
|
|
2852
|
+
// variables entirely if they're not needed.
|
|
2853
|
+
exit: path => {
|
|
2854
|
+
path.traverse({
|
|
2855
|
+
CallExpression(path) {
|
|
2856
|
+
transformStyleXMerge(path, state);
|
|
2857
|
+
}
|
|
2858
|
+
});
|
|
2859
|
+
const varsToKeep = new Set([...state.styleVarsToKeep.values()].map(_ref => {
|
|
2860
|
+
let [varName, _namespaceName] = _ref;
|
|
2861
|
+
return varName;
|
|
2862
|
+
}));
|
|
2863
|
+
state.styleVars.forEach((path, varName) => {
|
|
2864
|
+
if (!varsToKeep.has(varName) && !isExported(path)) {
|
|
2865
|
+
path.remove();
|
|
2866
|
+
}
|
|
2867
|
+
});
|
|
2868
|
+
}
|
|
2869
|
+
},
|
|
2870
|
+
CallExpression(path) {
|
|
2871
|
+
// Don't traverse the children of `stylex(...)` calls.
|
|
2872
|
+
// This is important for detecting which `stylex.create()` calls
|
|
2873
|
+
// should be kept.
|
|
2874
|
+
skipStylexMergeChildren(path, state);
|
|
2875
|
+
},
|
|
2876
|
+
Identifier(path) {
|
|
2877
|
+
// Look for variables bound to `stylex.create` calls that are used
|
|
2878
|
+
// outside of `stylex(...)` calls
|
|
2879
|
+
if (path.isReferencedIdentifier()) {
|
|
2880
|
+
const {
|
|
2881
|
+
name
|
|
2882
|
+
} = path.node;
|
|
2883
|
+
if (state.styleMap.has(name)) {
|
|
2884
|
+
if (path.parentPath.isMemberExpression()) {
|
|
2885
|
+
const {
|
|
2886
|
+
property,
|
|
2887
|
+
computed
|
|
2888
|
+
} = path.parentPath.node;
|
|
2889
|
+
if (property.type === 'Identifier' && !computed) {
|
|
2890
|
+
state.markComposedNamespace([name, property.name]);
|
|
2891
|
+
}
|
|
2892
|
+
if (property.type === 'StringLiteral' && computed) {
|
|
2893
|
+
state.markComposedNamespace([name, property.value]);
|
|
2894
|
+
}
|
|
2895
|
+
state.markComposedNamespace([name, null]);
|
|
2896
|
+
} else {
|
|
2897
|
+
state.markComposedNamespace([name, null]);
|
|
2898
|
+
}
|
|
2899
|
+
}
|
|
2900
|
+
}
|
|
2901
|
+
}
|
|
2902
|
+
}
|
|
2903
|
+
};
|
|
2904
|
+
}
|
|
2905
|
+
function isExported(path) {
|
|
2906
|
+
if (path == null || path.isProgram()) {
|
|
2907
|
+
return false;
|
|
2908
|
+
}
|
|
2909
|
+
if (path.isExportNamedDeclaration() || path.isExportDefaultDeclaration()) {
|
|
2910
|
+
return true;
|
|
2911
|
+
}
|
|
2912
|
+
return isExported(path.parentPath);
|
|
2913
|
+
}
|
|
2914
|
+
|
|
2915
|
+
/**
|
|
2916
|
+
*
|
|
2917
|
+
* @param rules An array of CSS rules that has been generated and collected from all JS files
|
|
2918
|
+
* in a project
|
|
2919
|
+
* @returns A string that represets the final CSS file.
|
|
2920
|
+
*
|
|
2921
|
+
* This function take an Array of CSS rules, de-duplicates them, sorts them priority and generates
|
|
2922
|
+
* a final CSS file.
|
|
2923
|
+
*
|
|
2924
|
+
* When Stylex is correctly configured, the babel plugin will return an array of CSS rule objects.
|
|
2925
|
+
* You're expected to concatenate all the Rules into a single Array and use this function to convert
|
|
2926
|
+
* that into the final CSS file.
|
|
2927
|
+
*
|
|
2928
|
+
* End-users can choose to not use this function and use their own logic instead.
|
|
2929
|
+
*/
|
|
2930
|
+
const processStylexRules = function processStylexRules(rules) {
|
|
2931
|
+
if (rules.length === 0) {
|
|
2932
|
+
return '';
|
|
2933
|
+
}
|
|
2934
|
+
const sortedRules = rules.sort((_ref2, _ref3) => {
|
|
2935
|
+
let [firstSelector,, firstPriority] = _ref2;
|
|
2936
|
+
let [secondSelector,, secondPriority] = _ref3;
|
|
2937
|
+
const priorityComparison = firstPriority - secondPriority;
|
|
2938
|
+
if (priorityComparison !== 0) return priorityComparison;
|
|
2939
|
+
return firstSelector < secondSelector ? -1 : 1;
|
|
2940
|
+
});
|
|
2941
|
+
const collectedCSS = Array.from(new Map(sortedRules).values()).flatMap(_ref4 => {
|
|
2942
|
+
let {
|
|
2943
|
+
ltr,
|
|
2944
|
+
rtl
|
|
2945
|
+
} = _ref4;
|
|
2946
|
+
return rtl != null ? [addAncestorSelector(ltr, "html:not([dir='rtl'])"), addAncestorSelector(rtl, "html[dir='rtl']")] : [ltr];
|
|
2947
|
+
}).join('\n');
|
|
2948
|
+
return collectedCSS;
|
|
2949
|
+
};
|
|
2950
|
+
styleXTransform.processStylexRules = processStylexRules;
|
|
2951
|
+
|
|
2952
|
+
/**
|
|
2953
|
+
* Adds an ancestor selector in a media-query-aware way.
|
|
2954
|
+
*
|
|
2955
|
+
* Helper function for `processStylexRules`.
|
|
2956
|
+
*/
|
|
2957
|
+
function addAncestorSelector(selector, ancestorSelector) {
|
|
2958
|
+
if (!selector.startsWith('@')) {
|
|
2959
|
+
return `${ancestorSelector} ${selector}`;
|
|
2960
|
+
}
|
|
2961
|
+
const firstBracketIndex = selector.indexOf('{');
|
|
2962
|
+
const mediaQueryPart = selector.slice(0, firstBracketIndex + 1);
|
|
2963
|
+
const rest = selector.slice(firstBracketIndex + 1);
|
|
2964
|
+
return `${mediaQueryPart}${ancestorSelector} ${rest}`;
|
|
2965
|
+
}
|
|
2966
|
+
|
|
2967
|
+
exports.default = styleXTransform;
|
|
2968
|
+
exports.processStylexRules = processStylexRules;
|