Skip to content

Commit

Permalink
fix: better matching with relative content paths in tailwind.config.js
Browse files Browse the repository at this point in the history
fixes: #57
  • Loading branch information
marklawlor committed May 25, 2022
1 parent e9020c1 commit f99bc1f
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 17 deletions.
2 changes: 2 additions & 0 deletions __tests__/visitor/content-positive-match/code.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Text } from "react-native";
import { TestComponent } from "./test";
export function Test() {
return (
<>
<Text className="font-bold">Hello world!</Text>
<TestComponent className="text-red-500" />
</>
);
}
5 changes: 5 additions & 0 deletions __tests__/visitor/content-positive-match/output.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { StyleSheet } from "react-native";
import { StyledComponent } from "tailwindcss-react-native";
import { Text } from "react-native";
import { TestComponent } from "./test";
export function Test() {
return (
<>
<StyledComponent className="font-bold" component={Text}>
Hello world!
</StyledComponent>
<StyledComponent className="text-red-500" component={TestComponent} />
</>
);
}
Expand All @@ -16,6 +18,9 @@ Object.assign(
"font-bold": {
fontWeight: "700",
},
"text-red-500": {
color: "#ef4444",
},
})
);
Object.assign(globalThis.tailwindcss_react_native_media, {});
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
content: [`./content-positive-match/code.{js,ts,jsx,tsx}`],
content: [`./content-positive-match/*.{js,ts,jsx,tsx}`],
};
5 changes: 5 additions & 0 deletions __tests__/visitor/content-positive-match/test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Text, TextProps } from "react-native";

export function TestComponent(props: TextProps & { className?: string }) {
return <Text {...props}>Hello world!</Text>;
}
11 changes: 9 additions & 2 deletions src/babel/root-visitor.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { resolve } from "node:path";
import { Program } from "@babel/types";
import { NodePath } from "@babel/traverse";

Expand All @@ -16,7 +17,12 @@ export default function rootVisitor(
) {
const errors: StyleError[] = [];

const tailwindConfig = getTailwindConfig(cwd, {
const tailwindConfigPath = resolve(
cwd,
options.tailwindConfigPath || "./tailwind.config.js"
);

const tailwindConfig = getTailwindConfig(tailwindConfigPath, {
...options,
onError(error) {
errors.push(error);
Expand Down Expand Up @@ -57,8 +63,8 @@ export default function rootVisitor(
}

const visitorState: VisitorState = {
cwd,
rem: 16,
tailwindConfigPath: "tailwind.config.js",
platform: "native",
hmr: true,
mode: "compileAndTransform",
Expand All @@ -75,6 +81,7 @@ export default function rootVisitor(
hasProvider: false,
hasStyleSheetImport: false,
tailwindConfig,
tailwindConfigPath,
};

// Traverse the file
Expand Down
16 changes: 9 additions & 7 deletions src/babel/utils/get-import-blocked-components.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createRequire } from "node:module";
import { join, dirname, basename } from "node:path";
import { join, dirname, basename, resolve } from "node:path";
import { readdirSync, lstatSync, existsSync } from "node:fs";
import micromatch from "micromatch";

Expand All @@ -24,6 +24,7 @@ export function getImportBlockedComponents(
allowRelativeModules,
blockModuleTransform,
filename,
cwd,
} = state;

const require = createRequire(filename);
Expand Down Expand Up @@ -85,12 +86,13 @@ export function getImportBlockedComponents(
isAllowed ??= micromatch.isMatch(moduleName, allowModuleTransform);
returnComponentsAsBlocked = isBlocked || !isAllowed;
} else {
const isNotAllowedRelative =
Array.isArray(allowRelativeModules) &&
allowRelativeModules.length > 0 &&
!modulePaths.some((modulePath) =>
micromatch.isMatch(modulePath, allowRelativeModules)
);
const normalizedAllowRelativeModules = !Array.isArray(allowRelativeModules)
? []
: allowRelativeModules.map((modulePath) => resolve(cwd, modulePath));

const isNotAllowedRelative = !modulePaths.some((modulePath) =>
micromatch.isMatch(modulePath, normalizedAllowRelativeModules)
);

returnComponentsAsBlocked = isNotAllowedRelative;
}
Expand Down
8 changes: 1 addition & 7 deletions src/babel/utils/get-tailwind-config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { resolve } from "node:path";
import { existsSync } from "node:fs";

import resolveTailwindConfig from "tailwindcss/resolveConfig";
Expand All @@ -11,16 +10,11 @@ export interface GetTailwindConfigOptions extends NativePluginOptions {
}

export function getTailwindConfig(
cwd: string,
fullConfigPath: string,
options: GetTailwindConfigOptions
): TailwindConfig {
const { tailwindConfigPath } = options;

const fullConfigPath = resolve(
cwd,
tailwindConfigPath || "./tailwind.config.js"
);

let userConfig: Partial<TailwindConfig> = {};
if (existsSync(fullConfigPath)) {
// eslint-disable-next-line unicorn/prefer-module
Expand Down
2 changes: 2 additions & 0 deletions src/babel/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ import {
export interface VisitorState
extends State,
Required<TailwindcssReactNativeBabelOptions> {
cwd: string;
allowRelativeModules: AllowPathOptions;
blockList: Set<string>;
hasClassNames: boolean;
hasStyledComponentImport: boolean;
hasProvider: boolean;
hasStyleSheetImport: boolean;
tailwindConfig: TailwindConfig;
tailwindConfigPath: string;
canCompile: boolean;
canTransform: boolean;
}
Expand Down

0 comments on commit f99bc1f

Please sign in to comment.