react-native-unistyles 3.0.0-beta.7 → 3.0.0-beta.8

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 (79) hide show
  1. package/README.md +16 -0
  2. package/android/CMakeLists.txt +2 -1
  3. package/android/src/main/java/com/unistyles/NativePlatform+insets.kt +9 -1
  4. package/cxx/common/Helpers.h +85 -0
  5. package/cxx/core/UnistylesCommitHook.cpp +20 -17
  6. package/cxx/core/UnistylesMountHook.cpp +2 -2
  7. package/cxx/core/UnistylesMountHook.h +1 -4
  8. package/cxx/core/UnistylesRegistry.cpp +8 -6
  9. package/cxx/hybridObjects/HybridShadowRegistry.cpp +2 -5
  10. package/cxx/hybridObjects/HybridStyleSheet.cpp +1 -1
  11. package/cxx/hybridObjects/HybridUnistylesRuntime.cpp +5 -1
  12. package/cxx/parser/Parser.cpp +74 -9
  13. package/cxx/shadowTree/ShadowTrafficController.h +10 -4
  14. package/cxx/shadowTree/ShadowTreeManager.cpp +38 -35
  15. package/lib/commonjs/components/native/ImageBackground.js +26 -30
  16. package/lib/commonjs/components/native/ImageBackground.js.map +1 -1
  17. package/lib/commonjs/components/native/Pressable.native.js +12 -1
  18. package/lib/commonjs/components/native/Pressable.native.js.map +1 -1
  19. package/lib/commonjs/core/createUnistylesElement.native.js +4 -0
  20. package/lib/commonjs/core/createUnistylesElement.native.js.map +1 -1
  21. package/lib/commonjs/core/createUnistylesImageBackground.js +4 -0
  22. package/lib/commonjs/core/createUnistylesImageBackground.js.map +1 -1
  23. package/lib/commonjs/core/useProxifiedUnistyles/useProxifiedUnistyles.js +1 -1
  24. package/lib/commonjs/core/useProxifiedUnistyles/useProxifiedUnistyles.js.map +1 -1
  25. package/lib/commonjs/core/withUnistyles/withUnistyles.native.js +14 -7
  26. package/lib/commonjs/core/withUnistyles/withUnistyles.native.js.map +1 -1
  27. package/lib/commonjs/specs/ShadowRegistry/index.js +1 -1
  28. package/lib/commonjs/specs/ShadowRegistry/index.js.map +1 -1
  29. package/lib/module/components/native/ImageBackground.js +26 -30
  30. package/lib/module/components/native/ImageBackground.js.map +1 -1
  31. package/lib/module/components/native/Pressable.native.js +12 -1
  32. package/lib/module/components/native/Pressable.native.js.map +1 -1
  33. package/lib/module/core/createUnistylesElement.native.js +4 -0
  34. package/lib/module/core/createUnistylesElement.native.js.map +1 -1
  35. package/lib/module/core/createUnistylesImageBackground.js +4 -0
  36. package/lib/module/core/createUnistylesImageBackground.js.map +1 -1
  37. package/lib/module/core/useProxifiedUnistyles/useProxifiedUnistyles.js +1 -1
  38. package/lib/module/core/useProxifiedUnistyles/useProxifiedUnistyles.js.map +1 -1
  39. package/lib/module/core/withUnistyles/withUnistyles.native.js +14 -7
  40. package/lib/module/core/withUnistyles/withUnistyles.native.js.map +1 -1
  41. package/lib/module/specs/ShadowRegistry/index.js +1 -1
  42. package/lib/module/specs/ShadowRegistry/index.js.map +1 -1
  43. package/lib/typescript/src/components/native/ImageBackground.d.ts.map +1 -1
  44. package/lib/typescript/src/components/native/Pressable.native.d.ts.map +1 -1
  45. package/lib/typescript/src/core/createUnistylesElement.native.d.ts.map +1 -1
  46. package/lib/typescript/src/core/createUnistylesImageBackground.d.ts.map +1 -1
  47. package/lib/typescript/src/core/withUnistyles/withUnistyles.native.d.ts.map +1 -1
  48. package/lib/typescript/src/specs/ShadowRegistry/index.d.ts.map +1 -1
  49. package/lib/typescript/src/types/stylesheet.d.ts +1 -1
  50. package/lib/typescript/src/types/stylesheet.d.ts.map +1 -1
  51. package/nitrogen/generated/android/c++/JFunc_void_UnistylesNativeMiniRuntime.hpp +2 -2
  52. package/nitrogen/generated/android/c++/JFunc_void_std__vector_UnistyleDependency__UnistylesNativeMiniRuntime.hpp +2 -2
  53. package/nitrogen/generated/android/c++/JHybridNativePlatformSpec.cpp +19 -19
  54. package/nitrogen/generated/android/kotlin/com/margelo/nitro/unistyles/Func_void_UnistylesNativeMiniRuntime.kt +7 -2
  55. package/nitrogen/generated/android/kotlin/com/margelo/nitro/unistyles/Func_void_std__vector_UnistyleDependency__UnistylesNativeMiniRuntime.kt +7 -2
  56. package/nitrogen/generated/android/kotlin/com/margelo/nitro/unistyles/HybridNativePlatformSpec.kt +22 -22
  57. package/nitrogen/generated/android/unistyles+autolinking.cmake +16 -0
  58. package/nitrogen/generated/ios/Unistyles-Swift-Cxx-Umbrella.hpp +0 -1
  59. package/nitrogen/generated/shared/c++/Dimensions.hpp +1 -0
  60. package/nitrogen/generated/shared/c++/Insets.hpp +1 -0
  61. package/nitrogen/generated/shared/c++/UnistylesCxxMiniRuntime.hpp +1 -0
  62. package/nitrogen/generated/shared/c++/UnistylesNativeMiniRuntime.hpp +1 -0
  63. package/package.json +28 -18
  64. package/plugin/index.d.ts +51 -18
  65. package/plugin/index.js +791 -178
  66. package/src/components/native/ImageBackground.tsx +3 -4
  67. package/src/components/native/Pressable.native.tsx +16 -1
  68. package/src/core/createUnistylesElement.native.tsx +5 -0
  69. package/src/core/createUnistylesImageBackground.tsx +5 -0
  70. package/src/core/useProxifiedUnistyles/useProxifiedUnistyles.ts +1 -1
  71. package/src/core/withUnistyles/withUnistyles.native.tsx +40 -14
  72. package/src/specs/ShadowRegistry/index.ts +1 -0
  73. package/src/types/stylesheet.ts +1 -1
  74. package/plugin/consts.js +0 -63
  75. package/plugin/exotic.js +0 -54
  76. package/plugin/import.js +0 -51
  77. package/plugin/ref.js +0 -11
  78. package/plugin/stylesheet.js +0 -565
  79. package/plugin/variants.js +0 -66
