react-native-unistyles 3.0.0-rc.2 → 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 +17 -17
- package/Unistyles.podspec +1 -0
- 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 +1 -0
- 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/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/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/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/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/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/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/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/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/package.json +18 -5
- package/plugin/index.js +88 -80
- 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/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/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/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 +42 -13
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,31 +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
171
|
return state.file.opts.filename?.includes("node_modules") && !state.file.replaceWithUnistyles;
|
173
172
|
}
|
174
|
-
function addUnistylesRequire(
|
173
|
+
function addUnistylesRequire(path, state) {
|
175
174
|
Object.entries(state.reactNativeImports).forEach(([componentName, uniqueName]) => {
|
176
175
|
const newRequire = t2.variableDeclaration("const", [
|
177
176
|
t2.variableDeclarator(
|
178
177
|
t2.identifier(uniqueName),
|
179
178
|
t2.callExpression(t2.identifier("require"), [
|
180
|
-
t2.stringLiteral(`react-native-unistyles/src/components/native/${componentName}`)
|
179
|
+
t2.stringLiteral(toPlatformPath(`react-native-unistyles/src/components/native/${componentName}`))
|
181
180
|
])
|
182
181
|
)
|
183
182
|
]);
|
184
|
-
|
183
|
+
path.node.body.unshift(newRequire);
|
185
184
|
});
|
186
185
|
}
|
187
186
|
|
188
187
|
// plugin/src/ref.ts
|
189
188
|
var t3 = __toESM(require("@babel/types"));
|
190
|
-
function hasStringRef(
|
191
|
-
return
|
189
|
+
function hasStringRef(path) {
|
190
|
+
return path.node.openingElement.attributes.find(
|
192
191
|
(attr) => t3.isJSXAttribute(attr) && t3.isJSXIdentifier(attr.name, { name: "ref" }) && t3.isStringLiteral(attr.value)
|
193
192
|
);
|
194
193
|
}
|
@@ -325,8 +324,8 @@ function stringToUniqueId(str) {
|
|
325
324
|
const absHash = Math.abs(hash);
|
326
325
|
return absHash % 1e9;
|
327
326
|
}
|
328
|
-
function isUnistylesStyleSheet(
|
329
|
-
const { callee } =
|
327
|
+
function isUnistylesStyleSheet(path, state) {
|
328
|
+
const { callee } = path.node;
|
330
329
|
if (!t4.isMemberExpression(callee) || !t4.isIdentifier(callee.property)) {
|
331
330
|
return false;
|
332
331
|
}
|
@@ -334,38 +333,38 @@ function isUnistylesStyleSheet(path2, state) {
|
|
334
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";
|
335
334
|
return isImport || isRequire;
|
336
335
|
}
|
337
|
-
function isUnistylesCommonJSRequire(
|
338
|
-
const isRequire = t4.isIdentifier(
|
339
|
-
if (isRequire && t4.isVariableDeclarator(
|
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)) {
|
340
339
|
state.file.hasUnistylesImport = true;
|
341
|
-
state.file.styleSheetLocalName =
|
340
|
+
state.file.styleSheetLocalName = path.parent.id.name;
|
342
341
|
}
|
343
342
|
return isRequire;
|
344
343
|
}
|
345
|
-
function isReactNativeCommonJSRequire(
|
346
|
-
const isRequire = t4.isIdentifier(
|
347
|
-
const requireImportName =
|
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));
|
348
347
|
const isReactNativeRequire = isRequire && requireImportName && (requireImportName.value === "react-native" || requireImportName.value === "react-native-web/dist/index");
|
349
|
-
if (isReactNativeRequire && t4.isVariableDeclarator(
|
350
|
-
state.file.reactNativeCommonJSName =
|
348
|
+
if (isReactNativeRequire && t4.isVariableDeclarator(path.parent) && t4.isIdentifier(path.parent.id)) {
|
349
|
+
state.file.reactNativeCommonJSName = path.parent.id.name;
|
351
350
|
}
|
352
351
|
return isRequire;
|
353
352
|
}
|
354
|
-
function isKindOfStyleSheet(
|
353
|
+
function isKindOfStyleSheet(path, state) {
|
355
354
|
if (!state.file.forceProcessing && !state.file.hasUnistylesImport) {
|
356
355
|
return false;
|
357
356
|
}
|
358
|
-
const { callee } =
|
357
|
+
const { callee } = path.node;
|
359
358
|
return t4.isMemberExpression(callee) && t4.isIdentifier(callee.property) && callee.property.name === "create" && t4.isIdentifier(callee.object);
|
360
359
|
}
|
361
|
-
function addStyleSheetTag(
|
360
|
+
function addStyleSheetTag(path, state) {
|
362
361
|
const str = state.filename?.replace(state.cwd, "") ?? "";
|
363
362
|
const uniqueId = stringToUniqueId(str) + ++state.file.tagNumber;
|
364
|
-
|
363
|
+
path.node.arguments.push(t4.numericLiteral(uniqueId));
|
365
364
|
}
|
366
|
-
function getStylesDependenciesFromObject(
|
365
|
+
function getStylesDependenciesFromObject(path) {
|
367
366
|
const detectedStylesWithVariants = /* @__PURE__ */ new Set();
|
368
|
-
const stylesheet =
|
367
|
+
const stylesheet = path.node.arguments[0];
|
369
368
|
if (t4.isObjectExpression(stylesheet)) {
|
370
369
|
stylesheet?.properties.forEach((property) => {
|
371
370
|
if (!t4.isObjectProperty(property) || !t4.isIdentifier(property.key)) {
|
@@ -664,8 +663,8 @@ function addDependencies(state, styleName, unistyle, detectedDependencies) {
|
|
664
663
|
|
665
664
|
// plugin/src/variants.ts
|
666
665
|
var t5 = __toESM(require("@babel/types"));
|
667
|
-
function extractVariants(
|
668
|
-
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));
|
669
668
|
if (maybeVariants.length === 0) {
|
670
669
|
return;
|
671
670
|
}
|
@@ -688,7 +687,7 @@ function extractVariants(path2, state) {
|
|
688
687
|
return;
|
689
688
|
}
|
690
689
|
const calleeName = callee.object.name;
|
691
|
-
const newUniqueName =
|
690
|
+
const newUniqueName = path.scope.generateUidIdentifier(calleeName);
|
692
691
|
const shadowDeclaration = t5.variableDeclaration("const", [
|
693
692
|
t5.variableDeclarator(newUniqueName, t5.identifier(calleeName))
|
694
693
|
]);
|
@@ -699,14 +698,14 @@ function extractVariants(path2, state) {
|
|
699
698
|
const finalDeclaration = t5.variableDeclaration("const", [
|
700
699
|
t5.variableDeclarator(t5.identifier(calleeName), newCallExpression)
|
701
700
|
]);
|
702
|
-
const pathIndex =
|
703
|
-
const rest =
|
701
|
+
const pathIndex = path.node.body.findIndex((bodyPath) => bodyPath === targetVariant);
|
702
|
+
const rest = path.node.body.slice(pathIndex + 1);
|
704
703
|
const statement = t5.blockStatement([
|
705
704
|
finalDeclaration,
|
706
705
|
...rest
|
707
706
|
]);
|
708
|
-
|
709
|
-
...
|
707
|
+
path.node.body = [
|
708
|
+
...path.node.body.slice(0, pathIndex),
|
710
709
|
shadowDeclaration,
|
711
710
|
statement
|
712
711
|
];
|
@@ -715,12 +714,18 @@ function extractVariants(path2, state) {
|
|
715
714
|
|
716
715
|
// plugin/src/index.ts
|
717
716
|
function index_default() {
|
717
|
+
if (process.env.NODE_ENV === "test") {
|
718
|
+
return {
|
719
|
+
name: "babel-react-native-unistyles",
|
720
|
+
visitor: {}
|
721
|
+
};
|
722
|
+
}
|
718
723
|
return {
|
719
724
|
name: "babel-react-native-unistyles",
|
720
725
|
visitor: {
|
721
726
|
Program: {
|
722
|
-
enter(
|
723
|
-
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));
|
724
729
|
state.file.hasAnyUnistyle = false;
|
725
730
|
state.file.hasUnistylesImport = false;
|
726
731
|
state.file.addUnistylesRequire = false;
|
@@ -730,7 +735,7 @@ function index_default() {
|
|
730
735
|
state.file.tagNumber = 0;
|
731
736
|
state.reactNativeImports = {};
|
732
737
|
state.file.forceProcessing = state.opts.autoProcessRoot && state.filename ? state.filename.includes(toPlatformPath(`${state.file.opts.root}/${state.opts.autoProcessRoot}/`)) : false;
|
733
|
-
|
738
|
+
path.traverse({
|
734
739
|
BlockStatement(blockPath) {
|
735
740
|
if (isInsideNodeModules(state)) {
|
736
741
|
return;
|
@@ -739,41 +744,41 @@ function index_default() {
|
|
739
744
|
}
|
740
745
|
});
|
741
746
|
},
|
742
|
-
exit(
|
747
|
+
exit(path, state) {
|
743
748
|
if (isInsideNodeModules(state)) {
|
744
749
|
return;
|
745
750
|
}
|
746
751
|
if (state.file.addUnistylesRequire) {
|
747
|
-
return addUnistylesRequire(
|
752
|
+
return addUnistylesRequire(path, state);
|
748
753
|
}
|
749
754
|
if (state.file.hasAnyUnistyle || state.file.hasVariants || state.file.replaceWithUnistyles || state.file.forceProcessing) {
|
750
|
-
addUnistylesImport(
|
755
|
+
addUnistylesImport(path, state);
|
751
756
|
}
|
752
757
|
}
|
753
758
|
},
|
754
|
-
FunctionDeclaration(
|
759
|
+
FunctionDeclaration(path, state) {
|
755
760
|
if (isInsideNodeModules(state)) {
|
756
761
|
return;
|
757
762
|
}
|
758
|
-
const componentName =
|
763
|
+
const componentName = path.node.id ? path.node.id.name : null;
|
759
764
|
if (componentName) {
|
760
765
|
state.file.hasVariants = false;
|
761
766
|
}
|
762
767
|
},
|
763
|
-
ClassDeclaration(
|
768
|
+
ClassDeclaration(path, state) {
|
764
769
|
if (isInsideNodeModules(state)) {
|
765
770
|
return;
|
766
771
|
}
|
767
|
-
const componentName =
|
772
|
+
const componentName = path.node.id ? path.node.id.name : null;
|
768
773
|
if (componentName) {
|
769
774
|
state.file.hasVariants = false;
|
770
775
|
}
|
771
776
|
},
|
772
|
-
VariableDeclaration(
|
777
|
+
VariableDeclaration(path, state) {
|
773
778
|
if (isInsideNodeModules(state)) {
|
774
779
|
return;
|
775
780
|
}
|
776
|
-
|
781
|
+
path.node.declarations.forEach((declaration) => {
|
777
782
|
if (t6.isArrowFunctionExpression(declaration.init) || t6.isFunctionExpression(declaration.init)) {
|
778
783
|
const componentName = declaration.id && t6.isIdentifier(declaration.id) ? declaration.id.name : null;
|
779
784
|
if (componentName) {
|
@@ -782,80 +787,83 @@ function index_default() {
|
|
782
787
|
}
|
783
788
|
});
|
784
789
|
},
|
785
|
-
ImportDeclaration(
|
790
|
+
ImportDeclaration(path, state) {
|
786
791
|
const exoticImport = REPLACE_WITH_UNISTYLES_EXOTIC_PATHS.concat(state.opts.autoRemapImports ?? []).find((exotic) => state.filename?.includes(exotic.path));
|
787
792
|
if (exoticImport) {
|
788
|
-
return handleExoticImport(
|
793
|
+
return handleExoticImport(path, state, exoticImport);
|
789
794
|
}
|
790
795
|
if (isInsideNodeModules(state)) {
|
791
796
|
return;
|
792
797
|
}
|
793
|
-
const importSource =
|
798
|
+
const importSource = path.node.source.value;
|
794
799
|
if (importSource.includes("react-native-unistyles")) {
|
795
800
|
state.file.hasUnistylesImport = true;
|
796
|
-
|
801
|
+
path.node.specifiers.forEach((specifier) => {
|
797
802
|
if (t6.isImportSpecifier(specifier) && t6.isIdentifier(specifier.imported) && specifier.imported.name === "StyleSheet") {
|
798
803
|
state.file.styleSheetLocalName = specifier.local.name;
|
799
804
|
}
|
800
805
|
});
|
801
806
|
}
|
802
807
|
if (importSource === "react-native") {
|
803
|
-
|
808
|
+
path.node.specifiers.forEach((specifier) => {
|
804
809
|
if (t6.isImportSpecifier(specifier) && t6.isIdentifier(specifier.imported) && REACT_NATIVE_COMPONENT_NAMES.includes(specifier.imported.name)) {
|
805
810
|
state.reactNativeImports[specifier.local.name] = specifier.imported.name;
|
806
811
|
}
|
807
812
|
});
|
808
813
|
}
|
809
814
|
if (importSource.includes(toPlatformPath("react-native/Libraries"))) {
|
810
|
-
handleExoticImport(
|
815
|
+
handleExoticImport(path, state, NATIVE_COMPONENTS_PATHS);
|
811
816
|
}
|
812
817
|
if (!state.file.forceProcessing && Array.isArray(state.opts.autoProcessImports)) {
|
813
818
|
state.file.forceProcessing = state.opts.autoProcessImports.includes(importSource);
|
814
819
|
}
|
815
820
|
},
|
816
|
-
JSXElement(
|
821
|
+
JSXElement(path, state) {
|
817
822
|
if (isInsideNodeModules(state)) {
|
818
823
|
return;
|
819
824
|
}
|
820
|
-
if (hasStringRef(
|
825
|
+
if (hasStringRef(path)) {
|
821
826
|
throw new Error("Detected string based ref which is not supported by Unistyles.");
|
822
827
|
}
|
823
828
|
},
|
824
|
-
MemberExpression(
|
829
|
+
MemberExpression(path, state) {
|
825
830
|
if (isInsideNodeModules(state)) {
|
826
831
|
return;
|
827
832
|
}
|
828
|
-
if (!state.file.reactNativeCommonJSName || !t6.isIdentifier(
|
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)) {
|
829
837
|
return;
|
830
838
|
}
|
831
|
-
if (
|
839
|
+
if (!REACT_NATIVE_COMPONENT_NAMES.includes(path.node.property.name)) {
|
832
840
|
return;
|
833
841
|
}
|
834
|
-
if (!state.reactNativeImports[
|
835
|
-
const uniqueId =
|
836
|
-
state.reactNativeImports[
|
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;
|
837
845
|
state.file.addUnistylesRequire = true;
|
838
846
|
}
|
839
|
-
|
847
|
+
path.node.object.name = state.reactNativeImports[path.node.property.name];
|
840
848
|
},
|
841
|
-
CallExpression(
|
849
|
+
CallExpression(path, state) {
|
842
850
|
if (isInsideNodeModules(state)) {
|
843
851
|
return;
|
844
852
|
}
|
845
|
-
if (isUnistylesCommonJSRequire(
|
853
|
+
if (isUnistylesCommonJSRequire(path, state)) {
|
846
854
|
return;
|
847
855
|
}
|
848
|
-
if (isReactNativeCommonJSRequire(
|
856
|
+
if (isReactNativeCommonJSRequire(path, state)) {
|
849
857
|
return;
|
850
858
|
}
|
851
|
-
if (!isUnistylesStyleSheet(
|
859
|
+
if (!isUnistylesStyleSheet(path, state) && !isKindOfStyleSheet(path, state)) {
|
852
860
|
return;
|
853
861
|
}
|
854
862
|
state.file.hasAnyUnistyle = true;
|
855
|
-
addStyleSheetTag(
|
856
|
-
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];
|
857
865
|
if (t6.isObjectExpression(arg)) {
|
858
|
-
const detectedDependencies = getStylesDependenciesFromObject(
|
866
|
+
const detectedDependencies = getStylesDependenciesFromObject(path);
|
859
867
|
if (detectedDependencies) {
|
860
868
|
if (t6.isObjectExpression(arg)) {
|
861
869
|
arg.properties.forEach((property) => {
|
@@ -867,7 +875,7 @@ function index_default() {
|
|
867
875
|
}
|
868
876
|
}
|
869
877
|
if (t6.isArrowFunctionExpression(arg) || t6.isFunctionExpression(arg)) {
|
870
|
-
const funcPath = t6.isAssignmentExpression(
|
878
|
+
const funcPath = t6.isAssignmentExpression(path.node.arguments[0]) ? path.get("arguments.0.right") : path.get("arguments.0");
|
871
879
|
const detectedDependencies = getStylesDependenciesFromFunction(funcPath);
|
872
880
|
if (detectedDependencies) {
|
873
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()
|
@@ -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(() => {})
|