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.
- package/README.md +2 -2
- package/android/src/main/java/com/unistyles/NativePlatform+android.kt +2 -15
- package/android/src/main/java/com/unistyles/NativePlatform+listener.kt +1 -1
- package/codemods/use-variants.js +155 -0
- package/components/native/TouchableHighlight/package.json +1 -1
- package/cxx/converters/TransformOriginConverter.cpp +3 -2
- package/cxx/core/DependencyTracker.h +162 -0
- package/cxx/core/StyleSheet.h +14 -0
- package/cxx/core/Unistyle.h +6 -3
- package/cxx/core/UnistylesRegistry.cpp +16 -2
- package/cxx/core/UnistylesRegistry.h +2 -1
- package/cxx/hybridObjects/HybridShadowRegistry.cpp +16 -5
- package/cxx/hybridObjects/HybridShadowRegistry.h +1 -1
- package/cxx/hybridObjects/HybridStyleSheet.cpp +8 -1
- package/cxx/hybridObjects/HybridUnistylesRuntime.cpp +1 -1
- package/cxx/parser/Parser.cpp +83 -31
- package/cxx/parser/Parser.h +5 -0
- package/cxx/shadowTree/NativeProps.h +23 -0
- package/cxx/shadowTree/ShadowTrafficController.h +5 -0
- package/cxx/shadowTree/ShadowTreeManager.cpp +73 -1
- package/ios/NativePlatform+ios.swift +4 -6
- package/ios/Unistyles.h +1 -0
- package/lib/commonjs/core/trackDependencies.js +10 -0
- package/lib/commonjs/core/trackDependencies.js.map +1 -0
- package/lib/commonjs/core/trackDependencies.metro.js +10 -0
- package/lib/commonjs/core/trackDependencies.metro.js.map +1 -0
- package/lib/commonjs/core/useProxifiedUnistyles/useProxifiedUnistyles.js +4 -0
- package/lib/commonjs/core/useProxifiedUnistyles/useProxifiedUnistyles.js.map +1 -1
- package/lib/commonjs/core/withUnistyles/withUnistyles.native.js +63 -14
- package/lib/commonjs/core/withUnistyles/withUnistyles.native.js.map +1 -1
- package/lib/commonjs/hooks/useMedia.js +6 -0
- package/lib/commonjs/hooks/useMedia.js.map +1 -1
- package/lib/commonjs/mocks.js +4 -2
- package/lib/commonjs/mocks.js.map +1 -1
- package/lib/commonjs/reanimated/variant/useUpdateVariantColor.js +13 -4
- package/lib/commonjs/reanimated/variant/useUpdateVariantColor.js.map +1 -1
- package/lib/commonjs/specs/ShadowRegistry/index.js +26 -1
- package/lib/commonjs/specs/ShadowRegistry/index.js.map +1 -1
- package/lib/commonjs/specs/StyleSheet/index.js +6 -1
- package/lib/commonjs/specs/StyleSheet/index.js.map +1 -1
- package/lib/commonjs/types/stylesheet.js.map +1 -1
- package/lib/commonjs/web/convert/index.js +2 -2
- package/lib/commonjs/web/convert/index.js.map +1 -1
- package/lib/commonjs/web/convert/style.js +6 -0
- package/lib/commonjs/web/convert/style.js.map +1 -1
- package/lib/commonjs/web/create.js +35 -6
- package/lib/commonjs/web/create.js.map +1 -1
- package/lib/commonjs/web/css/core.js +12 -2
- package/lib/commonjs/web/css/core.js.map +1 -1
- package/lib/commonjs/web/css/sheet.js +223 -0
- package/lib/commonjs/web/css/sheet.js.map +1 -0
- package/lib/commonjs/web/css/state.js +98 -78
- package/lib/commonjs/web/css/state.js.map +1 -1
- package/lib/commonjs/web/css/utils.js +1 -31
- package/lib/commonjs/web/css/utils.js.map +1 -1
- package/lib/commonjs/web/dependencyTracker.js +72 -0
- package/lib/commonjs/web/dependencyTracker.js.map +1 -0
- package/lib/commonjs/web/registry.js +73 -4
- package/lib/commonjs/web/registry.js.map +1 -1
- package/lib/commonjs/web/runtime.js +0 -1
- package/lib/commonjs/web/runtime.js.map +1 -1
- package/lib/commonjs/web/shadowRegistry.js +83 -11
- package/lib/commonjs/web/shadowRegistry.js.map +1 -1
- package/lib/commonjs/web/types.js +3 -1
- package/lib/commonjs/web/types.js.map +1 -1
- package/lib/module/core/trackDependencies.js +6 -0
- package/lib/module/core/trackDependencies.js.map +1 -0
- package/lib/module/core/trackDependencies.metro.js +6 -0
- package/lib/module/core/trackDependencies.metro.js.map +1 -0
- package/lib/module/core/useProxifiedUnistyles/useProxifiedUnistyles.js +4 -0
- package/lib/module/core/useProxifiedUnistyles/useProxifiedUnistyles.js.map +1 -1
- package/lib/module/core/withUnistyles/withUnistyles.native.js +63 -15
- package/lib/module/core/withUnistyles/withUnistyles.native.js.map +1 -1
- package/lib/module/hooks/useMedia.js +6 -0
- package/lib/module/hooks/useMedia.js.map +1 -1
- package/lib/module/mocks.js +4 -2
- package/lib/module/mocks.js.map +1 -1
- package/lib/module/reanimated/variant/useUpdateVariantColor.js +15 -5
- package/lib/module/reanimated/variant/useUpdateVariantColor.js.map +1 -1
- package/lib/module/specs/ShadowRegistry/index.js +25 -1
- package/lib/module/specs/ShadowRegistry/index.js.map +1 -1
- package/lib/module/specs/StyleSheet/index.js +6 -1
- package/lib/module/specs/StyleSheet/index.js.map +1 -1
- package/lib/module/types/stylesheet.js.map +1 -1
- package/lib/module/web/convert/index.js +2 -2
- package/lib/module/web/convert/index.js.map +1 -1
- package/lib/module/web/convert/style.js +6 -0
- package/lib/module/web/convert/style.js.map +1 -1
- package/lib/module/web/create.js +35 -6
- package/lib/module/web/create.js.map +1 -1
- package/lib/module/web/css/core.js +10 -1
- package/lib/module/web/css/core.js.map +1 -1
- package/lib/module/web/css/sheet.js +216 -0
- package/lib/module/web/css/sheet.js.map +1 -0
- package/lib/module/web/css/state.js +98 -78
- package/lib/module/web/css/state.js.map +1 -1
- package/lib/module/web/css/utils.js +0 -29
- package/lib/module/web/css/utils.js.map +1 -1
- package/lib/module/web/dependencyTracker.js +67 -0
- package/lib/module/web/dependencyTracker.js.map +1 -0
- package/lib/module/web/registry.js +73 -4
- package/lib/module/web/registry.js.map +1 -1
- package/lib/module/web/runtime.js +0 -1
- package/lib/module/web/runtime.js.map +1 -1
- package/lib/module/web/shadowRegistry.js +83 -11
- package/lib/module/web/shadowRegistry.js.map +1 -1
- package/lib/module/web/types.js +2 -0
- package/lib/module/web/types.js.map +1 -1
- package/lib/typescript/src/core/trackDependencies.d.ts +2 -0
- package/lib/typescript/src/core/trackDependencies.d.ts.map +1 -0
- package/lib/typescript/src/core/trackDependencies.metro.d.ts +2 -0
- package/lib/typescript/src/core/trackDependencies.metro.d.ts.map +1 -0
- package/lib/typescript/src/core/useProxifiedUnistyles/useProxifiedUnistyles.d.ts.map +1 -1
- package/lib/typescript/src/core/withUnistyles/withUnistyles.native.d.ts +2 -1
- package/lib/typescript/src/core/withUnistyles/withUnistyles.native.d.ts.map +1 -1
- package/lib/typescript/src/hooks/useMedia.d.ts.map +1 -1
- package/lib/typescript/src/reanimated/variant/useUpdateVariantColor.d.ts.map +1 -1
- package/lib/typescript/src/specs/ShadowRegistry/index.d.ts +1 -1
- package/lib/typescript/src/specs/ShadowRegistry/index.d.ts.map +1 -1
- package/lib/typescript/src/specs/StyleSheet/index.d.ts +3 -1
- package/lib/typescript/src/specs/StyleSheet/index.d.ts.map +1 -1
- package/lib/typescript/src/types/stylesheet.d.ts +3 -3
- package/lib/typescript/src/types/stylesheet.d.ts.map +1 -1
- package/lib/typescript/src/web/convert/style.d.ts.map +1 -1
- package/lib/typescript/src/web/create.d.ts.map +1 -1
- package/lib/typescript/src/web/css/core.d.ts +4 -1
- package/lib/typescript/src/web/css/core.d.ts.map +1 -1
- package/lib/typescript/src/web/css/sheet.d.ts +26 -0
- package/lib/typescript/src/web/css/sheet.d.ts.map +1 -0
- package/lib/typescript/src/web/css/state.d.ts +6 -6
- package/lib/typescript/src/web/css/state.d.ts.map +1 -1
- package/lib/typescript/src/web/css/utils.d.ts +0 -2
- package/lib/typescript/src/web/css/utils.d.ts.map +1 -1
- package/lib/typescript/src/web/dependencyTracker.d.ts +9 -0
- package/lib/typescript/src/web/dependencyTracker.d.ts.map +1 -0
- package/lib/typescript/src/web/registry.d.ts +9 -1
- package/lib/typescript/src/web/registry.d.ts.map +1 -1
- package/lib/typescript/src/web/runtime.d.ts.map +1 -1
- package/lib/typescript/src/web/shadowRegistry.d.ts.map +1 -1
- package/lib/typescript/src/web/types.d.ts +1 -0
- package/lib/typescript/src/web/types.d.ts.map +1 -1
- package/metro/index.d.ts +2 -12
- package/metro/index.js +57 -33
- package/package.json +64 -17
- package/plugin/index.d.ts +0 -7
- package/plugin/index.js +9 -15
- package/src/core/trackDependencies.metro.ts +3 -0
- package/src/core/trackDependencies.ts +3 -0
- package/src/core/useProxifiedUnistyles/useProxifiedUnistyles.ts +5 -0
- package/src/core/withUnistyles/withUnistyles.native.tsx +76 -17
- package/src/hooks/useMedia.ts +6 -0
- package/src/mocks.ts +5 -2
- package/src/reanimated/variant/useUpdateVariantColor.ts +15 -5
- package/src/specs/ShadowRegistry/index.ts +29 -2
- package/src/specs/StyleSheet/index.ts +4 -2
- package/src/types/stylesheet.ts +5 -4
- package/src/web/convert/index.ts +2 -2
- package/src/web/convert/style.ts +8 -0
- package/src/web/create.ts +45 -8
- package/src/web/css/core.ts +17 -3
- package/src/web/css/sheet.ts +274 -0
- package/src/web/css/state.ts +108 -95
- package/src/web/css/utils.ts +0 -42
- package/src/web/dependencyTracker.ts +71 -0
- package/src/web/registry.ts +88 -8
- package/src/web/runtime.ts +0 -1
- package/src/web/shadowRegistry.ts +101 -16
- package/src/web/types.ts +3 -0
- package/metro/babel-transformer.js +0 -1102
package/src/web/registry.ts
CHANGED
|
@@ -3,15 +3,23 @@ import type { UnistylesTheme, UnistylesValues } from '../types'
|
|
|
3
3
|
import type { StyleSheet, StyleSheetWithSuperPowers } from '../types/stylesheet'
|
|
4
4
|
import type { UnistylesServices } from './types'
|
|
5
5
|
|
|
6
|
+
import { TRACK_DEPENDENCIES } from '../core/trackDependencies'
|
|
7
|
+
import { UnistyleDependency as Dependency } from '../specs/NativePlatform/NativePlatform.nitro'
|
|
6
8
|
import { CSSState } from './css'
|
|
9
|
+
import { createTrackedMiniRuntime, trackDependencies } from './dependencyTracker'
|
|
7
10
|
import { error, extractUnistyleDependencies, generateHash } from './utils'
|
|
8
11
|
|
|
12
|
+
type StyleSheetFunction = (theme: UnistylesTheme, miniRuntime: UnistylesMiniRuntime) => StyleSheet
|
|
13
|
+
|
|
9
14
|
export class UnistylesRegistry {
|
|
10
15
|
private readonly stylesheets = new Map<StyleSheetWithSuperPowers<StyleSheet>, StyleSheet>()
|
|
11
16
|
private readonly stylesCache = new Set<string>()
|
|
12
17
|
private readonly stylesCounter = new Map<string, Set<HTMLElement>>()
|
|
13
18
|
private readonly disposeListenersMap = new Map<object, VoidFunction>()
|
|
14
19
|
private readonly dependenciesMap = new Map<StyleSheetWithSuperPowers<StyleSheet>, Set<UnistyleDependency>>()
|
|
20
|
+
// without the Babel plugin, what the StyleSheet function itself read, without the reads of its dynamic functions
|
|
21
|
+
private readonly readDependenciesMap = new Map<StyleSheetWithSuperPowers<StyleSheet>, Set<UnistyleDependency>>()
|
|
22
|
+
private readonly readDependenciesWatchers = new Map<StyleSheetWithSuperPowers<StyleSheet>, Set<VoidFunction>>()
|
|
15
23
|
readonly css: CSSState
|
|
16
24
|
|
|
17
25
|
constructor(private services: UnistylesServices) {
|
|
@@ -32,7 +40,15 @@ export class UnistylesRegistry {
|
|
|
32
40
|
)
|
|
33
41
|
}
|
|
34
42
|
|
|
35
|
-
|
|
43
|
+
const { result, dependencies } = this.callStylesheet(
|
|
44
|
+
stylesheet,
|
|
45
|
+
scopedTheme,
|
|
46
|
+
this.services.runtime.getMiniRuntime(scopedThemeName),
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
this.addRuntimeDependenciesToStylesheet(stylesheet, dependencies)
|
|
50
|
+
|
|
51
|
+
return result
|
|
36
52
|
}
|
|
37
53
|
|
|
38
54
|
const computedStylesheet = this.stylesheets.get(stylesheet)
|
|
@@ -43,8 +59,14 @@ export class UnistylesRegistry {
|
|
|
43
59
|
|
|
44
60
|
const themeName = this.services.runtime.themeName
|
|
45
61
|
const currentTheme = this.services.runtime.getTheme(themeName, this.services.state.CSSVars)
|
|
46
|
-
const createdStylesheet =
|
|
47
|
-
|
|
62
|
+
const { result: createdStylesheet, dependencies: runtimeDependencies } = this.callStylesheet(
|
|
63
|
+
stylesheet,
|
|
64
|
+
currentTheme,
|
|
65
|
+
this.services.runtime.getMiniRuntime(themeName),
|
|
66
|
+
)
|
|
67
|
+
const dependencies = Object.values(createdStylesheet)
|
|
68
|
+
.flatMap((value) => extractUnistyleDependencies(value))
|
|
69
|
+
.concat(runtimeDependencies)
|
|
48
70
|
|
|
49
71
|
this.addDependenciesToStylesheet(stylesheet, dependencies)
|
|
50
72
|
this.stylesheets.set(stylesheet, createdStylesheet)
|
|
@@ -52,10 +74,24 @@ export class UnistylesRegistry {
|
|
|
52
74
|
return createdStylesheet
|
|
53
75
|
}
|
|
54
76
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
77
|
+
// without the Babel plugin, every dependency the StyleSheet function read so far
|
|
78
|
+
// a dynamic function's reads belong to the views that call it, see shadowRegistry
|
|
79
|
+
getStylesheetDependencies = (stylesheet: StyleSheetWithSuperPowers<StyleSheet>) =>
|
|
80
|
+
Array.from(this.readDependenciesMap.get(stylesheet) ?? [])
|
|
81
|
+
|
|
82
|
+
// without the Babel plugin, called when the StyleSheet function reads something new
|
|
83
|
+
watchStylesheetDependencies = (stylesheet: StyleSheetWithSuperPowers<StyleSheet>, onChange: VoidFunction) => {
|
|
84
|
+
const watchers = this.readDependenciesWatchers.get(stylesheet) ?? new Set()
|
|
85
|
+
|
|
86
|
+
watchers.add(onChange)
|
|
87
|
+
this.readDependenciesWatchers.set(stylesheet, watchers)
|
|
88
|
+
|
|
89
|
+
return () => {
|
|
90
|
+
watchers.delete(onChange)
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
addDependenciesToStylesheet = (stylesheet: StyleSheetFunction, dependencies: Array<UnistyleDependency>) => {
|
|
59
95
|
this.disposeListenersMap.get(stylesheet)?.()
|
|
60
96
|
|
|
61
97
|
const dependenciesMap = this.dependenciesMap.get(stylesheet) ?? new Set(dependencies)
|
|
@@ -64,18 +100,60 @@ export class UnistylesRegistry {
|
|
|
64
100
|
|
|
65
101
|
const dispose = this.services.listener.addStylesheetListeners(Array.from(dependenciesMap), () => {
|
|
66
102
|
const themeName = this.services.runtime.themeName
|
|
67
|
-
const newComputedStylesheet =
|
|
103
|
+
const { result: newComputedStylesheet, dependencies: runtimeDependencies } = this.callStylesheet(
|
|
104
|
+
stylesheet,
|
|
68
105
|
this.services.runtime.getTheme(themeName, this.services.state.CSSVars),
|
|
69
106
|
this.services.runtime.getMiniRuntime(themeName),
|
|
70
107
|
)
|
|
71
108
|
|
|
72
109
|
this.stylesheets.set(stylesheet, newComputedStylesheet)
|
|
110
|
+
this.addRuntimeDependenciesToStylesheet(stylesheet, runtimeDependencies)
|
|
73
111
|
})
|
|
74
112
|
|
|
75
113
|
this.dependenciesMap.set(stylesheet, dependenciesMap)
|
|
76
114
|
this.disposeListenersMap.set(stylesheet, dispose)
|
|
77
115
|
}
|
|
78
116
|
|
|
117
|
+
// a StyleSheet may read new dependencies each time it runs, eg. behind a condition
|
|
118
|
+
private addRuntimeDependenciesToStylesheet = (
|
|
119
|
+
stylesheet: StyleSheetFunction,
|
|
120
|
+
dependencies: Array<UnistyleDependency>,
|
|
121
|
+
) => {
|
|
122
|
+
const knownDependencies = this.dependenciesMap.get(stylesheet)
|
|
123
|
+
|
|
124
|
+
if (dependencies.some((dependency) => !knownDependencies?.has(dependency))) {
|
|
125
|
+
this.addDependenciesToStylesheet(stylesheet, dependencies)
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// calls the user's StyleSheet function, without the Babel plugin it also returns the dependencies it read
|
|
130
|
+
private callStylesheet = (
|
|
131
|
+
stylesheet: StyleSheetFunction,
|
|
132
|
+
theme: UnistylesTheme,
|
|
133
|
+
miniRuntime: UnistylesMiniRuntime,
|
|
134
|
+
): { result: StyleSheet; dependencies: Array<UnistyleDependency> } => {
|
|
135
|
+
if (!TRACK_DEPENDENCIES) {
|
|
136
|
+
return { result: stylesheet(theme, miniRuntime), dependencies: [] }
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const { result, dependencies: readDependencies } = trackDependencies(() =>
|
|
140
|
+
stylesheet(theme, createTrackedMiniRuntime(miniRuntime)),
|
|
141
|
+
)
|
|
142
|
+
// every StyleSheet function receives the theme
|
|
143
|
+
const dependencies = readDependencies.concat(Dependency.Theme)
|
|
144
|
+
const knownDependencies = this.readDependenciesMap.get(stylesheet) ?? new Set()
|
|
145
|
+
const hasNewDependencies = dependencies.some((dependency) => !knownDependencies.has(dependency))
|
|
146
|
+
|
|
147
|
+
dependencies.forEach((dependency) => knownDependencies.add(dependency))
|
|
148
|
+
this.readDependenciesMap.set(stylesheet, knownDependencies)
|
|
149
|
+
|
|
150
|
+
if (hasNewDependencies) {
|
|
151
|
+
this.readDependenciesWatchers.get(stylesheet)?.forEach((onChange) => onChange())
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return { result, dependencies }
|
|
155
|
+
}
|
|
156
|
+
|
|
79
157
|
connect = (ref: HTMLElement, hash: string) => {
|
|
80
158
|
const stylesCounter = this.stylesCounter.get(hash) ?? new Set()
|
|
81
159
|
|
|
@@ -133,6 +211,8 @@ export class UnistylesRegistry {
|
|
|
133
211
|
this.stylesheets.clear()
|
|
134
212
|
this.stylesCache.clear()
|
|
135
213
|
this.dependenciesMap.clear()
|
|
214
|
+
this.readDependenciesMap.clear()
|
|
215
|
+
this.readDependenciesWatchers.clear()
|
|
136
216
|
this.disposeListenersMap.clear()
|
|
137
217
|
this.stylesCounter.clear()
|
|
138
218
|
}
|
package/src/web/runtime.ts
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import type { UnistylesTheme, UnistylesValues } from '../types'
|
|
2
|
+
import type { StyleSheet, StyleSheetWithSuperPowers } from '../types/stylesheet'
|
|
2
3
|
import type { UniGeneratedStyle, UnistylesServices } from './types'
|
|
3
4
|
|
|
5
|
+
import { TRACK_DEPENDENCIES } from '../core/trackDependencies'
|
|
4
6
|
import { UnistyleDependency } from '../specs/NativePlatform/NativePlatform.nitro'
|
|
5
7
|
import { deepMergeObjects } from '../utils'
|
|
8
|
+
import { trackDependencies } from './dependencyTracker'
|
|
9
|
+
import { CONTENTS_CLASS_NAME } from './types'
|
|
6
10
|
import { extractSecrets, extractUnistyleDependencies, isGeneratedUnistyle, isServer } from './utils'
|
|
7
11
|
import { getVariants } from './variants'
|
|
8
12
|
|
|
@@ -36,7 +40,12 @@ export class UnistylesShadowRegistry {
|
|
|
36
40
|
return firstStyle as UniGeneratedStyle
|
|
37
41
|
}
|
|
38
42
|
|
|
43
|
+
const stylesheets = new Set<StyleSheetWithSuperPowers<StyleSheet>>()
|
|
44
|
+
// without the Babel plugin, what the dynamic functions and getters of this view read in the last run
|
|
45
|
+
let styleDependencies: Array<UnistyleDependency> = []
|
|
39
46
|
const getParsedStyles = () => {
|
|
47
|
+
styleDependencies = []
|
|
48
|
+
|
|
40
49
|
const allStyles = unistyles.map((unistyle) => {
|
|
41
50
|
const secrets = extractSecrets(unistyle)
|
|
42
51
|
|
|
@@ -51,18 +60,31 @@ export class UnistylesShadowRegistry {
|
|
|
51
60
|
}
|
|
52
61
|
|
|
53
62
|
const { __uni__key, __uni__stylesheet, __uni__args = [], __stylesheetVariants: variants } = secrets
|
|
63
|
+
|
|
64
|
+
stylesheets.add(__uni__stylesheet)
|
|
65
|
+
|
|
54
66
|
const newComputedStylesheet = this.services.registry.getComputedStylesheet(
|
|
55
67
|
__uni__stylesheet,
|
|
56
68
|
scopedTheme,
|
|
57
69
|
)
|
|
58
70
|
const style = newComputedStylesheet[__uni__key] as UnistylesValues | ((...args: any) => UnistylesValues)
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
71
|
+
const getStyle = () => {
|
|
72
|
+
const result = typeof style === 'function' ? style(...__uni__args) : style
|
|
73
|
+
const variantsResult = getVariants(result, variants)
|
|
74
|
+
|
|
75
|
+
return deepMergeObjects(result, variantsResult)
|
|
76
|
+
}
|
|
77
|
+
// without the Babel plugin, a dynamic function or a getter (copied by deepMergeObjects) records what it reads
|
|
78
|
+
const { result: resultWithVariants, dependencies: runtimeDependencies } = TRACK_DEPENDENCIES
|
|
79
|
+
? trackDependencies(getStyle)
|
|
80
|
+
: { result: getStyle(), dependencies: [] }
|
|
81
|
+
const dependencies = extractUnistyleDependencies(resultWithVariants).concat(runtimeDependencies)
|
|
82
|
+
|
|
83
|
+
styleDependencies.push(...runtimeDependencies)
|
|
63
84
|
|
|
64
85
|
if (typeof __uni__stylesheet === 'function') {
|
|
65
86
|
// Add dependencies from dynamic styles to stylesheet
|
|
87
|
+
// it recomputes, so dynamic functions read a fresh mini runtime
|
|
66
88
|
this.services.registry.addDependenciesToStylesheet(__uni__stylesheet, dependencies)
|
|
67
89
|
}
|
|
68
90
|
|
|
@@ -75,24 +97,87 @@ export class UnistylesShadowRegistry {
|
|
|
75
97
|
return deepMergeObjects(...allStyles)
|
|
76
98
|
}
|
|
77
99
|
|
|
100
|
+
// without the Babel plugin, styles depend on everything their StyleSheet read and on what their own dynamic functions and getters read
|
|
101
|
+
// reads of dynamic functions called by other views don't update this one
|
|
102
|
+
const getParsedStylesWithDependencies = () => {
|
|
103
|
+
const styles = getParsedStyles()
|
|
104
|
+
const runtimeDependencies = TRACK_DEPENDENCIES
|
|
105
|
+
? Array.from(stylesheets)
|
|
106
|
+
.flatMap((stylesheet) => this.services.registry.getStylesheetDependencies(stylesheet))
|
|
107
|
+
.concat(styleDependencies)
|
|
108
|
+
: []
|
|
109
|
+
|
|
110
|
+
return { styles, runtimeDependencies }
|
|
111
|
+
}
|
|
112
|
+
|
|
78
113
|
// Copy scoped theme to not use referenced value
|
|
79
114
|
const scopedTheme = this.scopedTheme
|
|
80
|
-
const parsedStyles =
|
|
115
|
+
const { styles: parsedStyles, runtimeDependencies } = getParsedStylesWithDependencies()
|
|
81
116
|
const { hash, existingHash } = this.services.registry.add(parsedStyles, forChild)
|
|
82
117
|
const injectedClassNames = parsedStyles?._web?._classNames ?? []
|
|
83
|
-
const
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
118
|
+
const userClassName = Array.isArray(injectedClassNames) ? injectedClassNames.join(' ') : injectedClassNames
|
|
119
|
+
const injectedClassName =
|
|
120
|
+
!forChild && parsedStyles?.display === 'contents'
|
|
121
|
+
? [userClassName, CONTENTS_CLASS_NAME].filter(Boolean).join(' ')
|
|
122
|
+
: userClassName
|
|
123
|
+
const filterDependencies = (dependencies: Array<UnistyleDependency>) =>
|
|
124
|
+
Array.from(new Set(dependencies)).filter(
|
|
125
|
+
(dependency) => !this.services.state.CSSVars || dependency !== UnistyleDependency.Theme,
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
// without the Babel plugin, every style of a StyleSheet listens to everything the StyleSheet read, most of them don't change
|
|
129
|
+
let lastStyles = TRACK_DEPENDENCIES ? JSON.stringify(parsedStyles) : ''
|
|
130
|
+
|
|
131
|
+
let dependencies: Array<UnistyleDependency> = []
|
|
132
|
+
let disposeListener: VoidFunction = () => {}
|
|
133
|
+
|
|
134
|
+
const onChange = () => {
|
|
135
|
+
if (!TRACK_DEPENDENCIES) {
|
|
136
|
+
this.services.registry.applyStyles(hash, getParsedStyles())
|
|
137
|
+
|
|
138
|
+
return
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const { styles, runtimeDependencies } = getParsedStylesWithDependencies()
|
|
142
|
+
const newStyles = JSON.stringify(styles)
|
|
143
|
+
|
|
144
|
+
if (newStyles !== lastStyles) {
|
|
145
|
+
lastStyles = newStyles
|
|
146
|
+
this.services.registry.applyStyles(hash, styles)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
listenToNewDependencies(runtimeDependencies)
|
|
150
|
+
}
|
|
151
|
+
const listen = (newDependencies: Array<UnistyleDependency>) => {
|
|
152
|
+
disposeListener()
|
|
153
|
+
dependencies = newDependencies
|
|
154
|
+
disposeListener = this.services.listener.addListeners(dependencies, onChange)
|
|
155
|
+
}
|
|
156
|
+
// eg. a dependency read behind a condition
|
|
157
|
+
const listenToNewDependencies = (runtimeDependencies: Array<UnistyleDependency>) => {
|
|
158
|
+
const newDependencies = filterDependencies(dependencies.concat(runtimeDependencies))
|
|
159
|
+
|
|
160
|
+
if (newDependencies.length !== dependencies.length) {
|
|
161
|
+
listen(newDependencies)
|
|
162
|
+
}
|
|
163
|
+
}
|
|
88
164
|
|
|
89
165
|
if (!existingHash) {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
166
|
+
listen(filterDependencies(extractUnistyleDependencies(parsedStyles).concat(runtimeDependencies)))
|
|
167
|
+
|
|
168
|
+
// a StyleSheet that reruns for another view (eg. for its dynamic function) may read something new
|
|
169
|
+
const unwatchStylesheets = TRACK_DEPENDENCIES
|
|
170
|
+
? Array.from(stylesheets).map((stylesheet) =>
|
|
171
|
+
this.services.registry.watchStylesheetDependencies(stylesheet, () =>
|
|
172
|
+
listenToNewDependencies(this.services.registry.getStylesheetDependencies(stylesheet)),
|
|
173
|
+
),
|
|
174
|
+
)
|
|
175
|
+
: []
|
|
176
|
+
|
|
177
|
+
this.disposeMap.set(hash, () => {
|
|
178
|
+
disposeListener()
|
|
179
|
+
unwatchStylesheets.forEach((unwatch) => unwatch())
|
|
180
|
+
})
|
|
96
181
|
}
|
|
97
182
|
|
|
98
183
|
const hashClassname = forChild ? hash.replace(' > *', '') : hash
|
package/src/web/types.ts
CHANGED
|
@@ -16,6 +16,9 @@ export type UnistylesServices = {
|
|
|
16
16
|
export const UNI_GENERATED_KEYS = ['$$css', 'hash', 'injectedClassName'] as const
|
|
17
17
|
export type UniGeneratedKey = (typeof UNI_GENERATED_KEYS)[number]
|
|
18
18
|
|
|
19
|
+
// Required for the Metro integration: marks display: contents elements for the forChild selector
|
|
20
|
+
export const CONTENTS_CLASS_NAME = 'unistyles-contents'
|
|
21
|
+
|
|
19
22
|
export type UniGeneratedStyle = Record<UniGeneratedKey, string> & {
|
|
20
23
|
parsedStyles?: UnistylesValues
|
|
21
24
|
}
|