@stylexjs/babel-plugin 0.2.0-beta.24 → 0.2.0-beta.25

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 t__namespace = /*#__PURE__*/_interopNamespaceDefault(t);
30
30
  var name = "@stylexjs/stylex";
31
31
 
32
32
  class StateManager {
33
+ importPaths = new Set();
33
34
  stylexImport = new Set();
34
35
  stylexPropsImport = new Set();
35
36
  stylexCreateImport = new Set();
@@ -64,6 +65,15 @@ class StateManager {
64
65
  this._state.opts = opts;
65
66
  return this._state.opts;
66
67
  }
68
+ get importPathString() {
69
+ if (this.importPaths.has('@stylexjs/stylex')) {
70
+ return '@stylexjs/stylex';
71
+ }
72
+ if (this.importPaths.size > 0) {
73
+ return [...this.importPaths][0];
74
+ }
75
+ return '@stylexjs/stylex';
76
+ }
67
77
  get canReferenceTheme() {
68
78
  return !!this.inStyleXCreate;
69
79
  }
@@ -149,7 +159,7 @@ const filePathResolver = (relativeFilePath, sourceFilePath) => {
149
159
  const fileToLookFor = relativeFilePath;
150
160
  if (EXTENSIONS.some(ext => fileToLookFor.endsWith(ext))) {
151
161
  try {
152
- const resolvedFilePath = fileToLookFor.startsWith('./') || fileToLookFor.startsWith('../') ? path.join(path.dirname(sourceFilePath), fileToLookFor) : require.resolve(fileToLookFor, {
162
+ const resolvedFilePath = require.resolve(fileToLookFor, {
153
163
  paths: [path.dirname(sourceFilePath)]
154
164
  });
155
165
  return resolvedFilePath;
@@ -157,7 +167,8 @@ const filePathResolver = (relativeFilePath, sourceFilePath) => {
157
167
  }
158
168
  for (const ext of EXTENSIONS) {
159
169
  try {
160
- const resolvedFilePath = fileToLookFor.startsWith('./') || fileToLookFor.startsWith('../') ? path.join(path.dirname(sourceFilePath), fileToLookFor + ext) : require.resolve(fileToLookFor + ext, {
170
+ const importPathStr = fileToLookFor.startsWith('.') ? fileToLookFor + ext : fileToLookFor;
171
+ const resolvedFilePath = require.resolve(importPathStr, {
161
172
  paths: [path.dirname(sourceFilePath)]
162
173
  });
163
174
  return resolvedFilePath;
@@ -182,6 +193,7 @@ function readImportDeclarations(path, state) {
182
193
  return;
183
194
  }
184
195
  if (state.options.importSources.includes(node.source.value)) {
196
+ state.importPaths.add(node.source.value);
185
197
  for (const specifier of node.specifiers) {
186
198
  if (specifier.type === 'ImportDefaultSpecifier') {
187
199
  state.stylexImport.add(specifier.local.name);
@@ -250,7 +262,10 @@ function readRequires(path, state) {
250
262
  const {
251
263
  node
252
264
  } = path;
253
- if (node.init?.type === 'CallExpression' && node.init?.callee?.type === 'Identifier' && node.init?.callee?.name === 'require' && node.init?.arguments?.length === 1 && node.init?.arguments?.[0].type === 'StringLiteral' && (node.init?.arguments?.[0].value === 'stylex' || state.options.importSources.includes(node.init?.arguments?.[0].value))) {
265
+ const init = node.init;
266
+ if (init != null && init.type === 'CallExpression' && init.callee?.type === 'Identifier' && init.callee?.name === 'require' && init.arguments?.length === 1 && init.arguments?.[0].type === 'StringLiteral' && state.options.importSources.includes(init.arguments[0].value)) {
267
+ const importPath = init.arguments[0].value;
268
+ importPath != null && state.importPaths.add(importPath);
254
269
  if (node.id.type === 'Identifier') {
255
270
  state.stylexImport.add(node.id.name);
256
271
  }
@@ -3108,6 +3123,9 @@ function isImportDeclaration(path, props) {
3108
3123
  function isImportDefaultSpecifier(path, props) {
3109
3124
  return path.isImportDefaultSpecifier(props);
3110
3125
  }
3126
+ function isImportNamespaceSpecifier(path, props) {
3127
+ return path.isImportNamespaceSpecifier(props);
3128
+ }
3111
3129
  function isImportSpecifier(path, props) {
3112
3130
  return path.isImportSpecifier(props);
3113
3131
  }
@@ -3365,7 +3383,7 @@ function _evaluate(path, state) {
3365
3383
  if (isReferencedIdentifier(path)) {
3366
3384
  const binding = path.scope?.getBinding(path.node.name);
3367
3385
  const bindingPath = binding?.path;
3368
- if (binding && bindingPath && !isImportDefaultSpecifier(bindingPath) && isImportSpecifier(bindingPath)) {
3386
+ if (binding && bindingPath && !isImportDefaultSpecifier(bindingPath) && !isImportNamespaceSpecifier(bindingPath) && isImportSpecifier(bindingPath)) {
3369
3387
  const importSpecifierPath = bindingPath;
3370
3388
  const importSpecifierNode = importSpecifierPath.node;
3371
3389
  const imported = importSpecifierNode.imported;
@@ -3379,6 +3397,10 @@ function _evaluate(path, state) {
3379
3397
  const [type, value] = absPath;
3380
3398
  const returnValue = type === 'themeNameRef' ? evaluateThemeRef(value, importedName, state) : evaluateImportedFile(value, importedName, state);
3381
3399
  if (state.confident) {
3400
+ if (!state.addedImports.has(importPath.node.source.value)) {
3401
+ importPath.insertBefore(t__namespace.importDeclaration([], importPath.node.source));
3402
+ state.addedImports.add(importPath.node.source.value);
3403
+ }
3382
3404
  return returnValue;
3383
3405
  } else {
3384
3406
  deopt(binding.path, state);
@@ -3647,15 +3669,19 @@ function evaluateQuasis(path, quasis, state) {
3647
3669
  if (!state.confident) return;
3648
3670
  return str;
3649
3671
  }
3672
+ const importsForState = new WeakMap();
3650
3673
  function evaluate(path, traversalState) {
3651
3674
  let functions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {
3652
3675
  identifiers: {},
3653
3676
  memberExpressions: {}
3654
3677
  };
3678
+ const addedImports = importsForState.get(traversalState) ?? new Set();
3679
+ importsForState.set(traversalState, addedImports);
3655
3680
  const state = {
3656
3681
  confident: true,
3657
3682
  deoptPath: null,
3658
3683
  seen: new Map(),
3684
+ addedImports,
3659
3685
  functions,
3660
3686
  traversalState
3661
3687
  };
@@ -4001,7 +4027,7 @@ function getStylexDefaultImport(path, state) {
4001
4027
  });
4002
4028
  if (stylexName == null) {
4003
4029
  stylexName = '__stylex__';
4004
- statementPath.insertBefore(t__namespace.importDeclaration([t__namespace.importDefaultSpecifier(t__namespace.identifier(stylexName))], t__namespace.stringLiteral('stylex')));
4030
+ statementPath.insertBefore(t__namespace.importDeclaration([t__namespace.importDefaultSpecifier(t__namespace.identifier(stylexName))], t__namespace.stringLiteral(state.importPathString)));
4005
4031
  state.stylexImport.add(stylexName);
4006
4032
  }
4007
4033
  return stylexName;
@@ -4070,7 +4096,7 @@ function transformStyleXDefineVars(callExpressionPath, state) {
4070
4096
  });
4071
4097
  if (stylexName == null) {
4072
4098
  stylexName = '__stylex__';
4073
- statementPath.insertBefore(t__namespace.importDeclaration([t__namespace.importDefaultSpecifier(t__namespace.identifier(stylexName))], t__namespace.stringLiteral('stylex')));
4099
+ statementPath.insertBefore(t__namespace.importDeclaration([t__namespace.importDefaultSpecifier(t__namespace.identifier(stylexName))], t__namespace.stringLiteral(state.importPathString)));
4074
4100
  }
4075
4101
  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)])));
4076
4102
  }
@@ -4151,7 +4177,7 @@ function transformStyleXCreateTheme(callExpressionPath, state) {
4151
4177
  });
4152
4178
  if (stylexName == null) {
4153
4179
  stylexName = '__stylex__';
4154
- statementPath?.insertBefore(t__namespace.importDeclaration([t__namespace.importDefaultSpecifier(t__namespace.identifier(stylexName))], t__namespace.stringLiteral('stylex')));
4180
+ statementPath?.insertBefore(t__namespace.importDeclaration([t__namespace.importDefaultSpecifier(t__namespace.identifier(stylexName))], t__namespace.stringLiteral(state.importPathString)));
4155
4181
  }
4156
4182
  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)])));
4157
4183
  }
@@ -4218,7 +4244,7 @@ function transformStyleXKeyframes(path, state) {
4218
4244
  });
4219
4245
  if (stylexName == null) {
4220
4246
  stylexName = '__stylex__';
4221
- statementPath.insertBefore(t__namespace.importDeclaration([t__namespace.importDefaultSpecifier(t__namespace.identifier(stylexName))], t__namespace.stringLiteral('stylex')));
4247
+ statementPath.insertBefore(t__namespace.importDeclaration([t__namespace.importDefaultSpecifier(t__namespace.identifier(stylexName))], t__namespace.stringLiteral(state.importPathString)));
4222
4248
  }
4223
4249
  statementPath.insertBefore(t__namespace.expressionStatement(t__namespace.callExpression(t__namespace.memberExpression(t__namespace.identifier(stylexName), t__namespace.identifier('inject')), [t__namespace.stringLiteral(ltr), t__namespace.numericLiteral(priority), ...(rtl != null ? [t__namespace.stringLiteral(rtl)] : [])])));
4224
4250
  }
@@ -22,22 +22,6 @@ export type FunctionConfig = {
22
22
  };
23
23
  };
24
24
  };
25
- /**
26
- * Walk the input `node` and statically evaluate it.
27
- *
28
- * Returns an object in the form `{ confident, value, deopt }`. `confident`
29
- * indicates whether or not we had to drop out of evaluating the expression
30
- * because of hitting an unknown node that we couldn't confidently find the
31
- * value of, in which case `deopt` is the path of said node.
32
- *
33
- * Example:
34
- *
35
- * evaluate(parse("5 + 5")) // { confident: true, value: 10 }
36
- * evaluate(parse("!true")) // { confident: true, value: false }
37
- * evaluate(parse("foo + foo")) // { confident: false, value: undefined, deopt: NodePath }
38
- *
39
- */
40
-
41
25
  export declare function evaluate(
42
26
  path: NodePath,
43
27
  traversalState: StateManager,
@@ -26,22 +26,6 @@ export type FunctionConfig = {
26
26
  },
27
27
  };
28
28
 
29
- /**
30
- * Walk the input `node` and statically evaluate it.
31
- *
32
- * Returns an object in the form `{ confident, value, deopt }`. `confident`
33
- * indicates whether or not we had to drop out of evaluating the expression
34
- * because of hitting an unknown node that we couldn't confidently find the
35
- * value of, in which case `deopt` is the path of said node.
36
- *
37
- * Example:
38
- *
39
- * evaluate(parse("5 + 5")) // { confident: true, value: 10 }
40
- * evaluate(parse("!true")) // { confident: true, value: false }
41
- * evaluate(parse("foo + foo")) // { confident: false, value: undefined, deopt: NodePath }
42
- *
43
- */
44
-
45
29
  declare export function evaluate(
46
30
  path: NodePath<>,
47
31
  traversalState: StateManager,
@@ -38,6 +38,7 @@ type StyleXOptions = Omit<
38
38
  };
39
39
  declare class StateManager {
40
40
  readonly _state: PluginPass;
41
+ readonly importPaths: Set<string>;
41
42
  readonly stylexImport: Set<string>;
42
43
  readonly stylexPropsImport: Set<string>;
43
44
  readonly stylexCreateImport: Set<string>;
@@ -53,6 +54,7 @@ declare class StateManager {
53
54
  inStyleXCreate: boolean;
54
55
  constructor(state: PluginPass);
55
56
  get options(): StyleXOptions;
57
+ get importPathString(): string;
56
58
  get canReferenceTheme(): boolean;
57
59
  get metadata(): { [key: string]: any };
58
60
  get stylexSheetName(): string | void;
@@ -43,6 +43,7 @@ type StyleXOptions = {
43
43
 
44
44
  declare export default class StateManager {
45
45
  +_state: PluginPass;
46
+ +importPaths: Set<string>;
46
47
  +stylexImport: Set<string>;
47
48
  +stylexPropsImport: Set<string>;
48
49
  +stylexCreateImport: Set<string>;
@@ -58,6 +59,7 @@ declare export default class StateManager {
58
59
  inStyleXCreate: boolean;
59
60
  constructor(state: PluginPass): void;
60
61
  get options(): StyleXOptions;
62
+ get importPathString(): string;
61
63
  get canReferenceTheme(): boolean;
62
64
  get metadata(): { [key: string]: any };
63
65
  get stylexSheetName(): string | void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stylexjs/babel-plugin",
3
- "version": "0.2.0-beta.24",
3
+ "version": "0.2.0-beta.25",
4
4
  "description": "StyleX babel plugin.",
5
5
  "main": "lib/index.js",
6
6
  "repository": "https://github.com/facebook/stylex",
@@ -12,7 +12,7 @@
12
12
  "test": "jest"
13
13
  },
14
14
  "dependencies": {
15
- "@stylexjs/shared": "0.2.0-beta.24"
15
+ "@stylexjs/shared": "0.2.0-beta.25"
16
16
  },
17
17
  "peerDependencies": {
18
18
  "@babel/core": "^7.19.6",