@stylexjs/babel-plugin 0.1.0-beta.5 → 0.1.0-beta.7

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 +57 -79
  2. package/package.json +2 -2
package/lib/index.js CHANGED
@@ -1414,7 +1414,7 @@ Object.defineProperty(objectUtils, "__esModule", {
1414
1414
  value: true
1415
1415
  });
1416
1416
  objectUtils.Pipe = void 0;
1417
- objectUtils.flattenObject = flattenObject$1;
1417
+ objectUtils.flattenObject = flattenObject;
1418
1418
  objectUtils.objEntries = objEntries;
1419
1419
  objectUtils.objFromEntries = objFromEntries;
1420
1420
  objectUtils.objMap = objMap;
@@ -1433,10 +1433,10 @@ var _stylexInclude$2 = stylexInclude$1;
1433
1433
 
1434
1434
  // A bunch of object utils with better Flow types
1435
1435
 
1436
- function flattenObject$1(obj) {
1436
+ function flattenObject(obj) {
1437
1437
  const result = {};
1438
1438
  for (const [key, value] of objEntries(obj)) {
1439
- if (typeof value === 'string') {
1439
+ if (typeof value === 'string' || value == null) {
1440
1440
  result[key] = value;
1441
1441
  } else if (value instanceof _stylexInclude$2.IncludedStyles) {
1442
1442
  result[key] = value;
@@ -1544,7 +1544,11 @@ function styleXCreateSet(namespaces) {
1544
1544
  throw new Error(messages$1.ILLEGAL_NAMESPACE_VALUE);
1545
1545
  }
1546
1546
  const [resolvedNamespace, injected] = styleXCreateNamespace(namespace, options);
1547
- resolvedNamespaces[namespaceName] = (0, _objectUtils$1.flattenObject)(resolvedNamespace);
1547
+ const compiledNamespace = (0, _objectUtils$1.flattenObject)(resolvedNamespace);
1548
+ resolvedNamespaces[namespaceName] = {
1549
+ ...compiledNamespace,
1550
+ $$css: true
1551
+ };
1548
1552
  for (const cn of Object.keys(injected)) {
1549
1553
  if (injectedStyles[cn] == null) {
1550
1554
  injectedStyles[cn] = injected[cn];
@@ -1591,7 +1595,7 @@ function styleXCreateNamespace(style, options) {
1591
1595
  return (0, _expandShorthands$1.default)([innerKey, innerValue]);
1592
1596
  }))]];
1593
1597
  } else {
1594
- if (typeof value !== 'string' && typeof value !== 'number' && !Array.isArray(value)) {
1598
+ if (value !== null && typeof value !== 'string' && typeof value !== 'number' && !Array.isArray(value)) {
1595
1599
  throw new Error(messages$1.ILLEGAL_PROP_VALUE);
1596
1600
  }
1597
1601
  if (Array.isArray(value) && value.some(val => typeof val === 'object')) {
@@ -1615,15 +1619,23 @@ function styleXCreateNamespace(style, options) {
1615
1619
  const pseudo = key;
1616
1620
  const innerObj = {};
1617
1621
  for (const [innerKey, innerVal] of (0, _objectUtils$1.objEntries)(val)) {
1618
- const [updatedKey, className, cssRule] = (0, _convertToClassName.default)([innerKey, innerVal], pseudo, options);
1619
- innerObj[updatedKey] = className;
1620
- injectedStyles[updatedKey + pseudo] = [className, cssRule];
1622
+ if (innerVal === null) {
1623
+ innerObj[innerKey] = null;
1624
+ } else {
1625
+ const [updatedKey, className, cssRule] = (0, _convertToClassName.default)([innerKey, innerVal], pseudo, options);
1626
+ innerObj[updatedKey] = className;
1627
+ injectedStyles[updatedKey + pseudo] = [className, cssRule];
1628
+ }
1621
1629
  }
1622
1630
  resolvedNamespace[key] = innerObj;
1623
1631
  } else {
1624
- const [updatedKey, className, cssRule] = (0, _convertToClassName.default)([key, val], undefined, options);
1625
- resolvedNamespace[updatedKey] = className;
1626
- injectedStyles[updatedKey] = [className, cssRule];
1632
+ if (val === null) {
1633
+ resolvedNamespace[key] = null;
1634
+ } else {
1635
+ const [updatedKey, className, cssRule] = (0, _convertToClassName.default)([key, val], undefined, options);
1636
+ resolvedNamespace[updatedKey] = className;
1637
+ injectedStyles[updatedKey] = [className, cssRule];
1638
+ }
1627
1639
  }
1628
1640
  }
1629
1641
  const finalInjectedStyles = (0, _objectUtils$1.objFromEntries)((0, _objectUtils$1.objValues)(injectedStyles));
@@ -1788,7 +1800,8 @@ function convertToTestStyles(obj, varName, state) {
1788
1800
  for (const [key, value] of Object.entries(obj)) {
1789
1801
  const devClassName = namespaceToDevClassName(key, varName, state.filename ?? 'UnkownFile');
1790
1802
  result[key] = {
1791
- [devClassName]: devClassName
1803
+ [devClassName]: devClassName,
1804
+ $$css: true
1792
1805
  };
1793
1806
  }
1794
1807
  return result;
@@ -1803,7 +1816,7 @@ function convertToTestStyles(obj, varName, state) {
1803
1816
  function convertObjectToAST(obj) {
1804
1817
  return t__namespace.objectExpression(Object.entries(obj).map(_ref => {
1805
1818
  let [key, value] = _ref;
1806
- return value instanceof IncludedStyles_1 ? t__namespace.spreadElement(value.astNode) : t__namespace.objectProperty(canBeIdentifier(key) ? t__namespace.identifier(key) : t__namespace.stringLiteral(key), typeof value === 'string' ? t__namespace.stringLiteral(value) : convertObjectToAST(value));
1819
+ return value instanceof IncludedStyles_1 ? t__namespace.spreadElement(value.astNode) : t__namespace.objectProperty(canBeIdentifier(key) ? t__namespace.identifier(key) : t__namespace.stringLiteral(key), typeof value === 'string' ? t__namespace.stringLiteral(value) : typeof value === 'boolean' ? t__namespace.booleanLiteral(value) : value === null ? t__namespace.nullLiteral() : convertObjectToAST(value));
1807
1820
  }));
1808
1821
  }
1809
1822
  function canBeIdentifier(str) {
@@ -2270,7 +2283,7 @@ function transformStyleXCreate(path, state) {
2270
2283
  if (firstArg == null || !firstArg.isObjectExpression()) {
2271
2284
  throw new Error(messages_1.ILLEGAL_ARGUMENT_LENGTH);
2272
2285
  }
2273
- const processingState = preProcessStyleArg(firstArg);
2286
+ preProcessStyleArg(firstArg, state);
2274
2287
  const injectedKeyframes = {};
2275
2288
  function keyframes(animation) {
2276
2289
  const [animationName, injectedStyle] = keyframes_1(animation, state.options);
@@ -2343,20 +2356,12 @@ function transformStyleXCreate(path, state) {
2343
2356
  state.styleVars.set(varName, path.parentPath);
2344
2357
  }
2345
2358
  path.replaceWith(convertObjectToAST(compiledStyles));
2346
- postProcessStyles(path, processingState);
2347
2359
  if (Object.keys(injectedStyles).length === 0) {
2348
2360
  return;
2349
2361
  }
2350
2362
  if (state.isDev || state.stylexSheetName == null) {
2351
2363
  const statementPath = findNearestStatementAncestor(path);
2352
- let stylexName;
2353
- state.stylexImport.forEach(importName => {
2354
- stylexName = importName;
2355
- });
2356
- if (stylexName == null) {
2357
- stylexName = '__stylex__';
2358
- statementPath.insertBefore(t__namespace.importDeclaration([t__namespace.importDefaultSpecifier(t__namespace.identifier(stylexName))], t__namespace.stringLiteral('stylex')));
2359
- }
2364
+ let stylexName = getStylexDefaultImport(path, state);
2360
2365
  for (const [key, {
2361
2366
  ltr,
2362
2367
  priority,
@@ -2402,13 +2407,8 @@ function findNearestStatementAncestor(path) {
2402
2407
  return findNearestStatementAncestor(path.parentPath);
2403
2408
  }
2404
2409
 
2405
- // These functions are for special handling of dynamic parts of the style object
2406
- // Currently they handle spreading pre-defined style objects with stylex.create calls.
2407
- //
2408
- // It converts the spreads into a special string and remembers the original value in an object...
2409
-
2410
- function preProcessStyleArg(objPath) {
2411
- const state = {};
2410
+ // Converts typed spreads to `stylex.include` calls.
2411
+ function preProcessStyleArg(objPath, state) {
2412
2412
  objPath.traverse({
2413
2413
  SpreadElement(path) {
2414
2414
  const argument = path.get('argument');
@@ -2431,36 +2431,23 @@ function preProcessStyleArg(objPath) {
2431
2431
  // Disallow spreads within pseudo or media query objects
2432
2432
  throw new Error(messages_1.ILLEGAL_NESTED_PSEUDO);
2433
2433
  }
2434
- const key = `include(${toString(expression)})`;
2435
- path.replaceWith(t__namespace.objectProperty(t__namespace.stringLiteral(key), t__namespace.stringLiteral(key)));
2436
- state[key] = expression.node;
2434
+ let stylexName = getStylexDefaultImport(path, state);
2435
+ argument.replaceWith(t__namespace.callExpression(t__namespace.memberExpression(t__namespace.identifier(stylexName), t__namespace.identifier('include')), [expression.node]));
2437
2436
  }
2438
2437
  });
2439
- return state;
2440
2438
  }
2441
-
2442
- // Later, it finds those strings and replaces them back with their original values.
2443
- function postProcessStyles(objPath, state) {
2444
- objPath.traverse({
2445
- ObjectProperty(path) {
2446
- const node = path.node.key;
2447
- if (node.type === 'StringLiteral' && state[node.value] != null) {
2448
- path.replaceWith(t__namespace.spreadElement(state[node.value]));
2449
- }
2450
- }
2439
+ function getStylexDefaultImport(path, state) {
2440
+ const statementPath = findNearestStatementAncestor(path);
2441
+ let stylexName;
2442
+ state.stylexImport.forEach(importName => {
2443
+ stylexName = importName;
2451
2444
  });
2452
- }
2453
-
2454
- // A function to deterministicly convert a spreadded expression to a string.
2455
- function toString(path) {
2456
- if (path.isIdentifier()) {
2457
- return path.node.name;
2458
- } else if (path.isStringLiteral() || path.isNumericLiteral()) {
2459
- return String(path.node.value);
2460
- } else if (path.isMemberExpression()) {
2461
- return `${toString(path.get('object'))}.${toString(path.get('property'))}`;
2445
+ if (stylexName == null) {
2446
+ stylexName = '__stylex__';
2447
+ statementPath.insertBefore(t__namespace.importDeclaration([t__namespace.importDefaultSpecifier(t__namespace.identifier(stylexName))], t__namespace.stringLiteral('stylex')));
2448
+ state.stylexImport.add(stylexName);
2462
2449
  }
2463
- throw new Error(path.node.type);
2450
+ return stylexName;
2464
2451
  }
2465
2452
 
2466
2453
  /**
@@ -2602,7 +2589,7 @@ function transformStyleXMerge(path, state) {
2602
2589
  if (style == null || style[propName] == null) {
2603
2590
  throw new Error(`Unknown style ${objName}.${propName}. The defined style ${objName}, contains the following keys: ${Object.keys(style ?? {}).join(', ')}`);
2604
2591
  }
2605
- const namespace = flattenObject(style[propName]);
2592
+ const namespace = style[propName];
2606
2593
  Object.assign(resolvedStyles, namespace);
2607
2594
  } else {
2608
2595
  // Unknown style found. bail out.
@@ -2627,12 +2614,13 @@ function transformStyleXMerge(path, state) {
2627
2614
  }
2628
2615
  const allKeys = new Set([...Object.keys(primary ?? {}), ...Object.keys(fallback ?? {})]);
2629
2616
  for (const key of allKeys) {
2630
- if (resolvedStyles[key] == null) {
2631
- const primaryValue = (primary === null || primary === void 0 ? void 0 : primary[key]) ?? resolvedStyles[key] ?? '';
2632
- const fallbackValue = (fallback === null || fallback === void 0 ? void 0 : fallback[key]) ?? resolvedStyles[key] ?? '';
2633
- resolvedStyles[key] = [test, primaryValue, fallbackValue];
2634
- }
2617
+ // if (resolvedStyles[key] === undefined) {
2618
+ const primaryValue = firstValidValue(primary === null || primary === void 0 ? void 0 : primary[key], resolvedStyles[key], '');
2619
+ const fallbackValue = firstValidValue(fallback === null || fallback === void 0 ? void 0 : fallback[key], resolvedStyles[key], '');
2620
+ resolvedStyles[key] = [test, primaryValue, fallbackValue];
2621
+ // }
2635
2622
  }
2623
+
2636
2624
  break;
2637
2625
  case 'LogicalExpression':
2638
2626
  if (arg.operator !== '&&') {
@@ -2696,23 +2684,13 @@ function transformStyleXMerge(path, state) {
2696
2684
  path.replaceWith(stringExpression);
2697
2685
  }
2698
2686
  }
2699
-
2700
- // This function takes nested objects with styles collapses them to a single level deep.
2701
- // `':hover': {color: 'red'}` becomes `':hover.color': 'red'`
2702
- function flattenObject(object) {
2703
- const result = {};
2704
- for (const [key, value] of Object.entries(object)) {
2705
- if (typeof value === 'string') {
2706
- result[key] = value;
2707
- } else if (typeof value === 'object') {
2708
- for (const [subKey, subValue] of Object.entries(value)) {
2709
- result[`${key}.${subKey}`] = subValue;
2710
- }
2711
- }
2687
+ function firstValidValue() {
2688
+ for (var _len = arguments.length, values = new Array(_len), _key = 0; _key < _len; _key++) {
2689
+ values[_key] = arguments[_key];
2712
2690
  }
2713
- return result;
2691
+ const [first, ...rest] = values;
2692
+ return first !== undefined ? first : firstValidValue(...rest);
2714
2693
  }
2715
-
2716
2694
  // Looks for Null or locally defined style namespaces.
2717
2695
  // Otherwise it returns the string "other"
2718
2696
  // Which is used as an indicator to bail out of this optimization.
@@ -2737,7 +2715,7 @@ function parseNullableStyle(node, state) {
2737
2715
  if (style == null) {
2738
2716
  return 'other';
2739
2717
  }
2740
- return flattenObject(style);
2718
+ return style;
2741
2719
  } else {
2742
2720
  return 'other';
2743
2721
  }
@@ -2749,8 +2727,8 @@ function makeStringExpression(values) {
2749
2727
  if (inTernary && strings !== '') {
2750
2728
  strings = ' ' + strings;
2751
2729
  }
2752
- const nonStrings = values.filter(value => typeof value !== 'string');
2753
- const groupedByTest = groupBy(nonStrings, _ref => {
2730
+ const nonPrimitive = values.filter(value => typeof value !== 'string' && typeof value !== 'boolean' && value != null);
2731
+ const groupedByTest = groupBy(nonPrimitive, _ref => {
2754
2732
  let [test, _a, _b] = _ref;
2755
2733
  return test;
2756
2734
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stylexjs/babel-plugin",
3
- "version": "0.1.0-beta.5",
3
+ "version": "0.1.0-beta.7",
4
4
  "description": "StyleX babel plugin.",
5
5
  "main": "lib/index.js",
6
6
  "repository": "https://github.com/facebookexternal/stylex",
@@ -11,7 +11,7 @@
11
11
  "test": "jest"
12
12
  },
13
13
  "dependencies": {
14
- "@stylexjs/shared": "0.1.0-beta.5"
14
+ "@stylexjs/shared": "0.1.0-beta.7"
15
15
  },
16
16
  "peerDependencies": {
17
17
  "@babel/core": "^7.19.6",