react-native-unistyles 3.0.0-alpha.6 → 3.0.0-alpha.7

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 (38) hide show
  1. package/cxx/common/Helpers.h +24 -0
  2. package/cxx/core/HostStyle.cpp +4 -5
  3. package/cxx/core/HostStyle.h +4 -0
  4. package/cxx/core/StyleSheet.h +0 -1
  5. package/cxx/core/StyleSheetRegistry.cpp +9 -17
  6. package/cxx/core/StyleSheetRegistry.h +3 -4
  7. package/cxx/core/UnistyleData.h +22 -0
  8. package/cxx/core/UnistylesCommitHook.cpp +4 -1
  9. package/cxx/core/UnistylesRegistry.cpp +33 -49
  10. package/cxx/core/UnistylesRegistry.h +8 -8
  11. package/cxx/hybridObjects/HybridShadowRegistry.cpp +4 -3
  12. package/cxx/hybridObjects/HybridStyleSheet.cpp +8 -11
  13. package/cxx/parser/Parser.cpp +41 -57
  14. package/cxx/parser/Parser.h +8 -13
  15. package/lib/commonjs/specs/ShadowRegistry/index.js +2 -2
  16. package/lib/commonjs/specs/ShadowRegistry/index.js.map +1 -1
  17. package/lib/commonjs/specs/StyleSheet/index.js.map +1 -1
  18. package/lib/module/specs/ShadowRegistry/index.js +2 -2
  19. package/lib/module/specs/ShadowRegistry/index.js.map +1 -1
  20. package/lib/module/specs/StyleSheet/index.js.map +1 -1
  21. package/lib/typescript/example/App.d.ts.map +1 -1
  22. package/lib/typescript/example/Typography.d.ts +11 -0
  23. package/lib/typescript/example/Typography.d.ts.map +1 -0
  24. package/lib/typescript/src/specs/ShadowRegistry/index.d.ts +2 -2
  25. package/lib/typescript/src/specs/ShadowRegistry/index.d.ts.map +1 -1
  26. package/lib/typescript/src/specs/StyleSheet/index.d.ts +2 -1
  27. package/lib/typescript/src/specs/StyleSheet/index.d.ts.map +1 -1
  28. package/package.json +1 -1
  29. package/plugin/__tests__/dependencies.spec.js +181 -103
  30. package/plugin/__tests__/ref.spec.js +222 -158
  31. package/plugin/__tests__/stylesheet.spec.js +148 -55
  32. package/plugin/index.js +22 -11
  33. package/plugin/ref.js +35 -7
  34. package/plugin/style.js +5 -22
  35. package/plugin/stylesheet.js +44 -1
  36. package/plugin/variants.js +33 -0
  37. package/src/specs/ShadowRegistry/index.ts +4 -4
  38. package/src/specs/StyleSheet/index.ts +3 -1
