react-native-unistyles 3.0.0-nightly-20250131 → 3.0.0-nightly-20250203

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.
@@ -18,7 +18,7 @@ void parser::Parser::buildUnistyles(jsi::Runtime& rt, std::shared_ptr<StyleSheet
18
18
 
19
19
  if (styleValue.isFunction(rt)) {
20
20
  styleSheet->unistyles[styleKey] = std::make_shared<UnistyleDynamicFunction>(
21
- helpers::HashGenerator::generateHash(styleKey),
21
+ helpers::HashGenerator::generateHash(styleKey + std::to_string(styleSheet->tag)),
22
22
  UnistyleType::DynamicFunction,
23
23
  styleKey,
24
24
  styleValue,
@@ -29,7 +29,7 @@ void parser::Parser::buildUnistyles(jsi::Runtime& rt, std::shared_ptr<StyleSheet
29
29
  }
30
30
 
31
31
  styleSheet->unistyles[styleKey] = std::make_shared<Unistyle>(
32
- helpers::HashGenerator::generateHash(styleKey),
32
+ helpers::HashGenerator::generateHash(styleKey + std::to_string(styleSheet->tag)),
33
33
  UnistyleType::Object,
34
34
  styleKey,
35
35
  styleValue,
@@ -361,7 +361,7 @@ void parser::Parser::rebuildUnistyle(jsi::Runtime& rt, Unistyle::Shared unistyle
361
361
  unistyleFn->unprocessedValue = std::move(functionResult);
362
362
  unistyleFn->parsedStyle = this->parseFirstLevel(rt, unistyleFn, variants);
363
363
  }
364
-
364
+
365
365
  if (unistyle->isDirty) {
366
366
  unistyle->isDirty = false;
367
367
  }
@@ -401,7 +401,7 @@ jsi::Object parser::Parser::parseFirstLevel(jsi::Runtime& rt, Unistyle::Shared u
401
401
  // parse dependencies only once
402
402
  if (propertyName == helpers::STYLE_DEPENDENCIES && !unistyle->isSealed()) {
403
403
  auto newDeps = this->parseDependencies(rt, propertyValue.asObject(rt));
404
-
404
+
405
405
  unistyle->dependencies.insert(unistyle->dependencies.end(), newDeps.begin(), newDeps.end());
406
406
 
407
407
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-unistyles",
3
- "version": "3.0.0-nightly-20250131",
3
+ "version": "3.0.0-nightly-20250203",
4
4
  "description": "Level up your React Native StyleSheet",
5
5
  "scripts": {
6
6
  "test": "jest",
package/plugin/common.js CHANGED
@@ -1,4 +1,16 @@
1
1
  function getIdentifierNameFromExpression(t, memberExpression) {
2
+ if (t.isIdentifier(memberExpression)) {
3
+ return [memberExpression.name]
4
+ }
5
+
6
+ if (t.isSpreadElement(memberExpression)) {
7
+ return [getIdentifierNameFromExpression(t, memberExpression.argument)].flat()
8
+ }
9
+
10
+ if (t.isObjectProperty(memberExpression)) {
11
+ return [getIdentifierNameFromExpression(t, memberExpression.value)].flat()
12
+ }
13
+
2
14
  if (t.isMemberExpression(memberExpression)) {
3
15
  if (memberExpression.computed) {
4
16
  return [
package/plugin/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  const { addUnistylesImport, isInsideNodeModules } = require('./import')
2
2
  const { hasStringRef } = require('./ref')
3
- const { isUnistylesStyleSheet, analyzeDependencies, addStyleSheetTag, getUnistyles, isKindOfStyleSheet, addThemeDependencyToMemberExpression } = require('./stylesheet')
3
+ const { isUnistylesStyleSheet, analyzeDependencies, addStyleSheetTag, getUnistyles, isKindOfStyleSheet, maybeAddThemeDependencyToMemberExpression, addThemeDependencyToMemberExpression, getStyleSheetLocalNames } = require('./stylesheet')
4
4
  const { extractVariants } = require('./variants')
5
5
  const { REACT_NATIVE_COMPONENT_NAMES, REPLACE_WITH_UNISTYLES_PATHS, REPLACE_WITH_UNISTYLES_EXOTIC_PATHS, NATIVE_COMPONENTS_PATHS } = require('./consts')
6
6
  const { handleExoticImport } = require('./exotic')
@@ -156,7 +156,7 @@ module.exports = function ({ types: t }) {
156
156
  const propertyValues = getUnistyles(t, property)
157
157
 
158
158
  propertyValues.forEach(propertyValue => {
159
- analyzeDependencies(t, state, property.key.name, propertyValue)
159
+ analyzeDependencies(t, state, property.key.name, propertyValue, [], [])
160
160
  })
161
161
  }
162
162
  })
@@ -164,15 +164,7 @@ module.exports = function ({ types: t }) {
164
164
 
165
165
  // Function passed to StyleSheet.create (e.g., theme => ({ container: {} }))
166
166
  if (t.isArrowFunctionExpression(arg) || t.isFunctionExpression(arg)) {
167
- const params = arg.params
168
- const hasTheme = params.length >= 1
169
- const hasMiniRuntime = params.length === 2
170
- const themeLocalName = hasTheme
171
- ? params[0].name
172
- : undefined
173
- const miniRuntimeLocalName = hasMiniRuntime
174
- ? params[1].name
175
- : undefined
167
+ const localNames = getStyleSheetLocalNames(t, arg)
176
168
  const body = t.isBlockStatement(arg.body)
177
169
  ? arg.body.body.find(statement => t.isReturnStatement(statement)).argument
178
170
  : arg.body
@@ -183,15 +175,14 @@ module.exports = function ({ types: t }) {
183
175
  if (t.isObjectProperty(property)) {
184
176
  const propertyValues = getUnistyles(t, property)
185
177
 
178
+ // special case for non object/function properties
186
179
  // maybe user used inlined theme? ({ container: theme.components.container })
187
- if (propertyValues.length === 0 && t.isMemberExpression(property.value)) {
188
- if (property.value.object.object.name === themeLocalName) {
189
- addThemeDependencyToMemberExpression(t, property)
190
- }
180
+ if (propertyValues.length === 0 && maybeAddThemeDependencyToMemberExpression(t, property, localNames.theme)) {
181
+ addThemeDependencyToMemberExpression(t, property)
191
182
  }
192
183
 
193
184
  propertyValues.forEach(propertyValue => {
194
- analyzeDependencies(t, state, property.key.name, propertyValue, themeLocalName, miniRuntimeLocalName)
185
+ analyzeDependencies(t, state, property.key.name, propertyValue, localNames.theme, localNames.miniRuntime)
195
186
  })
196
187
  }
197
188
  })
@@ -63,8 +63,71 @@ function addStyleSheetTag(t, path, state) {
63
63
  callee.container.arguments.push(t.numericLiteral(uniqueId))
64
64
  }
65
65
 
66
+ function getStyleSheetLocalNames(t, functionArg) {
67
+ const params = functionArg.params
68
+ const hasTheme = params.length >= 1
69
+ const hasMiniRuntime = params.length === 2
70
+ const getProperty = (property, allowNested) => {
71
+ if (t.isIdentifier(property.value)) {
72
+ return property.value.name
73
+ }
74
+
75
+ if (!t.isObjectPattern(property.value)) {
76
+ return undefined
77
+ }
78
+
79
+ if (allowNested) {
80
+ return property.value.properties.flatMap(getProperty)
81
+ }
82
+
83
+ // we can force allow nested only for insets
84
+ const hasIme = property.value.properties.find(property => property.key.name === 'ime')
85
+ const lastKeyValue = property.value.properties.flatMap(getProperty)
86
+
87
+ if (hasIme) {
88
+ return lastKeyValue
89
+ }
90
+
91
+ return `${property.key.name}.${lastKeyValue}`
92
+ }
93
+ const getLocalNames = (param, allowNested) => {
94
+ if (t.isObjectPattern(param)) {
95
+ return param.properties
96
+ .flatMap(property => getProperty(property, allowNested))
97
+ .filter(Boolean)
98
+ }
99
+
100
+
101
+ if (t.isIdentifier(param)) {
102
+ return [param.name]
103
+ }
104
+
105
+ return []
106
+ }
107
+
108
+ return {
109
+ theme: hasTheme ? getLocalNames(params[0], true) : [],
110
+ miniRuntime: hasMiniRuntime ? getLocalNames(params[1], false) : []
111
+ }
112
+ }
113
+
114
+ function maybeAddThemeDependencyToMemberExpression(t, property, themeLocalNames) {
115
+ if (t.isIdentifier(property)) {
116
+ return themeLocalNames.includes(property.name)
117
+ }
118
+
119
+ if (t.isObjectProperty(property)) {
120
+ return maybeAddThemeDependencyToMemberExpression(t, property.value, themeLocalNames)
121
+ }
122
+
123
+ if (t.isMemberExpression(property)) {
124
+ return maybeAddThemeDependencyToMemberExpression(t, property.object, themeLocalNames)
125
+ }
126
+ }
127
+
128
+
66
129
  /** @param {import('./index').UnistylesPluginPass} state */
67
- function analyzeDependencies(t, state, name, unistyleObj, themeName, rtName) {
130
+ function analyzeDependencies(t, state, name, unistyleObj, themeNames, rtNames) {
68
131
  const debugMessage = deps => {
69
132
  if (state.opts.debug) {
70
133
  const mappedDeps = deps
@@ -78,16 +141,42 @@ function analyzeDependencies(t, state, name, unistyleObj, themeName, rtName) {
78
141
  const dependencies = []
79
142
 
80
143
  Object.values(unistyle).forEach(uni => {
81
- const identifier = getIdentifierNameFromExpression(t, uni.value)
144
+ const identifiers = getIdentifierNameFromExpression(t, uni)
82
145
 
83
- if (identifier.includes(themeName)) {
146
+ if (themeNames.some(name => identifiers.some(id => id === name))) {
84
147
  dependencies.push(UnistyleDependency.Theme)
85
148
  }
86
149
 
87
- if (identifier.includes(rtName)) {
150
+ const matchingRtNames = rtNames.reduce((acc, name) => {
151
+ if (name.includes('.')) {
152
+ const key = name.split('.').at(0)
153
+
154
+ if (identifiers.some(id => name.includes(id))) {
155
+ return [
156
+ ...acc,
157
+ key
158
+ ]
159
+ }
160
+
161
+ return acc
162
+ }
163
+
164
+
165
+ if (identifiers.some(id => id === name)) {
166
+ return [
167
+ ...acc,
168
+ name
169
+ ]
170
+ }
171
+
172
+ return acc
173
+ }, [])
174
+
175
+ if (matchingRtNames.length > 0) {
88
176
  const propertyNames = getSecondPropertyName(t, uni.value)
89
177
 
90
- propertyNames
178
+ matchingRtNames
179
+ .concat(propertyNames)
91
180
  .filter(Boolean)
92
181
  .forEach(propertyName => {
93
182
  switch (propertyName) {
@@ -235,5 +324,7 @@ module.exports = {
235
324
  addStyleSheetTag,
236
325
  getUnistyles,
237
326
  isKindOfStyleSheet,
327
+ getStyleSheetLocalNames,
328
+ maybeAddThemeDependencyToMemberExpression,
238
329
  addThemeDependencyToMemberExpression
239
330
  }