Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: babel rewrite dependency detection with scopes #561

Merged
merged 4 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docs/src/content/docs/v3/other/babel-plugin.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ const stylesheet = StyleSheet.create((theme, rt) => ({
}))
```

<Aside type="caution" title="Dependency detection limitation">
We put a lot of effort into making dependency detection as accurate as possible, thus we support:
- destructuring of `theme` and `rt` objects
- style's as functions / arrow functions / objects
- nested ifs and other conditionals
- ternary operators
- logical operators
- binary operators
- and much more...

**What we don't support**:
- moving functions/arrow functions out of StyleSheet.create
- `theme` and `rt` reassignment to other variables (we don't track them)
</Aside>


### 2. Attaching unique id to each StyleSheet

Expand Down
111 changes: 105 additions & 6 deletions plugin/__tests__/dependencies.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ pluginTester({
container: (headerColors, colorMap) => ({
backgroundColor: headerColors[rt.colorScheme],
paddingBottom: colorMap[theme.colors.primary],
uni__dependencies: [5, 0]
uni__dependencies: [0, 5]
})
}),
664955283
Expand Down Expand Up @@ -559,7 +559,7 @@ pluginTester({
translateY: -rt.insets.ime
}
],
uni__dependencies: [9]
uni__dependencies: [14]
}
}),
664955283
Expand Down Expand Up @@ -627,27 +627,27 @@ pluginTester({
if (someRandomInt === 5) {
return {
backgroundColor: theme.colors.background,
uni__dependencies: [0]
uni__dependencies: [0, 9, 11]
}
}

if (someRandomInt === 10) {
return {
backgroundColor: theme.colors.barbie,
paddingBottom: rt.insets.bottom,
uni__dependencies: [0, 9]
uni__dependencies: [0, 9, 11]
}
}

if (someRandomInt === 15) {
return {
fontSize: rt.fontScale * 10,
uni__dependencies: [11]
uni__dependencies: [0, 9, 11]
}
} else {
return {
backgroundColor: theme.colors.blood,
uni__dependencies: [0]
uni__dependencies: [0, 9, 11]
}
}
}
Expand Down Expand Up @@ -857,6 +857,105 @@ pluginTester({
664955283
)
`
},
{
title: 'Should correctly detect dependencies in weirdest syntax',
code: `
import { View, Text } from 'react-native'
import { StyleSheet } from 'react-native-unistyles'

export const Example = () => {
return (
<View style={styles.container}>
<Text style={styles.container2}>Hello world</Text>
</View>
)
}

const styles = StyleSheet.create(({ components: { test, other: { nested }} }, { insets: { ime }, screen, statusBar: { width, height } }) => {
const otherVariable = 2

return {
container: () => {
if (otherVariable === 2) {
return {
backgroundColor: nested
}
}

if (otherVariable === 3) {
return {
marginTop: ime,
height: screen.height
}
}

return nested
},
container2: () => ({
paddingBottom: ime,
height,
width
})
}
})
`,
output: `
import { Text } from 'react-native-unistyles/components/native/Text'
import { View } from 'react-native-unistyles/components/native/View'

import { StyleSheet } from 'react-native-unistyles'

export const Example = () => {
return (
<View style={styles.container}>
<Text style={styles.container2}>Hello world</Text>
</View>
)
}

const styles = StyleSheet.create(
(
{
components: {
test,
other: { nested }
}
},
{ insets: { ime }, screen, statusBar: { width, height } }
) => {
const otherVariable = 2

return {
container: () => {
if (otherVariable === 2) {
return {
backgroundColor: nested,
uni__dependencies: [0, 14, 6]
}
}

if (otherVariable === 3) {
return {
marginTop: ime,
height: screen.height,
uni__dependencies: [0, 14, 6]
}
}

return { ...nested, uni__dependencies: [0, 14, 6] }
},
container2: () => ({
paddingBottom: ime,
height,
width,
uni__dependencies: [14, 12]
})
}
},
664955283
)
`
}
]
})
Loading