@stylexjs/babel-plugin 0.17.1 → 0.17.3

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
@@ -283,6 +283,38 @@ function getProgramStatement(path) {
283
283
  }
284
284
  return programPath;
285
285
  }
286
+ function isVariableNamedExported(path) {
287
+ const declaration = path.parentPath;
288
+ const idPath = path.get('id');
289
+ if (!declaration || !declaration.isVariableDeclaration() || declaration.node.kind !== 'const' || !idPath.isIdentifier()) {
290
+ return false;
291
+ }
292
+ const variableName = idPath.node.name;
293
+ if (declaration.parentPath?.isExportNamedDeclaration() && declaration.parentPath.node.source == null) {
294
+ return true;
295
+ }
296
+ const programPath = getProgramPath(path);
297
+ if (programPath == null) {
298
+ return false;
299
+ }
300
+ let result = false;
301
+ programPath.traverse({
302
+ ExportNamedDeclaration(p) {
303
+ const node = p.node;
304
+ if (node.source != null) {
305
+ return;
306
+ }
307
+ if (node.declaration != null) {
308
+ return;
309
+ }
310
+ if (node.specifiers == null || node.specifiers.length === 0) {
311
+ return;
312
+ }
313
+ result = result || node.specifiers.some(s => s.type === 'ExportSpecifier' && s.local.type === 'Identifier' && s.exported.type === 'Identifier' && s.local.name === variableName && s.exported.name === variableName);
314
+ }
315
+ });
316
+ return result;
317
+ }
286
318
 
287
319
  const defaultOptions = {
288
320
  classNamePrefix: 'x',
@@ -602,15 +634,14 @@ class StateManager {
602
634
  ltr,
603
635
  rtl
604
636
  } = styleObj;
605
- const args = [t__namespace.stringLiteral(ltr), t__namespace.numericLiteral(priority), ...(rtl != null ? [t__namespace.stringLiteral(rtl)] : [])];
606
- if ('constKey' in styleObj && 'constVal' in styleObj) {
607
- const {
608
- constKey,
609
- constVal
610
- } = styleObj;
611
- args.push(t__namespace.stringLiteral(constKey), typeof constVal === 'number' ? t__namespace.numericLiteral(constVal) : t__namespace.stringLiteral(String(constVal)));
637
+ let constKey = null;
638
+ let constVal = null;
639
+ if (styleObj.constKey != null && styleObj.constVal != null) {
640
+ constKey = styleObj.constKey;
641
+ constVal = styleObj.constVal;
612
642
  }
613
- statementPath.insertBefore(t__namespace.expressionStatement(t__namespace.callExpression(injectName, args)));
643
+ const properties = [t__namespace.objectProperty(t__namespace.identifier('ltr'), t__namespace.stringLiteral(ltr)), ...(rtl != null ? [t__namespace.objectProperty(t__namespace.identifier('rtl'), t__namespace.stringLiteral(rtl))] : []), t__namespace.objectProperty(t__namespace.identifier('priority'), t__namespace.numericLiteral(priority)), ...(constKey != null ? [t__namespace.objectProperty(t__namespace.identifier('constKey'), t__namespace.stringLiteral(constKey)), t__namespace.objectProperty(t__namespace.identifier('constVal'), typeof constVal === 'number' ? t__namespace.numericLiteral(constVal) : t__namespace.stringLiteral(String(constVal)))] : [])];
644
+ statementPath.insertBefore(t__namespace.expressionStatement(t__namespace.callExpression(injectName, [t__namespace.objectExpression(properties)])));
614
645
  }
615
646
  }
