@stylexjs/babel-plugin 0.2.0-beta.23 → 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
@@ -5,7 +5,6 @@ var path = require('path');
5
5
  var require$$0 = require('postcss-value-parser');
6
6
  var core = require('@babel/core');
7
7
  var traverse = require('@babel/traverse');
8
- var fs = require('fs');
9
8
  var require$$0$1 = require('invariant');
10
9
  var require$$1 = require('styleq');
11
10
 
@@ -31,6 +30,7 @@ var t__namespace = /*#__PURE__*/_interopNamespaceDefault(t);
31
30
  var name = "@stylexjs/stylex";
32
31
 
33
32
  class StateManager {
33
+ importPaths = new Set();
34
34
  stylexImport = new Set();
35
35
  stylexPropsImport = new Set();
36
36
  stylexCreateImport = new Set();
@@ -65,6 +65,15 @@ class StateManager {
65
65
  this._state.opts = opts;
66
66
  return this._state.opts;
67
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
+ }
68
77
  get canReferenceTheme() {
69
78
  return !!this.inStyleXCreate;
70
79
  }
@@ -158,7 +167,8 @@ const filePathResolver = (relativeFilePath, sourceFilePath) => {
158
167
  }
159
168
  for (const ext of EXTENSIONS) {
160
169
  try {
161
- const resolvedFilePath = require.resolve(fileToLookFor + ext, {
170
+ const importPathStr = fileToLookFor.startsWith('.') ? fileToLookFor + ext : fileToLookFor;
171
+ const resolvedFilePath = require.resolve(importPathStr, {
162
172
  paths: [path.dirname(sourceFilePath)]
163
173
  });
164
174
  return resolvedFilePath;
@@ -183,6 +193,7 @@ function readImportDeclarations(path, state) {
183
193
  return;
184
194
  }
185
195
  if (state.options.importSources.includes(node.source.value)) {
196
+ state.importPaths.add(node.source.value);
186
197
  for (const specifier of node.specifiers) {
187
198
  if (specifier.type === 'ImportDefaultSpecifier') {
188
199
  state.stylexImport.add(specifier.local.name);
@@ -251,7 +262,10 @@ function readRequires(path, state) {
251
262
  const {
252
263
  node
253
264
  } = path;
254
- 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);
255
269
  if (node.id.type === 'Identifier') {
256
270
  state.stylexImport.add(node.id.name);
257
271
  }
@@ -3109,6 +3123,9 @@ function isImportDeclaration(path, props) {
3109
3123
  function isImportDefaultSpecifier(path, props) {
3110
3124
  return path.isImportDefaultSpecifier(props);
3111
3125
  }
3126
+ function isImportNamespaceSpecifier(path, props) {
3127
+ return path.isImportNamespaceSpecifier(props);
3128
+ }
3112
3129
  function isImportSpecifier(path, props) {
3113
3130
  return path.isImportSpecifier(props);
3114
3131
  }
@@ -3181,6 +3198,7 @@ function deopt(path, state) {
3181
3198
  state.confident = false;
3182
3199
  }
3183
3200
  function evaluateImportedFile(filePath, namedExport, state) {
3201
+ const fs = require('fs');
3184
3202
  const fileContents = fs.readFileSync(filePath, 'utf8');
3185
3203
  const ast = core.parseSync(fileContents, {
3186
3204
  babelrc: true
@@ -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.23",
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.23"
15
+ "@stylexjs/shared": "0.2.0-beta.25"
16
16
  },
17
17
  "peerDependencies": {
18
18
  "@babel/core": "^7.19.6",