@stylexjs/babel-plugin 0.17.2 → 0.17.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
|
@@ -283,6 +283,45 @@ function getProgramStatement(path) {
|
|
|
283
283
|
}
|
|
284
284
|
return programPath;
|
|
285
285
|
}
|
|
286
|
+
function isVariableNamedExported(path) {
|
|
287
|
+
const idPath = path.get('id');
|
|
288
|
+
if (!idPath.isIdentifier()) return false;
|
|
289
|
+
const variableName = idPath.node.name;
|
|
290
|
+
const binding = path.scope.getBinding(variableName);
|
|
291
|
+
if (!binding) return false;
|
|
292
|
+
const programPath = getProgramPath(path);
|
|
293
|
+
if (programPath == null) {
|
|
294
|
+
return false;
|
|
295
|
+
}
|
|
296
|
+
let exported = false;
|
|
297
|
+
programPath.traverse({
|
|
298
|
+
ExportNamedDeclaration(p) {
|
|
299
|
+
const node = p.node;
|
|
300
|
+
if (node.source != null) return;
|
|
301
|
+
if (node.declaration) {
|
|
302
|
+
if (node.declaration.type === 'VariableDeclaration' && node.declaration.declarations.some(d => d.id.type === 'Identifier' && p.scope.getBinding(d.id.name) === binding)) {
|
|
303
|
+
exported = true;
|
|
304
|
+
p.stop();
|
|
305
|
+
}
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
for (const s of node.specifiers ?? []) {
|
|
309
|
+
if (s.type !== 'ExportSpecifier') continue;
|
|
310
|
+
if (s.local.type !== 'Identifier') continue;
|
|
311
|
+
if (s.exported.type !== 'Identifier') continue;
|
|
312
|
+
if (s.exportKind === 'type') continue;
|
|
313
|
+
if (s.local.name !== s.exported.name) continue;
|
|
314
|
+
const localBinding = p.scope.getBinding(s.local.name);
|
|
315
|
+
if (localBinding === binding) {
|
|
316
|
+
exported = true;
|
|
317
|
+
p.stop();
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
});
|
|
323
|
+
return exported;
|
|
324
|
+
}
|
|
286
325
|
|
|
287
326
|
const defaultOptions = {
|
|
288
327
|
classNamePrefix: 'x',
|
|
@@ -602,15 +641,14 @@ class StateManager {
|
|
|
602
641
|
ltr,
|
|
603
642
|
rtl
|
|
604
643
|
} = styleObj;
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
} = styleObj;
|
|
611
|
-
args.push(t__namespace.stringLiteral(constKey), typeof constVal === 'number' ? t__namespace.numericLiteral(constVal) : t__namespace.stringLiteral(String(constVal)));
|
|
644
|
+
let constKey = null;
|
|
645
|
+
let constVal = null;
|
|
646
|
+
if (styleObj.constKey != null && styleObj.constVal != null) {
|
|
647
|
+
constKey = styleObj.constKey;
|
|
648
|
+
constVal = styleObj.constVal;
|
|
612
649
|
}
|
|
613
|
-
|
|
650
|
+
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)))] : [])];
|
|
651
|
+
statementPath.insertBefore(t__namespace.expressionStatement(t__namespace.callExpression(injectName, [t__namespace.objectExpression(properties)])));
|
|
614
652
|
}
|
|
615
653
|
}
|
|
616
654
|
markComposedNamespace(memberExpression) {
|
|
@@ -5669,7 +5707,6 @@ function deopt(path, state, reason) {
|
|
|
5669
5707
|
state.deoptReason = reason;
|
|
5670
5708
|
}
|
|
5671
5709
|
function evaluateImportedFile(filePath, namedExport, state, bindingPath) {
|
|
5672
|
-
const fs = require('node:fs');
|
|
5673
5710
|
const fileContents = fs.readFileSync(filePath, 'utf8');
|
|
5674
5711
|
const ast = core.parseSync(fileContents, {
|
|
5675
5712
|
babelrc: true
|
|
@@ -6951,11 +6988,10 @@ function transformStyleXDefineVars(callExpressionPath, state) {
|
|
|
6951
6988
|
}
|
|
6952
6989
|
function validateStyleXDefineVars(callExpressionPath) {
|
|
6953
6990
|
const variableDeclaratorPath = callExpressionPath.parentPath;
|
|
6954
|
-
const exportNamedDeclarationPath = variableDeclaratorPath.parentPath?.parentPath;
|
|
6955
6991
|
if (variableDeclaratorPath == null || variableDeclaratorPath.isExpressionStatement() || !variableDeclaratorPath.isVariableDeclarator() || variableDeclaratorPath.node.id.type !== 'Identifier') {
|
|
6956
6992
|
throw callExpressionPath.buildCodeFrameError(messages.unboundCallValue('defineVars'), SyntaxError);
|
|
6957
6993
|
}
|
|
6958
|
-
if (
|
|
6994
|
+
if (!isVariableNamedExported(variableDeclaratorPath)) {
|
|
6959
6995
|
throw callExpressionPath.buildCodeFrameError(messages.nonExportNamedDeclaration('defineVars'), SyntaxError);
|
|
6960
6996
|
}
|
|
6961
6997
|
if (callExpressionPath.node.arguments.length !== 1) {
|
|
@@ -7010,11 +7046,10 @@ function transformStyleXDefineConsts(callExpressionPath, state) {
|
|
|
7010
7046
|
}
|
|
7011
7047
|
function validateStyleXDefineConsts(callExpressionPath) {
|
|
7012
7048
|
const variableDeclaratorPath = callExpressionPath.parentPath;
|
|
7013
|
-
const exportNamedDeclarationPath = variableDeclaratorPath.parentPath?.parentPath;
|
|
7014
7049
|
if (variableDeclaratorPath == null || !variableDeclaratorPath.isVariableDeclarator() || variableDeclaratorPath.node.id.type !== 'Identifier') {
|
|
7015
7050
|
throw callExpressionPath.buildCodeFrameError(messages.unboundCallValue('defineConsts'), SyntaxError);
|
|
7016
7051
|
}
|
|
7017
|
-
if (
|
|
7052
|
+
if (!isVariableNamedExported(variableDeclaratorPath)) {
|
|
7018
7053
|
throw callExpressionPath.buildCodeFrameError(messages.nonExportNamedDeclaration('defineConsts'), SyntaxError);
|
|
7019
7054
|
}
|
|
7020
7055
|
if (callExpressionPath.node.arguments.length !== 1) {
|
|
@@ -7857,11 +7892,10 @@ function transformStyleXDefineMarker(path, state) {
|
|
|
7857
7892
|
}
|
|
7858
7893
|
function validateStyleXDefineMarker(path) {
|
|
7859
7894
|
const variableDeclaratorPath = path.parentPath;
|
|
7860
|
-
const exportNamedDeclarationPath = variableDeclaratorPath.parentPath?.parentPath;
|
|
7861
7895
|
if (variableDeclaratorPath == null || variableDeclaratorPath.isExpressionStatement() || !variableDeclaratorPath.isVariableDeclarator() || variableDeclaratorPath.node.id.type !== 'Identifier') {
|
|
7862
7896
|
throw path.buildCodeFrameError(unboundCallValue('defineMarker'), SyntaxError);
|
|
7863
7897
|
}
|
|
7864
|
-
if (
|
|
7898
|
+
if (!isVariableNamedExported(variableDeclaratorPath)) {
|
|
7865
7899
|
throw path.buildCodeFrameError(nonExportNamedDeclaration('defineMarker'), SyntaxError);
|
|
7866
7900
|
}
|
|
7867
7901
|
}
|
|
@@ -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, aliasing 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, aliasing 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.
|
|
3
|
+
"version": "0.17.4",
|
|
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.
|
|
25
|
-
"@stylexjs/stylex": "0.17.
|
|
26
|
+
"@stylexjs/shared": "0.17.4",
|
|
27
|
+
"@stylexjs/stylex": "0.17.4",
|
|
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.
|
|
40
|
+
"scripts": "0.17.4"
|
|
38
41
|
},
|
|
39
42
|
"files": [
|
|
40
43
|
"flow_modules/*",
|