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

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
@@ -145,6 +145,118 @@ const logAndDefault = (check, value, def, name) => {
145
145
  return result;
146
146
  };
147
147
 
148
+ function isArrayExpression(path, props) {
149
+ return path.isArrayExpression(props);
150
+ }
151
+ function isArrowFunctionExpression(path, props) {
152
+ return path.isArrowFunctionExpression(props);
153
+ }
154
+ function isBinaryExpression(path, props) {
155
+ return path.isBinaryExpression(props);
156
+ }
157
+ function isBooleanLiteral(path, props) {
158
+ return path.isBooleanLiteral(props);
159
+ }
160
+ function isCallExpression(path, props) {
161
+ return path.isCallExpression(props);
162
+ }
163
+ function isClass(path, props) {
164
+ return path.isClass(props);
165
+ }
166
+ function isConditionalExpression(path, props) {
167
+ return path.isConditionalExpression(props);
168
+ }
169
+ function isExportDefaultDeclaration(path, props) {
170
+ return path.isExportDefaultDeclaration(props);
171
+ }
172
+ function isExportNamedDeclaration(path, props) {
173
+ return path.isExportNamedDeclaration(props);
174
+ }
175
+ function isExpression(path, props) {
176
+ return path.isExpression(props);
177
+ }
178
+ function isExpressionStatement(path, props) {
179
+ return path.isExpressionStatement(props);
180
+ }
181
+ function isExpressionWrapper(path, props) {
182
+ return path.isExpressionWrapper(props);
183
+ }
184
+ function isFunction(path, props) {
185
+ return path.isFunction(props);
186
+ }
187
+ function isIdentifier(path, props) {
188
+ return path.isIdentifier(props);
189
+ }
190
+ function isImportDeclaration(path, props) {
191
+ return path.isImportDeclaration(props);
192
+ }
193
+ function isImportDefaultSpecifier(path, props) {
194
+ return path.isImportDefaultSpecifier(props);
195
+ }
196
+ function isImportNamespaceSpecifier(path, props) {
197
+ return path.isImportNamespaceSpecifier(props);
198
+ }
199
+ function isImportSpecifier(path, props) {
200
+ return path.isImportSpecifier(props);
201
+ }
202
+ function isLogicalExpression(path, props) {
203
+ return path.isLogicalExpression(props);
204
+ }
205
+ function isMemberExpression(path, props) {
206
+ return path.isMemberExpression(props);
207
+ }
208
+ function isNullLiteral(path, props) {
209
+ return path.isNullLiteral(props);
210
+ }
211
+ function isNumericLiteral(path, props) {
212
+ return path.isNumericLiteral(props);
213
+ }
214
+ function isObjectExpression(path, props) {
215
+ return path.isObjectExpression(props);
216
+ }
217
+ function isObjectMethod(path, props) {
218
+ return path.isObjectMethod(props);
219
+ }
220
+ function isObjectProperty(path, props) {
221
+ return path.isObjectProperty(props);
222
+ }
223
+ function isProgram(path, props) {
224
+ return path.isProgram(props);
225
+ }
226
+ function isSequenceExpression(path, props) {
227
+ return path.isSequenceExpression(props);
228
+ }
229
+ function isSpreadElement(path, props) {
230
+ return path.isSpreadElement(props);
231
+ }
232
+ function isStatement(path, props) {
233
+ return path.isStatement(props);
234
+ }
235
+ function isStringLiteral(path, props) {
236
+ return path.isStringLiteral(props);
237
+ }
238
+ function isTSAsExpression(path, props) {
239
+ return path.isTSAsExpression(props);
240
+ }
241
+ function isTaggedTemplateExpression(path, props) {
242
+ return path.isTaggedTemplateExpression(props);
243
+ }
244
+ function isTemplateLiteral(path, props) {
245
+ return path.isTemplateLiteral(props);
246
+ }
247
+ function isUnaryExpression(path, props) {
248
+ return path.isUnaryExpression(props);
249
+ }
250
+ function isVariableDeclaration(path, props) {
251
+ return path.isVariableDeclaration(props);
252
+ }
253
+ function isVariableDeclarator(path, props) {
254
+ return path.isVariableDeclarator(props);
255
+ }
256
+ function isReferencedIdentifier(path, props) {
257
+ return path.isReferencedIdentifier(props);
258
+ }
259
+
148
260
  const CheckModuleResolution = unionOf3(object({
149
261
  type: literal('commonJS'),
150
262
  rootDir: string(),
@@ -253,6 +365,56 @@ class StateManager {
253
365
  from: runInj
254
366
  } : runInj || null;
255
367
  }
368
+ addNamedImport(statementPath, as, from, options) {
369
+ const identifier = helperModuleImports.addNamed(statementPath, as, from, options);
370
+ const programPath = getProgramPath(statementPath);
371
+ if (programPath == null) {
372
+ return identifier;
373
+ }
374
+ const bodyPath = programPath.get('body');
375
+ let lastImportIndex = -1;
376
+ for (let i = 0; i < bodyPath.length; i++) {
377
+ const statement = bodyPath[i];
378
+ if (isImportDeclaration(statement)) {
379
+ lastImportIndex = i;
380
+ }
381
+ }
382
+ if (lastImportIndex === -1) {
383
+ return identifier;
384
+ }
385
+ const lastImport = bodyPath[lastImportIndex];
386
+ if (lastImport == null) {
387
+ return identifier;
388
+ }
389
+ const importName = statementPath.scope.generateUidIdentifier(as);
390
+ lastImport.insertAfter(t__namespace.variableDeclaration('var', [t__namespace.variableDeclarator(importName, identifier)]));
391
+ return importName;
392
+ }
393
+ addDefaultImport(statementPath, from, options) {
394
+ const identifier = helperModuleImports.addDefault(statementPath, from, options);
395
+ const programPath = getProgramPath(statementPath);
396
+ if (programPath == null) {
397
+ return identifier;
398
+ }
399
+ const bodyPath = programPath.get('body');
400
+ let lastImportIndex = -1;
401
+ for (let i = 0; i < bodyPath.length; i++) {
402
+ const statement = bodyPath[i];
403
+ if (isImportDeclaration(statement)) {
404
+ lastImportIndex = i;
405
+ }
406
+ }
407
+ if (lastImportIndex === -1) {
408
+ return identifier;
409
+ }
410
+ const lastImport = bodyPath[lastImportIndex];
411
+ if (lastImport == null) {
412
+ return identifier;
413
+ }
414
+ const importName = statementPath.scope.generateUidIdentifier('inject');
415
+ lastImport.insertAfter(t__namespace.variableDeclaration('var', [t__namespace.variableDeclarator(importName, identifier)]));
416
+ return importName;
417
+ }
256
418
  get isDev() {
257
419
  return !!this.options.dev;
258
420
  }
@@ -357,6 +519,17 @@ const addFileExtension = (importedFilePath, sourceFile) => {
357
519
  return importedFilePath + fileExtension;
358
520
  };
359
521
  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);
522
+ const getProgramPath = path => {
523
+ let programPath = path;
524
+ while (programPath != null && !isProgram(programPath)) {
525
+ if (programPath.parentPath) {
526
+ programPath = programPath.parentPath;
527
+ } else {
528
+ return null;
529
+ }
530
+ }
531
+ return programPath;
532
+ };
360
533
 
361
534
  function readImportDeclarations(path, state) {
362
535
  const {
@@ -3224,118 +3397,6 @@ function canBeIdentifier(str) {
3224
3397
  return str.match(/^[a-zA-Z_$][a-zA-Z0-9_$]*$/) != null;
3225
3398
  }
3226
3399
 
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
3400
  const VALID_CALLEES = ['String', 'Number', 'Math', 'Object', 'Array'];
3340
3401
  const INVALID_METHODS = ['random', 'assign', 'defineProperties', 'defineProperty', 'freeze', 'seal', 'splice'];
3341
3402
  function isValidCallee(val) {
@@ -4141,9 +4202,9 @@ function transformStyleXCreate(path, state) {
4141
4202
  from,
4142
4203
  as
4143
4204
  } = state.runtimeInjection;
4144
- injectName = as != null ? helperModuleImports.addNamed(statementPath, as, from, {
4205
+ injectName = as != null ? state.addNamedImport(statementPath, as, from, {
4145
4206
  nameHint: 'inject'
4146
- }) : helperModuleImports.addDefault(statementPath, from, {
4207
+ }) : state.addDefaultImport(statementPath, from, {
4147
4208
  nameHint: 'inject'
4148
4209
  });
4149
4210
  state.injectImportInserted = injectName;
@@ -4272,9 +4333,9 @@ function transformStyleXDefineVars(callExpressionPath, state) {
4272
4333
  from,
4273
4334
  as
4274
4335
  } = state.runtimeInjection;
4275
- injectName = as != null ? helperModuleImports.addNamed(statementPath, as, from, {
4336
+ injectName = as != null ? state.addNamedImport(statementPath, as, from, {
4276
4337
  nameHint: 'inject'
4277
- }) : helperModuleImports.addDefault(statementPath, from, {
4338
+ }) : state.addDefaultImport(statementPath, from, {
4278
4339
  nameHint: 'inject'
4279
4340
  });
4280
4341
  state.injectImportInserted = injectName;
@@ -4372,9 +4433,9 @@ function transformStyleXCreateTheme(callExpressionPath, state) {
4372
4433
  from,
4373
4434
  as
4374
4435
  } = state.runtimeInjection;
4375
- injectName = as != null ? helperModuleImports.addNamed(statementPath, as, from, {
4436
+ injectName = as != null ? state.addNamedImport(statementPath, as, from, {
4376
4437
  nameHint: 'inject'
4377
- }) : helperModuleImports.addDefault(statementPath, from, {
4438
+ }) : state.addDefaultImport(statementPath, from, {
4378
4439
  nameHint: 'inject'
4379
4440
  });
4380
4441
  state.injectImportInserted = injectName;
@@ -4474,9 +4535,9 @@ function transformStyleXKeyframes(path, state) {
4474
4535
  from,
4475
4536
  as
4476
4537
  } = state.runtimeInjection;
4477
- injectName = as != null ? helperModuleImports.addNamed(statementPath, as, from, {
4538
+ injectName = as != null ? state.addNamedImport(statementPath, as, from, {
4478
4539
  nameHint: 'inject'
4479
- }) : helperModuleImports.addDefault(statementPath, from, {
4540
+ }) : state.addDefaultImport(statementPath, from, {
4480
4541
  nameHint: 'inject'
4481
4542
  });
4482
4543
  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];
@@ -103,6 +108,17 @@ declare class StateManager {
103
108
  | null
104
109
  | undefined
105
110
  | Readonly<{ from: string; as?: null | undefined | string }>;
111
+ addNamedImport(
112
+ statementPath: NodePath,
113
+ as: string,
114
+ from: string,
115
+ options: ImportAdditionOptions,
116
+ ): t.Identifier;
117
+ addDefaultImport(
118
+ statementPath: NodePath,
119
+ from: string,
120
+ options: ImportAdditionOptions,
121
+ ): t.Identifier;
106
122
  get isDev(): boolean;
107
123
  get isTest(): boolean;
108
124
  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];
@@ -79,6 +85,17 @@ declare export default class StateManager {
79
85
  get canReferenceTheme(): boolean;
80
86
  get metadata(): { [key: string]: any };
81
87
  get runtimeInjection(): ?$ReadOnly<{ from: string, as?: ?string }>;
88
+ addNamedImport(
89
+ statementPath: NodePath<>,
90
+ as: string,
91
+ from: string,
92
+ options: ImportAdditionOptions,
93
+ ): t.Identifier;
94
+ addDefaultImport(
95
+ statementPath: NodePath<>,
96
+ from: string,
97
+ options: ImportAdditionOptions,
98
+ ): t.Identifier;
82
99
  get isDev(): boolean;
83
100
  get isTest(): boolean;
84
101
  get filename(): string | void;
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-alpha.4",
4
4
  "description": "StyleX babel plugin.",
5
5
  "main": "lib/index.js",
6
6
  "repository": "https://github.com/facebook/stylex",
@@ -13,8 +13,8 @@
13
13
  },
14
14
  "dependencies": {
15
15
  "@babel/helper-module-imports": "^7.22.15",
16
- "@stylexjs/shared": "0.5.0-alpha.3",
17
- "@stylexjs/stylex": "0.5.0-alpha.3",
16
+ "@stylexjs/shared": "0.5.0-alpha.4",
17
+ "@stylexjs/stylex": "0.5.0-alpha.4",
18
18
  "@babel/core": "^7.23.6",
19
19
  "@babel/traverse": "^7.23.6",
20
20
  "@babel/types": "^7.23.6"