react-native-unistyles 3.7.0 → 3.8.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.
Files changed (169) hide show
  1. package/README.md +2 -2
  2. package/android/src/main/java/com/unistyles/NativePlatform+android.kt +2 -15
  3. package/android/src/main/java/com/unistyles/NativePlatform+listener.kt +1 -1
  4. package/codemods/use-variants.js +155 -0
  5. package/components/native/TouchableHighlight/package.json +1 -1
  6. package/cxx/converters/TransformOriginConverter.cpp +3 -2
  7. package/cxx/core/DependencyTracker.h +162 -0
  8. package/cxx/core/StyleSheet.h +14 -0
  9. package/cxx/core/Unistyle.h +6 -3
  10. package/cxx/core/UnistylesRegistry.cpp +16 -2
  11. package/cxx/core/UnistylesRegistry.h +2 -1
  12. package/cxx/hybridObjects/HybridShadowRegistry.cpp +16 -5
  13. package/cxx/hybridObjects/HybridShadowRegistry.h +1 -1
  14. package/cxx/hybridObjects/HybridStyleSheet.cpp +8 -1
  15. package/cxx/hybridObjects/HybridUnistylesRuntime.cpp +1 -1
  16. package/cxx/parser/Parser.cpp +83 -31
  17. package/cxx/parser/Parser.h +5 -0
  18. package/cxx/shadowTree/NativeProps.h +23 -0
  19. package/cxx/shadowTree/ShadowTrafficController.h +5 -0
  20. package/cxx/shadowTree/ShadowTreeManager.cpp +73 -1
  21. package/ios/NativePlatform+ios.swift +4 -6
  22. package/ios/Unistyles.h +1 -0
  23. package/lib/commonjs/core/trackDependencies.js +10 -0
  24. package/lib/commonjs/core/trackDependencies.js.map +1 -0
  25. package/lib/commonjs/core/trackDependencies.metro.js +10 -0
  26. package/lib/commonjs/core/trackDependencies.metro.js.map +1 -0
  27. package/lib/commonjs/core/useProxifiedUnistyles/useProxifiedUnistyles.js +4 -0
  28. package/lib/commonjs/core/useProxifiedUnistyles/useProxifiedUnistyles.js.map +1 -1
  29. package/lib/commonjs/core/withUnistyles/withUnistyles.native.js +63 -14
  30. package/lib/commonjs/core/withUnistyles/withUnistyles.native.js.map +1 -1
  31. package/lib/commonjs/hooks/useMedia.js +6 -0
  32. package/lib/commonjs/hooks/useMedia.js.map +1 -1
  33. package/lib/commonjs/mocks.js +4 -2
  34. package/lib/commonjs/mocks.js.map +1 -1
  35. package/lib/commonjs/reanimated/variant/useUpdateVariantColor.js +13 -4
  36. package/lib/commonjs/reanimated/variant/useUpdateVariantColor.js.map +1 -1
  37. package/lib/commonjs/specs/ShadowRegistry/index.js +26 -1
  38. package/lib/commonjs/specs/ShadowRegistry/index.js.map +1 -1
  39. package/lib/commonjs/specs/StyleSheet/index.js +6 -1
  40. package/lib/commonjs/specs/StyleSheet/index.js.map +1 -1
  41. package/lib/commonjs/types/stylesheet.js.map +1 -1
  42. package/lib/commonjs/web/convert/index.js +2 -2
  43. package/lib/commonjs/web/convert/index.js.map +1 -1
  44. package/lib/commonjs/web/convert/style.js +6 -0
  45. package/lib/commonjs/web/convert/style.js.map +1 -1
  46. package/lib/commonjs/web/create.js +35 -6
  47. package/lib/commonjs/web/create.js.map +1 -1
  48. package/lib/commonjs/web/css/core.js +12 -2
  49. package/lib/commonjs/web/css/core.js.map +1 -1
  50. package/lib/commonjs/web/css/sheet.js +223 -0
  51. package/lib/commonjs/web/css/sheet.js.map +1 -0
  52. package/lib/commonjs/web/css/state.js +98 -78
  53. package/lib/commonjs/web/css/state.js.map +1 -1
  54. package/lib/commonjs/web/css/utils.js +1 -31
  55. package/lib/commonjs/web/css/utils.js.map +1 -1
  56. package/lib/commonjs/web/dependencyTracker.js +72 -0
  57. package/lib/commonjs/web/dependencyTracker.js.map +1 -0
  58. package/lib/commonjs/web/registry.js +73 -4
  59. package/lib/commonjs/web/registry.js.map +1 -1
  60. package/lib/commonjs/web/runtime.js +0 -1
  61. package/lib/commonjs/web/runtime.js.map +1 -1
  62. package/lib/commonjs/web/shadowRegistry.js +83 -11
  63. package/lib/commonjs/web/shadowRegistry.js.map +1 -1
  64. package/lib/commonjs/web/types.js +3 -1
  65. package/lib/commonjs/web/types.js.map +1 -1
  66. package/lib/module/core/trackDependencies.js +6 -0
  67. package/lib/module/core/trackDependencies.js.map +1 -0
  68. package/lib/module/core/trackDependencies.metro.js +6 -0
  69. package/lib/module/core/trackDependencies.metro.js.map +1 -0
  70. package/lib/module/core/useProxifiedUnistyles/useProxifiedUnistyles.js +4 -0
  71. package/lib/module/core/useProxifiedUnistyles/useProxifiedUnistyles.js.map +1 -1
  72. package/lib/module/core/withUnistyles/withUnistyles.native.js +63 -15
  73. package/lib/module/core/withUnistyles/withUnistyles.native.js.map +1 -1
  74. package/lib/module/hooks/useMedia.js +6 -0
  75. package/lib/module/hooks/useMedia.js.map +1 -1
  76. package/lib/module/mocks.js +4 -2
  77. package/lib/module/mocks.js.map +1 -1
  78. package/lib/module/reanimated/variant/useUpdateVariantColor.js +15 -5
  79. package/lib/module/reanimated/variant/useUpdateVariantColor.js.map +1 -1
  80. package/lib/module/specs/ShadowRegistry/index.js +25 -1
  81. package/lib/module/specs/ShadowRegistry/index.js.map +1 -1
  82. package/lib/module/specs/StyleSheet/index.js +6 -1
  83. package/lib/module/specs/StyleSheet/index.js.map +1 -1
  84. package/lib/module/types/stylesheet.js.map +1 -1
  85. package/lib/module/web/convert/index.js +2 -2
  86. package/lib/module/web/convert/index.js.map +1 -1
  87. package/lib/module/web/convert/style.js +6 -0
  88. package/lib/module/web/convert/style.js.map +1 -1
  89. package/lib/module/web/create.js +35 -6
  90. package/lib/module/web/create.js.map +1 -1
  91. package/lib/module/web/css/core.js +10 -1
  92. package/lib/module/web/css/core.js.map +1 -1
  93. package/lib/module/web/css/sheet.js +216 -0
  94. package/lib/module/web/css/sheet.js.map +1 -0
  95. package/lib/module/web/css/state.js +98 -78
  96. package/lib/module/web/css/state.js.map +1 -1
  97. package/lib/module/web/css/utils.js +0 -29
  98. package/lib/module/web/css/utils.js.map +1 -1
  99. package/lib/module/web/dependencyTracker.js +67 -0
  100. package/lib/module/web/dependencyTracker.js.map +1 -0
  101. package/lib/module/web/registry.js +73 -4
  102. package/lib/module/web/registry.js.map +1 -1
  103. package/lib/module/web/runtime.js +0 -1
  104. package/lib/module/web/runtime.js.map +1 -1
  105. package/lib/module/web/shadowRegistry.js +83 -11
  106. package/lib/module/web/shadowRegistry.js.map +1 -1
  107. package/lib/module/web/types.js +2 -0
  108. package/lib/module/web/types.js.map +1 -1
  109. package/lib/typescript/src/core/trackDependencies.d.ts +2 -0
  110. package/lib/typescript/src/core/trackDependencies.d.ts.map +1 -0
  111. package/lib/typescript/src/core/trackDependencies.metro.d.ts +2 -0
  112. package/lib/typescript/src/core/trackDependencies.metro.d.ts.map +1 -0
  113. package/lib/typescript/src/core/useProxifiedUnistyles/useProxifiedUnistyles.d.ts.map +1 -1
  114. package/lib/typescript/src/core/withUnistyles/withUnistyles.native.d.ts +2 -1
  115. package/lib/typescript/src/core/withUnistyles/withUnistyles.native.d.ts.map +1 -1
  116. package/lib/typescript/src/hooks/useMedia.d.ts.map +1 -1
  117. package/lib/typescript/src/reanimated/variant/useUpdateVariantColor.d.ts.map +1 -1
  118. package/lib/typescript/src/specs/ShadowRegistry/index.d.ts +1 -1
  119. package/lib/typescript/src/specs/ShadowRegistry/index.d.ts.map +1 -1
  120. package/lib/typescript/src/specs/StyleSheet/index.d.ts +3 -1
  121. package/lib/typescript/src/specs/StyleSheet/index.d.ts.map +1 -1
  122. package/lib/typescript/src/types/stylesheet.d.ts +3 -3
  123. package/lib/typescript/src/types/stylesheet.d.ts.map +1 -1
  124. package/lib/typescript/src/web/convert/style.d.ts.map +1 -1
  125. package/lib/typescript/src/web/create.d.ts.map +1 -1
  126. package/lib/typescript/src/web/css/core.d.ts +4 -1
  127. package/lib/typescript/src/web/css/core.d.ts.map +1 -1
  128. package/lib/typescript/src/web/css/sheet.d.ts +26 -0
  129. package/lib/typescript/src/web/css/sheet.d.ts.map +1 -0
  130. package/lib/typescript/src/web/css/state.d.ts +6 -6
  131. package/lib/typescript/src/web/css/state.d.ts.map +1 -1
  132. package/lib/typescript/src/web/css/utils.d.ts +0 -2
  133. package/lib/typescript/src/web/css/utils.d.ts.map +1 -1
  134. package/lib/typescript/src/web/dependencyTracker.d.ts +9 -0
  135. package/lib/typescript/src/web/dependencyTracker.d.ts.map +1 -0
  136. package/lib/typescript/src/web/registry.d.ts +9 -1
  137. package/lib/typescript/src/web/registry.d.ts.map +1 -1
  138. package/lib/typescript/src/web/runtime.d.ts.map +1 -1
  139. package/lib/typescript/src/web/shadowRegistry.d.ts.map +1 -1
  140. package/lib/typescript/src/web/types.d.ts +1 -0
  141. package/lib/typescript/src/web/types.d.ts.map +1 -1
  142. package/metro/index.d.ts +2 -12
  143. package/metro/index.js +57 -33
  144. package/package.json +64 -17
  145. package/plugin/index.d.ts +0 -7
  146. package/plugin/index.js +9 -15
  147. package/src/core/trackDependencies.metro.ts +3 -0
  148. package/src/core/trackDependencies.ts +3 -0
  149. package/src/core/useProxifiedUnistyles/useProxifiedUnistyles.ts +5 -0
  150. package/src/core/withUnistyles/withUnistyles.native.tsx +76 -17
  151. package/src/hooks/useMedia.ts +6 -0
  152. package/src/mocks.ts +5 -2
  153. package/src/reanimated/variant/useUpdateVariantColor.ts +15 -5
  154. package/src/specs/ShadowRegistry/index.ts +29 -2
  155. package/src/specs/StyleSheet/index.ts +4 -2
  156. package/src/types/stylesheet.ts +5 -4
  157. package/src/web/convert/index.ts +2 -2
  158. package/src/web/convert/style.ts +8 -0
  159. package/src/web/create.ts +45 -8
  160. package/src/web/css/core.ts +17 -3
  161. package/src/web/css/sheet.ts +274 -0
  162. package/src/web/css/state.ts +108 -95
  163. package/src/web/css/utils.ts +0 -42
  164. package/src/web/dependencyTracker.ts +71 -0
  165. package/src/web/registry.ts +88 -8
  166. package/src/web/runtime.ts +0 -1
  167. package/src/web/shadowRegistry.ts +101 -16
  168. package/src/web/types.ts +3 -0
  169. package/metro/babel-transformer.js +0 -1102
