Skip to content

Commit 7f469ab

Browse files
committed
feat: allow passing in custom tailwind config path
1 parent 76047bf commit 7f469ab

File tree

5 files changed

+67
-29
lines changed

5 files changed

+67
-29
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,42 @@ const App = () => {
241241
}
242242
```
243243

244+
### Macro Options
245+
246+
You can apply options to the macro by adding a `babel-plugin-macros.config.js` or specifying them in your `package.json` like below:
247+
248+
```js
249+
// babel-plugin-macros.config.js
250+
module.exports = {
251+
reactNativeTailwind: {
252+
// your options
253+
},
254+
}
255+
```
256+
257+
Alternatively:
258+
259+
```js
260+
// package.json
261+
{
262+
//...
263+
"babelMacros": {
264+
"reactNativeTailwind": {
265+
// your options
266+
}
267+
}
268+
}
269+
```
270+
271+
<details>
272+
<summary><strong>Available Options</strong></summary>
273+
274+
| Name | Default | Description |
275+
| ------ | ------------------------ | --------------------------------- |
276+
| config | `"./tailwind.config.js"` | The path to your Tailwind config. |
277+
278+
</details>
279+
244280
## Next.js SSR Setup
245281

246282
In order to enable SSR support via media queries on Next.js, update your [custom document](https://nextjs.org/docs/advanced-features/custom-document) as follows:
File renamed without changes.

examples/next/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,10 @@
3030
"trailingComma": "es5",
3131
"useTabs": false,
3232
"semi": false
33+
},
34+
"babelMacros": {
35+
"reactNativeTailwind": {
36+
"config": "./custom-tailwind-config-name.js"
37+
}
3338
}
3439
}

packages/react-native-tailwind.macro/src/macro/index.tsx

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
import { createMacro, MacroHandler, MacroParams } from "babel-plugin-macros"
1+
import {
2+
createMacro,
3+
MacroError,
4+
MacroHandler,
5+
MacroParams,
6+
} from "babel-plugin-macros"
27
import { NodePath } from "@babel/core"
38
import { JSXAttribute, ObjectExpression } from "@babel/types"
9+
import fs from "fs"
10+
import { resolve } from "path"
411
import {
512
addCreateUseTailwindStyles,
613
addImport,
@@ -22,15 +29,24 @@ const macro = (params: MacroParams & { source: string }) => {
2229
},
2330
state,
2431
babel: { types: t },
32+
config = {},
2533
} = params
2634

2735
const program = state.file.path
2836

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

31-
program.state.tailwindConfig = resolveTailwindConfig(
32-
state.file.opts.filename!
39+
// Resolve custom tailwind config
40+
const sourceRoot = state.file.opts.sourceRoot ?? "."
41+
const configPath = resolve(
42+
sourceRoot,
43+
config.config ?? "./tailwind.config.js"
3344
)
45+
if (config.config && !fs.existsSync(configPath))
46+
throw new MacroError(
47+
`Couldn't find custom tailwind config at ${configPath}.`
48+
)
49+
program.state.tailwindConfig = resolveTailwindConfig(configPath)
3450

3551
/**
3652
* Traverse and collect all tw props on the surrounding component function in a first run.
@@ -110,4 +126,6 @@ const macro = (params: MacroParams & { source: string }) => {
110126
program.scope.crawl()
111127
}
112128

113-
export default createMacro(macro as MacroHandler)
129+
export default createMacro(macro as MacroHandler, {
130+
configName: "reactNativeTailwind",
131+
})
Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,11 @@
11
import resolveConfig from "tailwindcss/resolveConfig"
2-
import path from "path"
32
import fs from "fs"
43

5-
const findConfigFilePath = (currentDir: string): string | null => {
6-
const files = fs.readdirSync(currentDir)
7-
8-
const configFile = files.find((file) => file === "tailwind.config.js")
9-
10-
if (configFile) return path.resolve(currentDir, configFile)
11-
12-
// Check if package.json exists on this level, in which case we conclude that there is no config file
13-
const packageJson = files.find((file) => file === "package.json")
14-
if (packageJson) return null
15-
16-
// Otherwise, recursively move upwards
17-
return findConfigFilePath(path.resolve(currentDir, ".."))
18-
}
19-
204
export const resolveTailwindConfig = (filePath: string | undefined | null) => {
21-
if (!filePath) return resolveConfig({ theme: {}, darkMode: "media" })
22-
23-
const configFilePath = fs.lstatSync(filePath).isDirectory()
24-
? findConfigFilePath(filePath)
25-
: findConfigFilePath(path.dirname(filePath))
26-
27-
const config = configFilePath
28-
? require(configFilePath)
29-
: { theme: {}, darkMode: "media" }
5+
const config =
6+
filePath && fs.existsSync(filePath)
7+
? require(filePath)
8+
: { theme: {}, darkMode: "media" }
309

3110
return resolveConfig(config)
3211
}

0 commit comments

Comments
 (0)