react-native-unistyles 3.0.0-rc.1 → 3.0.0-rc.3
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 +18 -18
- package/Unistyles.podspec +1 -0
- package/android/src/main/java/com/unistyles/NativePlatform+android.kt +5 -1
- package/android/src/main/java/com/unistyles/NativePlatform+insets.kt +2 -3
- package/components/native/ActivityIndicator/package.json +1 -0
- package/components/native/Animated/package.json +1 -0
- package/components/native/FlatList/package.json +1 -0
- package/components/native/Image/package.json +2 -1
- package/components/native/ImageBackground/package.json +1 -0
- package/components/native/KeyboardAvoidingView/package.json +1 -0
- package/components/native/NativeText/package.json +1 -0
- package/components/native/NativeView/package.json +1 -0
- package/components/native/Pressable/package.json +1 -0
- package/components/native/RefreshControl/package.json +1 -0
- package/components/native/ScrollView/package.json +1 -0
- package/components/native/SectionList/package.json +1 -0
- package/components/native/Switch/package.json +1 -0
- package/components/native/Text/package.json +1 -0
- package/components/native/TextInput/package.json +1 -0
- package/components/native/TouchableHighlight/package.json +1 -0
- package/components/native/TouchableOpacity/package.json +1 -0
- package/components/native/View/package.json +1 -0
- package/components/native/VirtualizedList/package.json +1 -0
- package/ios/NativePlatform+ios.swift +1 -1
- package/ios/NativePlatformListener+ios.swift +12 -7
- package/lib/commonjs/components/native/Image.js +4 -3
- package/lib/commonjs/components/native/Image.js.map +1 -1
- package/lib/commonjs/components/native/ImageBackground.js +3 -2
- package/lib/commonjs/components/native/ImageBackground.js.map +1 -1
- package/lib/commonjs/core/createUnistylesElement.js +17 -5
- package/lib/commonjs/core/createUnistylesElement.js.map +1 -1
- package/lib/commonjs/core/getClassname.js +10 -4
- package/lib/commonjs/core/getClassname.js.map +1 -1
- package/lib/commonjs/core/useProxifiedUnistyles/listener.js +5 -3
- package/lib/commonjs/core/useProxifiedUnistyles/listener.js.map +1 -1
- package/lib/commonjs/core/useProxifiedUnistyles/useProxifiedUnistyles.js +2 -1
- package/lib/commonjs/core/useProxifiedUnistyles/useProxifiedUnistyles.js.map +1 -1
- package/lib/commonjs/core/withUnistyles/withUnistyles.js +2 -1
- package/lib/commonjs/core/withUnistyles/withUnistyles.js.map +1 -1
- package/lib/commonjs/hooks/useMedia.js +4 -4
- package/lib/commonjs/hooks/useMedia.js.map +1 -1
- package/lib/commonjs/hooks/useMedia.native.js +4 -4
- package/lib/commonjs/hooks/useMedia.native.js.map +1 -1
- package/lib/commonjs/mocks.js +203 -0
- package/lib/commonjs/mocks.js.map +1 -0
- package/lib/commonjs/mq.js +1 -23
- package/lib/commonjs/mq.js.map +1 -1
- package/lib/commonjs/server/getServerUnistyles.js +5 -3
- package/lib/commonjs/server/getServerUnistyles.js.map +1 -1
- package/lib/commonjs/server/hydrateServerUnistyles.js +4 -2
- package/lib/commonjs/server/hydrateServerUnistyles.js.map +1 -1
- package/lib/commonjs/server/resetServerUnistyles.js +4 -2
- package/lib/commonjs/server/resetServerUnistyles.js.map +1 -1
- package/lib/commonjs/specs/NativePlatform/NativePlatform.nitro.js +1 -0
- package/lib/commonjs/specs/NativePlatform/NativePlatform.nitro.js.map +1 -1
- package/lib/commonjs/utils.js +23 -1
- package/lib/commonjs/utils.js.map +1 -1
- package/lib/commonjs/web/convert/index.js +4 -7
- package/lib/commonjs/web/convert/index.js.map +1 -1
- package/lib/commonjs/web/convert/object/filter.js +7 -8
- package/lib/commonjs/web/convert/object/filter.js.map +1 -1
- package/lib/commonjs/web/create.js +6 -4
- package/lib/commonjs/web/create.js.map +1 -1
- package/lib/commonjs/web/css/core.js +2 -2
- package/lib/commonjs/web/css/core.js.map +1 -1
- package/lib/commonjs/web/css/state.js +1 -1
- package/lib/commonjs/web/css/state.js.map +1 -1
- package/lib/commonjs/web/index.js +7 -12
- package/lib/commonjs/web/index.js.map +1 -1
- package/lib/commonjs/web/services.js +7 -2
- package/lib/commonjs/web/services.js.map +1 -1
- package/lib/commonjs/web/shadowRegistry.js +11 -3
- package/lib/commonjs/web/shadowRegistry.js.map +1 -1
- package/lib/commonjs/web/utils/createUnistylesRef.js +7 -4
- package/lib/commonjs/web/utils/createUnistylesRef.js.map +1 -1
- package/lib/commonjs/web/utils/index.js +0 -11
- package/lib/commonjs/web/utils/index.js.map +1 -1
- package/lib/commonjs/web/utils/unistyle.js +35 -12
- package/lib/commonjs/web/utils/unistyle.js.map +1 -1
- package/lib/module/components/native/Image.js +4 -3
- package/lib/module/components/native/Image.js.map +1 -1
- package/lib/module/components/native/ImageBackground.js +2 -1
- package/lib/module/components/native/ImageBackground.js.map +1 -1
- package/lib/module/core/createUnistylesElement.js +18 -6
- package/lib/module/core/createUnistylesElement.js.map +1 -1
- package/lib/module/core/getClassname.js +8 -4
- package/lib/module/core/getClassname.js.map +1 -1
- package/lib/module/core/useProxifiedUnistyles/listener.js +3 -3
- package/lib/module/core/useProxifiedUnistyles/listener.js.map +1 -1
- package/lib/module/core/useProxifiedUnistyles/useProxifiedUnistyles.js +2 -1
- package/lib/module/core/useProxifiedUnistyles/useProxifiedUnistyles.js.map +1 -1
- package/lib/module/core/withUnistyles/withUnistyles.js +2 -1
- package/lib/module/core/withUnistyles/withUnistyles.js.map +1 -1
- package/lib/module/hooks/useMedia.js +1 -1
- package/lib/module/hooks/useMedia.js.map +1 -1
- package/lib/module/hooks/useMedia.native.js +1 -1
- package/lib/module/hooks/useMedia.native.js.map +1 -1
- package/lib/module/mocks.js +201 -0
- package/lib/module/mocks.js.map +1 -0
- package/lib/module/mq.js +0 -19
- package/lib/module/mq.js.map +1 -1
- package/lib/module/server/getServerUnistyles.js +3 -3
- package/lib/module/server/getServerUnistyles.js.map +1 -1
- package/lib/module/server/hydrateServerUnistyles.js +2 -2
- package/lib/module/server/hydrateServerUnistyles.js.map +1 -1
- package/lib/module/server/resetServerUnistyles.js +2 -2
- package/lib/module/server/resetServerUnistyles.js.map +1 -1
- package/lib/module/specs/NativePlatform/NativePlatform.nitro.js +1 -0
- package/lib/module/specs/NativePlatform/NativePlatform.nitro.js.map +1 -1
- package/lib/module/utils.js +19 -0
- package/lib/module/utils.js.map +1 -1
- package/lib/module/web/convert/index.js +4 -7
- package/lib/module/web/convert/index.js.map +1 -1
- package/lib/module/web/convert/object/filter.js +3 -4
- package/lib/module/web/convert/object/filter.js.map +1 -1
- package/lib/module/web/create.js +4 -4
- package/lib/module/web/create.js.map +1 -1
- package/lib/module/web/css/core.js +2 -2
- package/lib/module/web/css/core.js.map +1 -1
- package/lib/module/web/css/state.js +1 -1
- package/lib/module/web/css/state.js.map +1 -1
- package/lib/module/web/index.js +4 -10
- package/lib/module/web/index.js.map +1 -1
- package/lib/module/web/services.js +7 -1
- package/lib/module/web/services.js.map +1 -1
- package/lib/module/web/shadowRegistry.js +10 -2
- package/lib/module/web/shadowRegistry.js.map +1 -1
- package/lib/module/web/utils/createUnistylesRef.js +5 -4
- package/lib/module/web/utils/createUnistylesRef.js.map +1 -1
- package/lib/module/web/utils/index.js +0 -1
- package/lib/module/web/utils/index.js.map +1 -1
- package/lib/module/web/utils/unistyle.js +28 -9
- package/lib/module/web/utils/unistyle.js.map +1 -1
- package/lib/typescript/src/components/native/Image.d.ts.map +1 -1
- package/lib/typescript/src/components/native/ImageBackground.d.ts.map +1 -1
- package/lib/typescript/src/core/createUnistylesElement.d.ts.map +1 -1
- package/lib/typescript/src/core/getClassname.d.ts +5 -5
- package/lib/typescript/src/core/getClassname.d.ts.map +1 -1
- package/lib/typescript/src/core/useProxifiedUnistyles/useProxifiedUnistyles.d.ts.map +1 -1
- package/lib/typescript/src/core/withUnistyles/withUnistyles.d.ts.map +1 -1
- package/lib/typescript/src/mocks.d.ts +2 -0
- package/lib/typescript/src/mocks.d.ts.map +1 -0
- package/lib/typescript/src/mq.d.ts +0 -8
- package/lib/typescript/src/mq.d.ts.map +1 -1
- package/lib/typescript/src/server/hydrateServerUnistyles.d.ts +0 -1
- package/lib/typescript/src/server/hydrateServerUnistyles.d.ts.map +1 -1
- package/lib/typescript/src/server/resetServerUnistyles.d.ts.map +1 -1
- package/lib/typescript/src/specs/NativePlatform/NativePlatform.nitro.d.ts +2 -1
- package/lib/typescript/src/specs/NativePlatform/NativePlatform.nitro.d.ts.map +1 -1
- package/lib/typescript/src/utils.d.ts +8 -0
- package/lib/typescript/src/utils.d.ts.map +1 -1
- package/lib/typescript/src/web/convert/index.d.ts +2 -1
- package/lib/typescript/src/web/convert/index.d.ts.map +1 -1
- package/lib/typescript/src/web/convert/object/filter.d.ts +2 -1
- package/lib/typescript/src/web/convert/object/filter.d.ts.map +1 -1
- package/lib/typescript/src/web/index.d.ts +0 -5
- package/lib/typescript/src/web/index.d.ts.map +1 -1
- package/lib/typescript/src/web/services.d.ts +6 -1
- package/lib/typescript/src/web/services.d.ts.map +1 -1
- package/lib/typescript/src/web/shadowRegistry.d.ts.map +1 -1
- package/lib/typescript/src/web/utils/createUnistylesRef.d.ts +8 -5
- package/lib/typescript/src/web/utils/createUnistylesRef.d.ts.map +1 -1
- package/lib/typescript/src/web/utils/index.d.ts +0 -1
- package/lib/typescript/src/web/utils/index.d.ts.map +1 -1
- package/lib/typescript/src/web/utils/unistyle.d.ts +3 -1
- package/lib/typescript/src/web/utils/unistyle.d.ts.map +1 -1
- package/nitrogen/generated/android/c++/JUnistyleDependency.hpp +3 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/unistyles/UnistyleDependency.kt +2 -1
- package/nitrogen/generated/ios/swift/UnistyleDependency.swift +4 -0
- package/nitrogen/generated/shared/c++/UnistyleDependency.hpp +2 -1
- package/package.json +23 -9
- package/plugin/index.js +137 -65
- package/server/package.json +6 -0
- package/src/components/native/Image.tsx +4 -3
- package/src/components/native/ImageBackground.tsx +2 -1
- package/src/core/createUnistylesElement.tsx +26 -9
- package/src/core/getClassname.ts +14 -4
- package/src/core/useProxifiedUnistyles/listener.ts +3 -3
- package/src/core/useProxifiedUnistyles/useProxifiedUnistyles.ts +1 -0
- package/src/core/withUnistyles/withUnistyles.tsx +2 -1
- package/src/hooks/useMedia.native.ts +1 -1
- package/src/hooks/useMedia.ts +1 -1
- package/src/mocks.ts +217 -0
- package/src/mq.ts +0 -24
- package/src/server/getServerUnistyles.tsx +3 -3
- package/src/server/hydrateServerUnistyles.ts +3 -2
- package/src/server/resetServerUnistyles.ts +4 -3
- package/src/specs/NativePlatform/NativePlatform.nitro.ts +2 -1
- package/src/utils.ts +25 -0
- package/src/web/convert/index.ts +5 -7
- package/src/web/convert/object/filter.ts +4 -4
- package/src/web/create.ts +4 -4
- package/src/web/css/core.ts +2 -2
- package/src/web/css/state.ts +1 -1
- package/src/web/index.ts +4 -16
- package/src/web/services.ts +14 -1
- package/src/web/shadowRegistry.ts +10 -2
- package/src/web/utils/createUnistylesRef.ts +12 -8
- package/src/web/utils/index.ts +0 -1
- package/src/web/utils/unistyle.ts +50 -21
package/plugin/index.js
CHANGED
@@ -85,20 +85,19 @@ var NATIVE_COMPONENTS_PATHS = {
|
|
85
85
|
var t = __toESM(require("@babel/types"));
|
86
86
|
|
87
87
|
// plugin/src/paths.ts
|
88
|
-
var path = __toESM(require("node:path"));
|
89
88
|
var isWindows = process.platform === "win32";
|
90
89
|
var toWinPath = (pathString) => {
|
91
|
-
return
|
90
|
+
return pathString;
|
92
91
|
};
|
93
92
|
var toPlatformPath = (pathString) => {
|
94
93
|
return isWindows ? toWinPath(pathString) : pathString;
|
95
94
|
};
|
96
95
|
|
97
96
|
// plugin/src/exotic.ts
|
98
|
-
function handleExoticImport(
|
99
|
-
const specifiers =
|
100
|
-
const source =
|
101
|
-
if (
|
97
|
+
function handleExoticImport(path, state, exoticImport) {
|
98
|
+
const specifiers = path.node.specifiers;
|
99
|
+
const source = path.node.source;
|
100
|
+
if (path.node.importKind !== "value") {
|
102
101
|
return;
|
103
102
|
}
|
104
103
|
specifiers.forEach((specifier) => {
|
@@ -116,7 +115,7 @@ function handleExoticImport(path2, state, exoticImport) {
|
|
116
115
|
state.opts.isLocal ? state.file.opts.filename?.split("react-native-unistyles").at(0)?.concat(toPlatformPath(`react-native-unistyles/components/native/${rule.mapTo}`)) ?? "" : toPlatformPath(`react-native-unistyles/components/native/${rule.mapTo}`)
|
117
116
|
)
|
118
117
|
);
|
119
|
-
|
118
|
+
path.replaceWith(newImport);
|
120
119
|
} else {
|
121
120
|
const newImport = t.importDeclaration(
|
122
121
|
[t.importSpecifier(t.identifier(rule.mapTo), t.identifier(rule.mapTo))],
|
@@ -124,11 +123,11 @@ function handleExoticImport(path2, state, exoticImport) {
|
|
124
123
|
state.opts.isLocal ? state.file.opts.filename?.split("react-native-unistyles").at(0)?.concat(toPlatformPath(`react-native-unistyles/components/native/${rule.mapTo}`)) ?? "" : toPlatformPath(`react-native-unistyles/components/native/${rule.mapTo}`)
|
125
124
|
)
|
126
125
|
);
|
127
|
-
|
128
|
-
if (
|
129
|
-
|
126
|
+
path.node.specifiers = specifiers.filter((s) => s !== specifier);
|
127
|
+
if (path.node.specifiers.length === 0) {
|
128
|
+
path.replaceWith(newImport);
|
130
129
|
} else {
|
131
|
-
|
130
|
+
path.insertBefore(newImport);
|
132
131
|
}
|
133
132
|
}
|
134
133
|
return;
|
@@ -138,12 +137,12 @@ function handleExoticImport(path2, state, exoticImport) {
|
|
138
137
|
|
139
138
|
// plugin/src/import.ts
|
140
139
|
var t2 = __toESM(require("@babel/types"));
|
141
|
-
function addUnistylesImport(
|
140
|
+
function addUnistylesImport(path, state) {
|
142
141
|
const localNames = Object.keys(state.reactNativeImports);
|
143
142
|
const names = Object.values(state.reactNativeImports);
|
144
143
|
const pairs = Object.entries(state.reactNativeImports);
|
145
144
|
const nodesToRemove = [];
|
146
|
-
|
145
|
+
path.node.body.forEach((node) => {
|
147
146
|
if (t2.isImportDeclaration(node) && node.source.value === "react-native") {
|
148
147
|
node.specifiers = node.specifiers.filter((specifier) => !localNames.some((name) => name === specifier.local.name));
|
149
148
|
if (node.specifiers.length === 0) {
|
@@ -152,7 +151,7 @@ function addUnistylesImport(path2, state) {
|
|
152
151
|
}
|
153
152
|
});
|
154
153
|
names.forEach((name) => {
|
155
|
-
const rnWebImport =
|
154
|
+
const rnWebImport = path.node.body.find((node) => t2.isImportDeclaration(node) && node.source.value === toPlatformPath(`react-native-web/dist/exports/${name}`));
|
156
155
|
if (rnWebImport) {
|
157
156
|
rnWebImport.specifiers = [];
|
158
157
|
}
|
@@ -164,18 +163,31 @@ function addUnistylesImport(path2, state) {
|
|
164
163
|
state.opts.isLocal ? state.file.opts.filename?.split("react-native-unistyles").at(0)?.concat(toPlatformPath(`react-native-unistyles/src/components/native/${name}`)) ?? "" : toPlatformPath(`react-native-unistyles/components/native/${name}`)
|
165
164
|
)
|
166
165
|
);
|
167
|
-
|
166
|
+
path.node.body.unshift(newImport);
|
168
167
|
});
|
169
|
-
nodesToRemove.forEach((node) =>
|
168
|
+
nodesToRemove.forEach((node) => path.node.body.splice(path.node.body.indexOf(node), 1));
|
170
169
|
}
|
171
170
|
function isInsideNodeModules(state) {
|
172
|
-
return state.file.opts.filename?.includes("node_modules");
|
171
|
+
return state.file.opts.filename?.includes("node_modules") && !state.file.replaceWithUnistyles;
|
172
|
+
}
|
173
|
+
function addUnistylesRequire(path, state) {
|
174
|
+
Object.entries(state.reactNativeImports).forEach(([componentName, uniqueName]) => {
|
175
|
+
const newRequire = t2.variableDeclaration("const", [
|
176
|
+
t2.variableDeclarator(
|
177
|
+
t2.identifier(uniqueName),
|
178
|
+
t2.callExpression(t2.identifier("require"), [
|
179
|
+
t2.stringLiteral(toPlatformPath(`react-native-unistyles/src/components/native/${componentName}`))
|
180
|
+
])
|
181
|
+
)
|
182
|
+
]);
|
183
|
+
path.node.body.unshift(newRequire);
|
184
|
+
});
|
173
185
|
}
|
174
186
|
|
175
187
|
// plugin/src/ref.ts
|
176
188
|
var t3 = __toESM(require("@babel/types"));
|
177
|
-
function hasStringRef(
|
178
|
-
return
|
189
|
+
function hasStringRef(path) {
|
190
|
+
return path.node.openingElement.attributes.find(
|
179
191
|
(attr) => t3.isJSXAttribute(attr) && t3.isJSXIdentifier(attr.name, { name: "ref" }) && t3.isStringLiteral(attr.value)
|
180
192
|
);
|
181
193
|
}
|
@@ -197,7 +209,8 @@ var UnistyleDependency = {
|
|
197
209
|
FontScale: 11,
|
198
210
|
StatusBar: 12,
|
199
211
|
NavigationBar: 13,
|
200
|
-
Ime: 14
|
212
|
+
Ime: 14,
|
213
|
+
Rtl: 15
|
201
214
|
};
|
202
215
|
function getProperty(property) {
|
203
216
|
if (!property) {
|
@@ -280,6 +293,9 @@ function toUnistylesDependency(dependency) {
|
|
280
293
|
case "variants": {
|
281
294
|
return UnistyleDependency.Variants;
|
282
295
|
}
|
296
|
+
case "rtl": {
|
297
|
+
return UnistyleDependency.Rtl;
|
298
|
+
}
|
283
299
|
default:
|
284
300
|
return null;
|
285
301
|
}
|
@@ -308,28 +324,47 @@ function stringToUniqueId(str) {
|
|
308
324
|
const absHash = Math.abs(hash);
|
309
325
|
return absHash % 1e9;
|
310
326
|
}
|
311
|
-
function isUnistylesStyleSheet(
|
312
|
-
const { callee } =
|
313
|
-
if (t4.isMemberExpression(callee)
|
314
|
-
return
|
327
|
+
function isUnistylesStyleSheet(path, state) {
|
328
|
+
const { callee } = path.node;
|
329
|
+
if (!t4.isMemberExpression(callee) || !t4.isIdentifier(callee.property)) {
|
330
|
+
return false;
|
331
|
+
}
|
332
|
+
const isImport = callee.property.name === "create" && t4.isIdentifier(callee.object) && callee.object.name === state.file.styleSheetLocalName;
|
333
|
+
const isRequire = state.file.hasUnistylesImport && callee.property.name === "create" && t4.isMemberExpression(callee.object) && t4.isIdentifier(callee.object.property) && t4.isIdentifier(callee.object.object) && callee.object.object.name === state.file.styleSheetLocalName && callee.object.property.name === "StyleSheet";
|
334
|
+
return isImport || isRequire;
|
335
|
+
}
|
336
|
+
function isUnistylesCommonJSRequire(path, state) {
|
337
|
+
const isRequire = t4.isIdentifier(path.node.callee) && path.node.arguments.length > 0 && t4.isStringLiteral(path.node.arguments[0]) && path.node.arguments[0].value === "react-native-unistyles";
|
338
|
+
if (isRequire && t4.isVariableDeclarator(path.parent) && t4.isIdentifier(path.parent.id)) {
|
339
|
+
state.file.hasUnistylesImport = true;
|
340
|
+
state.file.styleSheetLocalName = path.parent.id.name;
|
315
341
|
}
|
316
|
-
return
|
342
|
+
return isRequire;
|
317
343
|
}
|
318
|
-
function
|
344
|
+
function isReactNativeCommonJSRequire(path, state) {
|
345
|
+
const isRequire = t4.isIdentifier(path.node.callee) && path.node.arguments.length > 0 && path.node.callee.name === "require";
|
346
|
+
const requireImportName = path.node.arguments.find((node) => t4.isStringLiteral(node));
|
347
|
+
const isReactNativeRequire = isRequire && requireImportName && (requireImportName.value === "react-native" || requireImportName.value === "react-native-web/dist/index");
|
348
|
+
if (isReactNativeRequire && t4.isVariableDeclarator(path.parent) && t4.isIdentifier(path.parent.id)) {
|
349
|
+
state.file.reactNativeCommonJSName = path.parent.id.name;
|
350
|
+
}
|
351
|
+
return isRequire;
|
352
|
+
}
|
353
|
+
function isKindOfStyleSheet(path, state) {
|
319
354
|
if (!state.file.forceProcessing && !state.file.hasUnistylesImport) {
|
320
355
|
return false;
|
321
356
|
}
|
322
|
-
const { callee } =
|
357
|
+
const { callee } = path.node;
|
323
358
|
return t4.isMemberExpression(callee) && t4.isIdentifier(callee.property) && callee.property.name === "create" && t4.isIdentifier(callee.object);
|
324
359
|
}
|
325
|
-
function addStyleSheetTag(
|
360
|
+
function addStyleSheetTag(path, state) {
|
326
361
|
const str = state.filename?.replace(state.cwd, "") ?? "";
|
327
362
|
const uniqueId = stringToUniqueId(str) + ++state.file.tagNumber;
|
328
|
-
|
363
|
+
path.node.arguments.push(t4.numericLiteral(uniqueId));
|
329
364
|
}
|
330
|
-
function getStylesDependenciesFromObject(
|
365
|
+
function getStylesDependenciesFromObject(path) {
|
331
366
|
const detectedStylesWithVariants = /* @__PURE__ */ new Set();
|
332
|
-
const stylesheet =
|
367
|
+
const stylesheet = path.node.arguments[0];
|
333
368
|
if (t4.isObjectExpression(stylesheet)) {
|
334
369
|
stylesheet?.properties.forEach((property) => {
|
335
370
|
if (!t4.isObjectProperty(property) || !t4.isIdentifier(property.key)) {
|
@@ -628,8 +663,8 @@ function addDependencies(state, styleName, unistyle, detectedDependencies) {
|
|
628
663
|
|
629
664
|
// plugin/src/variants.ts
|
630
665
|
var t5 = __toESM(require("@babel/types"));
|
631
|
-
function extractVariants(
|
632
|
-
const maybeVariants =
|
666
|
+
function extractVariants(path, state) {
|
667
|
+
const maybeVariants = path.node.body.filter((node2) => t5.isExpressionStatement(node2) && t5.isCallExpression(node2.expression) && t5.isMemberExpression(node2.expression.callee));
|
633
668
|
if (maybeVariants.length === 0) {
|
634
669
|
return;
|
635
670
|
}
|
@@ -652,7 +687,7 @@ function extractVariants(path2, state) {
|
|
652
687
|
return;
|
653
688
|
}
|
654
689
|
const calleeName = callee.object.name;
|
655
|
-
const newUniqueName =
|
690
|
+
const newUniqueName = path.scope.generateUidIdentifier(calleeName);
|
656
691
|
const shadowDeclaration = t5.variableDeclaration("const", [
|
657
692
|
t5.variableDeclarator(newUniqueName, t5.identifier(calleeName))
|
658
693
|
]);
|
@@ -663,14 +698,14 @@ function extractVariants(path2, state) {
|
|
663
698
|
const finalDeclaration = t5.variableDeclaration("const", [
|
664
699
|
t5.variableDeclarator(t5.identifier(calleeName), newCallExpression)
|
665
700
|
]);
|
666
|
-
const pathIndex =
|
667
|
-
const rest =
|
701
|
+
const pathIndex = path.node.body.findIndex((bodyPath) => bodyPath === targetVariant);
|
702
|
+
const rest = path.node.body.slice(pathIndex + 1);
|
668
703
|
const statement = t5.blockStatement([
|
669
704
|
finalDeclaration,
|
670
705
|
...rest
|
671
706
|
]);
|
672
|
-
|
673
|
-
...
|
707
|
+
path.node.body = [
|
708
|
+
...path.node.body.slice(0, pathIndex),
|
674
709
|
shadowDeclaration,
|
675
710
|
statement
|
676
711
|
];
|
@@ -679,20 +714,28 @@ function extractVariants(path2, state) {
|
|
679
714
|
|
680
715
|
// plugin/src/index.ts
|
681
716
|
function index_default() {
|
717
|
+
if (process.env.NODE_ENV === "test") {
|
718
|
+
return {
|
719
|
+
name: "babel-react-native-unistyles",
|
720
|
+
visitor: {}
|
721
|
+
};
|
722
|
+
}
|
682
723
|
return {
|
683
724
|
name: "babel-react-native-unistyles",
|
684
725
|
visitor: {
|
685
726
|
Program: {
|
686
|
-
enter(
|
687
|
-
state.file.replaceWithUnistyles = REPLACE_WITH_UNISTYLES_PATHS.map(toPlatformPath).concat(state.opts.autoProcessPaths ?? []).some((
|
727
|
+
enter(path, state) {
|
728
|
+
state.file.replaceWithUnistyles = REPLACE_WITH_UNISTYLES_PATHS.map(toPlatformPath).concat(state.opts.autoProcessPaths ?? []).some((path2) => state.filename?.includes(path2));
|
688
729
|
state.file.hasAnyUnistyle = false;
|
689
730
|
state.file.hasUnistylesImport = false;
|
731
|
+
state.file.addUnistylesRequire = false;
|
690
732
|
state.file.hasVariants = false;
|
691
733
|
state.file.styleSheetLocalName = "";
|
734
|
+
state.file.reactNativeCommonJSName = "";
|
692
735
|
state.file.tagNumber = 0;
|
693
736
|
state.reactNativeImports = {};
|
694
737
|
state.file.forceProcessing = state.opts.autoProcessRoot && state.filename ? state.filename.includes(toPlatformPath(`${state.file.opts.root}/${state.opts.autoProcessRoot}/`)) : false;
|
695
|
-
|
738
|
+
path.traverse({
|
696
739
|
BlockStatement(blockPath) {
|
697
740
|
if (isInsideNodeModules(state)) {
|
698
741
|
return;
|
@@ -701,38 +744,41 @@ function index_default() {
|
|
701
744
|
}
|
702
745
|
});
|
703
746
|
},
|
704
|
-
exit(
|
705
|
-
if (isInsideNodeModules(state)
|
747
|
+
exit(path, state) {
|
748
|
+
if (isInsideNodeModules(state)) {
|
706
749
|
return;
|
707
750
|
}
|
751
|
+
if (state.file.addUnistylesRequire) {
|
752
|
+
return addUnistylesRequire(path, state);
|
753
|
+
}
|
708
754
|
if (state.file.hasAnyUnistyle || state.file.hasVariants || state.file.replaceWithUnistyles || state.file.forceProcessing) {
|
709
|
-
addUnistylesImport(
|
755
|
+
addUnistylesImport(path, state);
|
710
756
|
}
|
711
757
|
}
|
712
758
|
},
|
713
|
-
FunctionDeclaration(
|
759
|
+
FunctionDeclaration(path, state) {
|
714
760
|
if (isInsideNodeModules(state)) {
|
715
761
|
return;
|
716
762
|
}
|
717
|
-
const componentName =
|
763
|
+
const componentName = path.node.id ? path.node.id.name : null;
|
718
764
|
if (componentName) {
|
719
765
|
state.file.hasVariants = false;
|
720
766
|
}
|
721
767
|
},
|
722
|
-
ClassDeclaration(
|
768
|
+
ClassDeclaration(path, state) {
|
723
769
|
if (isInsideNodeModules(state)) {
|
724
770
|
return;
|
725
771
|
}
|
726
|
-
const componentName =
|
772
|
+
const componentName = path.node.id ? path.node.id.name : null;
|
727
773
|
if (componentName) {
|
728
774
|
state.file.hasVariants = false;
|
729
775
|
}
|
730
776
|
},
|
731
|
-
VariableDeclaration(
|
777
|
+
VariableDeclaration(path, state) {
|
732
778
|
if (isInsideNodeModules(state)) {
|
733
779
|
return;
|
734
780
|
}
|
735
|
-
|
781
|
+
path.node.declarations.forEach((declaration) => {
|
736
782
|
if (t6.isArrowFunctionExpression(declaration.init) || t6.isFunctionExpression(declaration.init)) {
|
737
783
|
const componentName = declaration.id && t6.isIdentifier(declaration.id) ? declaration.id.name : null;
|
738
784
|
if (componentName) {
|
@@ -741,57 +787,83 @@ function index_default() {
|
|
741
787
|
}
|
742
788
|
});
|
743
789
|
},
|
744
|
-
ImportDeclaration(
|
790
|
+
ImportDeclaration(path, state) {
|
745
791
|
const exoticImport = REPLACE_WITH_UNISTYLES_EXOTIC_PATHS.concat(state.opts.autoRemapImports ?? []).find((exotic) => state.filename?.includes(exotic.path));
|
746
792
|
if (exoticImport) {
|
747
|
-
return handleExoticImport(
|
793
|
+
return handleExoticImport(path, state, exoticImport);
|
748
794
|
}
|
749
|
-
if (isInsideNodeModules(state)
|
795
|
+
if (isInsideNodeModules(state)) {
|
750
796
|
return;
|
751
797
|
}
|
752
|
-
const importSource =
|
798
|
+
const importSource = path.node.source.value;
|
753
799
|
if (importSource.includes("react-native-unistyles")) {
|
754
800
|
state.file.hasUnistylesImport = true;
|
755
|
-
|
801
|
+
path.node.specifiers.forEach((specifier) => {
|
756
802
|
if (t6.isImportSpecifier(specifier) && t6.isIdentifier(specifier.imported) && specifier.imported.name === "StyleSheet") {
|
757
803
|
state.file.styleSheetLocalName = specifier.local.name;
|
758
804
|
}
|
759
805
|
});
|
760
806
|
}
|
761
807
|
if (importSource === "react-native") {
|
762
|
-
|
808
|
+
path.node.specifiers.forEach((specifier) => {
|
763
809
|
if (t6.isImportSpecifier(specifier) && t6.isIdentifier(specifier.imported) && REACT_NATIVE_COMPONENT_NAMES.includes(specifier.imported.name)) {
|
764
810
|
state.reactNativeImports[specifier.local.name] = specifier.imported.name;
|
765
811
|
}
|
766
812
|
});
|
767
813
|
}
|
768
814
|
if (importSource.includes(toPlatformPath("react-native/Libraries"))) {
|
769
|
-
handleExoticImport(
|
815
|
+
handleExoticImport(path, state, NATIVE_COMPONENTS_PATHS);
|
770
816
|
}
|
771
817
|
if (!state.file.forceProcessing && Array.isArray(state.opts.autoProcessImports)) {
|
772
818
|
state.file.forceProcessing = state.opts.autoProcessImports.includes(importSource);
|
773
819
|
}
|
774
820
|
},
|
775
|
-
JSXElement(
|
821
|
+
JSXElement(path, state) {
|
776
822
|
if (isInsideNodeModules(state)) {
|
777
823
|
return;
|
778
824
|
}
|
779
|
-
if (hasStringRef(
|
825
|
+
if (hasStringRef(path)) {
|
780
826
|
throw new Error("Detected string based ref which is not supported by Unistyles.");
|
781
827
|
}
|
782
828
|
},
|
783
|
-
|
829
|
+
MemberExpression(path, state) {
|
784
830
|
if (isInsideNodeModules(state)) {
|
785
831
|
return;
|
786
832
|
}
|
787
|
-
if (!
|
833
|
+
if (!state.file.reactNativeCommonJSName || !t6.isIdentifier(path.node.object)) {
|
834
|
+
return;
|
835
|
+
}
|
836
|
+
if (path.node.object.name !== state.file.reactNativeCommonJSName || !t6.isIdentifier(path.node.property)) {
|
837
|
+
return;
|
838
|
+
}
|
839
|
+
if (!REACT_NATIVE_COMPONENT_NAMES.includes(path.node.property.name)) {
|
840
|
+
return;
|
841
|
+
}
|
842
|
+
if (!state.reactNativeImports[path.node.property.name]) {
|
843
|
+
const uniqueId = path.scope.generateUidIdentifier(`reactNativeUnistyles_${path.node.property.name}`);
|
844
|
+
state.reactNativeImports[path.node.property.name] = uniqueId.name;
|
845
|
+
state.file.addUnistylesRequire = true;
|
846
|
+
}
|
847
|
+
path.node.object.name = state.reactNativeImports[path.node.property.name];
|
848
|
+
},
|
849
|
+
CallExpression(path, state) {
|
850
|
+
if (isInsideNodeModules(state)) {
|
851
|
+
return;
|
852
|
+
}
|
853
|
+
if (isUnistylesCommonJSRequire(path, state)) {
|
854
|
+
return;
|
855
|
+
}
|
856
|
+
if (isReactNativeCommonJSRequire(path, state)) {
|
857
|
+
return;
|
858
|
+
}
|
859
|
+
if (!isUnistylesStyleSheet(path, state) && !isKindOfStyleSheet(path, state)) {
|
788
860
|
return;
|
789
861
|
}
|
790
862
|
state.file.hasAnyUnistyle = true;
|
791
|
-
addStyleSheetTag(
|
792
|
-
const arg = t6.isAssignmentExpression(
|
863
|
+
addStyleSheetTag(path, state);
|
864
|
+
const arg = t6.isAssignmentExpression(path.node.arguments[0]) ? path.node.arguments[0].right : path.node.arguments[0];
|
793
865
|
if (t6.isObjectExpression(arg)) {
|
794
|
-
const detectedDependencies = getStylesDependenciesFromObject(
|
866
|
+
const detectedDependencies = getStylesDependenciesFromObject(path);
|
795
867
|
if (detectedDependencies) {
|
796
868
|
if (t6.isObjectExpression(arg)) {
|
797
869
|
arg.properties.forEach((property) => {
|
@@ -803,7 +875,7 @@ function index_default() {
|
|
803
875
|
}
|
804
876
|
}
|
805
877
|
if (t6.isArrowFunctionExpression(arg) || t6.isFunctionExpression(arg)) {
|
806
|
-
const funcPath = t6.isAssignmentExpression(
|
878
|
+
const funcPath = t6.isAssignmentExpression(path.node.arguments[0]) ? path.get("arguments.0.right") : path.get("arguments.0");
|
807
879
|
const detectedDependencies = getStylesDependenciesFromFunction(funcPath);
|
808
880
|
if (detectedDependencies) {
|
809
881
|
const body = t6.isBlockStatement(arg.body) ? arg.body.body.find((statement) => t6.isReturnStatement(statement))?.argument : arg.body;
|
@@ -4,7 +4,8 @@ import { getClassName } from '../../core'
|
|
4
4
|
import { maybeWarnAboutMultipleUnistyles } from '../../core/warn'
|
5
5
|
import type { UnistylesValues } from '../../types'
|
6
6
|
import { copyComponentProperties } from '../../utils'
|
7
|
-
import {
|
7
|
+
import { checkForProp } from '../../web/utils'
|
8
|
+
import { createUnistylesRef } from '../../web/utils/createUnistylesRef'
|
8
9
|
|
9
10
|
type Props = ComponentProps<typeof NativeImage> & {
|
10
11
|
style?: UnistylesValues
|
@@ -14,8 +15,8 @@ type Props = ComponentProps<typeof NativeImage> & {
|
|
14
15
|
const UnistylesImage = forwardRef<unknown, Props>((props, forwardedRef) => {
|
15
16
|
const classNames = getClassName(props.style)
|
16
17
|
const ref = createUnistylesRef(classNames, forwardedRef)
|
17
|
-
const hasWidthStyle =
|
18
|
-
const hasHeightStyle =
|
18
|
+
const hasWidthStyle = checkForProp(props.style, 'width')
|
19
|
+
const hasHeightStyle = checkForProp(props.style, 'height')
|
19
20
|
|
20
21
|
maybeWarnAboutMultipleUnistyles(props.style as ViewStyle, 'Image')
|
21
22
|
|
@@ -5,7 +5,8 @@ import { getClassName } from '../../core'
|
|
5
5
|
import { maybeWarnAboutMultipleUnistyles } from '../../core/warn'
|
6
6
|
import type { UnistylesValues } from '../../types'
|
7
7
|
import { copyComponentProperties } from '../../utils'
|
8
|
-
import {
|
8
|
+
import { keyInObject } from '../../web/utils'
|
9
|
+
import { createUnistylesRef } from '../../web/utils/createUnistylesRef'
|
9
10
|
|
10
11
|
type Props = {
|
11
12
|
style?: UnistylesValues
|
@@ -2,26 +2,43 @@ import React from 'react'
|
|
2
2
|
import type { ViewStyle } from 'react-native'
|
3
3
|
import type { UnistylesValues } from '../types'
|
4
4
|
import { copyComponentProperties } from '../utils'
|
5
|
-
import {
|
5
|
+
import { isServer } from '../web/utils'
|
6
|
+
import { createUnistylesRef } from '../web/utils/createUnistylesRef'
|
6
7
|
import { getClassName } from './getClassname'
|
7
8
|
import { maybeWarnAboutMultipleUnistyles } from './warn'
|
8
9
|
|
10
|
+
const STYLE_PROPS = ['style', 'contentContainerStyle', 'columnWrapperStyle'] as const
|
11
|
+
|
9
12
|
type ComponentProps = {
|
10
|
-
|
13
|
+
[K in typeof STYLE_PROPS[number]]?: UnistylesValues
|
11
14
|
}
|
12
15
|
|
13
|
-
|
14
|
-
const
|
15
|
-
|
16
|
-
|
16
|
+
const buildUnistylesProps = (Component: any, props: ComponentProps, forwardedRef: React.ForwardedRef<unknown>) => {
|
17
|
+
const componentStyleProps = STYLE_PROPS.filter(styleProp => styleProp in props)
|
18
|
+
const classNames = Object.fromEntries(componentStyleProps.map(styleProp => [styleProp, getClassName(props[styleProp])]))
|
19
|
+
const refs = componentStyleProps.map(styleProp => {
|
20
|
+
return createUnistylesRef(
|
21
|
+
classNames[styleProp],
|
22
|
+
styleProp === 'style' ? forwardedRef : undefined
|
23
|
+
)
|
24
|
+
})
|
17
25
|
|
18
|
-
|
26
|
+
componentStyleProps.forEach(styleProp => {
|
27
|
+
maybeWarnAboutMultipleUnistyles(props[styleProp] as ViewStyle, Component.displayName)
|
28
|
+
})
|
29
|
+
|
30
|
+
return {
|
31
|
+
...classNames,
|
32
|
+
ref: isServer() ? undefined : (componentRef: any) => refs.forEach(ref => ref?.(componentRef))
|
33
|
+
}
|
34
|
+
}
|
19
35
|
|
36
|
+
export const createUnistylesElement = (Component: any) => {
|
37
|
+
const UnistylesComponent = React.forwardRef<unknown, ComponentProps>((props, forwardedRef) => {
|
20
38
|
return (
|
21
39
|
<Component
|
22
40
|
{...props}
|
23
|
-
|
24
|
-
ref={ref}
|
41
|
+
{...buildUnistylesProps(Component, props, forwardedRef)}
|
25
42
|
/>
|
26
43
|
)
|
27
44
|
})
|
package/src/core/getClassname.ts
CHANGED
@@ -1,15 +1,25 @@
|
|
1
1
|
import type { UnistylesValues } from '../types'
|
2
|
-
import
|
2
|
+
import * as unistyles from '../web/services'
|
3
|
+
import { checkForAnimated } from '../web/utils'
|
3
4
|
|
4
5
|
export const getClassName = (unistyle: UnistylesValues | undefined | Array<UnistylesValues>, forChild?: boolean) => {
|
5
6
|
if (!unistyle) {
|
6
7
|
return undefined
|
7
8
|
}
|
8
9
|
|
9
|
-
const
|
10
|
-
|
10
|
+
const flattenedStyles = Array.isArray(unistyle) ? unistyle.flat(Number.POSITIVE_INFINITY) : [unistyle]
|
11
|
+
const animatedStyles = flattenedStyles.filter(checkForAnimated)
|
12
|
+
const regularStyles = flattenedStyles.filter(style => !checkForAnimated(style))
|
13
|
+
|
14
|
+
const { hash, injectedClassName } = unistyles.services.shadowRegistry.addStyles(
|
15
|
+
regularStyles,
|
11
16
|
forChild
|
12
17
|
)
|
13
18
|
|
14
|
-
return hash
|
19
|
+
return hash
|
20
|
+
? [
|
21
|
+
{ $$css: true, hash, injectedClassName },
|
22
|
+
animatedStyles,
|
23
|
+
] as const
|
24
|
+
: undefined
|
15
25
|
}
|
@@ -1,10 +1,10 @@
|
|
1
1
|
import { UnistyleDependency } from '../../specs'
|
2
|
-
import
|
2
|
+
import * as unistyles from '../../web/services'
|
3
3
|
import type { ListenerProps } from './types'
|
4
4
|
|
5
5
|
export const listener = ({ dependencies, updateTheme, updateRuntime }: ListenerProps) => {
|
6
|
-
const disposeTheme =
|
7
|
-
const disposeRuntime =
|
6
|
+
const disposeTheme = unistyles.services.listener.addListeners(dependencies.filter(dependency => dependency === UnistyleDependency.Theme), updateTheme)
|
7
|
+
const disposeRuntime = unistyles.services.listener.addListeners(dependencies.filter(dependency => dependency !== UnistyleDependency.Theme), updateRuntime)
|
8
8
|
|
9
9
|
return () => {
|
10
10
|
disposeTheme()
|
@@ -24,6 +24,7 @@ const RTDependencyMap = {
|
|
24
24
|
statusBar: UnistyleDependency.StatusBar,
|
25
25
|
pixelRatio: UnistyleDependency.PixelRatio,
|
26
26
|
themeName: UnistyleDependency.ThemeName,
|
27
|
+
rtl: UnistyleDependency.Rtl
|
27
28
|
} satisfies Partial<Record<keyof UnistylesMiniRuntime, UnistyleDependency>>
|
28
29
|
|
29
30
|
export const useProxifiedUnistyles = (forcedTheme?: UnistylesTheme) => {
|
@@ -56,10 +56,11 @@ export const withUnistyles = <TComponent, TMappings extends GenericComponentProp
|
|
56
56
|
maybeWarnAboutMultipleUnistyles(narrowedProps.contentContainerStyle, `withUnistyles(${Component.displayName ?? Component.name ?? 'Unknown'})`)
|
57
57
|
|
58
58
|
const NativeComponent = Component as ComponentType
|
59
|
+
const [classNames] = styleClassNames ?? []
|
59
60
|
|
60
61
|
return (
|
61
62
|
<div
|
62
|
-
className={
|
63
|
+
className={classNames?.hash}
|
63
64
|
style={{ display: 'contents' }}
|
64
65
|
>
|
65
66
|
<NativeComponent {...combinedProps} ref={ref} />
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { useEffect, useLayoutEffect, useState } from 'react'
|
2
|
-
import { isUnistylesMq, isValidMq, parseMq } from '../mq'
|
3
2
|
import { StyleSheet, UnistyleDependency, UnistylesRuntime } from '../specs'
|
3
|
+
import { isUnistylesMq, isValidMq, parseMq } from '../utils'
|
4
4
|
|
5
5
|
export const useMedia = (config: { mq: symbol }) => {
|
6
6
|
const computeIsVisible = (): boolean => {
|
package/src/hooks/useMedia.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import { useEffect, useRef, useState } from 'react'
|
2
|
-
import { isUnistylesMq, isValidMq, parseMq } from '../
|
2
|
+
import { isUnistylesMq, isValidMq, parseMq } from '../utils'
|
3
3
|
|
4
4
|
export const useMedia = (config: { mq: symbol }) => {
|
5
5
|
const disposeRef = useRef(() => {})
|