@stylexjs/babel-plugin 0.2.0-beta.13 → 0.2.0-beta.15

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.
Files changed (2) hide show
  1. package/lib/index.js +636 -101
  2. package/package.json +2 -2
package/lib/index.js CHANGED
@@ -1,8 +1,11 @@
1
1
  'use strict';
2
2
 
3
+ var path = require('path');
3
4
  var t = require('@babel/types');
4
5
  var require$$0 = require('postcss-value-parser');
5
- var path = require('path');
6
+ var core = require('@babel/core');
7
+ var traverse = require('@babel/traverse');
8
+ var fs = require('fs');
6
9
  var require$$0$1 = require('invariant');
7
10
  var require$$1 = require('styleq');
8
11
 
@@ -41,6 +44,8 @@ class StateManager {
41
44
  stylexIncludeImport = new Set();
42
45
  stylexFirstThatWorksImport = new Set();
43
46
  stylexKeyframesImport = new Set();
47
+ stylexCreateVarsImport = new Set();
48
+ stylexOverrideVarsImport = new Set();
44
49
 
45
50
  // `stylex.create` calls
46
51
  styleMap = new Map();
@@ -48,6 +53,7 @@ class StateManager {
48
53
 
49
54
  // resuls of `stylex.create` calls that should be kept
50
55
  styleVarsToKeep = new Set();
56
+ inStyleXCreate = false;
51
57
  constructor(state) {
52
58
  this._state = state;
53
59
  state.file.metadata.stylex = [];
@@ -63,10 +69,16 @@ class StateManager {
63
69
  importSources: options.importSources ?? [name, 'stylex'],
64
70
  definedStylexCSSVariables: options.definedStylexCSSVariables ?? {},
65
71
  genConditionalClasses: !!options.genConditionalClasses,
66
- styleResolution: options.styleResolution ?? 'application-order'
72
+ styleResolution: options.styleResolution ?? 'application-order',
73
+ unstable_moduleResolution: options.unstable_moduleResolution ?? undefined
67
74
  };
68
75
  return this._state.opts;
69
76
  }
77
+ get canReferenceTheme() {
78
+ return !!this.inStyleXCreate;
79
+ // || this.isStyleXCreateVars
80
+ }
81
+
70
82
  get metadata() {
71
83
  return this._state.file.metadata;
72
84
  }
@@ -85,6 +97,59 @@ class StateManager {
85
97
  get cssVars() {
86
98
  return this.options.definedStylexCSSVariables;
87
99
  }
100
+ get fileNameForHashing() {
101
+ var _this$options$unstabl;
102
+ const filename = this.filename;
103
+ const themeFileExtension = ((_this$options$unstabl = this.options.unstable_moduleResolution) === null || _this$options$unstabl === void 0 ? void 0 : _this$options$unstabl.themeFileExtension) ?? `.stylex`;
104
+ if (filename == null || !matchesFileSuffix(themeFileExtension)(filename) || this.options.unstable_moduleResolution == null) {
105
+ return null;
106
+ }
107
+ switch (this.options.unstable_moduleResolution.type) {
108
+ case 'haste':
109
+ return path.basename(filename);
110
+ default:
111
+ const rootDir = this.options.unstable_moduleResolution.rootDir;
112
+ return path.relative(rootDir, filename);
113
+ }
114
+ }
115
+ importPathResolver(importPath) {
116
+ var _this$options$unstabl2;
117
+ const sourceFilePath = this.filename;
118
+ if (sourceFilePath == null) {
119
+ return false;
120
+ }
121
+ switch ((_this$options$unstabl2 = this.options.unstable_moduleResolution) === null || _this$options$unstabl2 === void 0 ? void 0 : _this$options$unstabl2.type) {
122
+ case 'commonJS':
123
+ {
124
+ this.options.unstable_moduleResolution.rootDir;
125
+ const themeFileExtension = this.options.unstable_moduleResolution.themeFileExtension ?? `.stylex`;
126
+ if (!matchesFileSuffix(themeFileExtension)(importPath)) {
127
+ return false;
128
+ }
129
+ const resolvedFilePath = filePathResolver(importPath, sourceFilePath);
130
+ return resolvedFilePath ? ['themeNameRef', resolvedFilePath] : false;
131
+ }
132
+ case 'haste':
133
+ {
134
+ const themeFileExtension = this.options.unstable_moduleResolution.themeFileExtension ?? `.stylex`;
135
+ if (!matchesFileSuffix(themeFileExtension)(importPath)) {
136
+ return false;
137
+ }
138
+ return ['themeNameRef', addFileExtension(importPath, sourceFilePath)];
139
+ }
140
+ case 'experimental_crossFileParsing':
141
+ {
142
+ const themeFileExtension = this.options.unstable_moduleResolution.themeFileExtension ?? `.stylex`;
143
+ if (!matchesFileSuffix(themeFileExtension)(importPath)) {
144
+ return false;
145
+ }
146
+ const resolvedFilePath = filePathResolver(importPath, sourceFilePath);
147
+ return resolvedFilePath ? ['filePath', resolvedFilePath] : false;
148
+ }
149
+ default:
150
+ return false;
151
+ }
152
+ }
88
153
  addStyle(style) {
89
154
  this.metadata.stylex.push(style);
90
155
  }
@@ -93,6 +158,26 @@ class StateManager {
93
158
  }
94
159
  }
95
160
 
161
+ // a function that resolves the absolute path of a file when given the
162
+ // relative path of the file from the source file
163
+ const filePathResolver = (relativeFilePath, sourceFilePath) => {
164
+ const fileToLookFor = addFileExtension(relativeFilePath, sourceFilePath);
165
+ try {
166
+ const resolvedFilePath = require.resolve(fileToLookFor, {
167
+ paths: [path.dirname(sourceFilePath)]
168
+ });
169
+ return resolvedFilePath;
170
+ } catch {}
171
+ };
172
+ const addFileExtension = (importedFilePath, sourceFile) => {
173
+ if (importedFilePath.endsWith('.js') || importedFilePath.endsWith('.ts') || importedFilePath.endsWith('.tsx') || importedFilePath.endsWith('.jsx') || importedFilePath.endsWith('.mjs') || importedFilePath.endsWith('.cjs')) {
174
+ return importedFilePath;
175
+ }
176
+ const fileExtension = path.extname(sourceFile);
177
+ return importedFilePath + fileExtension;
178
+ };
179
+ const matchesFileSuffix = allowedSuffix => filename => filename.endsWith(`${allowedSuffix}.js`) || filename.endsWith(`${allowedSuffix}.ts`) || filename.endsWith(`${allowedSuffix}.tsx`) || filename.endsWith(`${allowedSuffix}.jsx`) || filename.endsWith(`${allowedSuffix}.mjs`) || filename.endsWith(`${allowedSuffix}.cjs`) || filename.endsWith(allowedSuffix);
180
+
96
181
  /**
97
182
  * Copyright (c) Meta Platforms, Inc. and affiliates.
98
183
  *
@@ -131,6 +216,12 @@ function readImportDeclarations(path, state) {
131
216
  if (specifier.imported.name === 'firstThatWorks') {
132
217
  state.stylexFirstThatWorksImport.add(specifier.local.name);
133
218
  }
219
+ if (specifier.imported.name === 'unstable_createVars') {
220
+ state.stylexCreateVarsImport.add(specifier.local.name);
221
+ }
222
+ if (specifier.imported.name === 'unstable_overrideVars') {
223
+ state.stylexCreateVarsImport.add(specifier.local.name);
224
+ }
134
225
  }
135
226
  if (specifier.imported.type === 'StringLiteral') {
136
227
  if (specifier.imported.value === 'create') {
@@ -145,6 +236,12 @@ function readImportDeclarations(path, state) {
145
236
  if (specifier.imported.value === 'firstThatWorks') {
146
237
  state.stylexFirstThatWorksImport.add(specifier.local.name);
147
238
  }
239
+ if (specifier.imported.value === 'unstable_createVars') {
240
+ state.stylexCreateVarsImport.add(specifier.local.name);
241
+ }
242
+ if (specifier.imported.value === 'unstable_overrideVars ') {
243
+ state.stylexCreateVarsImport.add(specifier.local.name);
244
+ }
148
245
  }
149
246
  }
150
247
  }
@@ -176,6 +273,12 @@ function readRequires(path, state) {
176
273
  if (prop.key.name === 'firstThatWorks') {
177
274
  state.stylexFirstThatWorksImport.add(prop.value.name);
178
275
  }
276
+ if (prop.key.name === 'unstable_createVars') {
277
+ state.stylexCreateVarsImport.add(prop.value.name);
278
+ }
279
+ if (prop.key.name === 'unstable_overrideVars') {
280
+ state.stylexCreateVarsImport.add(prop.value.name);
281
+ }
179
282
  }
180
283
  }
181
284
  }
@@ -205,7 +308,7 @@ var messages$4 = {};
205
308
  Object.defineProperty(messages$4, "__esModule", {
206
309
  value: true
207
310
  });
208
- messages$4.UNKNOWN_PROP_KEY = messages$4.UNKNOWN_NAMESPACE = messages$4.UNEXPECTED_ARGUMENT = messages$4.UNBOUND_STYLEX_CALL_VALUE = messages$4.ONLY_TOP_LEVEL_INLCUDES = messages$4.ONLY_TOP_LEVEL = messages$4.NO_PARENT_PATH = messages$4.NO_CONDITIONAL_SHORTHAND = 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_OR_AT_RULE = 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 = messages$4.DUPLICATE_CONDITIONAL = void 0;
311
+ messages$4.UNKNOWN_PROP_KEY = messages$4.UNKNOWN_NAMESPACE = messages$4.UNEXPECTED_ARGUMENT = messages$4.UNBOUND_STYLEX_CALL_VALUE = messages$4.ONLY_TOP_LEVEL_INLCUDES = messages$4.ONLY_TOP_LEVEL = messages$4.NO_PROJECT_ROOT_DIRECTORY = messages$4.NO_PARENT_PATH = messages$4.NO_CONDITIONAL_SHORTHAND = messages$4.NON_STATIC_VALUE = messages$4.NON_OBJECT_FOR_STYLEX_CALL = messages$4.NON_EXPORT_NAMED_DECLARATION = messages$4.LOCAL_ONLY = messages$4.LINT_UNCLOSED_FUNCTION = messages$4.INVALID_SPREAD = messages$4.INVALID_PSEUDO_OR_AT_RULE = 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 = messages$4.DUPLICATE_CONDITIONAL = void 0;
209
312
  const ILLEGAL_ARGUMENT_LENGTH = 'stylex() should have 1 argument.';
210
313
  messages$4.ILLEGAL_ARGUMENT_LENGTH = ILLEGAL_ARGUMENT_LENGTH;
211
314
  const NON_STATIC_VALUE = 'Only static values are allowed inside of a stylex.create() call.';
@@ -254,6 +357,10 @@ const ONLY_TOP_LEVEL_INLCUDES = 'stylex.include() is only at the top level of a
254
357
  messages$4.ONLY_TOP_LEVEL_INLCUDES = ONLY_TOP_LEVEL_INLCUDES;
255
358
  const DUPLICATE_CONDITIONAL = 'The same pseudo selector or at-rule cannot be used more than once.';
256
359
  messages$4.DUPLICATE_CONDITIONAL = DUPLICATE_CONDITIONAL;
360
+ const NO_PROJECT_ROOT_DIRECTORY = 'The project root directory `rootDir` is not configured.';
361
+ messages$4.NO_PROJECT_ROOT_DIRECTORY = NO_PROJECT_ROOT_DIRECTORY;
362
+ const NON_EXPORT_NAMED_DECLARATION = 'The return value of stylex.createVars() must be bound to a named export.';
363
+ messages$4.NON_EXPORT_NAMED_DECLARATION = NON_EXPORT_NAMED_DECLARATION;
257
364
 
258
365
  Object.defineProperty(stylexInclude$1, "__esModule", {
259
366
  value: true
@@ -453,8 +560,8 @@ Object.defineProperty(splitCssValue, "__esModule", {
453
560
  value: true
454
561
  });
455
562
  splitCssValue.default = splitValue;
456
- var _postcssValueParser$6 = _interopRequireDefault$i(require$$0);
457
- function _interopRequireDefault$i(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
563
+ var _postcssValueParser$6 = _interopRequireDefault$k(require$$0);
564
+ function _interopRequireDefault$k(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
458
565
  /**
459
566
  * Copyright (c) Meta Platforms, Inc. and affiliates.
460
567
  *
@@ -499,8 +606,8 @@ Object.defineProperty(applicationOrder, "__esModule", {
499
606
  value: true
500
607
  });
501
608
  applicationOrder.default = void 0;
502
- var _splitCssValue$2 = _interopRequireDefault$h(splitCssValue);
503
- function _interopRequireDefault$h(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
609
+ var _splitCssValue$2 = _interopRequireDefault$j(splitCssValue);
610
+ function _interopRequireDefault$j(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
504
611
  /**
505
612
  * Copyright (c) Meta Platforms, Inc. and affiliates.
506
613
  *
@@ -759,8 +866,8 @@ Object.defineProperty(legacyExpandShorthands, "__esModule", {
759
866
  value: true
760
867
  });
761
868
  legacyExpandShorthands.default = void 0;
762
- var _splitCssValue$1 = _interopRequireDefault$g(splitCssValue);
763
- function _interopRequireDefault$g(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
869
+ var _splitCssValue$1 = _interopRequireDefault$i(splitCssValue);
870
+ function _interopRequireDefault$i(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
764
871
  /**
765
872
  * Copyright (c) Meta Platforms, Inc. and affiliates.
766
873
  *
@@ -840,8 +947,8 @@ Object.defineProperty(propertySpecificity, "__esModule", {
840
947
  value: true
841
948
  });
842
949
  propertySpecificity.default = void 0;
843
- var _splitCssValue = _interopRequireDefault$f(splitCssValue);
844
- function _interopRequireDefault$f(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
950
+ var _splitCssValue = _interopRequireDefault$h(splitCssValue);
951
+ function _interopRequireDefault$h(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
845
952
  /**
846
953
  * Copyright (c) Meta Platforms, Inc. and affiliates.
847
954
  *
@@ -975,10 +1082,10 @@ Object.defineProperty(preprocessRules, "__esModule", {
975
1082
  });
976
1083
  preprocessRules.default = flatMapExpandedShorthands;
977
1084
  preprocessRules.getExpandedKeys = getExpandedKeys;
978
- var _applicationOrder = _interopRequireDefault$e(applicationOrder);
979
- var _legacyExpandShorthands = _interopRequireDefault$e(legacyExpandShorthands);
980
- var _propertySpecificity = _interopRequireDefault$e(propertySpecificity);
981
- function _interopRequireDefault$e(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1085
+ var _applicationOrder = _interopRequireDefault$g(applicationOrder);
1086
+ var _legacyExpandShorthands = _interopRequireDefault$g(legacyExpandShorthands);
1087
+ var _propertySpecificity = _interopRequireDefault$g(propertySpecificity);
1088
+ function _interopRequireDefault$g(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
982
1089
  /**
983
1090
  * Copyright (c) Meta Platforms, Inc. and affiliates.
984
1091
  *
@@ -1042,7 +1149,8 @@ hash$1.default = void 0;
1042
1149
  * @param {number} seed Positive integer only
1043
1150
  * @return {number} 32-bit positive integer hash
1044
1151
  */
1045
- function murmurhash2_32_gc(str, seed) {
1152
+ function murmurhash2_32_gc(str) {
1153
+ let seed = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
1046
1154
  let l = str.length,
1047
1155
  h = seed ^ l,
1048
1156
  i = 0,
@@ -1111,8 +1219,8 @@ Object.defineProperty(fontSizePxToRem, "__esModule", {
1111
1219
  value: true
1112
1220
  });
1113
1221
  fontSizePxToRem.default = convertFontSizeToRem;
1114
- var _postcssValueParser$5 = _interopRequireDefault$d(require$$0);
1115
- function _interopRequireDefault$d(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1222
+ var _postcssValueParser$5 = _interopRequireDefault$f(require$$0);
1223
+ function _interopRequireDefault$f(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1116
1224
  const ROOT_FONT_SIZE = 16;
1117
1225
 
1118
1226
  /**
@@ -1151,8 +1259,8 @@ Object.defineProperty(leadingZero, "__esModule", {
1151
1259
  value: true
1152
1260
  });
1153
1261
  leadingZero.default = normalizeLeadingZero;
1154
- var _postcssValueParser$4 = _interopRequireDefault$c(require$$0);
1155
- function _interopRequireDefault$c(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1262
+ var _postcssValueParser$4 = _interopRequireDefault$e(require$$0);
1263
+ function _interopRequireDefault$e(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1156
1264
  /**
1157
1265
  * Remove leading zeros from numbers
1158
1266
  */
@@ -1220,8 +1328,8 @@ Object.defineProperty(timings$1, "__esModule", {
1220
1328
  value: true
1221
1329
  });
1222
1330
  timings$1.default = normalizeTimings;
1223
- var _postcssValueParser$3 = _interopRequireDefault$b(require$$0);
1224
- function _interopRequireDefault$b(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1331
+ var _postcssValueParser$3 = _interopRequireDefault$d(require$$0);
1332
+ function _interopRequireDefault$d(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1225
1333
  /**
1226
1334
  * Turn millisecond values to seconds (shorter), except when < 10ms
1227
1335
  */
@@ -1315,8 +1423,8 @@ Object.defineProperty(zeroDimensions, "__esModule", {
1315
1423
  value: true
1316
1424
  });
1317
1425
  zeroDimensions.default = normalizeZeroDimensions;
1318
- var _postcssValueParser$2 = _interopRequireDefault$a(require$$0);
1319
- function _interopRequireDefault$a(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1426
+ var _postcssValueParser$2 = _interopRequireDefault$c(require$$0);
1427
+ function _interopRequireDefault$c(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1320
1428
  const angles = ['deg', 'grad', 'turn', 'rad'];
1321
1429
  const timings = ['ms', 's'];
1322
1430
 
@@ -1389,15 +1497,15 @@ Object.defineProperty(normalizeValue$1, "__esModule", {
1389
1497
  value: true
1390
1498
  });
1391
1499
  normalizeValue$1.default = normalizeValue;
1392
- var _fontSizePxToRem = _interopRequireDefault$9(fontSizePxToRem);
1393
- var _leadingZero = _interopRequireDefault$9(leadingZero);
1394
- var _quotes = _interopRequireDefault$9(quotes);
1395
- var _timings = _interopRequireDefault$9(timings$1);
1396
- var _whitespace = _interopRequireDefault$9(whitespace);
1397
- var _zeroDimensions = _interopRequireDefault$9(zeroDimensions);
1398
- var _detectUnclosedFns = _interopRequireDefault$9(detectUnclosedFns$1);
1399
- var _postcssValueParser$1 = _interopRequireDefault$9(require$$0);
1400
- function _interopRequireDefault$9(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1500
+ var _fontSizePxToRem = _interopRequireDefault$b(fontSizePxToRem);
1501
+ var _leadingZero = _interopRequireDefault$b(leadingZero);
1502
+ var _quotes = _interopRequireDefault$b(quotes);
1503
+ var _timings = _interopRequireDefault$b(timings$1);
1504
+ var _whitespace = _interopRequireDefault$b(whitespace);
1505
+ var _zeroDimensions = _interopRequireDefault$b(zeroDimensions);
1506
+ var _detectUnclosedFns = _interopRequireDefault$b(detectUnclosedFns$1);
1507
+ var _postcssValueParser$1 = _interopRequireDefault$b(require$$0);
1508
+ function _interopRequireDefault$b(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1401
1509
  // `Timings` should be before `LeadingZero`, because it
1402
1510
  // changes 500ms to 0.5s, then `LeadingZero` makes it .5s
1403
1511
  const normalizers = [_detectUnclosedFns.default, _whitespace.default, _timings.default, _zeroDimensions.default, _leadingZero.default, _quotes.default, _fontSizePxToRem.default];
@@ -1413,8 +1521,8 @@ Object.defineProperty(transformValue$1, "__esModule", {
1413
1521
  value: true
1414
1522
  });
1415
1523
  transformValue$1.default = transformValue;
1416
- var _normalizeValue = _interopRequireDefault$8(normalizeValue$1);
1417
- function _interopRequireDefault$8(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1524
+ var _normalizeValue = _interopRequireDefault$a(normalizeValue$1);
1525
+ function _interopRequireDefault$a(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1418
1526
  /**
1419
1527
  * Copyright (c) Meta Platforms, Inc. and affiliates.
1420
1528
  *
@@ -1610,8 +1718,8 @@ Object.defineProperty(generateRtl, "__esModule", {
1610
1718
  value: true
1611
1719
  });
1612
1720
  generateRtl.default = generateRTL;
1613
- var _postcssValueParser = _interopRequireDefault$7(require$$0);
1614
- function _interopRequireDefault$7(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1721
+ var _postcssValueParser = _interopRequireDefault$9(require$$0);
1722
+ function _interopRequireDefault$9(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1615
1723
  /**
1616
1724
  * Copyright (c) Meta Platforms, Inc. and affiliates.
1617
1725
  *
@@ -1968,11 +2076,11 @@ Object.defineProperty(generateCssRule, "__esModule", {
1968
2076
  value: true
1969
2077
  });
1970
2078
  generateCssRule.generateRule = generateRule;
1971
- var _generateLtr$1 = _interopRequireDefault$6(generateLtr);
1972
- var _generateRtl$1 = _interopRequireDefault$6(generateRtl);
2079
+ var _generateLtr$1 = _interopRequireDefault$8(generateLtr);
2080
+ var _generateRtl$1 = _interopRequireDefault$8(generateRtl);
1973
2081
  var _genCSSRule = genCSSRule$1;
1974
- var _propertyPriorities = _interopRequireDefault$6(propertyPriorities);
1975
- function _interopRequireDefault$6(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
2082
+ var _propertyPriorities = _interopRequireDefault$8(propertyPriorities);
2083
+ function _interopRequireDefault$8(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1976
2084
  /**
1977
2085
  * Copyright (c) Meta Platforms, Inc. and affiliates.
1978
2086
  *
@@ -2003,13 +2111,13 @@ Object.defineProperty(convertToClassName, "__esModule", {
2003
2111
  value: true
2004
2112
  });
2005
2113
  convertToClassName.convertStyleToClassName = convertStyleToClassName;
2006
- var _hash$1 = _interopRequireDefault$5(hash$1);
2007
- var _dashify$1 = _interopRequireDefault$5(dashify$1);
2008
- var _transformValue$1 = _interopRequireDefault$5(transformValue$1);
2114
+ var _hash$4 = _interopRequireDefault$7(hash$1);
2115
+ var _dashify$1 = _interopRequireDefault$7(dashify$1);
2116
+ var _transformValue$1 = _interopRequireDefault$7(transformValue$1);
2009
2117
  var _generateCssRule = generateCssRule;
2010
- var _defaultOptions$2 = defaultOptions$1;
2011
- var _objectUtils$4 = objectUtils;
2012
- function _interopRequireDefault$5(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
2118
+ var _defaultOptions$4 = defaultOptions$1;
2119
+ var _objectUtils$5 = objectUtils;
2120
+ function _interopRequireDefault$7(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
2013
2121
  /**
2014
2122
  * Copyright (c) Meta Platforms, Inc. and affiliates.
2015
2123
  *
@@ -2030,17 +2138,17 @@ function convertStyleToClassName(objEntry, pseudos, atRules) {
2030
2138
  let {
2031
2139
  stylexSheetName = '<>',
2032
2140
  classNamePrefix = 'x'
2033
- } = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : _defaultOptions$2.defaultOptions;
2141
+ } = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : _defaultOptions$4.defaultOptions;
2034
2142
  const [key, rawValue] = objEntry;
2035
2143
  const dashedKey = (0, _dashify$1.default)(key);
2036
2144
  const value = Array.isArray(rawValue) ? rawValue.map(eachValue => (0, _transformValue$1.default)(key, eachValue)) : (0, _transformValue$1.default)(key, rawValue);
2037
- const sortedPseudos = (0, _objectUtils$4.arraySort)(pseudos ?? []);
2038
- const sortedAtRules = (0, _objectUtils$4.arraySort)(atRules ?? []);
2145
+ const sortedPseudos = (0, _objectUtils$5.arraySort)(pseudos ?? []);
2146
+ const sortedAtRules = (0, _objectUtils$5.arraySort)(atRules ?? []);
2039
2147
  const atRuleHashString = sortedPseudos.join('');
2040
2148
  const pseudoHashString = sortedAtRules.join('');
2041
2149
  const modifierHashString = atRuleHashString + pseudoHashString || 'null';
2042
2150
  const stringToHash = Array.isArray(value) ? dashedKey + value.join(', ') + modifierHashString : dashedKey + value + modifierHashString;
2043
- const className = classNamePrefix + (0, _hash$1.default)(stylexSheetName + stringToHash);
2151
+ const className = classNamePrefix + (0, _hash$4.default)(stylexSheetName + stringToHash);
2044
2152
  const cssRules = (0, _generateCssRule.generateRule)(className, dashedKey, value, pseudos, atRules);
2045
2153
  return [key, className, cssRules];
2046
2154
  }
@@ -2050,7 +2158,7 @@ Object.defineProperty(PreRule$1, "__esModule", {
2050
2158
  });
2051
2159
  PreRule$1.PreRuleSet = PreRule$1.PreRule = PreRule$1.PreIncludedStylesRule = PreRule$1.NullPreRule = void 0;
2052
2160
  var _convertToClassName = convertToClassName;
2053
- var _objectUtils$3 = objectUtils;
2161
+ var _objectUtils$4 = objectUtils;
2054
2162
  /**
2055
2163
  * Copyright (c) Meta Platforms, Inc. and affiliates.
2056
2164
  *
@@ -2099,8 +2207,8 @@ class PreRule {
2099
2207
  constructor(property, value, pseudos, atRules) {
2100
2208
  this.property = property;
2101
2209
  this.value = value;
2102
- this.pseudos = pseudos ? (0, _objectUtils$3.arraySort)(pseudos, stringComparator) : [];
2103
- this.atRules = atRules ? (0, _objectUtils$3.arraySort)(atRules) : [];
2210
+ this.pseudos = pseudos ? (0, _objectUtils$4.arraySort)(pseudos, stringComparator) : [];
2211
+ this.atRules = atRules ? (0, _objectUtils$4.arraySort)(atRules) : [];
2104
2212
  }
2105
2213
  compiled(options) {
2106
2214
  const [_key, className, rule] = (0, _convertToClassName.convertStyleToClassName)([this.property, this.value], this.pseudos ?? [], this.atRules ?? [], options);
@@ -2110,8 +2218,8 @@ class PreRule {
2110
2218
  if (!(other instanceof PreRule)) {
2111
2219
  return false;
2112
2220
  }
2113
- const valuesEqual = Array.isArray(this.value) && Array.isArray(other.value) ? (0, _objectUtils$3.arrayEquals)(this.value, other.value) : this.value === other.value;
2114
- return this.property === other.property && valuesEqual && (0, _objectUtils$3.arrayEquals)(this.pseudos, other.pseudos) && (0, _objectUtils$3.arrayEquals)(this.atRules, other.atRules);
2221
+ const valuesEqual = Array.isArray(this.value) && Array.isArray(other.value) ? (0, _objectUtils$4.arrayEquals)(this.value, other.value) : this.value === other.value;
2222
+ return this.property === other.property && valuesEqual && (0, _objectUtils$4.arrayEquals)(this.pseudos, other.pseudos) && (0, _objectUtils$4.arrayEquals)(this.atRules, other.atRules);
2115
2223
  }
2116
2224
  }
2117
2225
  PreRule$1.PreRule = PreRule;
@@ -2140,7 +2248,7 @@ class PreRuleSet {
2140
2248
  if (this.rules.length !== other.rules.length) {
2141
2249
  return false;
2142
2250
  }
2143
- return (0, _objectUtils$3.arrayEquals)(this.rules, other.rules, (a, b) => a.equals(b));
2251
+ return (0, _objectUtils$4.arrayEquals)(this.rules, other.rules, (a, b) => a.equals(b));
2144
2252
  }
2145
2253
  }
2146
2254
  PreRule$1.PreRuleSet = PreRuleSet;
@@ -2150,10 +2258,10 @@ Object.defineProperty(flattenRawStyleObj, "__esModule", {
2150
2258
  });
2151
2259
  flattenRawStyleObj._flattenRawStyleObject = _flattenRawStyleObject;
2152
2260
  flattenRawStyleObj.flattenRawStyleObject = flattenRawStyleObject;
2153
- var _index$1 = _interopRequireDefault$4(preprocessRules);
2261
+ var _index$1 = _interopRequireDefault$6(preprocessRules);
2154
2262
  var _PreRule = PreRule$1;
2155
2263
  var _stylexInclude$3 = stylexInclude$1;
2156
- function _interopRequireDefault$4(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
2264
+ function _interopRequireDefault$6(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
2157
2265
  /**
2158
2266
  * Copyright (c) Meta Platforms, Inc. and affiliates.
2159
2267
  *
@@ -2300,7 +2408,7 @@ Object.defineProperty(basicValidation, "__esModule", {
2300
2408
  basicValidation.validateNamespace = validateNamespace;
2301
2409
  var _stylexInclude$2 = stylexInclude$1;
2302
2410
  var messages$1 = _interopRequireWildcard$1(messages$4);
2303
- var _objectUtils$2 = objectUtils;
2411
+ var _objectUtils$3 = objectUtils;
2304
2412
  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); }
2305
2413
  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; }
2306
2414
  /**
@@ -2314,7 +2422,7 @@ function _interopRequireWildcard$1(obj, nodeInterop) { if (!nodeInterop && obj &
2314
2422
 
2315
2423
  function validateNamespace(namespace) {
2316
2424
  let conditions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
2317
- if (!(0, _objectUtils$2.isPlainObject)(namespace)) {
2425
+ if (!(0, _objectUtils$3.isPlainObject)(namespace)) {
2318
2426
  throw new Error(messages$1.ILLEGAL_NAMESPACE_VALUE);
2319
2427
  }
2320
2428
  const ns = namespace;
@@ -2338,7 +2446,7 @@ function validateNamespace(namespace) {
2338
2446
  }
2339
2447
  throw new Error(messages$1.ONLY_TOP_LEVEL_INLCUDES);
2340
2448
  }
2341
- if ((0, _objectUtils$2.isPlainObject)(val)) {
2449
+ if ((0, _objectUtils$3.isPlainObject)(val)) {
2342
2450
  if (key.startsWith('@') || key.startsWith(':')) {
2343
2451
  if (conditions.includes(key)) {
2344
2452
  throw new Error(messages$1.DUPLICATE_CONDITIONAL);
@@ -2377,7 +2485,7 @@ function validateConditionalStyles(val) {
2377
2485
  if (v instanceof _stylexInclude$2.IncludedStyles) {
2378
2486
  throw new Error(messages$1.ONLY_TOP_LEVEL_INLCUDES);
2379
2487
  }
2380
- if ((0, _objectUtils$2.isPlainObject)(v)) {
2488
+ if ((0, _objectUtils$3.isPlainObject)(v)) {
2381
2489
  validateConditionalStyles(v, [...conditions, key]);
2382
2490
  continue;
2383
2491
  }
@@ -2389,9 +2497,9 @@ Object.defineProperty(stylexCreate$1, "__esModule", {
2389
2497
  value: true
2390
2498
  });
2391
2499
  stylexCreate$1.default = styleXCreateSet;
2392
- var _objectUtils$1 = objectUtils;
2500
+ var _objectUtils$2 = objectUtils;
2393
2501
  var _stylexInclude$1 = stylexInclude$1;
2394
- var _defaultOptions$1 = defaultOptions$1;
2502
+ var _defaultOptions$3 = defaultOptions$1;
2395
2503
  var _flattenRawStyleObj = flattenRawStyleObj;
2396
2504
  var _basicValidation = basicValidation;
2397
2505
  /**
@@ -2414,7 +2522,7 @@ var _basicValidation = basicValidation;
2414
2522
  //
2415
2523
  // Before returning, it ensures that there are no duplicate styles being injected.
2416
2524
  function styleXCreateSet(namespaces) {
2417
- let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _defaultOptions$1.defaultOptions;
2525
+ let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _defaultOptions$3.defaultOptions;
2418
2526
  const resolvedNamespaces = {};
2419
2527
  const injectedStyles = {};
2420
2528
  for (const namespaceName of Object.keys(namespaces)) {
@@ -2425,7 +2533,7 @@ function styleXCreateSet(namespaces) {
2425
2533
  let [key, value] = _ref;
2426
2534
  return [key, value.compiled(options)];
2427
2535
  });
2428
- const compiledNamespace = (0, _objectUtils$1.objFromEntries)(compiledNamespaceTuples);
2536
+ const compiledNamespace = (0, _objectUtils$2.objFromEntries)(compiledNamespaceTuples);
2429
2537
  const namespaceObj = {};
2430
2538
  for (const key of Object.keys(compiledNamespace)) {
2431
2539
  const value = compiledNamespace[key];
@@ -2453,13 +2561,122 @@ function styleXCreateSet(namespaces) {
2453
2561
  return [resolvedNamespaces, injectedStyles];
2454
2562
  }
2455
2563
 
2564
+ var stylexCreateVars$1 = {};
2565
+
2566
+ Object.defineProperty(stylexCreateVars$1, "__esModule", {
2567
+ value: true
2568
+ });
2569
+ stylexCreateVars$1.default = styleXCreateVars;
2570
+ var _hash$3 = _interopRequireDefault$5(hash$1);
2571
+ var _objectUtils$1 = objectUtils;
2572
+ var _defaultOptions$2 = defaultOptions$1;
2573
+ function _interopRequireDefault$5(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
2574
+ /**
2575
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
2576
+ *
2577
+ * This source code is licensed under the MIT license found in the
2578
+ * LICENSE file in the root directory of this source tree.
2579
+ *
2580
+ *
2581
+ */
2582
+
2583
+ // Similar to `stylex.create` it takes an object of variables with their values
2584
+ // and returns a string after hashing it.
2585
+ function styleXCreateVars(variables, options) {
2586
+ const {
2587
+ classNamePrefix,
2588
+ themeName
2589
+ } = {
2590
+ ..._defaultOptions$2.defaultOptions,
2591
+ ...options
2592
+ };
2593
+ const themeNameHash = classNamePrefix + (0, _hash$3.default)(themeName);
2594
+ const variablesMap = (0, _objectUtils$1.objMap)(variables, (value, key) => {
2595
+ // Created hashed variable names with fileName//themeName//key
2596
+ const nameHash = classNamePrefix + (0, _hash$3.default)(`${themeName}.${key}`);
2597
+ return {
2598
+ nameHash,
2599
+ value
2600
+ };
2601
+ });
2602
+ const themeVariablesObject = (0, _objectUtils$1.objMap)(variablesMap, _ref => {
2603
+ let {
2604
+ nameHash
2605
+ } = _ref;
2606
+ return `var(--${nameHash})`;
2607
+ });
2608
+ const cssVariablesString = constructCssVariablesString(variablesMap);
2609
+ return [{
2610
+ ...themeVariablesObject,
2611
+ __themeName__: themeNameHash
2612
+ }, {
2613
+ css: cssVariablesString
2614
+ }];
2615
+ }
2616
+ function constructCssVariablesString(variables) {
2617
+ const vars = (0, _objectUtils$1.objEntries)(variables).map(_ref2 => {
2618
+ let [_, value] = _ref2;
2619
+ return `--${value.nameHash}:${value.value};`;
2620
+ }).join('');
2621
+ return `:root{${vars}}`;
2622
+ }
2623
+
2624
+ var stylexOverrideVars$1 = {};
2625
+
2626
+ Object.defineProperty(stylexOverrideVars$1, "__esModule", {
2627
+ value: true
2628
+ });
2629
+ stylexOverrideVars$1.default = styleXOverrideVars;
2630
+ var _hash$2 = _interopRequireDefault$4(hash$1);
2631
+ var _defaultOptions$1 = defaultOptions$1;
2632
+ function _interopRequireDefault$4(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
2633
+ /**
2634
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
2635
+ *
2636
+ * This source code is licensed under the MIT license found in the
2637
+ * LICENSE file in the root directory of this source tree.
2638
+ *
2639
+ *
2640
+ */
2641
+
2642
+ // It takes an object of variables with their values and the original set of variables to override
2643
+ // and returns a hashed className with variables overrides.
2644
+ //
2645
+ function styleXOverrideVars(themeVars, variables, options) {
2646
+ if (typeof themeVars.__themeName__ !== 'string') {
2647
+ throw new Error('Can only override variables theme created with stylex.unstable_createVars().');
2648
+ }
2649
+ const {
2650
+ classNamePrefix
2651
+ } = {
2652
+ ..._defaultOptions$1.defaultOptions,
2653
+ ...options
2654
+ };
2655
+ const sortedKeys = Object.keys(variables).sort();
2656
+ const cssVariablesOverrideString = sortedKeys.map(key => {
2657
+ const varNameHash = themeVars[key].slice(4, -1);
2658
+ return varNameHash != null ? `${varNameHash}:${variables[key]};` : '';
2659
+ }).join('');
2660
+ const overrideClassName = classNamePrefix + (0, _hash$2.default)(cssVariablesOverrideString);
2661
+ return [{
2662
+ $$css: true,
2663
+ [themeVars.__themeName__]: overrideClassName
2664
+ }, {
2665
+ [overrideClassName]: {
2666
+ ltr: `.${overrideClassName}{${cssVariablesOverrideString}}`,
2667
+ priority: 0.99,
2668
+ rtl: undefined
2669
+ }
2670
+ }];
2671
+ }
2672
+
2456
2673
  var stylexKeyframes = {};
2457
2674
 
2458
2675
  Object.defineProperty(stylexKeyframes, "__esModule", {
2459
2676
  value: true
2460
2677
  });
2461
2678
  stylexKeyframes.default = styleXKeyframes;
2462
- var _hash = _interopRequireDefault$3(hash$1);
2679
+ var _hash$1 = _interopRequireDefault$3(hash$1);
2463
2680
  var _index = _interopRequireDefault$3(preprocessRules);
2464
2681
  var _generateLtr = _interopRequireDefault$3(generateLtr);
2465
2682
  var _generateRtl = _interopRequireDefault$3(generateRtl);
@@ -2495,7 +2712,7 @@ function styleXKeyframes(frames) {
2495
2712
  const rtlString = constructKeyframesObj(rtlStyles);
2496
2713
 
2497
2714
  // This extra `-B` is kept for some idiosyncratic legacy compatibility for now.
2498
- const animationName = classNamePrefix + (0, _hash.default)(stylexSheetName + ltrString) + '-B';
2715
+ const animationName = classNamePrefix + (0, _hash$1.default)(stylexSheetName + ltrString) + '-B';
2499
2716
  const ltr = `@keyframes ${animationName}{${ltrString}}`;
2500
2717
  const rtl = ltrString === rtlString ? null : `@keyframes ${animationName}{${rtlString}}`;
2501
2718
  return [animationName, {
@@ -2553,14 +2770,41 @@ function stylexFirstThatWorks() {
2553
2770
  return [...args].reverse();
2554
2771
  }
2555
2772
 
2773
+ var fileBasedIdentifier = {};
2774
+
2775
+ Object.defineProperty(fileBasedIdentifier, "__esModule", {
2776
+ value: true
2777
+ });
2778
+ fileBasedIdentifier.default = genFileBasedIdentifier;
2779
+ /**
2780
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
2781
+ *
2782
+ * This source code is licensed under the MIT license found in the
2783
+ * LICENSE file in the root directory of this source tree.
2784
+ *
2785
+ */
2786
+
2787
+ function genFileBasedIdentifier(_ref) {
2788
+ let {
2789
+ fileName,
2790
+ exportName,
2791
+ key
2792
+ } = _ref;
2793
+ return `${fileName}//${exportName}${key != null ? `.${key}` : ''}`;
2794
+ }
2795
+
2556
2796
  Object.defineProperty(lib, "__esModule", {
2557
2797
  value: true
2558
2798
  });
2559
- 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;
2799
+ var utils_1 = lib.utils = overrideVars_1 = lib.overrideVars = messages_1 = lib.messages = keyframes_1 = lib.keyframes = include_1 = lib.include = firstThatWorks_1 = lib.firstThatWorks = createVars_1 = lib.createVars = create_1 = lib.create = IncludedStyles_1 = lib.IncludedStyles = void 0;
2560
2800
  var _stylexCreate = _interopRequireDefault$2(stylexCreate$1);
2801
+ var _stylexCreateVars = _interopRequireDefault$2(stylexCreateVars$1);
2802
+ var _stylexOverrideVars = _interopRequireDefault$2(stylexOverrideVars$1);
2561
2803
  var _stylexKeyframes = _interopRequireDefault$2(stylexKeyframes);
2562
2804
  var _stylexInclude = _interopRequireWildcard(stylexInclude$1);
2563
2805
  var _stylexFirstThatWorks = _interopRequireDefault$2(stylexFirstThatWorks$1);
2806
+ var _hash = _interopRequireDefault$2(hash$1);
2807
+ var _fileBasedIdentifier = _interopRequireDefault$2(fileBasedIdentifier);
2564
2808
  var m = _interopRequireWildcard(messages$4);
2565
2809
  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); }
2566
2810
  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; }
@@ -2581,12 +2825,21 @@ function _interopRequireDefault$2(obj) { return obj && obj.__esModule ? obj : {
2581
2825
 
2582
2826
  const create$1 = _stylexCreate.default;
2583
2827
  var create_1 = lib.create = create$1;
2828
+ const createVars = _stylexCreateVars.default;
2829
+ var createVars_1 = lib.createVars = createVars;
2830
+ const overrideVars = _stylexOverrideVars.default;
2831
+ var overrideVars_1 = lib.overrideVars = overrideVars;
2584
2832
  const keyframes$1 = _stylexKeyframes.default;
2585
2833
  var keyframes_1 = lib.keyframes = keyframes$1;
2586
2834
  const include$1 = _stylexInclude.default;
2587
2835
  var include_1 = lib.include = include$1;
2836
+ const utils = {
2837
+ hash: _hash.default,
2838
+ genFileBasedIdentifier: _fileBasedIdentifier.default
2839
+ };
2840
+ utils_1 = lib.utils = utils;
2588
2841
  const messages = m;
2589
- messages_1 = lib.messages = messages;
2842
+ var messages_1 = lib.messages = messages;
2590
2843
  const IncludedStyles = _stylexInclude.IncludedStyles;
2591
2844
  var IncludedStyles_1 = lib.IncludedStyles = IncludedStyles;
2592
2845
  const firstThatWorks$1 = _stylexFirstThatWorks.default;
@@ -2654,20 +2907,6 @@ function canBeIdentifier(str) {
2654
2907
  * LICENSE file in the root directory of this source tree.
2655
2908
  */
2656
2909
 
2657
- /**
2658
- * This is a extended version of the path evaluation code from Babel.
2659
- *
2660
- * The original can be found at:
2661
- * https://github.com/babel/babel/blob/main/packages/babel-traverse/src/path/evaluation.ts
2662
- *
2663
- * The following extensions were made:
2664
- * - It can accept a mapping from variable names to functions
2665
- * which when encountered will be evaluated instead of deopting.
2666
- * - The functions can be configured to accept the raw path instead of
2667
- * static values to handle dynamic values.
2668
- * - It can handle object spreads when the spread value itself is statically evaluated.
2669
- */
2670
-
2671
2910
  // This file contains Babels metainterpreter that can evaluate static code.
2672
2911
 
2673
2912
  const VALID_CALLEES = ['String', 'Number', 'Math'];
@@ -2686,6 +2925,74 @@ function deopt(path, state) {
2686
2925
  state.deoptPath = path;
2687
2926
  state.confident = false;
2688
2927
  }
2928
+ function evaluateImportedFile(filePath, namedExport, state) {
2929
+ const fileContents = fs.readFileSync(filePath, 'utf8');
2930
+ // It's safe to use `.babelrc` here because we're only
2931
+ // interested in the JS runtime, and not the CSS.
2932
+ // TODO: in environments where `.babelrc` is not available,
2933
+ // we need to find a way to decide whether to use Flow or TS syntax extensions.
2934
+ const ast = core.parseSync(fileContents, {
2935
+ babelrc: true
2936
+ });
2937
+ if (!ast) {
2938
+ state.confident = false;
2939
+ return;
2940
+ }
2941
+ let result;
2942
+ traverse(ast, {
2943
+ ExportNamedDeclaration(path) {
2944
+ const declaration = path.get('declaration');
2945
+ if (declaration.isVariableDeclaration()) {
2946
+ const decls = declaration.get('declarations');
2947
+ const finder = decl => {
2948
+ if (decl.isVariableDeclarator()) {
2949
+ const id = decl.get('id');
2950
+ const init = decl.get('init');
2951
+ if (id.isIdentifier() && id.node.name === namedExport && init != null && init.isExpression()) {
2952
+ result = evaluateCached(init, state);
2953
+ }
2954
+ }
2955
+ };
2956
+ if (Array.isArray(decls)) {
2957
+ decls.forEach(finder);
2958
+ } else {
2959
+ finder(decls);
2960
+ }
2961
+ }
2962
+ }
2963
+ });
2964
+ if (state.confident) {
2965
+ return result;
2966
+ } else {
2967
+ state.confident = false;
2968
+ return;
2969
+ }
2970
+ }
2971
+ function evaluateThemeRef(fileName, exportName, state) {
2972
+ const resolveKey = key => {
2973
+ let strToHash = key === '__themeName__' ? utils_1.genFileBasedIdentifier({
2974
+ fileName,
2975
+ exportName
2976
+ }) : utils_1.genFileBasedIdentifier({
2977
+ fileName,
2978
+ exportName,
2979
+ key
2980
+ });
2981
+ const varName = state.traversalState.options.classNamePrefix + utils_1.hash(strToHash);
2982
+ return `var(--${varName})`;
2983
+ };
2984
+
2985
+ // A JS proxy that uses the key to generate a string value using the `resolveKey` function
2986
+ const proxy = new Proxy({}, {
2987
+ get(_, key) {
2988
+ return resolveKey(key);
2989
+ },
2990
+ set(_, key, value) {
2991
+ throw new Error(`Cannot set value ${value} to key ${key} in theme ${fileName}`);
2992
+ }
2993
+ });
2994
+ return proxy;
2995
+ }
2689
2996
 
2690
2997
  /**
2691
2998
  * We wrap the _evaluate method so we can track `seen` nodes, we push an item
@@ -2772,19 +3079,47 @@ function _evaluate(path, state) {
2772
3079
  if (path.isMemberExpression() && !path.parentPath.isCallExpression({
2773
3080
  callee: path.node
2774
3081
  })) {
2775
- const property = path.get('property');
2776
- const object = path.get('object');
2777
- if (object.isLiteral() && property.isIdentifier()) {
2778
- // @ts-expect-error todo(flow->ts): instead of typeof - would it be better to check type of ast node?
2779
- const value = object.node.value;
2780
- const type = typeof value;
2781
- if (type === 'number' || type === 'string') {
2782
- return value[property.node.name];
3082
+ const object = evaluateCached(path.get('object'), state);
3083
+ if (!state.confident) {
3084
+ return;
3085
+ }
3086
+ const propPath = path.get('property');
3087
+ let property;
3088
+ if (path.node.computed) {
3089
+ property = evaluateCached(propPath, state);
3090
+ if (!state.confident) {
3091
+ return;
2783
3092
  }
3093
+ } else if (propPath.isIdentifier()) {
3094
+ property = propPath.node.name;
3095
+ } else if (propPath.isStringLiteral()) {
3096
+ property = propPath.node.value;
3097
+ } else {
3098
+ return deopt(propPath, state);
2784
3099
  }
3100
+ return object[property];
2785
3101
  }
2786
3102
  if (path.isReferencedIdentifier()) {
2787
3103
  const binding = path.scope.getBinding(path.node.name);
3104
+ if (binding && binding.path.isImportSpecifier() && !binding.path.isImportDefaultSpecifier()) {
3105
+ // const localName = binding.path.node.local.name;
3106
+ const imported = binding.path.node.imported;
3107
+ const importedName = imported.type === 'Identifier' ? imported.name : imported.value;
3108
+ const importPath = binding.path.parentPath;
3109
+ if (importPath.isImportDeclaration()) {
3110
+ const absPath = state.traversalState.importPathResolver(importPath.node.source.value);
3111
+ if (!absPath) {
3112
+ return deopt(binding.path, state);
3113
+ }
3114
+ const [type, value] = absPath;
3115
+ const returnValue = type === 'themeNameRef' ? evaluateThemeRef(value, importedName, state) : evaluateImportedFile(value, importedName, state);
3116
+ if (state.confident) {
3117
+ return returnValue;
3118
+ } else {
3119
+ deopt(binding.path, state);
3120
+ }
3121
+ }
3122
+ }
2788
3123
  if (binding && binding.constantViolations.length > 0) {
2789
3124
  return deopt(binding.path, state);
2790
3125
  }
@@ -2839,7 +3174,7 @@ function _evaluate(path, state) {
2839
3174
  const arr = [];
2840
3175
  const elems = path.get('elements');
2841
3176
  for (const elem of elems) {
2842
- const elemValue = evaluate(elem, state.functions);
3177
+ const elemValue = evaluate(elem, state.traversalState, state.functions);
2843
3178
  if (elemValue.confident) {
2844
3179
  arr.push(elemValue.value);
2845
3180
  } else {
@@ -2866,7 +3201,7 @@ function _evaluate(path, state) {
2866
3201
  const keyPath = prop.get('key');
2867
3202
  let key = keyPath;
2868
3203
  if (prop.node.computed) {
2869
- key = evaluate(key);
3204
+ key = evaluate(key, state.traversalState, state.functions);
2870
3205
  if (!key.confident) {
2871
3206
  return deopt(key.deopt, state);
2872
3207
  }
@@ -2878,7 +3213,7 @@ function _evaluate(path, state) {
2878
3213
  }
2879
3214
  // todo(flow->ts): remove typecast
2880
3215
  const valuePath = prop.get('value');
2881
- let value = evaluate(valuePath, state.functions);
3216
+ let value = evaluate(valuePath, state.traversalState, state.functions);
2882
3217
  if (!value.confident) {
2883
3218
  return deopt(value.deopt, state);
2884
3219
  }
@@ -3061,8 +3396,8 @@ function evaluateQuasis(path, quasis, state) {
3061
3396
  *
3062
3397
  */
3063
3398
 
3064
- function evaluate(path) {
3065
- let functions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
3399
+ function evaluate(path, traversalState) {
3400
+ let functions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {
3066
3401
  identifiers: {},
3067
3402
  memberExpressions: {}
3068
3403
  };
@@ -3070,7 +3405,8 @@ function evaluate(path) {
3070
3405
  confident: true,
3071
3406
  deoptPath: null,
3072
3407
  seen: new Map(),
3073
- functions
3408
+ functions,
3409
+ traversalState
3074
3410
  };
3075
3411
  let value = evaluateCached(path, state);
3076
3412
  if (!state.confident) value = undefined;
@@ -3110,7 +3446,11 @@ function transformStyleXCreate(path, state) {
3110
3446
  if (firstArg == null || !firstArg.isObjectExpression()) {
3111
3447
  throw new Error(messages_1.ILLEGAL_ARGUMENT_LENGTH);
3112
3448
  }
3449
+
3450
+ // TODO: This should be removed soon since we should disallow spreads without
3451
+ // `stylex.include` in the future.
3113
3452
  preProcessStyleArg(firstArg, state);
3453
+ state.inStyleXCreate = true;
3114
3454
  const injectedKeyframes = {};
3115
3455
  function keyframes(animation) {
3116
3456
  const [animationName, injectedStyle] = keyframes_1(animation, state.options);
@@ -3153,7 +3493,7 @@ function transformStyleXCreate(path, state) {
3153
3493
  const {
3154
3494
  confident,
3155
3495
  value
3156
- } = evaluate(firstArg, {
3496
+ } = evaluate(firstArg, state, {
3157
3497
  identifiers,
3158
3498
  memberExpressions
3159
3499
  });
@@ -3204,6 +3544,7 @@ function transformStyleXCreate(path, state) {
3204
3544
  state.addStyle([key, rest, priority]);
3205
3545
  }
3206
3546
  }
3547
+ state.inStyleXCreate = false;
3207
3548
  }
3208
3549
 
3209
3550
  // Validates the first argument to `stylex.create`.
@@ -3277,6 +3618,186 @@ function getStylexDefaultImport(path, state) {
3277
3618
  return stylexName;
3278
3619
  }
3279
3620
 
3621
+ /**
3622
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3623
+ *
3624
+ * This source code is licensed under the MIT license found in the
3625
+ * LICENSE file in the root directory of this source tree.
3626
+ */
3627
+ /// This function looks for `stylex.createVars` calls and transforms them.
3628
+ //. 1. It finds the first argument to `stylex.createVars` and validates it.
3629
+ /// 2. It envalues the style object to get a JS object. This also handles local constants automatically.
3630
+ /// 4. It uses the `stylexCreateVars` from `@stylexjs/shared` to transform the JS
3631
+ /// object and to get a list of injected styles.
3632
+ /// 5. It converts the resulting Object back into an AST and replaces the call with it.
3633
+ /// 6. It also inserts `stylex.inject` calls above the current statement as needed.
3634
+ function transformStyleXCreateVars(callExpressionPath, state) {
3635
+ const callExpressionNode = callExpressionPath.node;
3636
+ if (callExpressionNode.type !== 'CallExpression') {
3637
+ return;
3638
+ }
3639
+ if (callExpressionNode.callee.type === 'Identifier' && state.stylexCreateVarsImport.has(callExpressionNode.callee.name) || callExpressionNode.callee.type === 'MemberExpression' && callExpressionNode.callee.object.type === 'Identifier' && callExpressionNode.callee.property.type === 'Identifier' && state.stylexImport.has(callExpressionNode.callee.object.name) && callExpressionNode.callee.property.name === 'unstable_createVars') {
3640
+ validateStyleXCreateVars(callExpressionPath);
3641
+
3642
+ // We know that parent is a variable declaration
3643
+ const variableDeclaratorPath = callExpressionPath.parentPath;
3644
+ if (!variableDeclaratorPath.isVariableDeclarator()) {
3645
+ return;
3646
+ }
3647
+ const variableDeclaratorNode = variableDeclaratorPath.node;
3648
+ if (variableDeclaratorNode.id.type !== 'Identifier') {
3649
+ return;
3650
+ }
3651
+ const args = callExpressionPath.get('arguments');
3652
+ const firstArg = args[0];
3653
+ const {
3654
+ confident,
3655
+ value
3656
+ } = evaluate(firstArg, state);
3657
+ if (!confident) {
3658
+ throw new Error(messages_1.NON_STATIC_VALUE);
3659
+ }
3660
+ const fileName = state.fileNameForHashing;
3661
+ if (fileName == null) {
3662
+ throw new Error('No filename found for generating theme name.');
3663
+ }
3664
+ const exportName = variableDeclaratorNode.id.name;
3665
+ let [variablesObj, {
3666
+ css
3667
+ }] = createVars_1(value, {
3668
+ ...state.options,
3669
+ themeName: utils_1.genFileBasedIdentifier({
3670
+ fileName,
3671
+ exportName
3672
+ })
3673
+ });
3674
+
3675
+ // This should be a transformed variables object
3676
+ callExpressionPath.replaceWith(convertObjectToAST(variablesObj));
3677
+ if (state.isDev || state.stylexSheetName == null) {
3678
+ // We know that the top level parent path is an export named declaration
3679
+ const statementPath = variableDeclaratorPath.parentPath.parentPath;
3680
+ let stylexName;
3681
+ state.stylexImport.forEach(importName => {
3682
+ stylexName = importName;
3683
+ });
3684
+ if (stylexName == null) {
3685
+ stylexName = '__stylex__';
3686
+ statementPath.insertBefore(t__namespace.importDeclaration([t__namespace.importDefaultSpecifier(t__namespace.identifier(stylexName))], t__namespace.stringLiteral('stylex')));
3687
+ }
3688
+ statementPath.insertBefore(t__namespace.expressionStatement(t__namespace.callExpression(t__namespace.memberExpression(t__namespace.identifier(stylexName), t__namespace.identifier('inject')), [t__namespace.stringLiteral(css), t__namespace.numericLiteral(0)])));
3689
+ }
3690
+ state.addStyle([variablesObj.__themeName__, {
3691
+ ltr: css
3692
+ }, 0]);
3693
+ }
3694
+ }
3695
+
3696
+ // Validates the call of `stylex.createVars`.
3697
+ function validateStyleXCreateVars(callExpressionPath) {
3698
+ var _variableDeclaratorPa;
3699
+ const variableDeclaratorPath = callExpressionPath.parentPath;
3700
+ const exportNamedDeclarationPath = (_variableDeclaratorPa = variableDeclaratorPath.parentPath) === null || _variableDeclaratorPa === void 0 ? void 0 : _variableDeclaratorPa.parentPath;
3701
+ if (variableDeclaratorPath == null || variableDeclaratorPath.isExpressionStatement() || !variableDeclaratorPath.isVariableDeclarator() || variableDeclaratorPath.node.id.type !== 'Identifier') {
3702
+ throw new Error(messages_1.UNBOUND_STYLEX_CALL_VALUE);
3703
+ }
3704
+ if (exportNamedDeclarationPath == null || !exportNamedDeclarationPath.isExportNamedDeclaration()) {
3705
+ throw new Error(messages_1.NON_EXPORT_NAMED_DECLARATION);
3706
+ }
3707
+ if (callExpressionPath.node.arguments.length !== 1) {
3708
+ throw new Error(messages_1.ILLEGAL_ARGUMENT_LENGTH);
3709
+ }
3710
+ if (callExpressionPath.node.arguments[0].type !== 'ObjectExpression') {
3711
+ throw new Error(messages_1.NON_OBJECT_FOR_STYLEX_CALL);
3712
+ }
3713
+ }
3714
+
3715
+ /**
3716
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3717
+ *
3718
+ * This source code is licensed under the MIT license found in the
3719
+ * LICENSE file in the root directory of this source tree.
3720
+ */
3721
+ /// This function looks for `stylex.unstable_overrideVars` calls and transforms them.
3722
+ //. 1. It finds the first two arguments to `stylex.unstable_overrideVars` and validates those.
3723
+ /// 2. This handles local constants automatically.
3724
+ /// 4. It uses the `stylexOverrideVars` from `@stylexjs/shared` to transform the JS
3725
+ /// object and to get a list of injected styles.
3726
+ /// 5. It converts the resulting Object back into an AST and replaces the call with it.
3727
+ /// 6. It also inserts `stylex.inject` calls above the current statement as needed.
3728
+ function transformStyleXOverrideVars(callExpressionPath, state) {
3729
+ const callExpressionNode = callExpressionPath.node;
3730
+ if (callExpressionNode.type !== 'CallExpression') {
3731
+ return;
3732
+ }
3733
+ if (callExpressionNode.callee.type === 'Identifier' && state.stylexOverrideVarsImport.has(callExpressionNode.callee.name) || callExpressionNode.callee.type === 'MemberExpression' && callExpressionNode.callee.object.type === 'Identifier' && callExpressionNode.callee.property.type === 'Identifier' && state.stylexImport.has(callExpressionNode.callee.object.name) && callExpressionNode.callee.property.name === 'unstable_overrideVars') {
3734
+ validateStyleXOverrideVars(callExpressionPath);
3735
+
3736
+ // We know that parent is a variable declaration
3737
+ const variableDeclaratorPath = callExpressionPath.parentPath;
3738
+ if (!variableDeclaratorPath.isVariableDeclarator()) {
3739
+ return;
3740
+ }
3741
+ const args = callExpressionPath.get('arguments');
3742
+ const firstArg = args[0];
3743
+ const secondArg = args[1];
3744
+ const {
3745
+ confident: confident1,
3746
+ value: variables
3747
+ } = evaluate(firstArg, state);
3748
+ if (!confident1) {
3749
+ throw new Error(messages_1.NON_STATIC_VALUE);
3750
+ }
3751
+ const {
3752
+ confident: confident2,
3753
+ value: overrides
3754
+ } = evaluate(secondArg, state);
3755
+ if (!confident2) {
3756
+ throw new Error(messages_1.NON_STATIC_VALUE);
3757
+ }
3758
+
3759
+ // Check that first arg has __themeName__ set
3760
+ if (typeof variables.__themeName__ !== 'string' || variables.__themeName__ === '') {
3761
+ throw new Error('Can only override variables theme created with stylex.unstable_createVars().');
3762
+ }
3763
+ const [overridesObj, css] = overrideVars_1(variables, overrides, state.options);
3764
+ const styleKey = overridesObj[variables.__themeName__];
3765
+
3766
+ // This should be a transformed variables object
3767
+ callExpressionPath.replaceWith(convertObjectToAST(overridesObj));
3768
+ if (state.isDev || state.stylexSheetName == null) {
3769
+ // We know that the top level parent path is an variable declarator
3770
+ const statementPath = variableDeclaratorPath.parentPath;
3771
+ let stylexName;
3772
+ state.stylexImport.forEach(importName => {
3773
+ stylexName = importName;
3774
+ });
3775
+ if (stylexName == null) {
3776
+ stylexName = '__stylex__';
3777
+ statementPath.insertBefore(t__namespace.importDeclaration([t__namespace.importDefaultSpecifier(t__namespace.identifier(stylexName))], t__namespace.stringLiteral('stylex')));
3778
+ }
3779
+ statementPath.insertBefore(t__namespace.expressionStatement(t__namespace.callExpression(t__namespace.memberExpression(t__namespace.identifier(stylexName), t__namespace.identifier('inject')), [t__namespace.stringLiteral(css[styleKey].ltr), t__namespace.numericLiteral(css[styleKey].priority)])));
3780
+ }
3781
+ state.addStyle([styleKey, {
3782
+ ltr: css[styleKey].ltr
3783
+ }, css[styleKey].priority]);
3784
+ }
3785
+ }
3786
+
3787
+ // Validates the call of `stylex.overrideVars`.
3788
+ function validateStyleXOverrideVars(callExpressionPath) {
3789
+ const variableDeclaratorPath = callExpressionPath.parentPath;
3790
+ if (variableDeclaratorPath == null || variableDeclaratorPath.isExpressionStatement() || !variableDeclaratorPath.isVariableDeclarator() || variableDeclaratorPath.node.id.type !== 'Identifier') {
3791
+ throw new Error(messages_1.UNBOUND_STYLEX_CALL_VALUE);
3792
+ }
3793
+ if (callExpressionPath.node.arguments.length !== 2) {
3794
+ throw new Error(messages_1.ILLEGAL_ARGUMENT_LENGTH);
3795
+ }
3796
+ if (callExpressionPath.node.arguments[1].type !== 'ObjectExpression') {
3797
+ throw new Error(messages_1.NON_OBJECT_FOR_STYLEX_CALL);
3798
+ }
3799
+ }
3800
+
3280
3801
  /**
3281
3802
  * Copyright (c) Meta Platforms, Inc. and affiliates.
3282
3803
  *
@@ -3688,7 +4209,7 @@ Object.defineProperty(stylex$1, "__esModule", {
3688
4209
  });
3689
4210
  stylex$1.keyframes = stylex$1.inject = stylex$1.include = stylex$1.firstThatWorks = default_1 = stylex$1.default = stylex$1.create = stylex$1.UNSUPPORTED_PROPERTY = void 0;
3690
4211
  stylex$1.spread = spread;
3691
- stylex$1.stylex = void 0;
4212
+ stylex$1.unstable_overrideVars = stylex$1.unstable_createVars = stylex$1.stylex = void 0;
3692
4213
  var _stylexInject = _interopRequireDefault(stylexInject);
3693
4214
  var _styleq = require$$1;
3694
4215
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
@@ -3702,11 +4223,21 @@ function spread(styles, _options) {
3702
4223
  function stylexCreate(_styles) {
3703
4224
  throw new Error('stylex.create should never be called. It should be compiled away.');
3704
4225
  }
4226
+ function stylexCreateVars(_styles) {
4227
+ throw new Error('stylex.createVars should never be called. It should be compiled away.');
4228
+ }
4229
+ function stylexOverrideVars(_styles) {
4230
+ throw new Error('stylex.overrideVars should never be called. It should be compiled away.');
4231
+ }
3705
4232
  function stylexIncludes(_styles) {
3706
4233
  throw new Error('stylex.extends should never be called. It should be compiled away.');
3707
4234
  }
3708
4235
  const create = stylexCreate;
3709
4236
  stylex$1.create = create;
4237
+ const unstable_createVars = stylexCreateVars;
4238
+ stylex$1.unstable_createVars = unstable_createVars;
4239
+ const unstable_overrideVars = stylexOverrideVars;
4240
+ stylex$1.unstable_overrideVars = unstable_overrideVars;
3710
4241
  const include = stylexIncludes;
3711
4242
  stylex$1.include = include;
3712
4243
  const keyframes = _keyframes => {
@@ -3732,6 +4263,8 @@ function _stylex() {
3732
4263
  }
3733
4264
  _stylex.spread = spread;
3734
4265
  _stylex.create = create;
4266
+ _stylex.unstable_createVars = unstable_createVars;
4267
+ _stylex.unstable_overrideVars = unstable_overrideVars;
3735
4268
  _stylex.include = include;
3736
4269
  _stylex.keyframes = keyframes;
3737
4270
  _stylex.firstThatWorks = firstThatWorks;
@@ -3999,6 +4532,8 @@ function styleXTransform() {
3999
4532
  // may use the generated animation name.
4000
4533
  transformStyleXKeyframes(path.parentPath, state);
4001
4534
  }
4535
+ transformStyleXCreateVars(path, state);
4536
+ transformStyleXOverrideVars(path, state);
4002
4537
  transformStyleXCreate(path, state);
4003
4538
  }
4004
4539
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stylexjs/babel-plugin",
3
- "version": "0.2.0-beta.13",
3
+ "version": "0.2.0-beta.15",
4
4
  "description": "StyleX babel plugin.",
5
5
  "main": "lib/index.js",
6
6
  "repository": "https://github.com/facebook/stylex",
@@ -11,7 +11,7 @@
11
11
  "test": "jest"
12
12
  },
13
13
  "dependencies": {
14
- "@stylexjs/shared": "0.2.0-beta.13"
14
+ "@stylexjs/shared": "0.2.0-beta.15"
15
15
  },
16
16
  "peerDependencies": {
17
17
  "@babel/core": "^7.19.6",