Skip to content

Commit

Permalink
feat: resolve tailwind config
Browse files Browse the repository at this point in the history
  • Loading branch information
finkef committed Nov 20, 2021
1 parent bdac17b commit d35f188
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/react-native-tailwind.macro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"@types/jest": "^26.0.0",
"@types/react": "^16.9.19",
"@types/react-native": "0.62.13",
"@types/tailwindcss": "^2.2.4",
"babel-plugin-module-resolver": "^4.1.0",
"babel-plugin-tester": "^10.1.0",
"commitlint": "^11.0.0",
Expand Down
5 changes: 5 additions & 0 deletions packages/react-native-tailwind.macro/src/macro/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { handleTwProp } from "./handle-tw-prop"
import { handleHook } from "./handle-hook"
import { handleLibImport } from "./handle-lib-import"
import { resolveTailwindConfig } from "./resolve-tailwind-config"

const macro = (params: MacroParams & { source: string }) => {
const {
Expand All @@ -27,6 +28,10 @@ const macro = (params: MacroParams & { source: string }) => {

addImport({ ...params, t, path: program, program })

program.state.tailwindConfig = resolveTailwindConfig(
state.file.opts.filename!
)

/**
* Traverse and collect all tw props on the surrounding component function in a first run.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import resolveConfig from "tailwindcss/resolveConfig"
import path from "path"
import fs from "fs"

const findConfigFilePath = (currentDir: string): string | null => {
const files = fs.readdirSync(currentDir)

const configFile = files.find((file) => file === "tailwind.config.js")

if (configFile) return path.resolve(currentDir, configFile)

// Check if package.json exists on this level, in which case we conclude that there is no config file
const packageJson = files.find((file) => file === "package.json")
if (packageJson) return null

// Otherwise, recursively move upwards
return findConfigFilePath(path.resolve(currentDir, ".."))
}

export const resolveTailwindConfig = (filePath: string | undefined | null) => {
if (!filePath) return resolveConfig({ theme: {}, darkMode: "media" })

const configFilePath = fs.lstatSync(filePath).isDirectory()
? findConfigFilePath(filePath)
: findConfigFilePath(path.dirname(filePath))

const config = configFilePath
? require(configFilePath)
: { theme: {}, darkMode: "media" }

return resolveConfig(config)
}

0 comments on commit d35f188

Please sign in to comment.