@stylexjs/babel-plugin 0.3.0 → 0.4.1
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/README.md +2 -4
- package/flow_modules/@babel/core/index.js.flow +6 -6
- package/flow_modules/@babel/helper-module-imports/index.js.flow +182 -0
- package/flow_modules/@babel/traverse/index.js.flow +7 -6
- package/lib/index.d.ts +15 -2
- package/lib/index.js +535 -515
- package/lib/index.js.flow +18 -2
- package/lib/utils/state-manager.d.ts +52 -15
- package/lib/utils/state-manager.js.flow +22 -7
- package/package.json +7 -10
- package/lib/visitors/stylex-override-vars.d.ts +0 -17
- package/lib/visitors/stylex-override-vars.js.flow +0 -23
package/README.md
CHANGED
|
@@ -5,8 +5,6 @@ In addition to transforming JS code, this plugin also produces an Array of CSS r
|
|
|
5
5
|
generated from all JS files within your project should be concatenated together and converted to a CSS
|
|
6
6
|
file using the `processStyles` function which is also exported from the same module.
|
|
7
7
|
|
|
8
|
-
(NOTE: StyleX only uses RTL-friendly logical values of `start` and `end` and disallows using `left` and `right` entirely.)
|
|
9
|
-
|
|
10
8
|
`@stylexjs/babel-plugin` is fairly lightweight. It pre-computes `stylex` related functions like
|
|
11
9
|
`stylex.create` and `stylex.keyframes` by converting the argument AST to a JS object and transforming them
|
|
12
10
|
by passing them to the functions of the corresponding names within `@stylex/shared`
|
|
@@ -14,12 +12,12 @@ by passing them to the functions of the corresponding names within `@stylex/shar
|
|
|
14
12
|
|
|
15
13
|
## Babel Metadata
|
|
16
14
|
|
|
17
|
-
The StyleX Babel plugin does more than transform JavaScript (or
|
|
15
|
+
The StyleX Babel plugin does more than transform JavaScript (or TypeScript) files. It also returns a list of injected styles. The way that such a value can be returned while transforming a JS file is by using Babel's `metadata` API.
|
|
18
16
|
|
|
19
17
|
An example of this can be seen in some of the tests, but the result of using Babel's `transform(...)` function returns an object contains at least two keys:
|
|
20
18
|
|
|
21
19
|
1. `code` which is the transformed JS code
|
|
22
|
-
2. `metadata` is an object of
|
|
20
|
+
2. `metadata` is an object of metadata that the plugin may want to return as a side-effect.
|
|
23
21
|
|
|
24
22
|
e.g.
|
|
25
23
|
|
|
@@ -301,14 +301,14 @@ export interface TransformOptions {
|
|
|
301
301
|
*
|
|
302
302
|
* Default: `[]`
|
|
303
303
|
*/
|
|
304
|
-
plugins?:
|
|
304
|
+
plugins?: ?$ReadOnlyArray<PluginItem>;
|
|
305
305
|
|
|
306
306
|
/**
|
|
307
307
|
* List of presets (a set of plugins) to load and use
|
|
308
308
|
*
|
|
309
309
|
* Default: `[]`
|
|
310
310
|
*/
|
|
311
|
-
presets?:
|
|
311
|
+
presets?: ?$ReadOnlyArray<PluginItem>;
|
|
312
312
|
|
|
313
313
|
/**
|
|
314
314
|
* Retain line numbers. This will lead to wacky code but is handy for scenarios where you can't use source maps. (**NOTE**: This will not retain the columns)
|
|
@@ -685,19 +685,19 @@ export interface ConfigItem {
|
|
|
685
685
|
};
|
|
686
686
|
}
|
|
687
687
|
|
|
688
|
-
export type PluginOptions = { [string]: mixed } | void | false;
|
|
688
|
+
export type PluginOptions = $ReadOnly<{ [string]: mixed }> | void | false;
|
|
689
689
|
|
|
690
690
|
export type PluginTarget =
|
|
691
691
|
| string
|
|
692
|
-
| { [string]: mixed }
|
|
692
|
+
| $ReadOnly<{ [string]: mixed }>
|
|
693
693
|
| ((...args: any[]) => any);
|
|
694
694
|
|
|
695
695
|
export type PluginItem =
|
|
696
696
|
| ConfigItem
|
|
697
697
|
| PluginObj<any>
|
|
698
698
|
| PluginTarget
|
|
699
|
-
| [PluginTarget, PluginOptions]
|
|
700
|
-
| [PluginTarget, PluginOptions, string | void]
|
|
699
|
+
| $ReadOnly<[PluginTarget, PluginOptions]>
|
|
700
|
+
| $ReadOnly<[PluginTarget, PluginOptions, string | void]>;
|
|
701
701
|
|
|
702
702
|
declare export function resolvePlugin(
|
|
703
703
|
name: string,
|
|
@@ -0,0 +1,182 @@
|
|
|
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 * as t from '../types';
|
|
11
|
+
import type { NodePath } from '@babel/traverse';
|
|
12
|
+
|
|
13
|
+
export type ImportOptions = $ReadOnly<{
|
|
14
|
+
/**
|
|
15
|
+
* The module being referenced.
|
|
16
|
+
*/
|
|
17
|
+
importedSource: string | null,
|
|
18
|
+
/**
|
|
19
|
+
* The type of module being imported:
|
|
20
|
+
*
|
|
21
|
+
* * 'es6' - An ES6 module.
|
|
22
|
+
* * 'commonjs' - A CommonJS module. (Default)
|
|
23
|
+
*/
|
|
24
|
+
importedType: 'es6' | 'commonjs',
|
|
25
|
+
/**
|
|
26
|
+
* The type of interop behavior for namespace/default/named when loading
|
|
27
|
+
* CommonJS modules.
|
|
28
|
+
*
|
|
29
|
+
* ## 'babel' (Default)
|
|
30
|
+
*
|
|
31
|
+
* Load using Babel's interop.
|
|
32
|
+
*
|
|
33
|
+
* If '.__esModule' is true, treat as 'compiled', else:
|
|
34
|
+
*
|
|
35
|
+
* * Namespace: A copy of the module.exports with .default
|
|
36
|
+
* populated by the module.exports object.
|
|
37
|
+
* * Default: The module.exports value.
|
|
38
|
+
* * Named: The .named property of module.exports.
|
|
39
|
+
*
|
|
40
|
+
* The 'ensureLiveReference' has no effect on the liveness of these.
|
|
41
|
+
*
|
|
42
|
+
* ## 'compiled'
|
|
43
|
+
*
|
|
44
|
+
* Assume the module is ES6 compiled to CommonJS. Useful to avoid injecting
|
|
45
|
+
* interop logic if you are confident that the module is a certain format.
|
|
46
|
+
*
|
|
47
|
+
* * Namespace: The root module.exports object.
|
|
48
|
+
* * Default: The .default property of the namespace.
|
|
49
|
+
* * Named: The .named property of the namespace.
|
|
50
|
+
*
|
|
51
|
+
* Will return erroneous results if the imported module is _not_ compiled
|
|
52
|
+
* from ES6 with Babel.
|
|
53
|
+
*
|
|
54
|
+
* ## 'uncompiled'
|
|
55
|
+
*
|
|
56
|
+
* Assume the module is _not_ ES6 compiled to CommonJS. Used a simplified
|
|
57
|
+
* access pattern that doesn't require additional function calls.
|
|
58
|
+
*
|
|
59
|
+
* Will return erroneous results if the imported module _is_ compiled
|
|
60
|
+
* from ES6 with Babel.
|
|
61
|
+
*
|
|
62
|
+
* * Namespace: The module.exports object.
|
|
63
|
+
* * Default: The module.exports object.
|
|
64
|
+
* * Named: The .named property of module.exports.
|
|
65
|
+
*/
|
|
66
|
+
importedInterop: 'babel' | 'node' | 'compiled' | 'uncompiled',
|
|
67
|
+
/**
|
|
68
|
+
* The type of CommonJS interop included in the environment that will be
|
|
69
|
+
* loading the output code.
|
|
70
|
+
*
|
|
71
|
+
* * 'babel' - CommonJS modules load with Babel's interop. (Default)
|
|
72
|
+
* * 'node' - CommonJS modules load with Node's interop.
|
|
73
|
+
*
|
|
74
|
+
* See descriptions in 'importedInterop' for more details.
|
|
75
|
+
*/
|
|
76
|
+
importingInterop: 'babel' | 'node',
|
|
77
|
+
/**
|
|
78
|
+
* Define whether we explicitly care that the import be a live reference.
|
|
79
|
+
* Only applies when importing default and named imports, not the namespace.
|
|
80
|
+
*
|
|
81
|
+
* * true - Force imported values to be live references.
|
|
82
|
+
* * false - No particular requirements. Keeps the code simplest. (Default)
|
|
83
|
+
*/
|
|
84
|
+
ensureLiveReference: boolean,
|
|
85
|
+
/**
|
|
86
|
+
* Define if we explicitly care that the result not be a property reference.
|
|
87
|
+
*
|
|
88
|
+
* * true - Force calls to exclude context. Useful if the value is going to
|
|
89
|
+
* be used as function callee.
|
|
90
|
+
* * false - No particular requirements for context of the access. (Default)
|
|
91
|
+
*/
|
|
92
|
+
ensureNoContext: boolean,
|
|
93
|
+
/**
|
|
94
|
+
* Define whether the import should be loaded before or after the existing imports.
|
|
95
|
+
* "after" is only allowed inside ECMAScript modules, since it's not possible to
|
|
96
|
+
* reliably pick the location _after_ require() calls but _before_ other code in CJS.
|
|
97
|
+
*/
|
|
98
|
+
importPosition: 'before' | 'after',
|
|
99
|
+
|
|
100
|
+
nameHint?: string,
|
|
101
|
+
blockHoist?: number,
|
|
102
|
+
}>;
|
|
103
|
+
|
|
104
|
+
declare export function addDefault(
|
|
105
|
+
path: NodePath<>,
|
|
106
|
+
importedSource: string,
|
|
107
|
+
opts?: Partial<ImportOptions>,
|
|
108
|
+
): t.Identifier;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* add a named import to the program path of given path
|
|
112
|
+
*
|
|
113
|
+
* @param path The starting path to find a program path
|
|
114
|
+
* @param name The name of the generated binding. Babel will prefix it with `_`
|
|
115
|
+
* @param importedSource The source of the import
|
|
116
|
+
* @param [opts]
|
|
117
|
+
* @returns If opts.ensureNoContext is true, returns a SequenceExpression,
|
|
118
|
+
* else if opts.ensureLiveReference is true, returns a MemberExpression, else returns an Identifier
|
|
119
|
+
*/
|
|
120
|
+
declare export const addNamed: (
|
|
121
|
+
path: NodePath<>,
|
|
122
|
+
name: string,
|
|
123
|
+
importedSource: string,
|
|
124
|
+
opts?: Omit<
|
|
125
|
+
Partial<ImportOptions>,
|
|
126
|
+
'ensureLiveReference' | 'ensureNoContext',
|
|
127
|
+
>,
|
|
128
|
+
) => t.Identifier;
|
|
129
|
+
|
|
130
|
+
// declare export function addNamed(
|
|
131
|
+
// path: NodePath<>,
|
|
132
|
+
// name: string,
|
|
133
|
+
// importedSource: string,
|
|
134
|
+
// opts?: Omit<Partial<ImportOptions>, "ensureLiveReference"> & {
|
|
135
|
+
// ensureLiveReference: true;
|
|
136
|
+
// },
|
|
137
|
+
// ): t.MemberExpression;
|
|
138
|
+
// declare export function addNamed(
|
|
139
|
+
// path: NodePath,
|
|
140
|
+
// name: string,
|
|
141
|
+
// importedSource: string,
|
|
142
|
+
// opts?: Omit<Partial<ImportOptions>, "ensureNoContext"> & {
|
|
143
|
+
// ensureNoContext: true;
|
|
144
|
+
// },
|
|
145
|
+
// ): t.SequenceExpression;
|
|
146
|
+
|
|
147
|
+
declare export function addNamespace(
|
|
148
|
+
path: NodePath<>,
|
|
149
|
+
importedSource: string,
|
|
150
|
+
opts?: Partial<ImportOptions>,
|
|
151
|
+
): t.Identifier;
|
|
152
|
+
|
|
153
|
+
// declare export function addSideEffect(
|
|
154
|
+
// path: NodePath<>,
|
|
155
|
+
// importedSource: string,
|
|
156
|
+
// opts?: Partial<ImportOptions>,
|
|
157
|
+
// ): void;
|
|
158
|
+
|
|
159
|
+
declare export function isModule(path: NodePath<t.Program>): boolean;
|
|
160
|
+
|
|
161
|
+
// declare export class ImportInjector {
|
|
162
|
+
// constructor(
|
|
163
|
+
// path: NodePath<>,
|
|
164
|
+
// importedSource?: string,
|
|
165
|
+
// opts?: Partial<ImportOptions>,
|
|
166
|
+
// ): ImportInjector;
|
|
167
|
+
|
|
168
|
+
// addDefault(
|
|
169
|
+
// importedSourceIn: string,
|
|
170
|
+
// opts: Partial<ImportOptions>,
|
|
171
|
+
// ): t.Identifier;
|
|
172
|
+
// addNamed(
|
|
173
|
+
// importName: string,
|
|
174
|
+
// importedSourceIn: string,
|
|
175
|
+
// opts: Partial<ImportOptions>,
|
|
176
|
+
// ): t.Identifier;
|
|
177
|
+
// addNamespace(
|
|
178
|
+
// importedSourceIn: string,
|
|
179
|
+
// opts: Partial<ImportOptions>,
|
|
180
|
+
// ): t.Identifier;
|
|
181
|
+
// addSideEffect(importedSourceIn: string, opts: Partial<ImportOptions>): void;
|
|
182
|
+
// }
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
6
7
|
* @flow strict
|
|
7
8
|
*/
|
|
8
9
|
|
|
@@ -307,8 +308,8 @@ declare export class NodePath<+T: Node = Node> {
|
|
|
307
308
|
type: T extends null | void
|
|
308
309
|
? void
|
|
309
310
|
: T extends Node
|
|
310
|
-
|
|
311
|
-
|
|
311
|
+
? T['type']
|
|
312
|
+
: string | void;
|
|
312
313
|
|
|
313
314
|
typeAnnotation: { ... };
|
|
314
315
|
|
|
@@ -996,7 +997,7 @@ export type NodePathResult<T: Node | $ReadOnlyArray<Node>> =
|
|
|
996
997
|
T extends $ReadOnlyArray<infer TNode>
|
|
997
998
|
? $ReadOnlyArray<NodePath<TNode>>
|
|
998
999
|
: T extends null | void
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1000
|
+
? void
|
|
1001
|
+
: T extends Node
|
|
1002
|
+
? NodePath<T>
|
|
1003
|
+
: T;
|
package/lib/index.d.ts
CHANGED
|
@@ -14,12 +14,14 @@ export type Options = StyleXOptions;
|
|
|
14
14
|
* Entry point for the StyleX babel plugin.
|
|
15
15
|
*/
|
|
16
16
|
declare function styleXTransform(): PluginObj;
|
|
17
|
-
|
|
17
|
+
declare function stylexPluginWithOptions(
|
|
18
|
+
options: Partial<StyleXOptions>,
|
|
19
|
+
): [typeof styleXTransform, Partial<StyleXOptions>];
|
|
18
20
|
/**
|
|
19
21
|
*
|
|
20
22
|
* @param rules An array of CSS rules that has been generated and collected from all JS files
|
|
21
23
|
* in a project
|
|
22
|
-
* @returns A string that
|
|
24
|
+
* @returns A string that represents the final CSS file.
|
|
23
25
|
*
|
|
24
26
|
* This function take an Array of CSS rules, de-duplicates them, sorts them priority and generates
|
|
25
27
|
* a final CSS file.
|
|
@@ -31,3 +33,14 @@ export default styleXTransform;
|
|
|
31
33
|
* End-users can choose to not use this function and use their own logic instead.
|
|
32
34
|
*/
|
|
33
35
|
export type Rule = [string, { ltr: string; rtl?: null | string }, number];
|
|
36
|
+
declare function processStylexRules(
|
|
37
|
+
rules: Array<Rule>,
|
|
38
|
+
useLayers: boolean,
|
|
39
|
+
): string;
|
|
40
|
+
export type StyleXTransformObj = Readonly<{
|
|
41
|
+
(): PluginObj;
|
|
42
|
+
withOptions: typeof stylexPluginWithOptions;
|
|
43
|
+
processStylexRules: typeof processStylexRules;
|
|
44
|
+
}>;
|
|
45
|
+
declare const $$EXPORT_DEFAULT_DECLARATION$$: StyleXTransformObj;
|
|
46
|
+
export default $$EXPORT_DEFAULT_DECLARATION$$;
|