mirror of
https://github.com/zhigang1992/nativewind.git
synced 2026-06-15 10:17:54 +08:00
fix: prevent duplicate identifiers when accidentally importing RWN
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { StyleSheet } from "react-native";
|
||||
import { StyleSheet as RNStyleSheet } from "react-native";
|
||||
import { StyledComponent } from "tailwindcss-react-native";
|
||||
import { Text } from "react-native";
|
||||
import { TailwindProvider } from "tailwindcss-react-native";
|
||||
@@ -19,7 +19,7 @@ export function Test() {
|
||||
}
|
||||
globalThis.tailwindcss_react_native_style = Object.assign(
|
||||
globalThis.tailwindcss_react_native_style || {},
|
||||
StyleSheet.create({
|
||||
RNStyleSheet.create({
|
||||
"font-bold": {
|
||||
fontWeight: "700",
|
||||
},
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { StyleSheet } from "react-native";
|
||||
import { StyleSheet as RNStyleSheet } from "react-native";
|
||||
import { StyledComponent } from "tailwindcss-react-native";
|
||||
import { Text, View } from "react-native";
|
||||
import { TailwindProvider } from "tailwindcss-react-native";
|
||||
@@ -17,7 +17,7 @@ export function Test() {
|
||||
}
|
||||
globalThis.tailwindcss_react_native_style = Object.assign(
|
||||
globalThis.tailwindcss_react_native_style || {},
|
||||
StyleSheet.create({
|
||||
RNStyleSheet.create({
|
||||
container: {
|
||||
width: "100%",
|
||||
},
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { StyleSheet } from "react-native";
|
||||
import { StyleSheet as RNStyleSheet } from "react-native";
|
||||
import { StyledComponent } from "tailwindcss-react-native";
|
||||
import { Text, View } from "react-native";
|
||||
import { TailwindProvider } from "tailwindcss-react-native";
|
||||
@@ -15,7 +15,7 @@ export function Test() {
|
||||
}
|
||||
globalThis.tailwindcss_react_native_style = Object.assign(
|
||||
globalThis.tailwindcss_react_native_style || {},
|
||||
StyleSheet.create({
|
||||
RNStyleSheet.create({
|
||||
container: {
|
||||
width: "100%",
|
||||
},
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { StyleSheet } from "react-native";
|
||||
import { StyleSheet as RNStyleSheet } from "react-native";
|
||||
import { StyledComponent } from "tailwindcss-react-native";
|
||||
import { Text, View } from "react-native";
|
||||
import { TailwindProvider } from "tailwindcss-react-native";
|
||||
@@ -15,7 +15,7 @@ export function Test() {
|
||||
}
|
||||
globalThis.tailwindcss_react_native_style = Object.assign(
|
||||
globalThis.tailwindcss_react_native_style || {},
|
||||
StyleSheet.create({
|
||||
RNStyleSheet.create({
|
||||
container: {
|
||||
width: "100%",
|
||||
},
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { StyleSheet } from "react-native";
|
||||
import { StyleSheet as RNStyleSheet } from "react-native";
|
||||
import { StyledComponent } from "tailwindcss-react-native";
|
||||
import { Text } from "react-native";
|
||||
import { MotiText } from "moti";
|
||||
@@ -14,7 +14,7 @@ export function Test() {
|
||||
}
|
||||
globalThis.tailwindcss_react_native_style = Object.assign(
|
||||
globalThis.tailwindcss_react_native_style || {},
|
||||
StyleSheet.create({
|
||||
RNStyleSheet.create({
|
||||
"font-bold": {
|
||||
fontWeight: "700",
|
||||
},
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { StyleSheet } from "react-native";
|
||||
import { StyleSheet as RNStyleSheet } from "react-native";
|
||||
import { StyledComponent } from "tailwindcss-react-native";
|
||||
import { Text } from "react-native";
|
||||
import { TailwindProvider } from "tailwindcss-react-native";
|
||||
@@ -16,7 +16,7 @@ export function Test({ isBold, isUnderline }) {
|
||||
}
|
||||
globalThis.tailwindcss_react_native_style = Object.assign(
|
||||
globalThis.tailwindcss_react_native_style || {},
|
||||
StyleSheet.create({
|
||||
RNStyleSheet.create({
|
||||
"font-bold": {
|
||||
fontWeight: "700",
|
||||
},
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { StyleSheet } from "react-native";
|
||||
import { StyleSheet as RNStyleSheet } from "react-native";
|
||||
import { Text, View } from "react-native";
|
||||
import { TailwindProvider } from "tailwindcss-react-native";
|
||||
export function Test() {
|
||||
@@ -12,7 +12,7 @@ export function Test() {
|
||||
}
|
||||
globalThis.tailwindcss_react_native_style = Object.assign(
|
||||
globalThis.tailwindcss_react_native_style || {},
|
||||
StyleSheet.create({
|
||||
RNStyleSheet.create({
|
||||
container: {
|
||||
width: "100%",
|
||||
},
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { StyleSheet } from "react-native";
|
||||
import { StyleSheet as RNStyleSheet } from "react-native";
|
||||
import { StyledComponent } from "tailwindcss-react-native";
|
||||
import { Text } from "react-native";
|
||||
import { TestComponent } from "./test-directory";
|
||||
@@ -14,7 +14,7 @@ export function Test() {
|
||||
}
|
||||
globalThis.tailwindcss_react_native_style = Object.assign(
|
||||
globalThis.tailwindcss_react_native_style || {},
|
||||
StyleSheet.create({
|
||||
RNStyleSheet.create({
|
||||
"font-bold": {
|
||||
fontWeight: "700",
|
||||
},
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { StyleSheet } from "react-native";
|
||||
import { StyleSheet as RNStyleSheet } from "react-native";
|
||||
import { StyledComponent } from "tailwindcss-react-native";
|
||||
import { Text } from "react-native";
|
||||
import { TestComponent } from "./test-directory";
|
||||
@@ -14,7 +14,7 @@ export function Test() {
|
||||
}
|
||||
globalThis.tailwindcss_react_native_style = Object.assign(
|
||||
globalThis.tailwindcss_react_native_style || {},
|
||||
StyleSheet.create({
|
||||
RNStyleSheet.create({
|
||||
"font-bold": {
|
||||
fontWeight: "700",
|
||||
},
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { StyleSheet } from "react-native";
|
||||
import { StyleSheet as RNStyleSheet } from "react-native";
|
||||
import { StyledComponent } from "tailwindcss-react-native";
|
||||
import { Text } from "react-native";
|
||||
import { ShouldNotBeTransformed } from "./should-not-be-transformed";
|
||||
@@ -14,7 +14,7 @@ export function Test() {
|
||||
}
|
||||
globalThis.tailwindcss_react_native_style = Object.assign(
|
||||
globalThis.tailwindcss_react_native_style || {},
|
||||
StyleSheet.create({
|
||||
RNStyleSheet.create({
|
||||
"font-bold": {
|
||||
fontWeight: "700",
|
||||
},
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { StyleSheet } from "react-native";
|
||||
import { StyleSheet as RNStyleSheet } from "react-native";
|
||||
import { StyledComponent } from "tailwindcss-react-native";
|
||||
import { Text } from "react-native";
|
||||
import { TestComponent } from "./test";
|
||||
@@ -14,7 +14,7 @@ export function Test() {
|
||||
}
|
||||
globalThis.tailwindcss_react_native_style = Object.assign(
|
||||
globalThis.tailwindcss_react_native_style || {},
|
||||
StyleSheet.create({
|
||||
RNStyleSheet.create({
|
||||
"font-bold": {
|
||||
fontWeight: "700",
|
||||
},
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { StyleSheet } from "react-native";
|
||||
import { StyledComponent } from "tailwindcss-react-native";
|
||||
import { Text } from "react-native";
|
||||
import { TailwindProvider } from "tailwindcss-react-native";
|
||||
@@ -11,11 +10,3 @@ export function Test() {
|
||||
</TailwindProvider>
|
||||
);
|
||||
}
|
||||
globalThis.tailwindcss_react_native_style = Object.assign(
|
||||
globalThis.tailwindcss_react_native_style || {},
|
||||
StyleSheet.create({})
|
||||
);
|
||||
globalThis.tailwindcss_react_native_media = Object.assign(
|
||||
globalThis.tailwindcss_react_native_media || {},
|
||||
{}
|
||||
);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { StyleSheet as RNStyleSheet } from "react-native";
|
||||
import { StyledComponent } from "tailwindcss-react-native";
|
||||
import { StyleSheet, Text } from "react-native";
|
||||
import { TailwindProvider } from "tailwindcss-react-native";
|
||||
@@ -21,7 +22,7 @@ const styles = StyleSheet.create({
|
||||
});
|
||||
globalThis.tailwindcss_react_native_style = Object.assign(
|
||||
globalThis.tailwindcss_react_native_style || {},
|
||||
StyleSheet.create({
|
||||
RNStyleSheet.create({
|
||||
"font-bold": {
|
||||
fontWeight: "700",
|
||||
},
|
||||
|
||||
@@ -116,10 +116,17 @@ export default function rootVisitor(
|
||||
);
|
||||
}
|
||||
|
||||
// If there are no styles, early exit
|
||||
if (Object.keys(styles).length === 0) return;
|
||||
|
||||
appendVariables(bodyNode, styles);
|
||||
|
||||
if (!hasStyleSheetImport) {
|
||||
prependImport(bodyNode, "StyleSheet", "react-native");
|
||||
prependImport(
|
||||
bodyNode,
|
||||
["RNStyleSheet", "StyleSheet"],
|
||||
"react-native"
|
||||
);
|
||||
}
|
||||
} else {
|
||||
if (!hasProvider) {
|
||||
@@ -131,11 +138,18 @@ export default function rootVisitor(
|
||||
*/
|
||||
const { styles } = extractStyles(tailwindConfig);
|
||||
|
||||
// If there are no styles, early exit
|
||||
if (Object.keys(styles).length === 0) return;
|
||||
|
||||
const bodyNode = path.node.body;
|
||||
appendVariables(bodyNode, styles);
|
||||
|
||||
if (!hasStyleSheetImport) {
|
||||
prependImport(bodyNode, "StyleSheet", "react-native");
|
||||
prependImport(
|
||||
bodyNode,
|
||||
["RNStyleSheet", "StyleSheet"],
|
||||
"react-native"
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
@@ -8,12 +8,14 @@ import {
|
||||
|
||||
export function prependImport(
|
||||
body: Statement[],
|
||||
variable: string,
|
||||
variable: string | string[],
|
||||
source: string
|
||||
) {
|
||||
body.unshift(
|
||||
importDeclaration(
|
||||
[importSpecifier(identifier(variable), identifier(variable))],
|
||||
typeof variable === "string"
|
||||
? [importSpecifier(identifier(variable), identifier(variable))]
|
||||
: [importSpecifier(identifier(variable[0]), identifier(variable[1]))],
|
||||
stringLiteral(source)
|
||||
)
|
||||
);
|
||||
|
||||
@@ -43,7 +43,10 @@ export function appendVariables(body: Statement[], styleRecord: StyleRecord) {
|
||||
identifier("{}")
|
||||
),
|
||||
callExpression(
|
||||
memberExpression(identifier("StyleSheet"), identifier("create")),
|
||||
memberExpression(
|
||||
identifier("RNStyleSheet"),
|
||||
identifier("create")
|
||||
),
|
||||
[serialize(styles)]
|
||||
),
|
||||
]
|
||||
@@ -86,7 +89,7 @@ function serialize(literal: unknown): Expression {
|
||||
case "string":
|
||||
if (literal === "hairlineWidth") {
|
||||
return memberExpression(
|
||||
identifier("StyleSheet"),
|
||||
identifier("RNStyleSheet"),
|
||||
identifier("hairlineWidth")
|
||||
);
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ export function hasNamedImport(
|
||||
|
||||
return isStringLiteral(specifier.imported)
|
||||
? specifier.imported.value === variable
|
||||
: specifier.imported.name === variable;
|
||||
: specifier.local.name === variable;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ export const visitor: Visitor<VisitorState> = {
|
||||
|
||||
state.hasStyleSheetImport ||= hasNamedImport(
|
||||
path,
|
||||
"StyleSheet",
|
||||
"RNStyleSheet",
|
||||
"react-native"
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user