@@ -41,7 +41,7 @@ pluginTester({
41
41
  <View
42
42
  style={styles.container}
43
43
  ref={ref => {
44
- UnistylesShadowRegistry.add(ref, styles.container)
44
+ UnistylesShadowRegistry.add(ref, styles.container, undefined)
45
45
  return () => UnistylesShadowRegistry.remove(ref, styles.container)
46
46
  }}
47
47
  >
@@ -50,11 +50,14 @@ pluginTester({
50
50
  )
51
51
  }
52
52
 
53
- const styles = StyleSheet.create({
54
- container: {
55
- backgroundColor: 'red'
56
- }
57
- })
53
+ const styles = StyleSheet.create(
54
+ {
55
+ container: {
56
+ backgroundColor: 'red'
57
+ }
58
+ },
59
+ 793953373
60
+ )
58
61
  `
59
62
  },
60
63
  {
@@ -85,7 +88,7 @@ pluginTester({
85
88
  <View
86
89
  style={styles.container}
87
90
  ref={ref => {
88
- UnistylesShadowRegistry.add(ref, styles.container)
91
+ UnistylesShadowRegistry.add(ref, styles.container, undefined)
89
92
  return () => UnistylesShadowRegistry.remove(ref, styles.container)
90
93
  }}
91
94
  >
@@ -94,12 +97,15 @@ pluginTester({
94
97
  )
95
98
  }
96
99
 
97
- const styles = StyleSheet.create(theme => ({
98
- container: {
99
- backgroundColor: theme.colors.background,
100
- uni__dependencies: [0]
101
- }
102
- }))
100
+ const styles = StyleSheet.create(
101
+ theme => ({
102
+ container: {
103
+ backgroundColor: theme.colors.background,
104
+ uni__dependencies: [0]
105
+ }
106
+ }),
107
+ 793953373
108
+ )
103
109
  `
104
110
  },
105
111
  {
@@ -130,7 +136,7 @@ pluginTester({
130
136
  <View
131
137
  style={styles.container}
132
138
  ref={ref => {
133
- UnistylesShadowRegistry.add(ref, styles.container)
139
+ UnistylesShadowRegistry.add(ref, styles.container, undefined)
134
140
  return () => UnistylesShadowRegistry.remove(ref, styles.container)
135
141
  }}
136
142
  >
@@ -139,12 +145,15 @@ pluginTester({
139
145
  )
140
146
  }
141
147
 
142
- const styles = ST.create(theme => ({
143
- container: {
144
- backgroundColor: theme.colors.background,
145
- uni__dependencies: [0]
146
- }
147
- }))
148
+ const styles = ST.create(
149
+ theme => ({
150
+ container: {
151
+ backgroundColor: theme.colors.background,
152
+ uni__dependencies: [0]
153
+ }
154
+ }),
155
+ 793953373
156
+ )
148
157
  `
149
158
  },
150
159
  {
@@ -176,7 +185,7 @@ pluginTester({
176
185
  <View
177
186
  style={styles.container}
178
187
  ref={ref => {
179
- UnistylesShadowRegistry.add(ref, styles.container)
188
+ UnistylesShadowRegistry.add(ref, styles.container, undefined)
180
189
  return () => UnistylesShadowRegistry.remove(ref, styles.container)
181
190
  }}
182
191
  >
@@ -185,13 +194,16 @@ pluginTester({
185
194
  )
186
195
  }
187
196
 
188
- const styles = StyleSheet.create({
189
- container: {
190
- backgroundColor: 'red',
191
- variants: {},
192
- uni__dependencies: [4]
193
- }
194
- })
197
+ const styles = StyleSheet.create(
198
+ {
199
+ container: {
200
+ backgroundColor: 'red',
201
+ variants: {},
202
+ uni__dependencies: [4]
203
+ }
204
+ },
205
+ 793953373
206
+ )
195
207
  `
196
208
  },
197
209
  {
@@ -223,7 +235,7 @@ pluginTester({
223
235
  <View
224
236
  style={styles.container}
225
237
  ref={ref => {
226
- UnistylesShadowRegistry.add(ref, styles.container)
238
+ UnistylesShadowRegistry.add(ref, styles.container, undefined)
227
239
  return () => UnistylesShadowRegistry.remove(ref, styles.container)
228
240
  }}
229
241
  >
@@ -232,13 +244,16 @@ pluginTester({
232
244
  )
233
245
  }
234
246
 
235
- const styles = StyleSheet.create({
236
- container: () => ({
237
- backgroundColor: 'red',
238
- variants: {},
239
- uni__dependencies: [4]
240
- })
241
- })
247
+ const styles = StyleSheet.create(
248
+ {
249
+ container: () => ({
250
+ backgroundColor: 'red',
251
+ variants: {},
252
+ uni__dependencies: [4]
253
+ })
254
+ },
255
+ 793953373
256
+ )
242
257
  `
243
258
  },
244
259
  {
@@ -271,7 +286,7 @@ pluginTester({
271
286
  <View
272
287
  style={styles.container}
273
288
  ref={ref => {
274
- UnistylesShadowRegistry.add(ref, styles.container)
289
+ UnistylesShadowRegistry.add(ref, styles.container, undefined)
275
290
  return () => UnistylesShadowRegistry.remove(ref, styles.container)
276
291
  }}
277
292
  >
@@ -280,14 +295,17 @@ pluginTester({
280
295
  )
281
296
  }
282
297
 
283
- const styles = StyleSheet.create((_, rt) => ({
284
- container: () => ({
285
- backgroundColor: 'red',
286
- variants: {},
287
- paddingTop: rt.insets.top,
288
- uni__dependencies: [4, 9]
289
- })
290
- }))
298
+ const styles = StyleSheet.create(
299
+ (_, rt) => ({
300
+ container: () => ({
301
+ backgroundColor: 'red',
302
+ variants: {},
303
+ paddingTop: rt.insets.top,
304
+ uni__dependencies: [4, 9]
305
+ })
306
+ }),
307
+ 793953373
308
+ )
291
309
  `
292
310
  },
293
311
  {
@@ -320,7 +338,7 @@ pluginTester({
320
338
  <View
321
339
  style={styles.container}
322
340
  ref={ref => {
323
- UnistylesShadowRegistry.add(ref, styles.container)
341
+ UnistylesShadowRegistry.add(ref, styles.container, undefined)
324
342
  return () => UnistylesShadowRegistry.remove(ref, styles.container)
325
343
  }}
326
344
  >
@@ -329,14 +347,17 @@ pluginTester({
329
347
  )
330
348
  }
331
349
 
332
- const styles = StyleSheet.create((hhsa, dee) => ({
333
- container: () => ({
334
- backgroundColor: hhsa.colors.background,
335
- variants: {},
336
- paddingTop: dee.colorScheme === 'dark' ? 0 : 10,
337
- uni__dependencies: [0, 4, 5]
338
- })
339
- }))
350
+ const styles = StyleSheet.create(
351
+ (hhsa, dee) => ({
352
+ container: () => ({
353
+ backgroundColor: hhsa.colors.background,
354
+ variants: {},
355
+ paddingTop: dee.colorScheme === 'dark' ? 0 : 10,
356
+ uni__dependencies: [0, 4, 5]
357
+ })
358
+ }),
359
+ 793953373
360
+ )
340
361
  `
341
362
  },
342
363
  {
@@ -371,7 +392,7 @@ pluginTester({
371
392
  <View
372
393
  style={styles.container}
373
394
  ref={ref => {
374
- UnistylesShadowRegistry.add(ref, styles.container)
395
+ UnistylesShadowRegistry.add(ref, styles.container, undefined)
375
396
  return () => UnistylesShadowRegistry.remove(ref, styles.container)
376
397
  }}
377
398
  >
@@ -389,7 +410,79 @@ pluginTester({
389
410
  uni__dependencies: [0, 4, 9]
390
411
  })
391
412
  }
413
+ }, 793953373)
414
+ `
415
+ },
416
+ {
417
+ title: 'Should generates two different ids for 2 stylesheets in the same file',
418
+ code: `
419
+ import { StyleSheet } from 'react-native-unistyles'
420
+
421
+ export const Example = () => {
422
+ return (
423
+ <View style={styles.container}>
424
+ <Text>Hello world</Text>
425
+ </View>
426
+ )
427
+ }
428
+
429
+ const styles = StyleSheet.create((theme, rt) => {
430
+ return {
431
+ container: () => ({
432
+ backgroundColor: theme.colors.background,
433
+ variants: {},
434
+ paddingTop: rt.insets.top
435
+ })
436
+ }
392
437
  })
438
+ const styles2 = StyleSheet.create((theme, rt) => {
439
+ return {
440
+ container: () => ({
441
+ backgroundColor: theme.colors.background,
442
+ variants: {},
443
+ paddingTop: rt.insets.top
444
+ })
445
+ }
446
+ })
447
+ `,
448
+ output: `
449
+ import { UnistylesShadowRegistry } from 'react-native-unistyles'
450
+ import { StyleSheet } from 'react-native-unistyles'
451
+
452
+ export const Example = () => {
453
+ return (
454
+ <View
455
+ style={styles.container}
456
+ ref={ref => {
457
+ UnistylesShadowRegistry.add(ref, styles.container, undefined)
458
+ return () => UnistylesShadowRegistry.remove(ref, styles.container)
459
+ }}
460
+ >
461
+ <Text>Hello world</Text>
462
+ </View>
463
+ )
464
+ }
465
+
466
+ const styles = StyleSheet.create((theme, rt) => {
467
+ return {
468
+ container: () => ({
469
+ backgroundColor: theme.colors.background,
470
+ variants: {},
471
+ paddingTop: rt.insets.top,
472
+ uni__dependencies: [0, 4, 9]
473
+ })
474
+ }
475
+ }, 793953373)
476
+ const styles2 = StyleSheet.create((theme, rt) => {
477
+ return {
478
+ container: () => ({
479
+ backgroundColor: theme.colors.background,
480
+ variants: {},
481
+ paddingTop: rt.insets.top,
482
+ uni__dependencies: [0, 4, 9]
483
+ })
484
+ }
485
+ }, 793953374)
393
486
  `
394
487
  },
395
488
  ]
package/plugin/index.js CHANGED
@@ -1,7 +1,8 @@
1
1
  const addShadowRegistryImport = require('./import')
2
2
  const { getStyleObjectPath, getStyleAttribute } = require('./style')
3
3
  const { getRefProp, addRef, overrideRef, hasStringRef } = require('./ref')
4
- const { isUnistylesStyleSheet, analyzeDependencies } = require('./stylesheet')
4
+ const { isUnistylesStyleSheet, analyzeDependencies, addStyleSheetTag, getUnistyle } = require('./stylesheet')
5
+ const { isUsingVariants, extractVariants } = require('./variants')
5
6
 
6
7
  module.exports = function ({ types: t }) {
7
8
  return {
@@ -12,7 +13,7 @@ module.exports = function ({ types: t }) {
12
13
  state.file.hasAnyUnistyle = false
13
14
  state.file.hasUnistylesImport = false
14
15
  state.file.styleSheetLocalName = ''
15
- state.file.webDynamicFunctions = {}
16
+ state.file.tagNumber = 0
16
17
  },
17
18
  exit(path, state) {
18
19
  if (state.file.hasAnyUnistyle) {
@@ -45,7 +46,7 @@ module.exports = function ({ types: t }) {
45
46
  return
46
47
  }
47
48
 
48
- const stylePath = getStyleObjectPath(t, styleAttr.value.expression, state)
49
+ const stylePath = getStyleObjectPath(t, styleAttr.value.expression)
49
50
 
50
51
  // style prop is not using object expression
51
52
  if (stylePath.length !== 2) {
@@ -65,23 +66,31 @@ module.exports = function ({ types: t }) {
65
66
  const styleProp = stylePath[1]
66
67
 
67
68
  refProp
68
- ? overrideRef(t, path, refProp, styleObj, styleProp)
69
- : addRef(t, path, styleObj, styleProp)
69
+ ? overrideRef(t, path, refProp, styleObj, styleProp, state)
70
+ : addRef(t, path, styleObj, styleProp, state)
70
71
  },
71
72
  CallExpression(path, state) {
73
+ if (isUsingVariants(t, path)) {
74
+ extractVariants(t, path, state)
75
+ }
76
+
72
77
  if (!isUnistylesStyleSheet(t, path, state)) {
73
78
  return
74
79
  }
75
80
 
81
+ addStyleSheetTag(t, path, state)
82
+
76
83
  const arg = path.node.arguments[0]
77
84
 
78
85
  // Object passed to StyleSheet.create
79
86
  if (t.isObjectExpression(arg)) {
80
87
  arg.properties.forEach(property => {
81
88
  if (t.isObjectProperty(property)) {
82
- const propertyValue = t.isArrowFunctionExpression(property.value)
83
- ? property.value.body
84
- : property.value
89
+ const propertyValue = getUnistyle(t, property)
90
+
91
+ if (!propertyValue) {
92
+ return
93
+ }
85
94
 
86
95
  analyzeDependencies(t, state, property.key.name, propertyValue)
87
96
  }
@@ -107,9 +116,11 @@ module.exports = function ({ types: t }) {
107
116
  if (t.isObjectExpression(body)) {
108
117
  body.properties.forEach(property => {
109
118
  if (t.isObjectProperty(property)) {
110
- const propertyValue = t.isArrowFunctionExpression(property.value)
111
- ? property.value.body
112
- : property.value
119
+ const propertyValue = getUnistyle(t, property)
120
+
121
+ if (!propertyValue) {
122
+ return
123
+ }
113
124
 
114
125
  analyzeDependencies(t, state, property.key.name, propertyValue, themeLocalName, miniRuntimeLocalName)
115
126
  }
package/plugin/ref.js CHANGED
@@ -14,14 +14,20 @@ function hasStringRef(t, path) {
14
14
  )
15
15
  }
16
16
 
17
- function addRef(t, path, styleObj, styleProp) {
17
+ function addRef(t, path, styleObj, styleProp, state) {
18
+ const hasVariants = state.file.hasVariants
19
+
18
20
  const newRefFunction = t.arrowFunctionExpression(
19
21
  [t.identifier('ref')],
20
22
  t.blockStatement([
21
23
  t.expressionStatement(
22
24
  t.callExpression(
23
25
  t.memberExpression(t.identifier('UnistylesShadowRegistry'), t.identifier('add')),
24
- [t.identifier('ref'), t.memberExpression(t.identifier(styleObj), t.identifier(styleProp))]
26
+ [
27
+ t.identifier('ref'),
28
+ t.memberExpression(t.identifier(styleObj), t.identifier(styleProp)),
29
+ t.identifier(hasVariants ? '__uni__variants' : 'undefined')
30
+ ]
25
31
  )
26
32
  ),
27
33
  t.returnStatement(
@@ -43,7 +49,8 @@ function addRef(t, path, styleObj, styleProp) {
43
49
  path.node.openingElement.attributes.push(newRefProp)
44
50
  }
45
51
 
46
- function overrideRef(t, path, refProp, styleObj, styleProp) {
52
+ function overrideRef(t, path, refProp, styleObj, styleProp, state) {
53
+ const hasVariants = state.file.hasVariants
47
54
  const uniqueRefName = path.scope.generateUidIdentifier('ref').name
48
55
  const isIdentifier = t.isIdentifier(refProp.value.expression)
49
56
  const binding = path.scope.getBinding(refProp.value.expression.name)
@@ -55,12 +62,21 @@ function overrideRef(t, path, refProp, styleObj, styleProp) {
55
62
  [t.identifier(uniqueRefName)],
56
63
  t.blockStatement([
57
64
  t.expressionStatement(
58
- t.assignmentExpression('=', t.identifier(userVariableName), t.identifier(uniqueRefName))
65
+ t.assignmentExpression(
66
+ '=',
67
+ t.memberExpression(t.identifier(userVariableName), t.identifier('current')),
68
+ t.identifier(uniqueRefName)
69
+ )
59
70
  ),
60
71
  t.expressionStatement(
61
72
  t.callExpression(
62
73
  t.memberExpression(t.identifier('UnistylesShadowRegistry'), t.identifier('add')),
63
- [t.identifier(uniqueRefName), t.memberExpression(t.identifier(styleObj), t.identifier(styleProp))]
74
+
75
+ [
76
+ t.identifier(uniqueRefName),
77
+ t.memberExpression(t.identifier(styleObj), t.identifier(styleProp)),
78
+ t.identifier(hasVariants ? '__uni__variants' : 'undefined')
79
+ ]
64
80
  )
65
81
  ),
66
82
  t.returnStatement(
@@ -95,7 +111,11 @@ function overrideRef(t, path, refProp, styleObj, styleProp) {
95
111
  t.expressionStatement(
96
112
  t.callExpression(
97
113
  t.memberExpression(t.identifier('UnistylesShadowRegistry'), t.identifier('add')),
98
- [t.identifier('ref'), t.memberExpression(t.identifier(styleObj), t.identifier(styleProp))]
114
+ [
115
+ t.identifier('ref'),
116
+ t.memberExpression(t.identifier(styleObj), t.identifier(styleProp)),
117
+ t.identifier(hasVariants ? '__uni__variants' : 'undefined')
118
+ ]
99
119
  )
100
120
  ),
101
121
  // Merged cleanup function
@@ -122,6 +142,10 @@ function overrideRef(t, path, refProp, styleObj, styleProp) {
122
142
  return
123
143
  }
124
144
 
145
+ if (!binding) {
146
+ return
147
+ }
148
+
125
149
  // ref={scopedFunction}
126
150
  const isArrowFunction = t.isArrowFunctionExpression(binding.path.node.init)
127
151
  const isFunction = t.isFunctionDeclaration(binding.path.node)
@@ -146,7 +170,11 @@ function overrideRef(t, path, refProp, styleObj, styleProp) {
146
170
  t.expressionStatement(
147
171
  t.callExpression(
148
172
  t.memberExpression(t.identifier('UnistylesShadowRegistry'), t.identifier('add')),
149
- [t.identifier(uniqueRefName), t.memberExpression(t.identifier(styleObj), t.identifier(styleProp))]
173
+ [
174
+ t.identifier(uniqueRefName),
175
+ t.memberExpression(t.identifier(styleObj), t.identifier(styleProp)),
176
+ t.identifier(hasVariants ? '__uni__variants' : 'undefined')
177
+ ]
150
178
  )
151
179
  ),
152
180
  t.returnStatement(
package/plugin/style.js CHANGED
@@ -1,4 +1,4 @@
1
- function getStyleObjectPath(t, node, state) {
1
+ function getStyleObjectPath(t, node) {
2
2
  // {styles.container}
3
3
  if (t.isMemberExpression(node)) {
4
4
  return [node.object.name, node.property.name]
@@ -6,39 +6,22 @@ function getStyleObjectPath(t, node, state) {
6
6
 
7
7
  // [styles.container]
8
8
  if (t.isArrayExpression(node)) {
9
- return node.elements.flatMap(element => getStyleObjectPath(t, element, state))
9
+ return node.elements.flatMap(element => getStyleObjectPath(t, element))
10
10
  }
11
11
 
12
12
  // [...styles.container]
13
13
  if (t.isSpreadElement(node)) {
14
- return getStyleObjectPath(t, node.argument, state)
14
+ return getStyleObjectPath(t, node.argument)
15
15
  }
16
16
 
17
17
  // {{ ...styles.container }}
18
18
  if (t.isObjectExpression(node)) {
19
- return node.properties.flatMap(prop => getStyleObjectPath(t, prop.argument, state))
19
+ return node.properties.flatMap(prop => getStyleObjectPath(t, prop.argument))
20
20
  }
21
21
 
22
22
  // {styles.container(arg1, arg2)}
23
23
  if (t.isCallExpression(node)) {
24
- const path = getStyleObjectPath(t, node.callee, state)
25
- const lastPart = path.slice(-1)
26
-
27
- if (!lastPart) {
28
- return path
29
- }
30
-
31
- // for web increment counter for dynamic functions
32
- if (state.file.webDynamicFunctions[lastPart]) {
33
- node.arguments.push(t.numericLiteral(++state.file.webDynamicFunctions[lastPart]))
34
-
35
- return path
36
- }
37
-
38
- state.file.webDynamicFunctions[lastPart] = 1
39
- node.arguments.push(t.numericLiteral(1))
40
-
41
- return path
24
+ return getStyleObjectPath(t, node.callee)
42
25
  }
43
26
 
44
27
  return []
@@ -17,6 +17,19 @@ const UnistyleDependency = {
17
17
  NavigationBar: 13
18
18
  }
19
19
 
20
+ function stringToUniqueId(str) {
21
+ let hash = 0;
22
+
23
+ for (let i = 0; i < str.length; i++) {
24
+ hash = (hash << 5) - hash + str.charCodeAt(i)
25
+ hash |= 0
26
+ }
27
+
28
+ const absHash = Math.abs(hash)
29
+
30
+ return absHash % 1000000000
31
+ }
32
+
20
33
  function isUnistylesStyleSheet(t, path, state) {
21
34
  const callee = path.get('callee')
22
35
 
@@ -28,6 +41,13 @@ function isUnistylesStyleSheet(t, path, state) {
28
41
  )
29
42
  }
30
43
 
44
+ function addStyleSheetTag(t, path, state) {
45
+ const callee = path.get('callee')
46
+ const uniqueId = stringToUniqueId(state.filename) + ++state.file.tagNumber
47
+
48
+ callee.container.arguments.push(t.numericLiteral(uniqueId))
49
+ }
50
+
31
51
  function analyzeDependencies(t, state, name, unistyleObj, themeName, rtName) {
32
52
  const debugMessage = deps => {
33
53
  if (state.opts.debug) {
@@ -141,7 +161,30 @@ function analyzeDependencies(t, state, name, unistyleObj, themeName, rtName) {
141
161
  }
142
162
  }
143
163
 
164
+ function getUnistyle(t, property) {
165
+ const propertyValue = t.isArrowFunctionExpression(property.value)
166
+ ? property.value.body
167
+ : property.value
168
+
169
+ if (t.isObjectExpression(propertyValue)) {
170
+ return propertyValue
171
+ }
172
+
173
+ if (t.isBlockStatement(propertyValue)) {
174
+ const returnStatement = propertyValue.body
175
+ .find(value => t.isReturnStatement(value))
176
+
177
+ return returnStatement
178
+ ? returnStatement.argument
179
+ : null
180
+ }
181
+
182
+ return null
183
+ }
184
+
144
185
  module.exports = {
145
186
  isUnistylesStyleSheet,
146
- analyzeDependencies
187
+ analyzeDependencies,
188
+ addStyleSheetTag,
189
+ getUnistyle
147
190
  }
@@ -0,0 +1,33 @@
1
+ function isUsingVariants(t, path) {
2
+ const callee = path.get('callee')
3
+
4
+ return (
5
+ t.isMemberExpression(callee) &&
6
+ t.isIdentifier(callee.node.object, { name: 'styles' }) &&
7
+ t.isIdentifier(callee.node.property, { name: 'useVariants' }) &&
8
+ t.isObjectExpression(path.node.arguments[0])
9
+ )
10
+ }
11
+
12
+ function extractVariants(t, path, state) {
13
+ const arg = path.node.arguments[0]
14
+
15
+ const variantDeclaration = t.variableDeclaration('const', [
16
+ t.variableDeclarator(
17
+ t.identifier('__uni__variants'),
18
+ arg
19
+ )
20
+ ])
21
+
22
+ // Replace useVariants argument with __uni__variants
23
+ path.node.arguments[0] = t.identifier('__uni__variants')
24
+
25
+ path.insertBefore(variantDeclaration)
26
+
27
+ state.file.hasVariants = true
28
+ }
29
+
30
+ module.exports = {
31
+ isUsingVariants,
32
+ extractVariants
33
+ }
@@ -4,10 +4,10 @@ import type { ShadowNode, Unistyle, ViewHandle } from './types'
4
4
 
5
5
  interface ShadowRegistry extends UnistylesShadowRegistrySpec {
6
6
  // Babel API
7
- add(handle?: ViewHandle, style?: Unistyle): void,
7
+ add(handle?: ViewHandle, style?: Unistyle, variants?: Record<string, string | boolean>): void,
8
8
  remove(handle?: ViewHandle, style?: Unistyle): void,
9
9
  // JSI
10
- link(node: ShadowNode, style: Unistyle): void,
10
+ link(node: ShadowNode, style: Unistyle, variants?: Record<string, string | boolean>): void,
11
11
  unlink(node: ShadowNode, style: Unistyle): void
12
12
  }
13
13
 
@@ -26,12 +26,12 @@ const findShadowNodeForHandle = (handle: ViewHandle) => {
26
26
  return node
27
27
  }
28
28
 
29
- HybridShadowRegistry.add = (handle, style) => {
29
+ HybridShadowRegistry.add = (handle, style, variants) => {
30
30
  if (!handle || !style?.__unid) {
31
31
  return
32
32
  }
33
33
 
34
- HybridShadowRegistry.link(findShadowNodeForHandle(handle), style)
34
+ HybridShadowRegistry.link(findShadowNodeForHandle(handle), style, variants ?? {})
35
35
  }
36
36
 
37
37
  HybridShadowRegistry.remove = (handle, style) => {
@@ -41,4 +41,6 @@ HybridUnistylesStyleSheet.jsMethods = {
41
41
  processColor
42
42
  }
43
43
 
44
- export const StyleSheet = HybridUnistylesStyleSheet as Omit<UnistylesStyleSheet, 'jsMethods'>
44
+ type PrivateMethods = 'jsMethods'
45
+
46
+ export const StyleSheet = HybridUnistylesStyleSheet as Omit<UnistylesStyleSheet, PrivateMethods>