Skip to content

Commit 190be16

Browse files
authored
Fix Tailwind config file resolution when Prettier config file is not present (#62)
* Update config file resolution * Add "no prettier config" test case
1 parent ed44540 commit 190be16

File tree

9 files changed

+54
-26
lines changed

9 files changed

+54
-26
lines changed

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
"predev": "npm run _pre",
2222
"dev": "npm run _esbuild -- --watch",
2323
"test": "jest",
24-
"prepublishOnly": "npm run build && npm test && node scripts/copy-licenses.js"
24+
"prepublishOnly": "npm run build && npm test && node scripts/copy-licenses.js",
25+
"format": "prettier \"src/**/*.js\" \"tests/test.js\" --write --print-width 80 --single-quote --no-semi --plugin-search-dir ./tests"
2526
},
2627
"devDependencies": {
2728
"@tailwindcss/line-clamp": "^0.3.0",
2829
"cpy-cli": "^3.1.1",
2930
"esbuild": "^0.14.11",
31+
"escalade": "^3.1.1",
3032
"import-fresh": "^3.3.0",
3133
"import-from": "^4.0.0",
3234
"jest": "^27.4.7",

prettier.config.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/index.js

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import resolveConfigFallback from 'tailwindcss/resolveConfig'
1212
import * as recast from 'recast'
1313
import * as astTypes from 'ast-types'
1414
import * as path from 'path'
15-
import * as fs from 'fs'
1615
import requireFrom from 'import-from'
1716
import requireFresh from 'import-fresh'
1817
import objectHash from 'object-hash'
1918
import * as svelte from 'prettier-plugin-svelte'
2019
import lineColumn from 'line-column'
2120
import jsesc from 'jsesc'
21+
import escalade from 'escalade/sync'
2222

2323
let contextMap = new Map()
2424

@@ -129,23 +129,35 @@ function createParser(original, transform) {
129129
let createContext = createContextFallback
130130
let generateRules = generateRulesFallback
131131

132+
let baseDir
132133
let prettierConfigPath = prettier.resolveConfigFile.sync(options.filepath)
133-
let baseDir = prettierConfigPath
134-
? path.dirname(prettierConfigPath)
135-
: process.env.VSCODE_CWD ?? process.cwd()
136134

137135
if (options.tailwindConfig) {
136+
baseDir = prettierConfigPath
137+
? path.dirname(prettierConfigPath)
138+
: process.cwd()
138139
tailwindConfigPath = path.resolve(baseDir, options.tailwindConfig)
139140
tailwindConfig = requireFresh(tailwindConfigPath)
140141
} else {
141-
let tailwindConfigPathJs = path.resolve(baseDir, 'tailwind.config.js')
142-
let tailwindConfigPathCjs = path.resolve(baseDir, 'tailwind.config.cjs')
143-
if (fs.existsSync(tailwindConfigPathJs)) {
144-
tailwindConfigPath = tailwindConfigPathJs
145-
tailwindConfig = requireFresh(tailwindConfigPathJs)
146-
} else if (fs.existsSync(tailwindConfigPathCjs)) {
147-
tailwindConfigPath = tailwindConfigPathCjs
148-
tailwindConfig = requireFresh(tailwindConfigPathCjs)
142+
baseDir = prettierConfigPath
143+
? path.dirname(prettierConfigPath)
144+
: options.filepath
145+
? path.dirname(options.filepath)
146+
: process.cwd()
147+
let configPath
148+
try {
149+
configPath = escalade(baseDir, (_dir, names) => {
150+
if (names.includes('tailwind.config.js')) {
151+
return 'tailwind.config.js'
152+
}
153+
if (names.includes('tailwind.config.cjs')) {
154+
return 'tailwind.config.cjs'
155+
}
156+
})
157+
} catch {}
158+
if (configPath) {
159+
tailwindConfigPath = configPath
160+
tailwindConfig = requireFresh(configPath)
149161
}
150162
}
151163

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
module.exports = {
2-
plugins: ['../../..'],
3-
}
1+
module.exports = {};
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
module.exports = {
2-
plugins: ['../../..'],
3-
}
1+
module.exports = {};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div class="sm:bg-tomato bg-red-500"></div>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
theme: {
3+
extend: {
4+
colors: {
5+
tomato: 'tomato',
6+
},
7+
},
8+
},
9+
}

tests/test.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@ function format(str, options = {}) {
1919
function formatFixture(name) {
2020
let binPath = path.resolve(__dirname, '../node_modules/.bin/prettier')
2121
let filePath = path.resolve(__dirname, `fixtures/${name}/index.html`)
22-
return execSync(`${binPath} ${filePath}`).toString().trim()
22+
return execSync(
23+
`${binPath} ${filePath} --plugin-search-dir ${__dirname} --plugin ${path.resolve(
24+
__dirname,
25+
'..'
26+
)}`
27+
)
28+
.toString()
29+
.trim()
2330
}
2431

2532
let yes = '__YES__'
@@ -193,6 +200,12 @@ test('non-tailwind classes', () => {
193200
).toEqual('<div class="potato text-sm uppercase sm:lowercase"></div>')
194201
})
195202

203+
test('no prettier config', () => {
204+
expect(formatFixture('no-prettier-config')).toEqual(
205+
'<div class="bg-red-500 sm:bg-tomato"></div>'
206+
)
207+
})
208+
196209
test('parasite utilities', () => {
197210
expect(
198211
format('<div class="group peer unknown-class p-0 container"></div>')

0 commit comments

Comments
 (0)