@@ -1,1102 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // metro/src/babel-transformer.ts
31
- var babel_transformer_exports = {};
32
- __export(babel_transformer_exports, {
33
- getCacheKey: () => getCacheKey,
34
- transform: () => transform
35
- });
36
- module.exports = __toCommonJS(babel_transformer_exports);
37
- var import_node_fs = __toESM(require("node:fs"));
38
- var import_node_path4 = __toESM(require("node:path"));
39
-
40
- // plugin/src/consts.ts
41
- var REACT_NATIVE_COMPONENT_NAMES = [
42
- "ActivityIndicator",
43
- "View",
44
- "Text",
45
- "Image",
46
- "ImageBackground",
47
- "KeyboardAvoidingView",
48
- "Pressable",
49
- "ScrollView",
50
- "FlatList",
51
- "SectionList",
52
- "Switch",
53
- "TextInput",
54
- "RefreshControl",
55
- "TouchableHighlight",
56
- "TouchableOpacity",
57
- "VirtualizedList",
58
- "Animated",
59
- "SafeAreaView"
60
- // Modal - there is no exposed native handle
61
- // TouchableWithoutFeedback - can't accept a ref
62
- ];
63
- var REPLACE_WITH_UNISTYLES_PATHS = [
64
- "react-native-reanimated/src/component",
65
- "react-native-reanimated/lib/module/component"
66
- ];
67
- var REPLACE_WITH_UNISTYLES_EXOTIC_PATHS = [];
68
- var NATIVE_COMPONENTS_PATHS = {
69
- imports: [
70
- {
71
- name: "NativeText",
72
- isDefault: false,
73
- path: "react-native/Libraries/Text/TextNativeComponent",
74
- mapTo: "NativeText"
75
- },
76
- {
77
- isDefault: true,
78
- path: "react-native/Libraries/Components/View/ViewNativeComponent",
79
- mapTo: "NativeView"
80
- },
81
- {
82
- name: "unstable_NativeText",
83
- isDefault: false,
84
- path: "react-native",
85
- mapTo: "NativeText"
86
- },
87
- {
88
- name: "unstable_NativeView",
89
- isDefault: false,
90
- path: "react-native",
91
- mapTo: "NativeView"
92
- }
93
- ]
94
- };
95
- var METRO_UPSTREAM_TRANSFORMER_ENV = "UNISTYLES_METRO_UPSTREAM_TRANSFORMER";
96
- var METRO_OPTIONS_ENV = "UNISTYLES_METRO_OPTIONS";
97
-
98
- // metro/src/transformer.ts
99
- var import_node_path3 = __toESM(require("node:path"));
100
-
101
- // plugin/src/index.ts
102
- var t6 = __toESM(require("@babel/types"));
103
- var import_node_path2 = __toESM(require("node:path"));
104
-
105
- // plugin/src/exotic.ts
106
- var t2 = __toESM(require("@babel/types"));
107
-
108
- // plugin/src/import.ts
109
- var t = __toESM(require("@babel/types"));
110
- function getComponentPath(state, name) {
111
- if (!state.opts.isLocal) {
112
- return `react-native-unistyles/components/native/${name}`;
113
- }
114
- if (state.opts.localPath) {
115
- return `${state.opts.localPath}/src/components/native/${name}`;
116
- }
117
- return state.file.opts.filename?.split("react-native-unistyles").at(0)?.concat(`react-native-unistyles/src/components/native/${name}`) ?? "";
118
- }
119
- function addUnistylesImport(path3, state) {
120
- const localNames = Object.keys(state.reactNativeImports);
121
- const names = Object.values(state.reactNativeImports);
122
- const pairs = Object.entries(state.reactNativeImports);
123
- const nodesToRemove = [];
124
- path3.node.body.forEach((node) => {
125
- if (t.isImportDeclaration(node) && node.source.value === "react-native") {
126
- node.specifiers = node.specifiers.filter(
127
- (specifier) => !localNames.some((name) => name === specifier.local.name)
128
- );
129
- if (node.specifiers.length === 0) {
130
- nodesToRemove.push(node);
131
- }
132
- }
133
- });
134
- names.forEach((name) => {
135
- const rnWebImport = path3.node.body.find(
136
- (node) => t.isImportDeclaration(node) && node.source.value === `react-native-web/dist/exports/${name}`
137
- );
138
- if (rnWebImport) {
139
- rnWebImport.specifiers = [];
140
- }
141
- });
142
- pairs.forEach(([localName, name]) => {
143
- const newImport = t.importDeclaration(
144
- [t.importSpecifier(t.identifier(localName), t.identifier(name))],
145
- t.stringLiteral(getComponentPath(state, name))
146
- );
147
- path3.node.body.unshift(newImport);
148
- });
149
- nodesToRemove.forEach((node) => path3.node.body.splice(path3.node.body.indexOf(node), 1));
150
- }
151
- function isInsideNodeModules(state) {
152
- if (state.opts.isMetro) {
153
- return false;
154
- }
155
- return state.file.opts.filename?.includes("node_modules") && !state.file.replaceWithUnistyles;
156
- }
157
- function addUnistylesRequire(path3, state) {
158
- Object.entries(state.reactNativeImports).forEach(([componentName, uniqueName]) => {
159
- const newRequire = t.variableDeclaration("const", [
160
- t.variableDeclarator(
161
- t.identifier(uniqueName),
162
- t.callExpression(t.identifier("require"), [
163
- t.stringLiteral(`react-native-unistyles/components/native/${componentName}`)
164
- ])
165
- )
166
- ]);
167
- path3.node.body.unshift(newRequire);
168
- });
169
- }
170
-
171
- // plugin/src/exotic.ts
172
- function handleExoticImport(path3, state, exoticImport) {
173
- const specifiers = path3.node.specifiers;
174
- const source = path3.node.source;
175
- if (path3.node.importKind !== "value") {
176
- return;
177
- }
178
- specifiers.forEach((specifier) => {
179
- for (const rule of exoticImport.imports) {
180
- const hasMatchingImportType = !rule.isDefault && t2.isImportSpecifier(specifier) || rule.isDefault && t2.isImportDefaultSpecifier(specifier);
181
- const isTypeOnly = t2.isImportSpecifier(specifier) && specifier.importKind !== "value";
182
- const hasMatchingImportName = rule.isDefault || t2.isImportSpecifier(specifier) && rule.name === getImportedName(specifier);
183
- const hasMatchingPath = rule.path === source.value;
184
- if (isTypeOnly || !hasMatchingImportType || !hasMatchingImportName || !hasMatchingPath) {
185
- continue;
186
- }
187
- if (t2.isImportDefaultSpecifier(specifier)) {
188
- const newImport = t2.importDeclaration(
189
- [t2.importDefaultSpecifier(t2.identifier(specifier.local.name))],
190
- t2.stringLiteral(getComponentPath(state, rule.mapTo))
191
- );
192
- path3.replaceWith(newImport);
193
- } else {
194
- const newImport = t2.importDeclaration(
195
- [t2.importSpecifier(t2.identifier(specifier.local.name), t2.identifier(rule.mapTo))],
196
- t2.stringLiteral(getComponentPath(state, rule.mapTo))
197
- );
198
- path3.node.specifiers = path3.node.specifiers.filter((s) => s !== specifier);
199
- if (path3.node.specifiers.length === 0) {
200
- path3.replaceWith(newImport);
201
- } else {
202
- path3.insertBefore(newImport);
203
- }
204
- }
205
- return;
206
- }
207
- });
208
- }
209
- function getImportedName(specifier) {
210
- return t2.isIdentifier(specifier.imported) ? specifier.imported.name : specifier.imported.value;
211
- }
212
-
213
- // plugin/src/paths.ts
214
- var import_node_path = __toESM(require("node:path"));
215
- var isWindows = process.platform === "win32";
216
- var toWinPath = (pathString) => {
217
- return import_node_path.default.normalize(pathString).replace(/\//g, "\\");
218
- };
219
- var toPlatformPath = (pathString) => {
220
- return isWindows ? toWinPath(pathString) : pathString;
221
- };
222
-
223
- // plugin/src/ref.ts
224
- var t3 = __toESM(require("@babel/types"));
225
- function hasStringRef(path3) {
226
- return path3.node.openingElement.attributes.find(
227
- (attr) => t3.isJSXAttribute(attr) && t3.isJSXIdentifier(attr.name, { name: "ref" }) && t3.isStringLiteral(attr.value)
228
- );
229
- }
230
-
231
- // plugin/src/stylesheet.ts
232
- var t4 = __toESM(require("@babel/types"));
233
- var UnistyleDependency = {
234
- Theme: 0,
235
- ThemeName: 1,
236
- AdaptiveThemes: 2,
237
- Breakpoints: 3,
238
- Variants: 4,
239
- ColorScheme: 5,
240
- Dimensions: 6,
241
- Orientation: 7,
242
- ContentSizeCategory: 8,
243
- Insets: 9,
244
- PixelRatio: 10,
245
- FontScale: 11,
246
- StatusBar: 12,
247
- NavigationBar: 13,
248
- Ime: 14,
249
- Rtl: 15
250
- };
251
- function getStyleKeyName(key) {
252
- if (!key) {
253
- return null;
254
- }
255
- if (t4.isIdentifier(key)) {
256
- return key.name;
257
- }
258
- if (t4.isStringLiteral(key) || t4.isNumericLiteral(key) || t4.isBooleanLiteral(key)) {
259
- return String(key.value);
260
- }
261
- return null;
262
- }
263
- function getProperty(property) {
264
- if (!property) {
265
- return void 0;
266
- }
267
- if (t4.isIdentifier(property)) {
268
- const prop = property;
269
- return {
270
- properties: [prop.name]
271
- };
272
- }
273
- if (t4.isObjectPattern(property)) {
274
- const prop = property;
275
- const matchingProperties = prop.properties.flatMap((p) => getProperty(p));
276
- return {
277
- properties: matchingProperties.flatMap((properties) => properties?.properties).filter((prop2) => prop2 !== void 0)
278
- };
279
- }
280
- if (t4.isObjectProperty(property) && t4.isIdentifier(property.value)) {
281
- const prop = property.key;
282
- return {
283
- properties: [prop.name]
284
- };
285
- }
286
- if (t4.isObjectProperty(property) && t4.isObjectPattern(property.value)) {
287
- const matchingProperties = property.value.properties.flatMap((p) => getProperty(p));
288
- const prop = property.key;
289
- return {
290
- parent: prop.name,
291
- properties: matchingProperties.flatMap((properties) => properties?.properties).filter((prop2) => prop2 !== void 0)
292
- };
293
- }
294
- return void 0;
295
- }
296
- function toUnistylesDependency(dependency) {
297
- switch (dependency) {
298
- case "theme": {
299
- return UnistyleDependency.Theme;
300
- }
301
- case "themeName": {
302
- return UnistyleDependency.ThemeName;
303
- }
304
- case "adaptiveThemes":
305
- case "hasAdaptiveThemes": {
306
- return UnistyleDependency.AdaptiveThemes;
307
- }
308
- case "breakpoint": {
309
- return UnistyleDependency.Breakpoints;
310
- }
311
- case "colorScheme": {
312
- return UnistyleDependency.ColorScheme;
313
- }
314
- case "screen": {
315
- return UnistyleDependency.Dimensions;
316
- }
317
- case "isPortrait":
318
- case "isLandscape": {
319
- return UnistyleDependency.Orientation;
320
- }
321
- case "contentSizeCategory": {
322
- return UnistyleDependency.ContentSizeCategory;
323
- }
324
- case "ime": {
325
- return UnistyleDependency.Ime;
326
- }
327
- case "insets": {
328
- return UnistyleDependency.Insets;
329
- }
330
- case "pixelRatio": {
331
- return UnistyleDependency.PixelRatio;
332
- }
333
- case "fontScale": {
334
- return UnistyleDependency.FontScale;
335
- }
336
- case "statusBar": {
337
- return UnistyleDependency.StatusBar;
338
- }
339
- case "navigationBar": {
340
- return UnistyleDependency.NavigationBar;
341
- }
342
- case "variants": {
343
- return UnistyleDependency.Variants;
344
- }
345
- case "rtl": {
346
- return UnistyleDependency.Rtl;
347
- }
348
- default:
349
- return null;
350
- }
351
- }
352
- function getReturnStatementsFromBody(node, results = []) {
353
- if (t4.isReturnStatement(node)) {
354
- results.push(node);
355
- }
356
- if (t4.isBlockStatement(node)) {
357
- node.body.forEach((child) => getReturnStatementsFromBody(child, results));
358
- }
359
- if (t4.isIfStatement(node)) {
360
- getReturnStatementsFromBody(node.consequent, results);
361
- if (node.alternate) {
362
- getReturnStatementsFromBody(node.alternate, results);
363
- }
364
- }
365
- return results;
366
- }
367
- function countReturnStatements(node) {
368
- if (t4.isFunction(node) || t4.isClass(node)) {
369
- return 0;
370
- }
371
- if (t4.isReturnStatement(node)) {
372
- return 1;
373
- }
374
- return (t4.VISITOR_KEYS[node.type] ?? []).reduce((count, key) => {
375
- const child = node[key];
376
- const children = (Array.isArray(child) ? child : [child]).filter(
377
- (childNode) => t4.isNode(childNode)
378
- );
379
- return count + children.reduce((childCount, childNode) => childCount + countReturnStatements(childNode), 0);
380
- }, 0);
381
- }
382
- function collectReturnedObjects(node, targets, unsupported) {
383
- if (t4.isObjectExpression(node)) {
384
- targets.push(node);
385
- return;
386
- }
387
- if (t4.isConditionalExpression(node)) {
388
- collectReturnedObjects(node.consequent, targets, unsupported);
389
- collectReturnedObjects(node.alternate, targets, unsupported);
390
- return;
391
- }
392
- if (t4.isLogicalExpression(node)) {
393
- if (node.operator !== "&&") {
394
- collectReturnedObjects(node.left, targets, unsupported);
395
- }
396
- collectReturnedObjects(node.right, targets, unsupported);
397
- return;
398
- }
399
- if (t4.isTSAsExpression(node) || t4.isTSSatisfiesExpression(node) || t4.isParenthesizedExpression(node)) {
400
- collectReturnedObjects(node.expression, targets, unsupported);
401
- return;
402
- }
403
- if (t4.isNullLiteral(node) || t4.isBooleanLiteral(node) || t4.isIdentifier(node) && node.name === "undefined") {
404
- return;
405
- }
406
- unsupported.push(node);
407
- }
408
- function isUnistylesStyleSheet(path3, state) {
409
- const { callee } = path3.node;
410
- if (!t4.isMemberExpression(callee) || !t4.isIdentifier(callee.property)) {
411
- return false;
412
- }
413
- const isImport = callee.property.name === "create" && t4.isIdentifier(callee.object) && callee.object.name === state.file.styleSheetLocalName;
414
- const isRequire = state.file.hasUnistylesImport && callee.property.name === "create" && t4.isMemberExpression(callee.object) && t4.isIdentifier(callee.object.property) && t4.isIdentifier(callee.object.object) && callee.object.object.name === state.file.styleSheetLocalName && callee.object.property.name === "StyleSheet";
415
- return isImport || isRequire;
416
- }
417
- function isUnistylesCommonJSRequire(path3, state) {
418
- const isRequire = t4.isIdentifier(path3.node.callee) && path3.node.arguments.length > 0 && t4.isStringLiteral(path3.node.arguments[0]) && path3.node.arguments[0].value === "react-native-unistyles";
419
- if (isRequire && t4.isVariableDeclarator(path3.parent) && t4.isIdentifier(path3.parent.id)) {
420
- state.file.hasUnistylesImport = true;
421
- state.file.styleSheetLocalName = path3.parent.id.name;
422
- }
423
- return isRequire;
424
- }
425
- function isReactNativeCommonJSRequire(path3, state) {
426
- const isRequire = t4.isIdentifier(path3.node.callee) && path3.node.arguments.length > 0 && path3.node.callee.name === "require";
427
- const requireImportName = path3.node.arguments.find((node) => t4.isStringLiteral(node));
428
- const isReactNativeRequire = isRequire && requireImportName && (requireImportName.value === "react-native" || requireImportName.value === "react-native-web/dist/index");
429
- if (isReactNativeRequire && t4.isVariableDeclarator(path3.parent) && t4.isIdentifier(path3.parent.id)) {
430
- state.file.reactNativeCommonJSName = path3.parent.id.name;
431
- }
432
- return isRequire;
433
- }
434
- function isKindOfStyleSheet(path3, state) {
435
- if (!state.file.forceProcessing && !state.file.hasUnistylesImport) {
436
- return false;
437
- }
438
- if (path3.node.arguments.length !== 1) {
439
- return false;
440
- }
441
- const { callee } = path3.node;
442
- const isCreateCall = t4.isMemberExpression(callee) && t4.isIdentifier(callee.property) && callee.property.name === "create" && (t4.isIdentifier(callee.object) || t4.isMemberExpression(callee.object));
443
- if (!isCreateCall) {
444
- return false;
445
- }
446
- const argument = path3.node.arguments[0];
447
- if (t4.isArrowFunctionExpression(argument)) {
448
- if (argument.params.length > 2) {
449
- return false;
450
- }
451
- if (t4.isObjectExpression(argument.body) && argument.body.properties.length > 0) {
452
- return true;
453
- }
454
- if (t4.isBlockStatement(argument.body)) {
455
- const returnStatements = getReturnStatementsFromBody(argument.body);
456
- return returnStatements.some(
457
- (ret) => ret.argument && t4.isObjectExpression(ret.argument) && ret.argument.properties.length > 0
458
- );
459
- }
460
- return false;
461
- }
462
- if (t4.isObjectExpression(argument)) {
463
- return argument.properties.some(
464
- (property) => t4.isObjectProperty(property) && t4.isObjectExpression(property.value)
465
- );
466
- }
467
- return false;
468
- }
469
- function getStylesDependenciesFromObject(path3) {
470
- const detectedStylesWithVariants = /* @__PURE__ */ new Set();
471
- const stylesheet = path3.node.arguments[0];
472
- if (t4.isObjectExpression(stylesheet)) {
473
- stylesheet?.properties.forEach((property) => {
474
- if (!t4.isObjectProperty(property)) {
475
- return;
476
- }
477
- const styleKey = getStyleKeyName(property.key);
478
- if (!styleKey) {
479
- return;
480
- }
481
- if (t4.isObjectExpression(property.value)) {
482
- property.value.properties.forEach((innerProp) => {
483
- if (t4.isObjectProperty(innerProp) && t4.isIdentifier(innerProp.key) && innerProp.key.name === "variants") {
484
- detectedStylesWithVariants.add({
485
- label: "variants",
486
- key: styleKey
487
- });
488
- }
489
- });
490
- }
491
- if (t4.isArrowFunctionExpression(property.value)) {
492
- if (t4.isObjectExpression(property.value.body)) {
493
- property.value.body.properties.forEach((innerProp) => {
494
- if (t4.isObjectProperty(innerProp) && t4.isIdentifier(innerProp.key) && innerProp.key.name === "variants") {
495
- detectedStylesWithVariants.add({
496
- label: "variants",
497
- key: styleKey
498
- });
499
- }
500
- });
501
- }
502
- }
503
- });
504
- }
505
- const variants = Array.from(detectedStylesWithVariants);
506
- return variants.reduce((acc, { key, label }) => {
507
- if (acc[key]) {
508
- acc[key] = [...acc[key], label];
509
- return acc;
510
- }
511
- acc[key] = [label];
512
- return acc;
513
- }, {});
514
- }
515
- function getStylesDependenciesFromFunction(funcPath) {
516
- if (!funcPath) {
517
- return;
518
- }
519
- if (Array.isArray(funcPath)) {
520
- return;
521
- }
522
- if (!t4.isFunctionExpression(funcPath.node) && !t4.isArrowFunctionExpression(funcPath.node)) {
523
- return;
524
- }
525
- const params = funcPath.node.params;
526
- const [themeParam, rtParam] = params;
527
- const themeNames = [];
528
- if (t4.isObjectPattern(themeParam)) {
529
- for (const prop of themeParam.properties) {
530
- const property = getProperty(prop);
531
- if (property) {
532
- themeNames.push(property);
533
- }
534
- }
535
- }
536
- if (t4.isIdentifier(themeParam)) {
537
- themeNames.push({
538
- properties: [themeParam.name]
539
- });
540
- }
541
- const rtNames = [];
542
- if (t4.isObjectPattern(rtParam)) {
543
- for (const prop of rtParam.properties) {
544
- const property = getProperty(prop);
545
- if (property) {
546
- rtNames.push(property);
547
- }
548
- }
549
- }
550
- if (t4.isIdentifier(rtParam)) {
551
- rtNames.push({
552
- properties: [rtParam.name]
553
- });
554
- }
555
- let returnedObjectPath = null;
556
- if (t4.isObjectExpression(funcPath.node.body)) {
557
- returnedObjectPath = funcPath.get("body");
558
- } else {
559
- funcPath.traverse({
560
- ReturnStatement(retPath) {
561
- if (!returnedObjectPath && retPath.get("argument").isObjectExpression()) {
562
- const argumentPath = retPath.get("argument");
563
- if (argumentPath.isObjectExpression()) {
564
- returnedObjectPath = argumentPath;
565
- }
566
- }
567
- }
568
- });
569
- }
570
- if (!returnedObjectPath) {
571
- return;
572
- }
573
- const detectedStylesWithVariants = /* @__PURE__ */ new Set();
574
- const properties = returnedObjectPath.get("properties");
575
- properties.forEach((propPath) => {
576
- const stylePath = propPath.get("key");
577
- if (Array.isArray(stylePath)) {
578
- return;
579
- }
580
- const styleKey = getStyleKeyName(stylePath.node);
581
- if (!styleKey) {
582
- return;
583
- }
584
- const valuePath = propPath.get("value");
585
- if (Array.isArray(valuePath)) {
586
- return;
587
- }
588
- if (valuePath.isObjectExpression()) {
589
- const hasVariants = valuePath.get("properties").some((innerProp) => {
590
- const innerKey = innerProp.get("key");
591
- if (Array.isArray(innerKey)) {
592
- return;
593
- }
594
- return innerKey.isIdentifier() && innerKey.node.name === "variants";
595
- });
596
- if (hasVariants) {
597
- detectedStylesWithVariants.add({
598
- label: "variants",
599
- key: styleKey
600
- });
601
- }
602
- }
603
- if (valuePath.isArrowFunctionExpression()) {
604
- if (t4.isObjectExpression(valuePath.node.body)) {
605
- const hasVariants = valuePath.node.body.properties.some((innerProp) => {
606
- return t4.isObjectProperty(innerProp) && t4.isIdentifier(innerProp.key) && innerProp.key.name === "variants";
607
- });
608
- if (hasVariants) {
609
- detectedStylesWithVariants.add({
610
- label: "variants",
611
- key: styleKey
612
- });
613
- }
614
- }
615
- }
616
- });
617
- const detectedStylesWithTheme = /* @__PURE__ */ new Set();
618
- themeNames.forEach(({ properties: properties2 }) => {
619
- properties2.forEach((property) => {
620
- const binding = funcPath.scope.getBinding(property);
621
- if (!binding) {
622
- return;
623
- }
624
- binding.referencePaths.forEach((refPath) => {
625
- const containerProp = refPath.findParent(
626
- (parent) => parent.isObjectProperty() && parent.parentPath === returnedObjectPath
627
- );
628
- if (!containerProp) {
629
- return;
630
- }
631
- const keyNode = containerProp.get("key");
632
- if (Array.isArray(keyNode)) {
633
- return;
634
- }
635
- const keyValue = keyNode.isLiteral() ? keyNode.isStringLiteral() || keyNode.isNumericLiteral() || keyNode.isBooleanLiteral() ? String(keyNode.node.value) : null : null;
636
- const styleKey = keyNode.isIdentifier() ? keyNode.node.name : keyValue;
637
- if (styleKey) {
638
- detectedStylesWithTheme.add({
639
- label: "theme",
640
- key: styleKey
641
- });
642
- }
643
- });
644
- });
645
- });
646
- const detectedStylesWithRt = /* @__PURE__ */ new Set();
647
- const localRtName = t4.isIdentifier(rtParam) ? rtParam.name : void 0;
648
- rtNames.forEach(({ properties: properties2, parent }) => {
649
- properties2.forEach((property) => {
650
- const rtBinding = funcPath.scope.getBinding(property);
651
- if (!rtBinding) {
652
- return;
653
- }
654
- const isValidDependency = Boolean(toUnistylesDependency(property));
655
- let validRtName = property;
656
- if (!isValidDependency && (!localRtName || localRtName && localRtName !== property)) {
657
- if (!parent) {
658
- return;
659
- }
660
- if (!toUnistylesDependency(parent)) {
661
- return;
662
- }
663
- validRtName = parent;
664
- }
665
- rtBinding.referencePaths.forEach((refPath) => {
666
- let usedLabel = validRtName;
667
- if (refPath.parentPath?.isMemberExpression() && refPath.parentPath.get("object") === refPath) {
668
- const memberExpr = refPath.parentPath;
669
- const propPath = memberExpr.get("property");
670
- if (propPath.isIdentifier()) {
671
- if (localRtName) {
672
- usedLabel = propPath.node.name;
673
- }
674
- if (usedLabel === "insets" && memberExpr.parentPath.isMemberExpression() && memberExpr.parentPath.get("object") === memberExpr) {
675
- const secondPropPath = memberExpr.parentPath.get("property");
676
- if (secondPropPath.isIdentifier() && secondPropPath.node.name === "ime") {
677
- usedLabel = "ime";
678
- }
679
- }
680
- if (usedLabel === "insets" && (memberExpr.parentPath.isBinaryExpression() || memberExpr.parentPath.isLogicalExpression())) {
681
- const secondPropPath = memberExpr.node.property;
682
- if (t4.isIdentifier(secondPropPath) && secondPropPath.name === "ime") {
683
- usedLabel = "ime";
684
- }
685
- }
686
- }
687
- }
688
- const containerProp = refPath.findParent(
689
- (parent2) => parent2.isObjectProperty() && parent2.parentPath === returnedObjectPath
690
- );
691
- if (!containerProp) {
692
- return;
693
- }
694
- const keyNode = containerProp.get("key");
695
- if (Array.isArray(keyNode)) {
696
- return;
697
- }
698
- const keyValue = keyNode.isLiteral() ? keyNode.isStringLiteral() || keyNode.isNumericLiteral() || keyNode.isBooleanLiteral() ? String(keyNode.node.value) : null : null;
699
- const styleKey = keyNode.isIdentifier() ? keyNode.node.name : keyValue;
700
- if (styleKey) {
701
- detectedStylesWithRt.add({
702
- label: usedLabel,
703
- key: styleKey
704
- });
705
- }
706
- });
707
- });
708
- });
709
- const variants = Array.from(detectedStylesWithVariants);
710
- const theme = Array.from(detectedStylesWithTheme);
711
- const rt = Array.from(detectedStylesWithRt);
712
- return theme.concat(rt).concat(variants).reduce((acc, { key, label }) => {
713
- if (acc[key]) {
714
- acc[key] = [...acc[key], label];
715
- return acc;
716
- }
717
- acc[key] = [label];
718
- return acc;
719
- }, {});
720
- }
721
- function addDependencies(state, styleName, unistyle, detectedDependencies) {
722
- const debugMessage = (deps, unsupportedReason) => {
723
- if (state.opts.debug) {
724
- const mappedDeps = deps.map(
725
- (dep) => Object.keys(UnistyleDependency).find(
726
- (key) => UnistyleDependency[key] === dep
727
- )
728
- ).join(", ");
729
- const style = `${state.filename?.replace(`${state.file.opts.root}/`, "")}: styles.${styleName}`;
730
- if (unsupportedReason) {
731
- console.warn(
732
- `Unistyles \u{1F984}: ${style}: detected [${mappedDeps}], but ${unsupportedReason}. Return an object literal, or a ternary/logical expression of object literals.`
733
- );
734
- return;
735
- }
736
- console.log(`${style}: [${mappedDeps}]`);
737
- }
738
- };
739
- const styleDependencies = detectedDependencies.map(toUnistylesDependency);
740
- if (styleDependencies.length > 0) {
741
- const uniqueDependencies = Array.from(new Set(styleDependencies));
742
- const targets = [];
743
- const unsupported = [];
744
- let unsupportedReturns = 0;
745
- if (t4.isArrowFunctionExpression(unistyle.value) || t4.isFunctionExpression(unistyle.value)) {
746
- if (t4.isBlockStatement(unistyle.value.body)) {
747
- const returnStatements = getReturnStatementsFromBody(unistyle.value.body);
748
- unsupportedReturns = countReturnStatements(unistyle.value.body) - returnStatements.length;
749
- returnStatements.forEach((node) => {
750
- if (t4.isIdentifier(node.argument)) {
751
- node.argument = t4.objectExpression([t4.spreadElement(node.argument)]);
752
- }
753
- if (node.argument) {
754
- collectReturnedObjects(node.argument, targets, unsupported);
755
- }
756
- });
757
- } else {
758
- collectReturnedObjects(unistyle.value.body, targets, unsupported);
759
- }
760
- } else if (t4.isMemberExpression(unistyle.value)) {
761
- unistyle.value = t4.objectExpression([t4.spreadElement(unistyle.value)]);
762
- targets.push(unistyle.value);
763
- } else {
764
- collectReturnedObjects(unistyle.value, targets, unsupported);
765
- }
766
- if (unsupported.length > 0 || unsupportedReturns > 0) {
767
- const reasons = unsupported.map((node) => `${node.type}${node.loc ? ` (line ${node.loc.start.line})` : ""}`).concat(
768
- unsupportedReturns > 0 ? `${unsupportedReturns} return statement(s) inside switch, loop or try blocks` : []
769
- );
770
- const consequence = targets.length > 0 ? "the style may not update when these dependencies change, eg. when one of these values is returned first" : "the style won't update when these dependencies change";
771
- debugMessage(uniqueDependencies, `they can't be attached to: ${reasons.join(", ")}, so ${consequence}`);
772
- } else if (targets.length === 0) {
773
- debugMessage(
774
- uniqueDependencies,
775
- "no returned object was found to attach them to, so the style won't update when these dependencies change"
776
- );
777
- } else {
778
- debugMessage(uniqueDependencies);
779
- }
780
- if (targets.length > 0) {
781
- targets.forEach((target) => {
782
- target.properties.push(
783
- t4.objectProperty(
784
- t4.identifier("uni__dependencies"),
785
- t4.arrayExpression(
786
- uniqueDependencies.filter((dep) => dep !== void 0 && dep !== null).map((dep) => t4.numericLiteral(dep))
787
- )
788
- )
789
- );
790
- });
791
- }
792
- }
793
- }
794
-
795
- // plugin/src/variants.ts
796
- var t5 = __toESM(require("@babel/types"));
797
- var scopedVariants = /* @__PURE__ */ new WeakMap();
798
- function extractVariants(path3, state) {
799
- const maybeVariants = path3.node.body.filter(
800
- (node2) => t5.isExpressionStatement(node2) && t5.isCallExpression(node2.expression) && t5.isMemberExpression(node2.expression.callee)
801
- );
802
- if (maybeVariants.length === 0) {
803
- return;
804
- }
805
- const targetVariant = maybeVariants.find((variant) => {
806
- if (!t5.isExpressionStatement(variant) || !t5.isCallExpression(variant.expression) || !t5.isMemberExpression(variant.expression.callee) || !t5.isIdentifier(variant.expression.callee.object)) {
807
- return false;
808
- }
809
- const calleeName2 = variant.expression.callee.object.name;
810
- return t5.isIdentifier(variant.expression.callee.object, { name: calleeName2 }) && t5.isIdentifier(variant.expression.callee.property, { name: "useVariants" }) && variant.expression.arguments.length === 1;
811
- });
812
- if (!targetVariant) {
813
- return;
814
- }
815
- const node = targetVariant.expression;
816
- if (!t5.isCallExpression(node)) {
817
- return;
818
- }
819
- const callee = node.callee;
820
- if (!t5.isMemberExpression(callee) || !t5.isIdentifier(callee.object)) {
821
- return;
822
- }
823
- const calleeName = callee.object.name;
824
- const binding = path3.scope.getBinding(calleeName);
825
- const existingShadowName = binding && t5.isVariableDeclarator(binding.path.node) ? scopedVariants.get(binding.path.node) : void 0;
826
- const shadowName = existingShadowName ?? path3.scope.generateUidIdentifier(calleeName).name;
827
- const newCallExpression = t5.callExpression(
828
- t5.memberExpression(t5.identifier(shadowName), t5.identifier("useVariants")),
829
- node.arguments
830
- );
831
- const finalDeclarator = t5.variableDeclarator(t5.identifier(calleeName), newCallExpression);
832
- const finalDeclaration = t5.variableDeclaration("const", [finalDeclarator]);
833
- scopedVariants.set(finalDeclarator, shadowName);
834
- const pathIndex = path3.node.body.findIndex((bodyPath) => bodyPath === targetVariant);
835
- const rest = path3.node.body.slice(pathIndex + 1);
836
- const statement = t5.blockStatement([finalDeclaration, ...rest]);
837
- state.file.hasVariants = true;
838
- if (existingShadowName) {
839
- path3.node.body = [...path3.node.body.slice(0, pathIndex), statement];
840
- return;
841
- }
842
- const shadowDeclaration = t5.variableDeclaration("const", [
843
- t5.variableDeclarator(t5.identifier(shadowName), t5.identifier(calleeName))
844
- ]);
845
- path3.node.body = [...path3.node.body.slice(0, pathIndex), shadowDeclaration, statement];
846
- path3.scope.registerDeclaration(path3.get(`body.${pathIndex}`));
847
- }
848
-
849
- // plugin/src/index.ts
850
- function src_default() {
851
- if (process.env.NODE_ENV === "test") {
852
- return {
853
- name: "babel-react-native-unistyles",
854
- visitor: {}
855
- };
856
- }
857
- return {
858
- name: "babel-react-native-unistyles",
859
- visitor: {
860
- Program: {
861
- enter(path3, state) {
862
- if (!state.opts.isMetro && process.env[METRO_UPSTREAM_TRANSFORMER_ENV]) {
863
- throw new Error(
864
- "Unistyles \u{1F984}: react-native-unistyles/metro is enabled in metro.config.js, remove react-native-unistyles/plugin from babel.config.js"
865
- );
866
- }
867
- if (!state.opts.root && !state.opts.isMetro) {
868
- throw new Error(
869
- "Unistyles \u{1F984}: Babel plugin requires `root` option to be set. Please check https://www.unistyl.es/v3/other/babel-plugin#extra-configuration"
870
- );
871
- }
872
- const appRoot = toPlatformPath(import_node_path2.default.join(state.file.opts.root, state.opts.root ?? ""));
873
- if (!state.opts.isMetro && state.file.opts.root === appRoot) {
874
- throw new Error(
875
- "Unistyles \u{1F984}: Root option can't resolve to project root as it will include node_modules folder. Please check https://www.unistyl.es/v3/other/babel-plugin#extra-configuration"
876
- );
877
- }
878
- state.file.replaceWithUnistyles = REPLACE_WITH_UNISTYLES_PATHS.concat(
879
- state.opts.autoProcessPaths ?? []
880
- ).map(toPlatformPath).some((path4) => state.filename?.includes(path4));
881
- state.file.hasAnyUnistyle = false;
882
- state.file.hasUnistylesImport = false;
883
- state.file.addUnistylesRequire = false;
884
- state.file.hasVariants = false;
885
- state.file.styleSheetLocalName = "";
886
- state.file.reactNativeCommonJSName = "";
887
- state.reactNativeImports = {};
888
- state.file.forceProcessing = state.opts.isMetro || (state.filename?.includes(appRoot) ?? false);
889
- path3.traverse({
890
- BlockStatement(blockPath) {
891
- if (isInsideNodeModules(state)) {
892
- return;
893
- }
894
- extractVariants(blockPath, state);
895
- }
896
- });
897
- },
898
- exit(path3, state) {
899
- if (state.opts.isMetro || isInsideNodeModules(state)) {
900
- return;
901
- }
902
- if (state.file.addUnistylesRequire) {
903
- return addUnistylesRequire(path3, state);
904
- }
905
- if (state.file.hasAnyUnistyle || state.file.hasVariants || state.file.replaceWithUnistyles || state.file.forceProcessing) {
906
- addUnistylesImport(path3, state);
907
- }
908
- }
909
- },
910
- FunctionDeclaration(path3, state) {
911
- if (isInsideNodeModules(state)) {
912
- return;
913
- }
914
- const componentName = path3.node.id ? path3.node.id.name : null;
915
- if (componentName) {
916
- state.file.hasVariants = false;
917
- }
918
- },
919
- ClassDeclaration(path3, state) {
920
- if (isInsideNodeModules(state)) {
921
- return;
922
- }
923
- const componentName = path3.node.id ? path3.node.id.name : null;
924
- if (componentName) {
925
- state.file.hasVariants = false;
926
- }
927
- },
928
- VariableDeclaration(path3, state) {
929
- if (isInsideNodeModules(state)) {
930
- return;
931
- }
932
- path3.node.declarations.forEach((declaration) => {
933
- if (t6.isArrowFunctionExpression(declaration.init) || t6.isFunctionExpression(declaration.init)) {
934
- const componentName = declaration.id && t6.isIdentifier(declaration.id) ? declaration.id.name : null;
935
- if (componentName) {
936
- state.file.hasVariants = false;
937
- }
938
- }
939
- });
940
- },
941
- ImportDeclaration(path3, state) {
942
- const exoticImport = state.opts.isMetro ? void 0 : REPLACE_WITH_UNISTYLES_EXOTIC_PATHS.concat(state.opts.autoRemapImports ?? []).find(
943
- (exotic) => state.filename?.includes(exotic.path)
944
- );
945
- if (exoticImport) {
946
- return handleExoticImport(path3, state, exoticImport);
947
- }
948
- if (isInsideNodeModules(state)) {
949
- return;
950
- }
951
- const importSource = path3.node.source.value;
952
- if (importSource.includes("react-native-unistyles")) {
953
- state.file.hasUnistylesImport = true;
954
- path3.node.specifiers.forEach((specifier) => {
955
- if (t6.isImportSpecifier(specifier) && t6.isIdentifier(specifier.imported) && specifier.imported.name === "StyleSheet") {
956
- state.file.styleSheetLocalName = specifier.local.name;
957
- }
958
- });
959
- }
960
- if (state.opts.isMetro) {
961
- return;
962
- }
963
- if (importSource === "react-native") {
964
- path3.node.specifiers.forEach((specifier) => {
965
- if (t6.isImportSpecifier(specifier) && t6.isIdentifier(specifier.imported) && REACT_NATIVE_COMPONENT_NAMES.includes(specifier.imported.name)) {
966
- state.reactNativeImports[specifier.local.name] = specifier.imported.name;
967
- }
968
- });
969
- }
970
- if (importSource === "react-native" || importSource.includes("react-native/Libraries")) {
971
- handleExoticImport(path3, state, NATIVE_COMPONENTS_PATHS);
972
- }
973
- if (!state.file.forceProcessing && Array.isArray(state.opts.autoProcessImports)) {
974
- state.file.forceProcessing = state.opts.autoProcessImports.includes(importSource);
975
- }
976
- },
977
- JSXElement(path3, state) {
978
- if (isInsideNodeModules(state)) {
979
- return;
980
- }
981
- if (hasStringRef(path3)) {
982
- throw new Error("Detected string based ref which is not supported by Unistyles.");
983
- }
984
- },
985
- MemberExpression(path3, state) {
986
- if (state.opts.isMetro || isInsideNodeModules(state)) {
987
- return;
988
- }
989
- if (!state.file.reactNativeCommonJSName || !t6.isIdentifier(path3.node.object)) {
990
- return;
991
- }
992
- if (path3.node.object.name !== state.file.reactNativeCommonJSName || !t6.isIdentifier(path3.node.property)) {
993
- return;
994
- }
995
- if (!REACT_NATIVE_COMPONENT_NAMES.includes(path3.node.property.name)) {
996
- return;
997
- }
998
- if (!state.reactNativeImports[path3.node.property.name]) {
999
- const uniqueId = path3.scope.generateUidIdentifier(`reactNativeUnistyles_${path3.node.property.name}`);
1000
- state.reactNativeImports[path3.node.property.name] = uniqueId.name;
1001
- state.file.addUnistylesRequire = true;
1002
- }
1003
- path3.node.object.name = state.reactNativeImports[path3.node.property.name];
1004
- },
1005
- CallExpression(path3, state) {
1006
- if (isInsideNodeModules(state)) {
1007
- return;
1008
- }
1009
- if (isUnistylesCommonJSRequire(path3, state)) {
1010
- return;
1011
- }
1012
- if (isReactNativeCommonJSRequire(path3, state)) {
1013
- return;
1014
- }
1015
- if (!isUnistylesStyleSheet(path3, state) && !isKindOfStyleSheet(path3, state)) {
1016
- return;
1017
- }
1018
- state.file.hasAnyUnistyle = true;
1019
- const arg = t6.isAssignmentExpression(path3.node.arguments[0]) ? path3.node.arguments[0].right : path3.node.arguments[0];
1020
- if (t6.isObjectExpression(arg)) {
1021
- const detectedDependencies = getStylesDependenciesFromObject(path3);
1022
- if (detectedDependencies) {
1023
- if (t6.isObjectExpression(arg)) {
1024
- arg.properties.forEach((property) => {
1025
- if (!t6.isObjectProperty(property)) {
1026
- return;
1027
- }
1028
- const styleKey = getStyleKeyName(property.key);
1029
- if (styleKey && Object.prototype.hasOwnProperty.call(detectedDependencies, styleKey)) {
1030
- addDependencies(state, styleKey, property, detectedDependencies[styleKey] ?? []);
1031
- }
1032
- });
1033
- }
1034
- }
1035
- }
1036
- if (t6.isArrowFunctionExpression(arg) || t6.isFunctionExpression(arg)) {
1037
- const funcPath = t6.isAssignmentExpression(path3.node.arguments[0]) ? path3.get("arguments.0.right") : path3.get("arguments.0");
1038
- const detectedDependencies = getStylesDependenciesFromFunction(funcPath);
1039
- if (detectedDependencies) {
1040
- const body = t6.isBlockStatement(arg.body) ? arg.body.body.find((statement) => t6.isReturnStatement(statement))?.argument : arg.body;
1041
- if (t6.isObjectExpression(body)) {
1042
- body.properties.forEach((property) => {
1043
- if (!t6.isObjectProperty(property)) {
1044
- return;
1045
- }
1046
- const styleKey = getStyleKeyName(property.key);
1047
- if (styleKey && Object.prototype.hasOwnProperty.call(detectedDependencies, styleKey)) {
1048
- addDependencies(state, styleKey, property, detectedDependencies[styleKey] ?? []);
1049
- }
1050
- });
1051
- }
1052
- }
1053
- }
1054
- }
1055
- }
1056
- };
1057
- }
1058
-
1059
- // metro/src/transformer.ts
1060
- var MAYBE_UNISTYLES = /\.create\s*\(|\.useVariants\s*\(/;
1061
- var createBabelTransformer = ({ upstream, options, unistylesRoot, path: path3 = import_node_path3.default }) => {
1062
- const pluginItem = [src_default, { debug: options.debug, isMetro: true }, "react-native-unistyles"];
1063
- const shouldProcess = ({ filename, src, options: { projectRoot } }) => {
1064
- if (!MAYBE_UNISTYLES.test(src)) {
1065
- return false;
1066
- }
1067
- const filePath = path3.resolve(projectRoot, filename);
1068
- if (filePath.startsWith(`${unistylesRoot}${path3.sep}`)) {
1069
- return false;
1070
- }
1071
- return !filePath.includes(`${path3.sep}node_modules${path3.sep}`) || src.includes("react-native-unistyles");
1072
- };
1073
- const transform2 = (args) => {
1074
- if (!shouldProcess(args)) {
1075
- return upstream.transform(args);
1076
- }
1077
- return upstream.transform({
1078
- ...args,
1079
- plugins: [...args.plugins ?? [], pluginItem]
1080
- });
1081
- };
1082
- const getCacheKey2 = (...args) => upstream.getCacheKey?.(...args) ?? "";
1083
- return { shouldProcess, transform: transform2, getCacheKey: getCacheKey2 };
1084
- };
1085
-
1086
- // metro/src/babel-transformer.ts
1087
- var upstreamPath = process.env[METRO_UPSTREAM_TRANSFORMER_ENV];
1088
- if (!upstreamPath) {
1089
- throw new Error(
1090
- "Unistyles \u{1F984}: react-native-unistyles/metro/babel-transformer can be only set by withUnistylesConfig in metro.config.js"
1091
- );
1092
- }
1093
- var { transform, getCacheKey } = createBabelTransformer({
1094
- upstream: require(upstreamPath),
1095
- options: JSON.parse(process.env[METRO_OPTIONS_ENV] ?? "{}"),
1096
- unistylesRoot: import_node_fs.default.realpathSync(import_node_path4.default.join(__dirname, ".."))
1097
- });
1098
- // Annotate the CommonJS export names for ESM import in node:
1099
- 0 && (module.exports = {
1100
- getCacheKey,
1101
- transform
1102
- });