616
647
  markComposedNamespace(memberExpression) {
@@ -5669,7 +5700,6 @@ function deopt(path, state, reason) {
5669
5700
  state.deoptReason = reason;
5670
5701
  }
5671
5702
  function evaluateImportedFile(filePath, namedExport, state, bindingPath) {
5672
- const fs = require('node:fs');
5673
5703
  const fileContents = fs.readFileSync(filePath, 'utf8');
5674
5704
  const ast = core.parseSync(fileContents, {
5675
5705
  babelrc: true
@@ -6951,11 +6981,10 @@ function transformStyleXDefineVars(callExpressionPath, state) {
6951
6981
  }
6952
6982
  function validateStyleXDefineVars(callExpressionPath) {
6953
6983
  const variableDeclaratorPath = callExpressionPath.parentPath;
6954
- const exportNamedDeclarationPath = variableDeclaratorPath.parentPath?.parentPath;
6955
6984
  if (variableDeclaratorPath == null || variableDeclaratorPath.isExpressionStatement() || !variableDeclaratorPath.isVariableDeclarator() || variableDeclaratorPath.node.id.type !== 'Identifier') {
6956
6985
  throw callExpressionPath.buildCodeFrameError(messages.unboundCallValue('defineVars'), SyntaxError);
6957
6986
  }
6958
- if (exportNamedDeclarationPath == null || !exportNamedDeclarationPath.isExportNamedDeclaration()) {
6987
+ if (!isVariableNamedExported(variableDeclaratorPath)) {
6959
6988
  throw callExpressionPath.buildCodeFrameError(messages.nonExportNamedDeclaration('defineVars'), SyntaxError);
6960
6989
  }
6961
6990
  if (callExpressionPath.node.arguments.length !== 1) {
@@ -7010,11 +7039,10 @@ function transformStyleXDefineConsts(callExpressionPath, state) {
7010
7039
  }
7011
7040
  function validateStyleXDefineConsts(callExpressionPath) {
7012
7041
  const variableDeclaratorPath = callExpressionPath.parentPath;
7013
- const exportNamedDeclarationPath = variableDeclaratorPath.parentPath?.parentPath;
7014
7042
  if (variableDeclaratorPath == null || !variableDeclaratorPath.isVariableDeclarator() || variableDeclaratorPath.node.id.type !== 'Identifier') {
7015
7043
  throw callExpressionPath.buildCodeFrameError(messages.unboundCallValue('defineConsts'), SyntaxError);
7016
7044
  }
7017
- if (exportNamedDeclarationPath == null || !exportNamedDeclarationPath.isExportNamedDeclaration()) {
7045
+ if (!isVariableNamedExported(variableDeclaratorPath)) {
7018
7046
  throw callExpressionPath.buildCodeFrameError(messages.nonExportNamedDeclaration('defineConsts'), SyntaxError);
7019
7047
  }
7020
7048
  if (callExpressionPath.node.arguments.length !== 1) {
@@ -7857,11 +7885,10 @@ function transformStyleXDefineMarker(path, state) {
7857
7885
  }
7858
7886
  function validateStyleXDefineMarker(path) {
7859
7887
  const variableDeclaratorPath = path.parentPath;
7860
- const exportNamedDeclarationPath = variableDeclaratorPath.parentPath?.parentPath;
7861
7888
  if (variableDeclaratorPath == null || variableDeclaratorPath.isExpressionStatement() || !variableDeclaratorPath.isVariableDeclarator() || variableDeclaratorPath.node.id.type !== 'Identifier') {
7862
7889
  throw path.buildCodeFrameError(unboundCallValue('defineMarker'), SyntaxError);
7863
7890
  }
7864
- if (exportNamedDeclarationPath == null || !exportNamedDeclarationPath.isExportNamedDeclaration()) {
7891
+ if (!isVariableNamedExported(variableDeclaratorPath)) {
7865
7892
  throw path.buildCodeFrameError(nonExportNamedDeclaration('defineMarker'), SyntaxError);
7866
7893
  }
7867
7894
  }
@@ -55,7 +55,6 @@ export type StyleXOptions = Readonly<{
55
55
  | 'property-specificity'
56
56
  | 'legacy-expand-shorthands';
57
57
  test: boolean;
58
- ...
59
58
  }>;
60
59
  export type MutableCompiledNamespaces = { [key: string]: FlatCompiledStyles };
61
60
  export type CompiledNamespaces = Readonly<MutableCompiledNamespaces>;
@@ -187,7 +187,6 @@ export declare class Num<T extends NumberValue>
187
187
  export declare const number: <T extends NumberValue = NumberValue>(
188
188
  value: NestedWithNumbers,
189
189
  ) => Num<T>;
190
- export declare type number = typeof number;
191
190
  type ResolutionValue = string | 0;
192
191
  export declare class Resolution<T extends ResolutionValue>
193
192
  extends BaseCSSType
@@ -35,3 +35,14 @@ export declare function addDefaultImport(
35
35
  ): t.Identifier;
36
36
  export declare function isProgramLevel(path: NodePath): boolean;
37
37
  export declare function getProgramStatement(path: NodePath): NodePath;
38
+ /**
39
+ * Checks if a variable with the given name is named exported in the program.
40
+ * This handles both:
41
+ * - Direct named exports: `export const x = ...`
42
+ * - Locally declared named exports: `const x = ...; export { x }`
43
+ *
44
+ * Default exports and re-exports from other files (e.g., `export { x } from './other'`) are NOT allowed.
45
+ */
46
+ export declare function isVariableNamedExported(
47
+ path: NodePath<t.VariableDeclarator>,
48
+ ): boolean;
@@ -42,3 +42,15 @@ declare export function addDefaultImport(
42
42
  declare export function isProgramLevel(path: NodePath<>): boolean;
43
43
 
44
44
  declare export function getProgramStatement(path: NodePath<>): NodePath<>;
45
+
46
+ /**
47
+ * Checks if a variable with the given name is named exported in the program.
48
+ * This handles both:
49
+ * - Direct named exports: `export const x = ...`
50
+ * - Locally declared named exports: `const x = ...; export { x }`
51
+ *
52
+ * Default exports and re-exports from other files (e.g., `export { x } from './other'`) are NOT allowed.
53
+ */
54
+ declare export function isVariableNamedExported(
55
+ path: NodePath<t.VariableDeclarator>,
56
+ ): boolean;
@@ -11,7 +11,6 @@ import type { NodePath } from '@babel/traverse';
11
11
 
12
12
  import * as t from '@babel/types';
13
13
  import StateManager from '../utils/state-manager';
14
-
15
14
  /// This function looks for `stylex.defineConsts` calls and transforms them.
16
15
  /// 1. It finds and validates the first argument to `stylex.defineConsts`.
17
16
  /// 2. It evaluates the style const object to a JS value, erroring on non-static or non-object values.
package/package.json CHANGED
@@ -1,8 +1,10 @@
1
1
  {
2
2
  "name": "@stylexjs/babel-plugin",
3
- "version": "0.17.1",
3
+ "version": "0.17.3",
4
4
  "description": "StyleX babel plugin.",
5
5
  "main": "lib/index.js",
6
+ "browser": "lib/index.browser.js",
7
+ "types": "./lib/index.d.ts",
6
8
  "repository": {
7
9
  "type": "git",
8
10
  "url": "git+https://github.com/facebook/stylex.git"
@@ -21,8 +23,8 @@
21
23
  "@babel/traverse": "^7.26.8",
22
24
  "@babel/types": "^7.26.8",
23
25
  "@dual-bundle/import-meta-resolve": "^4.1.0",
24
- "@stylexjs/shared": "0.17.1",
25
- "@stylexjs/stylex": "0.17.1",
26
+ "@stylexjs/shared": "0.17.3",
27
+ "@stylexjs/stylex": "0.17.3",
26
28
  "postcss-value-parser": "^4.1.0"
27
29
  },
28
30
  "devDependencies": {
@@ -33,8 +35,9 @@
33
35
  "@rollup/plugin-node-resolve": "^15.3.0",
34
36
  "@rollup/plugin-replace": "^6.0.1",
35
37
  "babel-plugin-syntax-hermes-parser": "^0.32.1",
38
+ "path-browserify": "^1.0.1",
36
39
  "rollup": "^4.24.0",
37
- "scripts": "0.17.1"
40
+ "scripts": "0.17.3"
38
41
  },
39
42
  "files": [
40
43
  "flow_modules/*",
@@ -1,17 +0,0 @@
1
- /**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- *
8
- */
9
-
10
- export declare const PSEUDO_CLASS_PRIORITIES: Readonly<{
11
- [$$Key$$: string]: number;
12
- }>;
13
- type AtRulePriorities = { '@supports': 30; '@media': 200; '@container': 300 };
14
- export declare const AT_RULE_PRIORITIES: Readonly<AtRulePriorities>;
15
- export declare const PSEUDO_ELEMENT_PRIORITY: number;
16
- declare function getPriority(key: string): number;
17
- export default getPriority;
@@ -1,78 +0,0 @@
1
- /**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- * @flow strict
8
- */
9
-
10
- // type PseudoClassPriorities = {
11
- // ':is': 40,
12
- // ':where': 40,
13
- // ':not': 40,
14
- // ':has': 45,
15
- // ':dir': 50,
16
- // ':lang': 51,
17
- // ':first-child': 52,
18
- // ':first-of-type': 53,
19
- // ':last-child': 54,
20
- // ':last-of-type': 55,
21
- // ':only-child': 56,
22
- // ':only-of-type': 57,
23
- // ':nth-child': 60,
24
- // ':nth-last-child': 61,
25
- // ':nth-of-type': 62,
26
- // ':nth-last-of-type': 63, // 'nth-last-of-type' is the same priority as 'nth-of-type
27
- // ':empty': 70,
28
- // ':link': 80,
29
- // ':any-link': 81,
30
- // ':local-link': 82,
31
- // ':target-within': 83,
32
- // ':target': 84,
33
- // ':visited': 85,
34
- // ':enabled': 91,
35
- // ':disabled': 92,
36
- // ':required': 93,
37
- // ':optional': 94,
38
- // ':read-only': 95,
39
- // ':read-write': 96,
40
- // ':placeholder-shown': 97,
41
- // ':in-range': 98,
42
- // ':out-of-range': 99,
43
- // ':default': 100,
44
- // ':checked': 101,
45
- // ':indeterminate': 101,
46
- // ':blank': 102,
47
- // ':valid': 103,
48
- // ':invalid': 104,
49
- // ':user-invalid': 105,
50
- // ':autofill': 110,
51
- // ':picture-in-picture': 120,
52
- // ':modal': 121,
53
- // ':fullscreen': 122,
54
- // ':paused': 123,
55
- // ':playing': 124,
56
- // ':current': 125,
57
- // ':past': 126,
58
- // ':future': 127,
59
- // ':hover': 130,
60
- // ':focusWithin': 140,
61
- // ':focus': 150,
62
- // ':focusVisible': 160,
63
- // ':active': 170,
64
- // };
65
-
66
- declare export const PSEUDO_CLASS_PRIORITIES: $ReadOnly<{ [string]: number }>;
67
-
68
- type AtRulePriorities = {
69
- '@supports': 30,
70
- '@media': 200,
71
- '@container': 300,
72
- };
73
-
74
- declare export const AT_RULE_PRIORITIES: $ReadOnly<AtRulePriorities>;
75
-
76
- declare export const PSEUDO_ELEMENT_PRIORITY: number;
77
-
78
- declare export default function getPriority(key: string): number;
@@ -1,20 +0,0 @@
1
- /**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- *
8
- */
9
-
10
- import type { NodePath } from '@babel/traverse';
11
- import type StateManager from '../utils/state-manager';
12
- import * as t from '@babel/types';
13
- /**
14
- * Transform `stylex.defaultTarget` calls and property access to use the configured classNamePrefix
15
- */
16
- declare function transformStyleXDefaultTarget(
17
- path: NodePath<t.CallExpression | t.MemberExpression>,
18
- state: StateManager,
19
- ): void;
20
- export default transformStyleXDefaultTarget;
@@ -1,21 +0,0 @@
1
- /**
2
- * Copyright (c) Meta Platforms, Inc. and affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- * @flow strict
8
- */
9
-
10
- import type { NodePath } from '../../flow_modules/@babel/traverse';
11
- import type StateManager from '../utils/state-manager';
12
-
13
- import * as t from '../../flow_modules/@babel/types';
14
-
15
- /**
16
- * Transform `stylex.defaultTarget` calls and property access to use the configured classNamePrefix
17
- */
18
- declare export default function transformStyleXDefaultTarget(
19
- path: NodePath<t.CallExpression | t.MemberExpression>,
20
- state: StateManager,
21
- ): void;