| 1 | import fs from 'fs'
|
|---|
| 2 | import path from 'path'
|
|---|
| 3 |
|
|---|
| 4 | const defaultConfigFiles = [
|
|---|
| 5 | './tailwind.config.js',
|
|---|
| 6 | './tailwind.config.cjs',
|
|---|
| 7 | './tailwind.config.mjs',
|
|---|
| 8 | './tailwind.config.ts',
|
|---|
| 9 | './tailwind.config.cts',
|
|---|
| 10 | './tailwind.config.mts',
|
|---|
| 11 | ]
|
|---|
| 12 |
|
|---|
| 13 | function isObject(value) {
|
|---|
| 14 | return typeof value === 'object' && value !== null
|
|---|
| 15 | }
|
|---|
| 16 |
|
|---|
| 17 | function isEmpty(obj) {
|
|---|
| 18 | return Object.keys(obj).length === 0
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | function isString(value) {
|
|---|
| 22 | return typeof value === 'string' || value instanceof String
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | export default function resolveConfigPath(pathOrConfig) {
|
|---|
| 26 | // require('tailwindcss')({ theme: ..., variants: ... })
|
|---|
| 27 | if (isObject(pathOrConfig) && pathOrConfig.config === undefined && !isEmpty(pathOrConfig)) {
|
|---|
| 28 | return null
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | // require('tailwindcss')({ config: 'custom-config.js' })
|
|---|
| 32 | if (
|
|---|
| 33 | isObject(pathOrConfig) &&
|
|---|
| 34 | pathOrConfig.config !== undefined &&
|
|---|
| 35 | isString(pathOrConfig.config)
|
|---|
| 36 | ) {
|
|---|
| 37 | return path.resolve(pathOrConfig.config)
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | // require('tailwindcss')({ config: { theme: ..., variants: ... } })
|
|---|
| 41 | if (
|
|---|
| 42 | isObject(pathOrConfig) &&
|
|---|
| 43 | pathOrConfig.config !== undefined &&
|
|---|
| 44 | isObject(pathOrConfig.config)
|
|---|
| 45 | ) {
|
|---|
| 46 | return null
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | // require('tailwindcss')('custom-config.js')
|
|---|
| 50 | if (isString(pathOrConfig)) {
|
|---|
| 51 | return path.resolve(pathOrConfig)
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | // require('tailwindcss')
|
|---|
| 55 | return resolveDefaultConfigPath()
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | export function resolveDefaultConfigPath() {
|
|---|
| 59 | for (const configFile of defaultConfigFiles) {
|
|---|
| 60 | try {
|
|---|
| 61 | const configPath = path.resolve(configFile)
|
|---|
| 62 | fs.accessSync(configPath)
|
|---|
| 63 | return configPath
|
|---|
| 64 | } catch (err) {}
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | return null
|
|---|
| 68 | }
|
|---|