package/plugin/index.js CHANGED
@@ -1,192 +1,805 @@
1
- const { addUnistylesImport, isInsideNodeModules } = require('./import')
2
- const { hasStringRef } = require('./ref')
3
- const { isUnistylesStyleSheet, addStyleSheetTag, isKindOfStyleSheet, getStylesDependenciesFromFunction, addDependencies, getStylesDependenciesFromObject } = require('./stylesheet')
4
- const { extractVariants } = require('./variants')
5
- const { REACT_NATIVE_COMPONENT_NAMES, REPLACE_WITH_UNISTYLES_PATHS, REPLACE_WITH_UNISTYLES_EXOTIC_PATHS, NATIVE_COMPONENTS_PATHS } = require('./consts')
6
- const { handleExoticImport } = require('./exotic')
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);
7
29
 
8
- module.exports = function ({ types: t }) {
9
- return {
10
- name: 'babel-react-native-unistyles',
11
- visitor: {
12
- Program: {
13
- /** @param {import('./index').UnistylesPluginPass} state */
14
- enter(path, state) {
15
- state.file.replaceWithUnistyles = REPLACE_WITH_UNISTYLES_PATHS
16
- .concat(state.opts.autoProcessPaths ?? [])
17
- .some(path => state.filename.includes(path))
18
-
19
- state.file.hasAnyUnistyle = false
20
- state.file.hasUnistylesImport = false
21
- state.file.hasVariants = false
22
- state.file.styleSheetLocalName = ''
23
- state.file.tagNumber = 0
24
- state.reactNativeImports = {}
25
- state.file.forceProcessing = state.opts.autoProcessRoot
26
- ? state.filename.includes(`${state.file.opts.root}/${state.opts.autoProcessRoot}/`)
27
- : false
28
- },
29
- exit(path, state) {
30
- if (isInsideNodeModules(state) && !state.file.replaceWithUnistyles) {
31
- return
32
- }
33
-
34
- if (state.file.hasAnyUnistyle || state.file.hasVariants || state.file.replaceWithUnistyles || state.file.forceProcessing) {
35
- addUnistylesImport(t, path, state)
36
- }
37
- }
38
- },
39
- FunctionDeclaration(path, state) {
40
- if (isInsideNodeModules(state)) {
41
- return
42
- }
43
-
44
- const componentName = path.node.id
45
- ? path.node.id.name
46
- : null
47
-
48
- if (componentName) {
49
- state.file.hasVariants = false
50
- }
51
- },
52
- ClassDeclaration(path, state) {
53
- if (isInsideNodeModules(state)) {
54
- return
55
- }
56
-
57
- const componentName = path.node.id
58
- ? path.node.id.name
59
- : null
60
-
61
- if (componentName) {
62
- state.file.hasVariants = false
63
- }
64
- },
65
- VariableDeclaration(path, state) {
66
- if (isInsideNodeModules(state)) {
67
- return
68
- }
69
-
70
- path.node.declarations.forEach((declaration) => {
71
- if (t.isArrowFunctionExpression(declaration.init) || t.isFunctionExpression(declaration.init)) {
72
- const componentName = declaration.id
73
- ? declaration.id.name
74
- : null
75
-
76
- if (componentName) {
77
- state.file.hasVariants = false
78
- }
79
- }
80
- })
81
- },
82
- /** @param {import('./index').UnistylesPluginPass} state */
83
- ImportDeclaration(path, state) {
84
- const exoticImport = REPLACE_WITH_UNISTYLES_EXOTIC_PATHS
85
- .concat(state.opts.autoRemapImports ?? [])
86
- .find(exotic => state.filename.includes(exotic.path))
87
-
88
- if (exoticImport) {
89
- return handleExoticImport(t, path, state, exoticImport)
90
- }
30
+ // plugin/src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ default: () => index_default
34
+ });
35
+ module.exports = __toCommonJS(index_exports);
36
+ var t6 = __toESM(require("@babel/types"));
91
37
 
92
- if (isInsideNodeModules(state) && !state.file.replaceWithUnistyles) {
93
- return
94
- }
95
-
96
- const importSource = path.node.source.value
97
-
98
- if (importSource.includes('react-native-unistyles')) {
99
- state.file.hasUnistylesImport = true
100
-
101
- path.node.specifiers.forEach(specifier => {
102
- if (specifier.imported && specifier.imported.name === 'StyleSheet') {
103
- state.file.styleSheetLocalName = specifier.local.name
104
- }
105
- })
106
- }
107
-
108
- if (importSource === 'react-native') {
109
- path.node.specifiers.forEach(specifier => {
110
- if (specifier.imported && REACT_NATIVE_COMPONENT_NAMES.includes(specifier.imported.name)) {
111
- state.reactNativeImports[specifier.local.name] = specifier.imported.name
112
- }
113
- })
114
- }
115
-
116
- if (importSource.includes('react-native/Libraries')) {
117
- handleExoticImport(t, path, state, NATIVE_COMPONENTS_PATHS)
118
- }
119
-
120
- if (!state.file.forceProcessing && Array.isArray(state.opts.autoProcessImports)) {
121
- state.file.forceProcessing = state.opts.autoProcessImports.includes(importSource)
122
- }
123
- },
124
- JSXElement(path, state) {
125
- if (isInsideNodeModules(state)) {
126
- return
127
- }
128
-
129
- if (hasStringRef(t, path)) {
130
- throw new Error("Detected string based ref which is not supported by Unistyles.")
131
- }
132
- },
133
- BlockStatement(path, state) {
134
- if (isInsideNodeModules(state)) {
135
- return
136
- }
137
-
138
- extractVariants(t, path, state)
139
- },
140
- CallExpression(path, state) {
141
- if (isInsideNodeModules(state)) {
142
- return
143
- }
38
+ // plugin/src/consts.ts
39
+ var REACT_NATIVE_COMPONENT_NAMES = [
40
+ "ActivityIndicator",
41
+ "View",
42
+ "Text",
43
+ "Image",
44
+ "ImageBackground",
45
+ "KeyboardAvoidingView",
46
+ "Pressable",
47
+ "ScrollView",
48
+ "FlatList",
49
+ "SectionList",
50
+ "Switch",
51
+ "TextInput",
52
+ "RefreshControl",
53
+ "TouchableHighlight",
54
+ "TouchableOpacity",
55
+ "VirtualizedList",
56
+ "Animated"
57
+ // Modal - there is no exposed native handle
58
+ // TouchableWithoutFeedback - can't accept a ref
59
+ ];
60
+ var REPLACE_WITH_UNISTYLES_PATHS = [
61
+ "react-native-reanimated/src/component",
62
+ "react-native-gesture-handler/src/components"
63
+ ];
64
+ var REPLACE_WITH_UNISTYLES_EXOTIC_PATHS = [];
65
+ var NATIVE_COMPONENTS_PATHS = {
66
+ imports: [
67
+ {
68
+ name: "NativeText",
69
+ isDefault: false,
70
+ path: "react-native/Libraries/Text/TextNativeComponent",
71
+ mapTo: "NativeText"
72
+ },
73
+ {
74
+ isDefault: true,
75
+ path: "react-native/Libraries/Components/View/ViewNativeComponent",
76
+ mapTo: "NativeView"
77
+ }
78
+ ]
79
+ };
144
80
 
145
- if (!isUnistylesStyleSheet(t, path, state) && !isKindOfStyleSheet(t, path, state)) {
146
- return
147
- }
81
+ // plugin/src/exotic.ts
82
+ var t = __toESM(require("@babel/types"));
83
+ function handleExoticImport(path, state, exoticImport) {
84
+ const specifiers = path.node.specifiers;
85
+ const source = path.node.source;
86
+ if (path.node.importKind !== "value") {
87
+ return;
88
+ }
89
+ specifiers.forEach((specifier) => {
90
+ for (const rule of exoticImport.imports) {
91
+ const hasMatchingImportType = !rule.isDefault && t.isImportSpecifier(specifier) || rule.isDefault && t.isImportDefaultSpecifier(specifier);
92
+ const hasMatchingImportName = rule.isDefault || !rule.isDefault && rule.name === specifier.local.name;
93
+ const hasMatchingPath = rule.path === source.value;
94
+ if (!hasMatchingImportType || !hasMatchingImportName || !hasMatchingPath) {
95
+ continue;
96
+ }
97
+ if (t.isImportDefaultSpecifier(specifier)) {
98
+ const newImport = t.importDeclaration(
99
+ [t.importDefaultSpecifier(t.identifier(specifier.local.name))],
100
+ t.stringLiteral(
101
+ state.opts.isLocal ? state.file.opts.filename?.split("react-native-unistyles").at(0)?.concat(`react-native-unistyles/components/native/${rule.mapTo}`) ?? "" : `react-native-unistyles/components/native/${rule.mapTo}`
102
+ )
103
+ );
104
+ path.replaceWith(newImport);
105
+ } else {
106
+ const newImport = t.importDeclaration(
107
+ [t.importSpecifier(t.identifier(rule.mapTo), t.identifier(rule.mapTo))],
108
+ t.stringLiteral(
109
+ state.opts.isLocal ? state.file.opts.filename?.split("react-native-unistyles").at(0)?.concat(`react-native-unistyles/components/native/${rule.mapTo}`) ?? "" : `react-native-unistyles/components/native/${rule.mapTo}`
110
+ )
111
+ );
112
+ path.node.specifiers = specifiers.filter((s) => s !== specifier);
113
+ if (path.node.specifiers.length === 0) {
114
+ path.replaceWith(newImport);
115
+ } else {
116
+ path.insertBefore(newImport);
117
+ }
118
+ }
119
+ return;
120
+ }
121
+ });
122
+ }
148
123
 
149
- state.file.hasAnyUnistyle = true
124
+ // plugin/src/import.ts
125
+ var t2 = __toESM(require("@babel/types"));
126
+ function addUnistylesImport(path, state) {
127
+ const localNames = Object.keys(state.reactNativeImports);
128
+ const names = Object.values(state.reactNativeImports);
129
+ const pairs = Object.entries(state.reactNativeImports);
130
+ const nodesToRemove = [];
131
+ path.node.body.forEach((node) => {
132
+ if (t2.isImportDeclaration(node) && node.source.value === "react-native") {
133
+ node.specifiers = node.specifiers.filter((specifier) => !localNames.some((name) => name === specifier.local.name));
134
+ if (node.specifiers.length === 0) {
135
+ nodesToRemove.push(node);
136
+ }
137
+ }
138
+ });
139
+ names.forEach((name) => {
140
+ const rnWebImport = path.node.body.find((node) => t2.isImportDeclaration(node) && node.source.value === `react-native-web/dist/exports/${name}`);
141
+ if (rnWebImport) {
142
+ rnWebImport.specifiers = [];
143
+ }
144
+ });
145
+ pairs.forEach(([localName, name]) => {
146
+ const newImport = t2.importDeclaration(
147
+ [t2.importSpecifier(t2.identifier(localName), t2.identifier(name))],
148
+ t2.stringLiteral(
149
+ state.opts.isLocal ? state.file.opts.filename?.split("react-native-unistyles").at(0)?.concat(`react-native-unistyles/src/components/native/${name}`) ?? "" : `react-native-unistyles/components/native/${name}`
150
+ )
151
+ );
152
+ path.node.body.unshift(newImport);
153
+ });
154
+ nodesToRemove.forEach((node) => path.node.body.splice(path.node.body.indexOf(node), 1));
155
+ }
156
+ function isInsideNodeModules(state) {
157
+ return state.file.opts.filename?.includes("node_modules");
158
+ }
150
159
 
151
- addStyleSheetTag(t, path, state)
160
+ // plugin/src/ref.ts
161
+ var t3 = __toESM(require("@babel/types"));
162
+ function hasStringRef(path) {
163
+ return path.node.openingElement.attributes.find(
164
+ (attr) => t3.isJSXAttribute(attr) && t3.isJSXIdentifier(attr.name, { name: "ref" }) && t3.isStringLiteral(attr.value)
165
+ );
166
+ }
152
167
 
153
- const arg = path.node.arguments[0]
168
+ // plugin/src/stylesheet.ts
169
+ var t4 = __toESM(require("@babel/types"));
170
+ var UnistyleDependency = {
171
+ Theme: 0,
172
+ ThemeName: 1,
173
+ AdaptiveThemes: 2,
174
+ Breakpoints: 3,
175
+ Variants: 4,
176
+ ColorScheme: 5,
177
+ Dimensions: 6,
178
+ Orientation: 7,
179
+ ContentSizeCategory: 8,
180
+ Insets: 9,
181
+ PixelRatio: 10,
182
+ FontScale: 11,
183
+ StatusBar: 12,
184
+ NavigationBar: 13,
185
+ Ime: 14
186
+ };
187
+ function getProperty(property) {
188
+ if (!property) {
189
+ return void 0;
190
+ }
191
+ if (t4.isIdentifier(property)) {
192
+ const prop = property;
193
+ return {
194
+ properties: [prop.name]
195
+ };
196
+ }
197
+ if (t4.isObjectPattern(property)) {
198
+ const prop = property;
199
+ const matchingProperties = prop.properties.flatMap((p) => getProperty(p));
200
+ return {
201
+ properties: matchingProperties.flatMap((properties) => properties?.properties).filter((prop2) => prop2 !== void 0)
202
+ };
203
+ }
204
+ if (t4.isObjectProperty(property) && t4.isIdentifier(property.value)) {
205
+ const prop = property.key;
206
+ return {
207
+ properties: [prop.name]
208
+ };
209
+ }
210
+ if (t4.isObjectProperty(property) && t4.isObjectPattern(property.value)) {
211
+ const matchingProperties = property.value.properties.flatMap((p) => getProperty(p));
212
+ const prop = property.key;
213
+ return {
214
+ parent: prop.name,
215
+ properties: matchingProperties.flatMap((properties) => properties?.properties).filter((prop2) => prop2 !== void 0)
216
+ };
217
+ }
218
+ return void 0;
219
+ }
220
+ function toUnistylesDependency(dependency) {
221
+ switch (dependency) {
222
+ case "theme": {
223
+ return UnistyleDependency.Theme;
224
+ }
225
+ case "themeName": {
226
+ return UnistyleDependency.ThemeName;
227
+ }
228
+ case "adaptiveThemes": {
229
+ return UnistyleDependency.AdaptiveThemes;
230
+ }
231
+ case "breakpoint": {
232
+ return UnistyleDependency.Breakpoints;
233
+ }
234
+ case "colorScheme": {
235
+ return UnistyleDependency.ColorScheme;
236
+ }
237
+ case "screen": {
238
+ return UnistyleDependency.Dimensions;
239
+ }
240
+ case "isPortrait":
241
+ case "isLandscape": {
242
+ return UnistyleDependency.Orientation;
243
+ }
244
+ case "contentSizeCategory": {
245
+ return UnistyleDependency.ContentSizeCategory;
246
+ }
247
+ case "ime": {
248
+ return UnistyleDependency.Ime;
249
+ }
250
+ case "insets": {
251
+ return UnistyleDependency.Insets;
252
+ }
253
+ case "pixelRatio": {
254
+ return UnistyleDependency.PixelRatio;
255
+ }
256
+ case "fontScale": {
257
+ return UnistyleDependency.FontScale;
258
+ }
259
+ case "statusBar": {
260
+ return UnistyleDependency.StatusBar;
261
+ }
262
+ case "navigationBar": {
263
+ return UnistyleDependency.NavigationBar;
264
+ }
265
+ case "variants": {
266
+ return UnistyleDependency.Variants;
267
+ }
268
+ default:
269
+ return null;
270
+ }
271
+ }
272
+ function getReturnStatementsFromBody(node, results = []) {
273
+ if (t4.isReturnStatement(node)) {
274
+ results.push(node);
275
+ }
276
+ if (t4.isBlockStatement(node)) {
277
+ node.body.forEach((child) => getReturnStatementsFromBody(child, results));
278
+ }
279
+ if (t4.isIfStatement(node)) {
280
+ getReturnStatementsFromBody(node.consequent, results);
281
+ if (node.alternate) {
282
+ getReturnStatementsFromBody(node.alternate, results);
283
+ }
284
+ }
285
+ return results;
286
+ }
287
+ function stringToUniqueId(str) {
288
+ let hash = 0;
289
+ for (let i = 0; i < str.length; i++) {
290
+ hash = (hash << 5) - hash + str.charCodeAt(i);
291
+ hash |= 0;
292
+ }
293
+ const absHash = Math.abs(hash);
294
+ return absHash % 1e9;
295
+ }
296
+ function isUnistylesStyleSheet(path, state) {
297
+ const { callee } = path.node;
298
+ if (t4.isMemberExpression(callee) && t4.isIdentifier(callee.property)) {
299
+ return callee.property.name === "create" && t4.isIdentifier(callee.object) && callee.object.name === state.file.styleSheetLocalName;
300
+ }
301
+ return false;
302
+ }
303
+ function isKindOfStyleSheet(path, state) {
304
+ if (!state.file.forceProcessing && !state.file.hasUnistylesImport) {
305
+ return false;
306
+ }
307
+ const { callee } = path.node;
308
+ return t4.isMemberExpression(callee) && t4.isIdentifier(callee.property) && callee.property.name === "create" && t4.isIdentifier(callee.object);
309
+ }
310
+ function addStyleSheetTag(path, state) {
311
+ const str = state.filename?.replace(state.cwd, "") ?? "";
312
+ const uniqueId = stringToUniqueId(str) + ++state.file.tagNumber;
313
+ path.node.arguments.push(t4.numericLiteral(uniqueId));
314
+ }
315
+ function getStylesDependenciesFromObject(path) {
316
+ const detectedStylesWithVariants = /* @__PURE__ */ new Set();
317
+ const stylesheet = path.node.arguments[0];
318
+ if (t4.isObjectExpression(stylesheet)) {
319
+ stylesheet?.properties.forEach((property) => {
320
+ if (!t4.isObjectProperty(property) || !t4.isIdentifier(property.key)) {
321
+ return;
322
+ }
323
+ if (t4.isObjectProperty(property)) {
324
+ if (t4.isObjectExpression(property.value)) {
325
+ property.value.properties.forEach((innerProp) => {
326
+ if (t4.isObjectProperty(innerProp) && t4.isIdentifier(innerProp.key) && t4.isIdentifier(property.key) && innerProp.key.name === "variants") {
327
+ detectedStylesWithVariants.add({
328
+ label: "variants",
329
+ key: property.key.name
330
+ });
331
+ }
332
+ });
333
+ }
334
+ }
335
+ if (t4.isArrowFunctionExpression(property.value)) {
336
+ if (t4.isObjectExpression(property.value.body)) {
337
+ property.value.body.properties.forEach((innerProp) => {
338
+ if (t4.isObjectProperty(innerProp) && t4.isIdentifier(innerProp.key) && t4.isIdentifier(property.key) && innerProp.key.name === "variants") {
339
+ detectedStylesWithVariants.add({
340
+ label: "variants",
341
+ key: property.key.name
342
+ });
343
+ }
344
+ });
345
+ }
346
+ }
347
+ });
348
+ }
349
+ const variants = Array.from(detectedStylesWithVariants);
350
+ return variants.reduce((acc, { key, label }) => {
351
+ if (acc[key]) {
352
+ acc[key] = [
353
+ ...acc[key],
354
+ label
355
+ ];
356
+ return acc;
357
+ }
358
+ acc[key] = [label];
359
+ return acc;
360
+ }, {});
361
+ }
362
+ function getStylesDependenciesFromFunction(path) {
363
+ const funcPath = path.get("arguments.0");
364
+ if (!funcPath) {
365
+ return;
366
+ }
367
+ if (Array.isArray(funcPath)) {
368
+ return;
369
+ }
370
+ if (!t4.isFunctionExpression(funcPath.node) && !t4.isArrowFunctionExpression(funcPath.node)) {
371
+ return;
372
+ }
373
+ const params = funcPath.node.params;
374
+ const [themeParam, rtParam] = params;
375
+ const themeNames = [];
376
+ if (t4.isObjectPattern(themeParam)) {
377
+ for (const prop of themeParam.properties) {
378
+ const property = getProperty(prop);
379
+ if (property) {
380
+ themeNames.push(property);
381
+ }
382
+ }
383
+ }
384
+ if (t4.isIdentifier(themeParam)) {
385
+ themeNames.push({
386
+ properties: [themeParam.name]
387
+ });
388
+ }
389
+ const rtNames = [];
390
+ if (t4.isObjectPattern(rtParam)) {
391
+ for (const prop of rtParam.properties) {
392
+ const property = getProperty(prop);
393
+ if (property) {
394
+ rtNames.push(property);
395
+ }
396
+ }
397
+ }
398
+ if (t4.isIdentifier(rtParam)) {
399
+ rtNames.push({
400
+ properties: [rtParam.name]
401
+ });
402
+ }
403
+ let returnedObjectPath = null;
404
+ if (t4.isObjectExpression(funcPath.node.body)) {
405
+ returnedObjectPath = funcPath.get("body");
406
+ } else {
407
+ funcPath.traverse({
408
+ ReturnStatement(retPath) {
409
+ if (!returnedObjectPath && retPath.get("argument").isObjectExpression()) {
410
+ const argumentPath = retPath.get("argument");
411
+ if (argumentPath.isObjectExpression()) {
412
+ returnedObjectPath = argumentPath;
413
+ }
414
+ }
415
+ }
416
+ });
417
+ }
418
+ if (!returnedObjectPath) {
419
+ return;
420
+ }
421
+ const detectedStylesWithVariants = /* @__PURE__ */ new Set();
422
+ const properties = returnedObjectPath.get("properties");
423
+ properties.forEach((propPath) => {
424
+ const stylePath = propPath.get("key");
425
+ if (Array.isArray(stylePath)) {
426
+ return;
427
+ }
428
+ if (!stylePath.isIdentifier()) {
429
+ return;
430
+ }
431
+ const styleKey = stylePath.node.name;
432
+ const valuePath = propPath.get("value");
433
+ if (Array.isArray(valuePath)) {
434
+ return;
435
+ }
436
+ if (valuePath.isObjectExpression()) {
437
+ const hasVariants = valuePath.get("properties").some((innerProp) => {
438
+ const innerKey = innerProp.get("key");
439
+ if (Array.isArray(innerKey)) {
440
+ return;
441
+ }
442
+ return innerKey.isIdentifier() && innerKey.node.name === "variants";
443
+ });
444
+ if (hasVariants) {
445
+ detectedStylesWithVariants.add({
446
+ label: "variants",
447
+ key: styleKey
448
+ });
449
+ }
450
+ }
451
+ if (valuePath.isArrowFunctionExpression()) {
452
+ if (t4.isObjectExpression(valuePath.node.body)) {
453
+ const hasVariants = valuePath.node.body.properties.some((innerProp) => {
454
+ return t4.isObjectProperty(innerProp) && t4.isIdentifier(innerProp.key) && innerProp.key.name === "variants";
455
+ });
456
+ if (hasVariants) {
457
+ detectedStylesWithVariants.add({
458
+ label: "variants",
459
+ key: styleKey
460
+ });
461
+ }
462
+ }
463
+ }
464
+ });
465
+ const detectedStylesWithTheme = /* @__PURE__ */ new Set();
466
+ themeNames.forEach(({ properties: properties2 }) => {
467
+ properties2.forEach((property) => {
468
+ const binding = funcPath.scope.getBinding(property);
469
+ if (!binding) {
470
+ return;
471
+ }
472
+ binding.referencePaths.forEach((refPath) => {
473
+ const containerProp = refPath.findParent((parent) => parent.isObjectProperty() && parent.parentPath === returnedObjectPath);
474
+ if (!containerProp) {
475
+ return;
476
+ }
477
+ const keyNode = containerProp.get("key");
478
+ if (Array.isArray(keyNode)) {
479
+ return;
480
+ }
481
+ const keyValue = keyNode.isLiteral() ? keyNode.isStringLiteral() || keyNode.isNumericLiteral() || keyNode.isBooleanLiteral() ? String(keyNode.node.value) : null : null;
482
+ const styleKey = keyNode.isIdentifier() ? keyNode.node.name : keyValue;
483
+ if (styleKey) {
484
+ detectedStylesWithTheme.add({
485
+ label: "theme",
486
+ key: styleKey
487
+ });
488
+ }
489
+ });
490
+ });
491
+ });
492
+ const detectedStylesWithRt = /* @__PURE__ */ new Set();
493
+ const localRtName = t4.isIdentifier(rtParam) ? rtParam.name : void 0;
494
+ rtNames.forEach(({ properties: properties2, parent }) => {
495
+ properties2.forEach((property) => {
496
+ const rtBinding = funcPath.scope.getBinding(property);
497
+ if (!rtBinding) {
498
+ return;
499
+ }
500
+ const isValidDependency = Boolean(toUnistylesDependency(property));
501
+ let validRtName = property;
502
+ if (!isValidDependency && (!localRtName || localRtName && localRtName !== property)) {
503
+ if (!parent) {
504
+ return;
505
+ }
506
+ if (!toUnistylesDependency(parent)) {
507
+ return;
508
+ }
509
+ validRtName = parent;
510
+ }
511
+ rtBinding.referencePaths.forEach((refPath) => {
512
+ let usedLabel = validRtName;
513
+ if (refPath.parentPath?.isMemberExpression() && refPath.parentPath.get("object") === refPath) {
514
+ const memberExpr = refPath.parentPath;
515
+ const propPath = memberExpr.get("property");
516
+ if (propPath.isIdentifier()) {
517
+ if (localRtName) {
518
+ usedLabel = propPath.node.name;
519
+ }
520
+ if (usedLabel === "insets" && memberExpr.parentPath.isMemberExpression() && memberExpr.parentPath.get("object") === memberExpr) {
521
+ const secondPropPath = memberExpr.parentPath.get("property");
522
+ if (secondPropPath.isIdentifier() && secondPropPath.node.name === "ime") {
523
+ usedLabel = "ime";
524
+ }
525
+ }
526
+ if (usedLabel === "insets" && (memberExpr.parentPath.isBinaryExpression() || memberExpr.parentPath.isLogicalExpression())) {
527
+ const secondPropPath = memberExpr.node.property;
528
+ if (t4.isIdentifier(secondPropPath) && secondPropPath.name === "ime") {
529
+ usedLabel = "ime";
530
+ }
531
+ }
532
+ }
533
+ }
534
+ const containerProp = refPath.findParent((parent2) => parent2.isObjectProperty() && parent2.parentPath === returnedObjectPath);
535
+ if (!containerProp) {
536
+ return;
537
+ }
538
+ const keyNode = containerProp.get("key");
539
+ if (Array.isArray(keyNode)) {
540
+ return;
541
+ }
542
+ const keyValue = keyNode.isLiteral() ? keyNode.isStringLiteral() || keyNode.isNumericLiteral() || keyNode.isBooleanLiteral() ? String(keyNode.node.value) : null : null;
543
+ const styleKey = keyNode.isIdentifier() ? keyNode.node.name : keyValue;
544
+ if (styleKey) {
545
+ detectedStylesWithRt.add({
546
+ label: usedLabel,
547
+ key: styleKey
548
+ });
549
+ }
550
+ });
551
+ });
552
+ });
553
+ const variants = Array.from(detectedStylesWithVariants);
554
+ const theme = Array.from(detectedStylesWithTheme);
555
+ const rt = Array.from(detectedStylesWithRt);
556
+ return theme.concat(rt).concat(variants).reduce((acc, { key, label }) => {
557
+ if (acc[key]) {
558
+ acc[key] = [
559
+ ...acc[key],
560
+ label
561
+ ];
562
+ return acc;
563
+ }
564
+ acc[key] = [label];
565
+ return acc;
566
+ }, {});
567
+ }
568
+ function addDependencies(state, styleName, unistyle, detectedDependencies) {
569
+ const debugMessage = (deps) => {
570
+ if (state.opts.debug) {
571
+ const mappedDeps = deps.map((dep) => Object.keys(UnistyleDependency).find((key) => UnistyleDependency[key] === dep)).join(", ");
572
+ console.log(`${state.filename?.replace(`${state.file.opts.root}/`, "")}: styles.${styleName}: [${mappedDeps}]`);
573
+ }
574
+ };
575
+ const styleDependencies = detectedDependencies.map(toUnistylesDependency);
576
+ if (styleDependencies.length > 0) {
577
+ const uniqueDependencies = Array.from(new Set(styleDependencies));
578
+ debugMessage(uniqueDependencies);
579
+ let targets = [];
580
+ if (t4.isArrowFunctionExpression(unistyle.value) || t4.isFunctionExpression(unistyle.value)) {
581
+ if (t4.isObjectExpression(unistyle.value.body)) {
582
+ targets.push(unistyle.value.body);
583
+ }
584
+ if (t4.isBlockStatement(unistyle.value.body)) {
585
+ targets = getReturnStatementsFromBody(unistyle.value.body).map((node) => {
586
+ if (t4.isIdentifier(node.argument)) {
587
+ node.argument = t4.objectExpression([
588
+ t4.spreadElement(node.argument)
589
+ ]);
590
+ }
591
+ return node.argument;
592
+ }).filter((node) => t4.isObjectExpression(node));
593
+ }
594
+ }
595
+ if (t4.isObjectExpression(unistyle.value)) {
596
+ targets.push(unistyle.value);
597
+ }
598
+ if (t4.isMemberExpression(unistyle.value)) {
599
+ unistyle.value = t4.objectExpression([t4.spreadElement(unistyle.value)]);
600
+ targets.push(unistyle.value);
601
+ }
602
+ if (targets.length > 0) {
603
+ targets.forEach((target) => {
604
+ target.properties.push(
605
+ t4.objectProperty(
606
+ t4.identifier("uni__dependencies"),
607
+ t4.arrayExpression(uniqueDependencies.filter((dep) => dep !== void 0).map((dep) => t4.numericLiteral(dep)))
608
+ )
609
+ );
610
+ });
611
+ }
612
+ }
613
+ }
154
614
 
155
- // Object passed to StyleSheet.create (may contain variants)
156
- if (t.isObjectExpression(arg)) {
157
- const detectedDependencies = getStylesDependenciesFromObject(t, path)
615
+ // plugin/src/variants.ts
616
+ var t5 = __toESM(require("@babel/types"));
617
+ function extractVariants(path, state) {
618
+ const maybeVariants = path.node.body.filter((node2) => t5.isExpressionStatement(node2) && t5.isCallExpression(node2.expression) && t5.isMemberExpression(node2.expression.callee));
619
+ if (maybeVariants.length === 0) {
620
+ return;
621
+ }
622
+ const targetVariant = maybeVariants.find((variant) => {
623
+ if (!t5.isExpressionStatement(variant) || !t5.isCallExpression(variant.expression) || !t5.isMemberExpression(variant.expression.callee) || !t5.isIdentifier(variant.expression.callee.object)) {
624
+ return false;
625
+ }
626
+ const calleeName2 = variant.expression.callee.object.name;
627
+ return t5.isIdentifier(variant.expression.callee.object, { name: calleeName2 }) && t5.isIdentifier(variant.expression.callee.property, { name: "useVariants" }) && variant.expression.arguments.length === 1;
628
+ });
629
+ if (!targetVariant) {
630
+ return;
631
+ }
632
+ const node = targetVariant.expression;
633
+ if (!t5.isCallExpression(node)) {
634
+ return;
635
+ }
636
+ const callee = node.callee;
637
+ if (!t5.isMemberExpression(callee) || !t5.isIdentifier(callee.object)) {
638
+ return;
639
+ }
640
+ const calleeName = callee.object.name;
641
+ const newUniqueName = path.scope.generateUidIdentifier(calleeName);
642
+ const shadowDeclaration = t5.variableDeclaration("const", [
643
+ t5.variableDeclarator(newUniqueName, t5.identifier(calleeName))
644
+ ]);
645
+ const newCallExpression = t5.callExpression(
646
+ t5.memberExpression(t5.identifier(newUniqueName.name), t5.identifier("useVariants")),
647
+ node.arguments
648
+ );
649
+ const finalDeclaration = t5.variableDeclaration("const", [
650
+ t5.variableDeclarator(t5.identifier(calleeName), newCallExpression)
651
+ ]);
652
+ const pathIndex = path.node.body.findIndex((bodyPath) => bodyPath === targetVariant);
653
+ const rest = path.node.body.slice(pathIndex + 1);
654
+ const statement = t5.blockStatement([
655
+ finalDeclaration,
656
+ ...rest
657
+ ]);
658
+ path.node.body = [
659
+ ...path.node.body.slice(0, pathIndex),
660
+ shadowDeclaration,
661
+ statement
662
+ ];
663
+ state.file.hasVariants = true;
664
+ }
158
665
 
159
- if (detectedDependencies) {
160
- if (t.isObjectExpression(arg)) {
161
- arg.properties.forEach(property => {
162
- if (detectedDependencies.hasOwnProperty(property.key.name)) {
163
- addDependencies(t, state, property.key.name, property, detectedDependencies[property.key.name])
164
- }
165
- })
166
- }
167
- }
666
+ // plugin/src/index.ts
667
+ function index_default() {
668
+ return {
669
+ name: "babel-react-native-unistyles",
670
+ visitor: {
671
+ Program: {
672
+ enter(_, state) {
673
+ state.file.replaceWithUnistyles = REPLACE_WITH_UNISTYLES_PATHS.concat(state.opts.autoProcessPaths ?? []).some((path) => state.filename?.includes(path));
674
+ state.file.hasAnyUnistyle = false;
675
+ state.file.hasUnistylesImport = false;
676
+ state.file.hasVariants = false;
677
+ state.file.styleSheetLocalName = "";
678
+ state.file.tagNumber = 0;
679
+ state.reactNativeImports = {};
680
+ state.file.forceProcessing = state.opts.autoProcessRoot && state.filename ? state.filename.includes(`${state.file.opts.root}/${state.opts.autoProcessRoot}/`) : false;
681
+ },
682
+ exit(path, state) {
683
+ if (isInsideNodeModules(state) && !state.file.replaceWithUnistyles) {
684
+ return;
685
+ }
686
+ if (state.file.hasAnyUnistyle || state.file.hasVariants || state.file.replaceWithUnistyles || state.file.forceProcessing) {
687
+ addUnistylesImport(path, state);
688
+ }
689
+ }
690
+ },
691
+ FunctionDeclaration(path, state) {
692
+ if (isInsideNodeModules(state)) {
693
+ return;
694
+ }
695
+ const componentName = path.node.id ? path.node.id.name : null;
696
+ if (componentName) {
697
+ state.file.hasVariants = false;
698
+ }
699
+ },
700
+ ClassDeclaration(path, state) {
701
+ if (isInsideNodeModules(state)) {
702
+ return;
703
+ }
704
+ const componentName = path.node.id ? path.node.id.name : null;
705
+ if (componentName) {
706
+ state.file.hasVariants = false;
707
+ }
708
+ },
709
+ VariableDeclaration(path, state) {
710
+ if (isInsideNodeModules(state)) {
711
+ return;
712
+ }
713
+ path.node.declarations.forEach((declaration) => {
714
+ if (t6.isArrowFunctionExpression(declaration.init) || t6.isFunctionExpression(declaration.init)) {
715
+ const componentName = declaration.id && t6.isIdentifier(declaration.id) ? declaration.id.name : null;
716
+ if (componentName) {
717
+ state.file.hasVariants = false;
718
+ }
719
+ }
720
+ });
721
+ },
722
+ ImportDeclaration(path, state) {
723
+ const exoticImport = REPLACE_WITH_UNISTYLES_EXOTIC_PATHS.concat(state.opts.autoRemapImports ?? []).find((exotic) => state.filename?.includes(exotic.path));
724
+ if (exoticImport) {
725
+ return handleExoticImport(path, state, exoticImport);
726
+ }
727
+ if (isInsideNodeModules(state) && !state.file.replaceWithUnistyles) {
728
+ return;
729
+ }
730
+ const importSource = path.node.source.value;
731
+ if (importSource.includes("react-native-unistyles")) {
732
+ state.file.hasUnistylesImport = true;
733
+ path.node.specifiers.forEach((specifier) => {
734
+ if (t6.isImportSpecifier(specifier) && t6.isIdentifier(specifier.imported) && specifier.imported.name === "StyleSheet") {
735
+ state.file.styleSheetLocalName = specifier.local.name;
736
+ }
737
+ });
738
+ }
739
+ if (importSource === "react-native") {
740
+ path.node.specifiers.forEach((specifier) => {
741
+ if (t6.isImportSpecifier(specifier) && t6.isIdentifier(specifier.imported) && REACT_NATIVE_COMPONENT_NAMES.includes(specifier.imported.name)) {
742
+ state.reactNativeImports[specifier.local.name] = specifier.imported.name;
743
+ }
744
+ });
745
+ }
746
+ if (importSource.includes("react-native/Libraries")) {
747
+ handleExoticImport(path, state, NATIVE_COMPONENTS_PATHS);
748
+ }
749
+ if (!state.file.forceProcessing && Array.isArray(state.opts.autoProcessImports)) {
750
+ state.file.forceProcessing = state.opts.autoProcessImports.includes(importSource);
751
+ }
752
+ },
753
+ JSXElement(path, state) {
754
+ if (isInsideNodeModules(state)) {
755
+ return;
756
+ }
757
+ if (hasStringRef(path)) {
758
+ throw new Error("Detected string based ref which is not supported by Unistyles.");
759
+ }
760
+ },
761
+ BlockStatement(path, state) {
762
+ if (isInsideNodeModules(state)) {
763
+ return;
764
+ }
765
+ extractVariants(path, state);
766
+ },
767
+ CallExpression(path, state) {
768
+ if (isInsideNodeModules(state)) {
769
+ return;
770
+ }
771
+ if (!isUnistylesStyleSheet(path, state) && !isKindOfStyleSheet(path, state)) {
772
+ return;
773
+ }
774
+ state.file.hasAnyUnistyle = true;
775
+ addStyleSheetTag(path, state);
776
+ const arg = path.node.arguments[0];
777
+ if (t6.isObjectExpression(arg)) {
778
+ const detectedDependencies = getStylesDependenciesFromObject(path);
779
+ if (detectedDependencies) {
780
+ if (t6.isObjectExpression(arg)) {
781
+ arg.properties.forEach((property) => {
782
+ if (t6.isObjectProperty(property) && t6.isIdentifier(property.key) && Object.prototype.hasOwnProperty.call(detectedDependencies, property.key.name)) {
783
+ addDependencies(state, property.key.name, property, detectedDependencies[property.key.name] ?? []);
168
784
  }
169
-
170
- // Function passed to StyleSheet.create (e.g., theme => ({ container: {} }))
171
- if (t.isArrowFunctionExpression(arg) || t.isFunctionExpression(arg)) {
172
- const detectedDependencies = getStylesDependenciesFromFunction(t, path)
173
-
174
- if (detectedDependencies) {
175
- const body = t.isBlockStatement(arg.body)
176
- ? arg.body.body.find(statement => t.isReturnStatement(statement)).argument
177
- : arg.body
178
-
179
- // Ensure the function body returns an object
180
- if (t.isObjectExpression(body)) {
181
- body.properties.forEach(property => {
182
- if (detectedDependencies.hasOwnProperty(property.key.name)) {
183
- addDependencies(t, state, property.key.name, property, detectedDependencies[property.key.name])
184
- }
185
- })
186
- }
187
- }
785
+ });
786
+ }
787
+ }
788
+ }
789
+ if (t6.isArrowFunctionExpression(arg) || t6.isFunctionExpression(arg)) {
790
+ const detectedDependencies = getStylesDependenciesFromFunction(path);
791
+ if (detectedDependencies) {
792
+ const body = t6.isBlockStatement(arg.body) ? arg.body.body.find((statement) => t6.isReturnStatement(statement))?.argument : arg.body;
793
+ if (t6.isObjectExpression(body)) {
794
+ body.properties.forEach((property) => {
795
+ if (t6.isObjectProperty(property) && t6.isIdentifier(property.key) && Object.prototype.hasOwnProperty.call(detectedDependencies, property.key.name)) {
796
+ addDependencies(state, property.key.name, property, detectedDependencies[property.key.name] ?? []);
188
797
  }
798
+ });
189
799
  }
800
+ }
190
801
  }
802
+ }
191
803
  }
804
+ };
192
805
  }