@stylexjs/babel-plugin 0.5.0-alpha.3 → 0.5.0

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 CHANGED
@@ -30,6 +30,7 @@ var name = "@stylexjs/stylex";
30
30
 
31
31
  const defaultMessage = expected => (value, name) => name ? `Expected (${name}) to be ${expected}, but got \`${JSON.stringify(value)}\`.` : expected;
32
32
  const defaultUnionMessage = expected => (value, name) => name ? `Expected (${name}) to be ${expected}` : expected;
33
+ const defaultObjectMessage = expected => (value, name) => name ? `Expected (${name}) to be ${expected} but:` : expected;
33
34
  const indent = str => str.split('\n').filter(line => !line.trim().startsWith('But got:')).map(line => line.includes(', but got') ? line.replace(/, but got.+$/, '') : line).map(line => line.trim()[0] === '-' ? line : `- ${line}`).map(line => `\n\t${line}`).join('');
34
35
  const string = function () {
35
36
  let message = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultMessage('a string');
@@ -104,6 +105,23 @@ const object = function (shape) {
104
105
  return result;
105
106
  };
106
107
  };
108
+ const objectOf = function (check) {
109
+ let message = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultObjectMessage('an object');
110
+ return (value, name) => {
111
+ if (typeof value !== 'object' || value == null) {
112
+ return new Error(message(value, name));
113
+ }
114
+ const result = {};
115
+ for (const key in value) {
116
+ const item = check(value[key], name ? `${name}.${key}` : `With the key '${key}':`);
117
+ if (item instanceof Error) {
118
+ return new Error(`${message(value, name)}${indent(item.message)}\nBut got: ${indent(JSON.stringify(value))}`);
119
+ }
120
+ result[key] = item;
121
+ }
122
+ return result;
123
+ };
124
+ };
107
125
  const unionOf = function (a, b) {
108
126
  let message = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultUnionMessage('one of');
109
127
  return (value, name) => {
@@ -145,6 +163,118 @@ const logAndDefault = (check, value, def, name) => {
145
163
  return result;
146
164
  };
147
165
 
166
+ function isArrayExpression(path, props) {
167
+ return path.isArrayExpression(props);
168
+ }
169
+ function isArrowFunctionExpression(path, props) {
170
+ return path.isArrowFunctionExpression(props);
171
+ }
172
+ function isBinaryExpression(path, props) {
173
+ return path.isBinaryExpression(props);
174
+ }
175
+ function isBooleanLiteral(path, props) {
176
+ return path.isBooleanLiteral(props);
177
+ }
178
+ function isCallExpression(path, props) {
179
+ return path.isCallExpression(props);
180
+ }
181
+ function isClass(path, props) {
182
+ return path.isClass(props);
183
+ }
184
+ function isConditionalExpression(path, props) {
185
+ return path.isConditionalExpression(props);
186
+ }
187
+ function isExportDefaultDeclaration(path, props) {
188
+ return path.isExportDefaultDeclaration(props);
189
+ }
190
+ function isExportNamedDeclaration(path, props) {
191
+ return path.isExportNamedDeclaration(props);
192
+ }
193
+ function isExpression(path, props) {
194
+ return path.isExpression(props);
195
+ }
196
+ function isExpressionStatement(path, props) {
197
+ return path.isExpressionStatement(props);
198
+ }
199
+ function isExpressionWrapper(path, props) {
200
+ return path.isExpressionWrapper(props);
201
+ }
202
+ function isFunction(path, props) {
203
+ return path.isFunction(props);
204
+ }
205
+ function isIdentifier(path, props) {
206
+ return path.isIdentifier(props);
207
+ }
208
+ function isImportDeclaration(path, props) {
209
+ return path.isImportDeclaration(props);
210
+ }
211
+ function isImportDefaultSpecifier(path, props) {
212
+ return path.isImportDefaultSpecifier(props);
213
+ }
214
+ function isImportNamespaceSpecifier(path, props) {
215
+ return path.isImportNamespaceSpecifier(props);
216
+ }
217
+ function isImportSpecifier(path, props) {
218
+ return path.isImportSpecifier(props);
219
+ }
220
+ function isLogicalExpression(path, props) {
221
+ return path.isLogicalExpression(props);
222
+ }
223
+ function isMemberExpression(path, props) {
224
+ return path.isMemberExpression(props);
225
+ }
226
+ function isNullLiteral(path, props) {
227
+ return path.isNullLiteral(props);
228
+ }
229
+ function isNumericLiteral(path, props) {
230
+ return path.isNumericLiteral(props);
231
+ }
232
+ function isObjectExpression(path, props) {
233
+ return path.isObjectExpression(props);
234
+ }
235
+ function isObjectMethod(path, props) {
236
+ return path.isObjectMethod(props);
237
+ }
238
+ function isObjectProperty(path, props) {
239
+ return path.isObjectProperty(props);
240
+ }
241
+ function isProgram(path, props) {
242
+ return path.isProgram(props);
243
+ }
244
+ function isSequenceExpression(path, props) {
245
+ return path.isSequenceExpression(props);
246
+ }
247
+ function isSpreadElement(path, props) {
248
+ return path.isSpreadElement(props);
249
+ }
250
+ function isStatement(path, props) {
251
+ return path.isStatement(props);
252
+ }
253
+ function isStringLiteral(path, props) {
254
+ return path.isStringLiteral(props);
255
+ }
256
+ function isTSAsExpression(path, props) {
257
+ return path.isTSAsExpression(props);
258
+ }
259
+ function isTaggedTemplateExpression(path, props) {
260
+ return path.isTaggedTemplateExpression(props);
261
+ }
262
+ function isTemplateLiteral(path, props) {
263
+ return path.isTemplateLiteral(props);
264
+ }
265
+ function isUnaryExpression(path, props) {
266
+ return path.isUnaryExpression(props);
267
+ }
268
+ function isVariableDeclaration(path, props) {
269
+ return path.isVariableDeclaration(props);
270
+ }
271
+ function isVariableDeclarator(path, props) {
272
+ return path.isVariableDeclarator(props);
273
+ }
274
+ function isReferencedIdentifier(path, props) {
275
+ return path.isReferencedIdentifier(props);
276
+ }
277
+
148
278
  const CheckModuleResolution = unionOf3(object({
149
279
  type: literal('commonJS'),
150
280
  rootDir: string(),
@@ -202,8 +332,16 @@ class StateManager {
202
332
  const styleResolution = logAndDefault(unionOf3(literal('application-order'), literal('property-specificity'), literal('legacy-expand-shorthands')), options.styleResolution ?? 'application-order', 'application-order', 'options.styleResolution');
203
333
  const unstable_moduleResolution = logAndDefault(unionOf(nullish(), CheckModuleResolution), options.unstable_moduleResolution, null, 'options.unstable_moduleResolution');
204
334
  const treeshakeCompensation = logAndDefault(boolean(), options.treeshakeCompensation ?? false, false, 'options.treeshakeCompensation');
335
+ const aliasesOption = logAndDefault(unionOf(nullish(), objectOf(unionOf(string(), array(string())))), options.aliases, null, 'options.aliases');
336
+ const aliases = aliasesOption == null ? aliasesOption : Object.fromEntries(Object.entries(aliasesOption).map(_ref => {
337
+ let [key, value] = _ref;
338
+ if (typeof value === 'string') {
339
+ return [key, [value]];
340
+ }
341
+ return [key, value];
342
+ }));
205
343
  const opts = {
206
- ...options,
344
+ aliases,
207
345
  dev,
208
346
  test,
209
347
  runtimeInjection,
@@ -253,6 +391,56 @@ class StateManager {
253
391
  from: runInj
254
392
  } : runInj || null;
255
393
  }
394
+ addNamedImport(statementPath, as, from, options) {
395
+ const identifier = helperModuleImports.addNamed(statementPath, as, from, options);
396
+ const programPath = getProgramPath(statementPath);
397
+ if (programPath == null) {
398
+ return identifier;
399
+ }
400
+ const bodyPath = programPath.get('body');
401
+ let lastImportIndex = -1;
402
+ for (let i = 0; i < bodyPath.length; i++) {
403
+ const statement = bodyPath[i];
404
+ if (isImportDeclaration(statement)) {
405
+ lastImportIndex = i;
406
+ }
407
+ }
408
+ if (lastImportIndex === -1) {
409
+ return identifier;
410
+ }
411
+ const lastImport = bodyPath[lastImportIndex];
412
+ if (lastImport == null) {
413
+ return identifier;
414
+ }
415
+ const importName = statementPath.scope.generateUidIdentifier(as);
416
+ lastImport.insertAfter(t__namespace.variableDeclaration('var', [t__namespace.variableDeclarator(importName, identifier)]));
417
+ return importName;
418
+ }
419
+ addDefaultImport(statementPath, from, options) {
420
+ const identifier = helperModuleImports.addDefault(statementPath, from, options);
421
+ const programPath = getProgramPath(statementPath);
422
+ if (programPath == null) {
423
+ return identifier;
424
+ }
425
+ const bodyPath = programPath.get('body');
426
+ let lastImportIndex = -1;
427
+ for (let i = 0; i < bodyPath.length; i++) {
428
+ const statement = bodyPath[i];
429
+ if (isImportDeclaration(statement)) {
430
+ lastImportIndex = i;
431
+ }
432
+ }
433
+ if (lastImportIndex === -1) {
434
+ return identifier;
435
+ }
436
+ const lastImport = bodyPath[lastImportIndex];
437
+ if (lastImport == null) {
438
+ return identifier;
439
+ }
440
+ const importName = statementPath.scope.generateUidIdentifier('inject');
441
+ lastImport.insertAfter(t__namespace.variableDeclaration('var', [t__namespace.variableDeclarator(importName, identifier)]));
442
+ return importName;
443
+ }
256
444
  get isDev() {
257
445
  return !!this.options.dev;
258
446
  }
@@ -293,11 +481,12 @@ class StateManager {
293
481
  case 'commonJS':
294
482
  {
295
483
  const rootDir = this.options.unstable_moduleResolution.rootDir;
296
- const themeFileExtension = this.options.unstable_moduleResolution.themeFileExtension ?? '.stylex';
484
+ const aliases = this.options.aliases;
485
+ const themeFileExtension = this.options.unstable_moduleResolution?.themeFileExtension ?? '.stylex';
297
486
  if (!matchesFileSuffix(themeFileExtension)(importPath)) {
298
487
  return false;
299
488
  }
300
- const resolvedFilePath = filePathResolver(importPath, sourceFilePath);
489
+ const resolvedFilePath = filePathResolver(importPath, sourceFilePath, aliases);
301
490
  return resolvedFilePath ? ['themeNameRef', path.relative(rootDir, resolvedFilePath)] : false;
302
491
  }
303
492
  case 'haste':
@@ -310,11 +499,12 @@ class StateManager {
310
499
  }
311
500
  case 'experimental_crossFileParsing':
312
501
  {
502
+ const aliases = this.options.aliases;
313
503
  const themeFileExtension = this.options.unstable_moduleResolution.themeFileExtension ?? '.stylex';
314
504
  if (!matchesFileSuffix(themeFileExtension)(importPath)) {
315
505
  return false;
316
506
  }
317
- const resolvedFilePath = filePathResolver(importPath, sourceFilePath);
507
+ const resolvedFilePath = filePathResolver(importPath, sourceFilePath, aliases);
318
508
  return resolvedFilePath ? ['filePath', resolvedFilePath] : false;
319
509
  }
320
510
  default:
@@ -328,25 +518,48 @@ class StateManager {
328
518
  this.styleVarsToKeep.add(memberExpression);
329
519
  }
330
520
  }
331
- const filePathResolver = (relativeFilePath, sourceFilePath) => {
332
- const fileToLookFor = relativeFilePath;
333
- if (EXTENSIONS.some(ext => fileToLookFor.endsWith(ext))) {
334
- try {
335
- const resolvedFilePath = require.resolve(fileToLookFor, {
336
- paths: [path.dirname(sourceFilePath)]
337
- });
338
- return resolvedFilePath;
339
- } catch {}
340
- }
341
- for (const ext of EXTENSIONS) {
342
- try {
343
- const importPathStr = fileToLookFor.startsWith('.') ? fileToLookFor + ext : fileToLookFor;
344
- const resolvedFilePath = require.resolve(importPathStr, {
345
- paths: [path.dirname(sourceFilePath)]
521
+ function possibleAliasedPaths(importPath, aliases) {
522
+ const result = [importPath];
523
+ if (aliases == null || Object.keys(aliases).length === 0) {
524
+ return result;
525
+ }
526
+ for (const [alias, value] of Object.entries(aliases)) {
527
+ if (alias.includes('*')) {
528
+ const [before, after] = alias.split('*');
529
+ if (importPath.startsWith(before) && importPath.endsWith(after)) {
530
+ const replacementString = importPath.slice(before.length, after.length > 0 ? -after.length : undefined);
531
+ value.forEach(v => {
532
+ result.push(v.split('*').join(replacementString));
533
+ });
534
+ }
535
+ } else if (alias === importPath) {
536
+ value.forEach(v => {
537
+ result.push(v);
346
538
  });
347
- return resolvedFilePath;
348
- } catch {}
539
+ }
540
+ }
541
+ return result;
542
+ }
543
+ const filePathResolver = (relativeFilePath, sourceFilePath, aliases) => {
544
+ for (const ext of ['', ...EXTENSIONS]) {
545
+ const importPathStr = relativeFilePath + ext;
546
+ if (importPathStr.startsWith('.')) {
547
+ try {
548
+ return require.resolve(importPathStr, {
549
+ paths: [path.dirname(sourceFilePath)]
550
+ });
551
+ } catch {}
552
+ }
553
+ const allAliases = possibleAliasedPaths(importPathStr, aliases);
554
+ for (const possiblePath of allAliases) {
555
+ try {
556
+ return require.resolve(possiblePath, {
557
+ paths: [path.dirname(sourceFilePath)]
558
+ });
559
+ } catch {}
560
+ }
349
561
  }
562
+ return null;
350
563
  };
351
564
  const EXTENSIONS = ['.js', '.ts', '.tsx', '.jsx', '.mjs', '.cjs'];
352
565
  const addFileExtension = (importedFilePath, sourceFile) => {
@@ -357,6 +570,17 @@ const addFileExtension = (importedFilePath, sourceFile) => {
357
570
  return importedFilePath + fileExtension;
358
571
  };
359
572
  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);
573
+ const getProgramPath = path => {
574
+ let programPath = path;
575
+ while (programPath != null && !isProgram(programPath)) {
576
+ if (programPath.parentPath) {
577
+ programPath = programPath.parentPath;
578
+ } else {
579
+ return null;
580
+ }
581
+ }
582
+ return programPath;
583
+ };
360
584
 
361
585
  function readImportDeclarations(path, state) {
362
586
  const {
@@ -1147,7 +1371,10 @@ function getExpandedKeys(options) {
1147
1371
  return Object.keys(expansions[options.styleResolution ?? 'application-order']);
1148
1372
  }
1149
1373
  function flatMapExpandedShorthands(objEntry, options) {
1150
- const [key, value] = objEntry;
1374
+ let [key, value] = objEntry;
1375
+ if (key.startsWith('var(') && key.endsWith(')')) {
1376
+ key = key.slice(4, -1);
1377
+ }
1151
1378
  const expansion = expansions[options.styleResolution ?? 'application-order'][key];
1152
1379
  if (expansion) {
1153
1380
  if (Array.isArray(value)) {
@@ -1329,6 +1556,16 @@ function normalizeWhitespace(ast, _) {
1329
1556
  break;
1330
1557
  }
1331
1558
  case 'div':
1559
+ {
1560
+ if (node.value === ',') {
1561
+ node.before = '';
1562
+ node.after = '';
1563
+ } else {
1564
+ node.before = ' ';
1565
+ node.after = ' ';
1566
+ }
1567
+ break;
1568
+ }
1332
1569
  case 'function':
1333
1570
  {
1334
1571
  node.before = '';
@@ -1360,7 +1597,14 @@ function _interopRequireDefault$b(obj) { return obj && obj.__esModule ? obj : {
1360
1597
  const angles = ['deg', 'grad', 'turn', 'rad'];
1361
1598
  const timings = ['ms', 's'];
1362
1599
  function normalizeZeroDimensions(ast, _) {
1600
+ let endFunction = 0;
1363
1601
  ast.walk(node => {
1602
+ if (node.type === 'function' && !endFunction) {
1603
+ endFunction = node.sourceEndIndex ?? 0;
1604
+ }
1605
+ if (endFunction > 0 && node.sourceIndex > endFunction) {
1606
+ endFunction = 0;
1607
+ }
1364
1608
  if (node.type !== 'word') {
1365
1609
  return;
1366
1610
  }
@@ -1372,7 +1616,7 @@ function normalizeZeroDimensions(ast, _) {
1372
1616
  node.value = '0deg';
1373
1617
  } else if (timings.indexOf(dimension.unit) !== -1) {
1374
1618
  node.value = '0s';
1375
- } else {
1619
+ } else if (!endFunction) {
1376
1620
  node.value = '0';
1377
1621
  }
1378
1622
  });
@@ -1809,6 +2053,7 @@ var propertyPriorities = {};
1809
2053
  Object.defineProperty(propertyPriorities, "__esModule", {
1810
2054
  value: true
1811
2055
  });
2056
+ propertyPriorities.PSEUDO_ELEMENT_PRIORITY = propertyPriorities.PSEUDO_CLASS_PRIORITIES = propertyPriorities.AT_RULE_PRIORITIES = void 0;
1812
2057
  propertyPriorities.default = getPriority;
1813
2058
  const longHandPhysical = new Set();
1814
2059
  const longHandLogical = new Set();
@@ -2246,7 +2491,7 @@ longHandLogical.add('math-depth');
2246
2491
  longHandLogical.add('math-shift');
2247
2492
  longHandLogical.add('math-style');
2248
2493
  longHandLogical.add('touch-action');
2249
- const PRIORITIES = {
2494
+ const PSEUDO_CLASS_PRIORITIES = propertyPriorities.PSEUDO_CLASS_PRIORITIES = {
2250
2495
  ':is': 40,
2251
2496
  ':where': 40,
2252
2497
  ':not': 40,
@@ -2301,25 +2546,31 @@ const PRIORITIES = {
2301
2546
  ':focusVisible': 160,
2302
2547
  ':active': 170
2303
2548
  };
2549
+ const AT_RULE_PRIORITIES = propertyPriorities.AT_RULE_PRIORITIES = {
2550
+ '@supports': 30,
2551
+ '@media': 200,
2552
+ '@container': 300
2553
+ };
2554
+ const PSEUDO_ELEMENT_PRIORITY = propertyPriorities.PSEUDO_ELEMENT_PRIORITY = 5000;
2304
2555
  function getPriority(key) {
2305
2556
  if (key.startsWith('--')) {
2306
2557
  return 1;
2307
2558
  }
2308
2559
  if (key.startsWith('@supports')) {
2309
- return 30;
2560
+ return AT_RULE_PRIORITIES['@supports'];
2310
2561
  }
2311
2562
  if (key.startsWith('@media')) {
2312
- return 200;
2563
+ return AT_RULE_PRIORITIES['@media'];
2313
2564
  }
2314
2565
  if (key.startsWith('@container')) {
2315
- return 300;
2566
+ return AT_RULE_PRIORITIES['@container'];
2316
2567
  }
2317
2568
  if (key.startsWith('::')) {
2318
- return 5000;
2569
+ return PSEUDO_ELEMENT_PRIORITY;
2319
2570
  }
2320
2571
  if (key.startsWith(':')) {
2321
2572
  const prop = key.startsWith(':') && key.includes('(') ? key.slice(0, key.indexOf('(')) : key;
2322
- return PRIORITIES[prop] ?? 40;
2573
+ return PSEUDO_CLASS_PRIORITIES[prop] ?? 40;
2323
2574
  }
2324
2575
  if (longHandPhysical.has(key)) {
2325
2576
  return 4000;
@@ -2343,7 +2594,7 @@ generateCssRule.generateRule = generateRule;
2343
2594
  var _generateLtr$1 = _interopRequireDefault$6(generateLtr);
2344
2595
  var _generateRtl$1 = _interopRequireDefault$6(generateRtl);
2345
2596
  var _genCSSRule = genCSSRule$1;
2346
- var _propertyPriorities = _interopRequireDefault$6(propertyPriorities);
2597
+ var _propertyPriorities$1 = _interopRequireDefault$6(propertyPriorities);
2347
2598
  function _interopRequireDefault$6(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
2348
2599
  function generateRule(className, key, value, pseudos, atRules) {
2349
2600
  const pairs = Array.isArray(value) ? value.map(eachValue => [key, eachValue]) : [[key, value]];
@@ -2352,7 +2603,7 @@ function generateRule(className, key, value, pseudos, atRules) {
2352
2603
  const rtlDecls = pairs.map(_generateRtl$1.default).filter(Boolean).map(pair => pair.join(':')).join(';');
2353
2604
  const ltrRule = (0, _genCSSRule.genCSSRule)(className, ltrDecls, pseudos, atRules);
2354
2605
  const rtlRule = !rtlDecls ? null : (0, _genCSSRule.genCSSRule)(className, rtlDecls, pseudos, atRules);
2355
- const priority = (0, _propertyPriorities.default)(key) + pseudos.map(_propertyPriorities.default).reduce((a, b) => a + b, 0) + atRules.map(_propertyPriorities.default).reduce((a, b) => a + b, 0);
2606
+ const priority = (0, _propertyPriorities$1.default)(key) + pseudos.map(_propertyPriorities$1.default).reduce((a, b) => a + b, 0) + atRules.map(_propertyPriorities$1.default).reduce((a, b) => a + b, 0);
2356
2607
  return {
2357
2608
  priority,
2358
2609
  ltr: ltrRule,
@@ -3158,7 +3409,7 @@ var messages_1;
3158
3409
  Object.defineProperty(lib, "__esModule", {
3159
3410
  value: true
3160
3411
  });
3161
- var utils_1 = lib.utils = lib.types = messages_1 = lib.messages = keyframes_1 = lib.keyframes = include_1 = lib.include = firstThatWorks_1 = lib.firstThatWorks = defineVars_1 = lib.defineVars = createTheme_1 = lib.createTheme = create_1 = lib.create = IncludedStyles_1 = lib.IncludedStyles = void 0;
3412
+ var utils_1 = lib.utils = lib.types = messages_1 = lib.messages = keyframes_1 = lib.keyframes = include_1 = lib.include = firstThatWorks_1 = lib.firstThatWorks = defineVars_1 = lib.defineVars = createTheme_1 = lib.createTheme = create_1 = lib.create = lib.PSEUDO_ELEMENT_PRIORITY = lib.PSEUDO_CLASS_PRIORITIES = IncludedStyles_1 = lib.IncludedStyles = lib.AT_RULE_PRIORITIES = void 0;
3162
3413
  var _stylexCreate = _interopRequireDefault(stylexCreate$1);
3163
3414
  var _stylexDefineVars = _interopRequireDefault(stylexDefineVars$1);
3164
3415
  var _stylexCreateTheme = _interopRequireDefault(stylexCreateTheme$1);
@@ -3170,6 +3421,7 @@ var _fileBasedIdentifier = _interopRequireDefault(fileBasedIdentifier);
3170
3421
  var m = _interopRequireWildcard(messages$3);
3171
3422
  var _types = _interopRequireWildcard(types$1);
3172
3423
  lib.types = _types;
3424
+ var _propertyPriorities = propertyPriorities;
3173
3425
  function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
3174
3426
  function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
3175
3427
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
@@ -3185,6 +3437,9 @@ utils_1 = lib.utils = {
3185
3437
  messages_1 = lib.messages = m;
3186
3438
  IncludedStyles_1 = lib.IncludedStyles = _stylexInclude.IncludedStyles;
3187
3439
  firstThatWorks_1 = lib.firstThatWorks = _stylexFirstThatWorks.default;
3440
+ lib.PSEUDO_CLASS_PRIORITIES = _propertyPriorities.PSEUDO_CLASS_PRIORITIES;
3441
+ lib.AT_RULE_PRIORITIES = _propertyPriorities.AT_RULE_PRIORITIES;
3442
+ lib.PSEUDO_ELEMENT_PRIORITY = _propertyPriorities.PSEUDO_ELEMENT_PRIORITY;
3188
3443
 
3189
3444
  function namespaceToDevClassName(namespace, varName, filename) {
3190
3445
  const basename = path.basename(filename).split('.')[0];
@@ -3224,118 +3479,6 @@ function canBeIdentifier(str) {
3224
3479
  return str.match(/^[a-zA-Z_$][a-zA-Z0-9_$]*$/) != null;
3225
3480
  }
3226
3481
 
3227
- function isArrayExpression(path, props) {
3228
- return path.isArrayExpression(props);
3229
- }
3230
- function isArrowFunctionExpression(path, props) {
3231
- return path.isArrowFunctionExpression(props);
3232
- }
3233
- function isBinaryExpression(path, props) {
3234
- return path.isBinaryExpression(props);
3235
- }
3236
- function isBooleanLiteral(path, props) {
3237
- return path.isBooleanLiteral(props);
3238
- }
3239
- function isCallExpression(path, props) {
3240
- return path.isCallExpression(props);
3241
- }
3242
- function isClass(path, props) {
3243
- return path.isClass(props);
3244
- }
3245
- function isConditionalExpression(path, props) {
3246
- return path.isConditionalExpression(props);
3247
- }
3248
- function isExportDefaultDeclaration(path, props) {
3249
- return path.isExportDefaultDeclaration(props);
3250
- }
3251
- function isExportNamedDeclaration(path, props) {
3252
- return path.isExportNamedDeclaration(props);
3253
- }
3254
- function isExpression(path, props) {
3255
- return path.isExpression(props);
3256
- }
3257
- function isExpressionStatement(path, props) {
3258
- return path.isExpressionStatement(props);
3259
- }
3260
- function isExpressionWrapper(path, props) {
3261
- return path.isExpressionWrapper(props);
3262
- }
3263
- function isFunction(path, props) {
3264
- return path.isFunction(props);
3265
- }
3266
- function isIdentifier(path, props) {
3267
- return path.isIdentifier(props);
3268
- }
3269
- function isImportDeclaration(path, props) {
3270
- return path.isImportDeclaration(props);
3271
- }
3272
- function isImportDefaultSpecifier(path, props) {
3273
- return path.isImportDefaultSpecifier(props);
3274
- }
3275
- function isImportNamespaceSpecifier(path, props) {
3276
- return path.isImportNamespaceSpecifier(props);
3277
- }
3278
- function isImportSpecifier(path, props) {
3279
- return path.isImportSpecifier(props);
3280
- }
3281
- function isLogicalExpression(path, props) {
3282
- return path.isLogicalExpression(props);
3283
- }
3284
- function isMemberExpression(path, props) {
3285
- return path.isMemberExpression(props);
3286
- }
3287
- function isNullLiteral(path, props) {
3288
- return path.isNullLiteral(props);
3289
- }
3290
- function isNumericLiteral(path, props) {
3291
- return path.isNumericLiteral(props);
3292
- }
3293
- function isObjectExpression(path, props) {
3294
- return path.isObjectExpression(props);
3295
- }
3296
- function isObjectMethod(path, props) {
3297
- return path.isObjectMethod(props);
3298
- }
3299
- function isObjectProperty(path, props) {
3300
- return path.isObjectProperty(props);
3301
- }
3302
- function isProgram(path, props) {
3303
- return path.isProgram(props);
3304
- }
3305
- function isSequenceExpression(path, props) {
3306
- return path.isSequenceExpression(props);
3307
- }
3308
- function isSpreadElement(path, props) {
3309
- return path.isSpreadElement(props);
3310
- }
3311
- function isStatement(path, props) {
3312
- return path.isStatement(props);
3313
- }
3314
- function isStringLiteral(path, props) {
3315
- return path.isStringLiteral(props);
3316
- }
3317
- function isTSAsExpression(path, props) {
3318
- return path.isTSAsExpression(props);
3319
- }
3320
- function isTaggedTemplateExpression(path, props) {
3321
- return path.isTaggedTemplateExpression(props);
3322
- }
3323
- function isTemplateLiteral(path, props) {
3324
- return path.isTemplateLiteral(props);
3325
- }
3326
- function isUnaryExpression(path, props) {
3327
- return path.isUnaryExpression(props);
3328
- }
3329
- function isVariableDeclaration(path, props) {
3330
- return path.isVariableDeclaration(props);
3331
- }
3332
- function isVariableDeclarator(path, props) {
3333
- return path.isVariableDeclarator(props);
3334
- }
3335
- function isReferencedIdentifier(path, props) {
3336
- return path.isReferencedIdentifier(props);
3337
- }
3338
-
3339
3482
  const VALID_CALLEES = ['String', 'Number', 'Math', 'Object', 'Array'];
3340
3483
  const INVALID_METHODS = ['random', 'assign', 'defineProperties', 'defineProperty', 'freeze', 'seal', 'splice'];
3341
3484
  function isValidCallee(val) {
@@ -3955,7 +4098,10 @@ function evaluatePartialObjectRecursively(path, traversalState) {
3955
4098
  value: null
3956
4099
  };
3957
4100
  }
3958
- const key = keyResult.value;
4101
+ let key = keyResult.value;
4102
+ if (key.startsWith('var(') && key.endsWith(')')) {
4103
+ key = key.slice(4, -1);
4104
+ }
3959
4105
  const valuePath = prop.get('value');
3960
4106
  if (isObjectExpression(valuePath)) {
3961
4107
  const result = evaluatePartialObjectRecursively(valuePath, traversalState, functions, [...keyPath, key]);
@@ -4141,9 +4287,9 @@ function transformStyleXCreate(path, state) {
4141
4287
  from,
4142
4288
  as
4143
4289
  } = state.runtimeInjection;
4144
- injectName = as != null ? helperModuleImports.addNamed(statementPath, as, from, {
4290
+ injectName = as != null ? state.addNamedImport(statementPath, as, from, {
4145
4291
  nameHint: 'inject'
4146
- }) : helperModuleImports.addDefault(statementPath, from, {
4292
+ }) : state.addDefaultImport(statementPath, from, {
4147
4293
  nameHint: 'inject'
4148
4294
  });
4149
4295
  state.injectImportInserted = injectName;
@@ -4272,9 +4418,9 @@ function transformStyleXDefineVars(callExpressionPath, state) {
4272
4418
  from,
4273
4419
  as
4274
4420
  } = state.runtimeInjection;
4275
- injectName = as != null ? helperModuleImports.addNamed(statementPath, as, from, {
4421
+ injectName = as != null ? state.addNamedImport(statementPath, as, from, {
4276
4422
  nameHint: 'inject'
4277
- }) : helperModuleImports.addDefault(statementPath, from, {
4423
+ }) : state.addDefaultImport(statementPath, from, {
4278
4424
  nameHint: 'inject'
4279
4425
  });
4280
4426
  state.injectImportInserted = injectName;
@@ -4372,9 +4518,9 @@ function transformStyleXCreateTheme(callExpressionPath, state) {
4372
4518
  from,
4373
4519
  as
4374
4520
  } = state.runtimeInjection;
4375
- injectName = as != null ? helperModuleImports.addNamed(statementPath, as, from, {
4521
+ injectName = as != null ? state.addNamedImport(statementPath, as, from, {
4376
4522
  nameHint: 'inject'
4377
- }) : helperModuleImports.addDefault(statementPath, from, {
4523
+ }) : state.addDefaultImport(statementPath, from, {
4378
4524
  nameHint: 'inject'
4379
4525
  });
4380
4526
  state.injectImportInserted = injectName;
@@ -4474,9 +4620,9 @@ function transformStyleXKeyframes(path, state) {
4474
4620
  from,
4475
4621
  as
4476
4622
  } = state.runtimeInjection;
4477
- injectName = as != null ? helperModuleImports.addNamed(statementPath, as, from, {
4623
+ injectName = as != null ? state.addNamedImport(statementPath, as, from, {
4478
4624
  nameHint: 'inject'
4479
- }) : helperModuleImports.addDefault(statementPath, from, {
4625
+ }) : state.addDefaultImport(statementPath, from, {
4480
4626
  nameHint: 'inject'
4481
4627
  });
4482
4628
  state.injectImportInserted = injectName;
@@ -14,6 +14,11 @@ import type {
14
14
  CompiledNamespaces,
15
15
  StyleXOptions as RuntimeOptions,
16
16
  } from '@stylexjs/shared';
17
+ import type { ImportOptions } from '@babel/helper-module-imports';
18
+ type ImportAdditionOptions = Omit<
19
+ Partial<ImportOptions>,
20
+ 'ensureLiveReference' | 'ensureNoContext'
21
+ >;
17
22
  export type ImportPathResolution =
18
23
  | false
19
24
  | ['themeNameRef' | 'filePath', string];
@@ -43,6 +48,10 @@ export type StyleXOptions = Readonly<
43
48
  treeshakeCompensation?: boolean;
44
49
  genConditionalClasses: boolean;
45
50
  unstable_moduleResolution: null | undefined | ModuleResolution;
51
+ aliases?:
52
+ | null
53
+ | undefined
54
+ | Readonly<{ [$$Key$$: string]: string | ReadonlyArray<string> }>;
46
55
  })
47
56
  > & {
48
57
  importSources: ReadonlyArray<
@@ -55,6 +64,10 @@ export type StyleXOptions = Readonly<
55
64
  treeshakeCompensation?: boolean;
56
65
  genConditionalClasses: boolean;
57
66
  unstable_moduleResolution: null | undefined | ModuleResolution;
67
+ aliases?:
68
+ | null
69
+ | undefined
70
+ | Readonly<{ [$$Key$$: string]: string | ReadonlyArray<string> }>;
58
71
  }
59
72
  >;
60
73
  type StyleXStateOptions = Readonly<
@@ -64,11 +77,19 @@ type StyleXStateOptions = Readonly<
64
77
  runtimeInjection:
65
78
  | (null | undefined | string)
66
79
  | Readonly<{ from: string; as: null | undefined | string }>;
80
+ aliases?:
81
+ | null
82
+ | undefined
83
+ | Readonly<{ [$$Key$$: string]: ReadonlyArray<string> }>;
67
84
  })
68
85
  > & {
69
86
  runtimeInjection:
70
87
  | (null | undefined | string)
71
88
  | Readonly<{ from: string; as: null | undefined | string }>;
89
+ aliases?:
90
+ | null
91
+ | undefined
92
+ | Readonly<{ [$$Key$$: string]: ReadonlyArray<string> }>;
72
93
  }
73
94
  >;
74
95
  declare class StateManager {
@@ -103,6 +124,17 @@ declare class StateManager {
103
124
  | null
104
125
  | undefined
105
126
  | Readonly<{ from: string; as?: null | undefined | string }>;
127
+ addNamedImport(
128
+ statementPath: NodePath,
129
+ as: string,
130
+ from: string,
131
+ options: ImportAdditionOptions,
132
+ ): t.Identifier;
133
+ addDefaultImport(
134
+ statementPath: NodePath,
135
+ from: string,
136
+ options: ImportAdditionOptions,
137
+ ): t.Identifier;
106
138
  get isDev(): boolean;
107
139
  get isTest(): boolean;
108
140
  get filename(): string | void;
@@ -14,6 +14,12 @@ import type {
14
14
  CompiledNamespaces,
15
15
  StyleXOptions as RuntimeOptions,
16
16
  } from '@stylexjs/shared';
17
+ import type { ImportOptions } from '../../flow_modules/@babel/helper-module-imports';
18
+ type ImportAdditionOptions = Omit<
19
+ Partial<ImportOptions>,
20
+ 'ensureLiveReference' | 'ensureNoContext',
21
+ >;
22
+
17
23
  export type ImportPathResolution =
18
24
  | false
19
25
  | ['themeNameRef' | 'filePath', string];
@@ -43,12 +49,14 @@ export type StyleXOptions = $ReadOnly<{
43
49
  treeshakeCompensation?: boolean,
44
50
  genConditionalClasses: boolean,
45
51
  unstable_moduleResolution: ?ModuleResolution,
52
+ aliases?: ?$ReadOnly<{ [string]: string | $ReadOnlyArray<string> }>,
46
53
  ...
47
54
  }>;
48
55
 
49
56
  type StyleXStateOptions = $ReadOnly<{
50
57
  ...StyleXOptions,
51
58
  runtimeInjection: ?string | $ReadOnly<{ from: string, as: ?string }>,
59
+ aliases?: ?$ReadOnly<{ [string]: $ReadOnlyArray<string> }>,
52
60
  ...
53
61
  }>;
54
62
 
@@ -79,6 +87,17 @@ declare export default class StateManager {
79
87
  get canReferenceTheme(): boolean;
80
88
  get metadata(): { [key: string]: any };
81
89
  get runtimeInjection(): ?$ReadOnly<{ from: string, as?: ?string }>;
90
+ addNamedImport(
91
+ statementPath: NodePath<>,
92
+ as: string,
93
+ from: string,
94
+ options: ImportAdditionOptions,
95
+ ): t.Identifier;
96
+ addDefaultImport(
97
+ statementPath: NodePath<>,
98
+ from: string,
99
+ options: ImportAdditionOptions,
100
+ ): t.Identifier;
82
101
  get isDev(): boolean;
83
102
  get isTest(): boolean;
84
103
  get filename(): string | void;
@@ -39,7 +39,7 @@ export declare const object: <
39
39
  ) => Check<ObjOfChecks<T>>;
40
40
  export declare const objectOf: <T>(
41
41
  $$PARAM_0$$: Check<T>,
42
- $$PARAM_1$$: Msg,
42
+ message?: Msg,
43
43
  ) => Check<{ readonly [$$Key$$: string]: T }>;
44
44
  export declare const unionOf: <A, B>(
45
45
  a: Check<A>,
@@ -40,7 +40,10 @@ declare export const object: <T: { +[string]: Check<mixed> }>(
40
40
  msg?: Msg,
41
41
  ) => Check<ObjOfChecks<T>>;
42
42
 
43
- declare export const objectOf: <T>(Check<T>, Msg) => Check<{ +[string]: T }>;
43
+ declare export const objectOf: <T>(
44
+ Check<T>,
45
+ message?: Msg,
46
+ ) => Check<{ +[string]: T }>;
44
47
 
45
48
  declare export const unionOf: <A, B>(
46
49
  a: Check<A>,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stylexjs/babel-plugin",
3
- "version": "0.5.0-alpha.3",
3
+ "version": "0.5.0",
4
4
  "description": "StyleX babel plugin.",
5
5
  "main": "lib/index.js",
6
6
  "repository": "https://github.com/facebook/stylex",
@@ -9,12 +9,13 @@
9
9
  "prebuild": "gen-types -i src/ -o lib/",
10
10
  "build": "rollup --config ./rollup.config.mjs",
11
11
  "build-haste": "HASTE=true rollup --config ./rollup.config.mjs",
12
+ "build-watch": "rollup --config ./rollup.config.mjs --watch",
12
13
  "test": "jest"
13
14
  },
14
15
  "dependencies": {
15
16
  "@babel/helper-module-imports": "^7.22.15",
16
- "@stylexjs/shared": "0.5.0-alpha.3",
17
- "@stylexjs/stylex": "0.5.0-alpha.3",
17
+ "@stylexjs/shared": "0.5.0",
18
+ "@stylexjs/stylex": "0.5.0",
18
19
  "@babel/core": "^7.23.6",
19
20
  "@babel/traverse": "^7.23.6",
20
21
  "@babel/types": "^7.23.6"