Skip to content

Commit 996fa68

Browse files
committed
Remove formatterConfigPath
1 parent 8a011d5 commit 996fa68

4 files changed

Lines changed: 19 additions & 47 deletions

File tree

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,6 @@ let sorter = await createSorter({
208208
let [sorted] = sorter.sortClassAttributes(['sm:bg-tomato bg-red-500'])
209209
```
210210

211-
If you already know the formatter config file path, pass `formatterConfigPath` to infer
212-
`base` automatically and improve warning messages.
213-
214211
### Using regex patterns
215212

216213
Like the `tailwindAttributes` option, the `tailwindFunctions` option also supports regular expressions to match multiple function names. Patterns should be enclosed in forward slashes. Note that JS regex literals are not supported with Prettier.

src/config.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ function cacheForDirs<V>(
3232

3333
let prettierConfigCache = expiringMap<string, string | null>(10_000)
3434

35-
async function resolvePrettierConfigPath(
35+
async function resolvePrettierConfigDir(
3636
filePath: string,
3737
inputDir: string,
38-
): Promise<[string, string | null]> {
38+
): Promise<string> {
3939
// Check cache for this directory
4040
let cached = prettierConfigCache.get(inputDir)
4141
if (cached !== undefined) {
42-
return cached ? [path.dirname(cached), cached] : [process.cwd(), null]
42+
return cached ?? process.cwd()
4343
}
4444

4545
const resolve = async () => {
@@ -56,24 +56,24 @@ async function resolvePrettierConfigPath(
5656

5757
// Cache all directories from inputDir up to config location
5858
if (prettierConfig) {
59-
cacheForDirs(prettierConfigCache, inputDir, prettierConfig, path.dirname(prettierConfig))
59+
let configDir = path.dirname(prettierConfig)
60+
cacheForDirs(prettierConfigCache, inputDir, configDir, configDir)
61+
return configDir
6062
} else {
6163
prettierConfigCache.set(inputDir, null)
64+
return process.cwd()
6265
}
63-
64-
return prettierConfig ? [path.dirname(prettierConfig), prettierConfig] : [process.cwd(), null]
6566
}
6667

6768
export async function getTailwindConfig(options: ParserOptions): Promise<UnifiedApi> {
6869
let cwd = process.cwd()
6970
let inputDir = options.filepath ? path.dirname(options.filepath) : cwd
7071

71-
let [configDir, formatterConfigPath] = await resolvePrettierConfigPath(
72+
let configDir = await resolvePrettierConfigDir(
7273
options.filepath,
7374
inputDir,
7475
)
7576

76-
let base = formatterConfigPath ? path.dirname(formatterConfigPath) : configDir
7777

7878
let configPath =
7979
options.tailwindConfig && !options.tailwindConfig.endsWith('.css')
@@ -84,7 +84,7 @@ export async function getTailwindConfig(options: ParserOptions): Promise<Unified
8484
if (!stylesheetPath && options.tailwindEntryPoint) {
8585
console.warn(
8686
'entrypoint-is-deprecated',
87-
formatterConfigPath ?? '',
87+
configDir,
8888
'Deprecated: Use the `tailwindStylesheet` option for v4 projects instead of `tailwindEntryPoint`.',
8989
)
9090
stylesheetPath = options.tailwindEntryPoint
@@ -93,15 +93,14 @@ export async function getTailwindConfig(options: ParserOptions): Promise<Unified
9393
if (!stylesheetPath && options.tailwindConfig && options.tailwindConfig.endsWith('.css')) {
9494
console.warn(
9595
'config-as-css-is-deprecated',
96-
formatterConfigPath ?? '',
96+
configDir,
9797
'Deprecated: Use the `tailwindStylesheet` option for v4 projects instead of `tailwindConfig`.',
9898
)
9999
stylesheetPath = options.tailwindConfig
100100
}
101101

102102
return getTailwindConfigFromLib({
103-
base,
104-
formatterConfigPath: formatterConfigPath ?? undefined,
103+
base: configDir,
105104
filepath: options.filepath,
106105
configPath,
107106
stylesheetPath,

src/lib.ts

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,10 @@ export interface SorterOptions {
1515
* The directory used to resolve relative file paths.
1616
*
1717
* When not provided this will be:
18-
* - Inferred from `formatterConfigPath` if given; otherwise
1918
* - The current working directory
2019
*/
2120
base?: string
2221

23-
/**
24-
* The path to the config file containing formatter settings.
25-
*
26-
* Used for warning context and to infer `base` when not provided.
27-
*/
28-
formatterConfigPath?: string
29-
3022
/**
3123
* The path to the file being formatted.
3224
*
@@ -97,7 +89,6 @@ export interface Sorter {
9789

9890
type TailwindConfigOptions = {
9991
base?: string
100-
formatterConfigPath?: string
10192
filepath?: string
10293
configPath?: string
10394
stylesheetPath?: string
@@ -147,11 +138,8 @@ function cacheForDirs<V>(
147138
let pathToApiMap = expiringMap<string | null, Promise<UnifiedApi>>(10_000)
148139

149140
export async function getTailwindConfig(options: TailwindConfigOptions): Promise<UnifiedApi> {
150-
let base =
151-
options.base ??
152-
(options.formatterConfigPath ? path.dirname(options.formatterConfigPath) : process.cwd())
141+
let base = options.base ?? process.cwd()
153142
let inputDir = options.filepath ? path.dirname(options.filepath) : base
154-
let formatterConfigPath = options.formatterConfigPath ?? ''
155143

156144
let configPath = resolveIfRelative(base, options.configPath)
157145
let stylesheetPath = resolveIfRelative(base, options.stylesheetPath)
@@ -168,7 +156,7 @@ export async function getTailwindConfig(options: TailwindConfigOptions): Promise
168156
// We resolve this relative to the config file because it is *required*
169157
// to work with a project's custom config. Given that, resolving it
170158
// relative to where the path is defined makes the most sense.
171-
let stylesheet = resolveStylesheet(stylesheetPath, formatterConfigPath)
159+
let stylesheet = resolveStylesheet(stylesheetPath, base)
172160

173161
// Locate *explicit* v3 configs relative to the formatter config file
174162
//
@@ -208,7 +196,7 @@ export async function getTailwindConfig(options: TailwindConfigOptions): Promise
208196
// Warn them about this and use the bundled v4.
209197
console.error(
210198
'explicit-stylesheet-and-config-together',
211-
formatterConfigPath,
199+
base,
212200
`You have specified a Tailwind CSS stylesheet and a Tailwind CSS config at the same time. Use stylesheetPath unless you are using v3. Preferring the stylesheet.`,
213201
)
214202
}
@@ -223,7 +211,7 @@ export async function getTailwindConfig(options: TailwindConfigOptions): Promise
223211
mod = null
224212
console.error(
225213
'stylesheet-unsupported',
226-
formatterConfigPath,
214+
base,
227215
'You have specified a Tailwind CSS stylesheet but your installed version of Tailwind CSS does not support this feature.',
228216
)
229217
}
@@ -317,7 +305,7 @@ function findClosestJsConfig(inputDir: string): string | null {
317305
return configPath
318306
}
319307

320-
function resolveStylesheet(stylesheetPath: string | null, formatterConfigPath: string): string | null {
308+
function resolveStylesheet(stylesheetPath: string | null, base: string): string | null {
321309
if (!stylesheetPath) return null
322310

323311
if (
@@ -330,7 +318,7 @@ function resolveStylesheet(stylesheetPath: string | null, formatterConfigPath: s
330318
) {
331319
console.error(
332320
'stylesheet-is-js-file',
333-
formatterConfigPath,
321+
base,
334322
"Your `stylesheetPath` option points to a JS/TS config file. You must point to your project's `.css` file for v4 projects.",
335323
)
336324
} else if (
@@ -341,13 +329,13 @@ function resolveStylesheet(stylesheetPath: string | null, formatterConfigPath: s
341329
) {
342330
console.error(
343331
'stylesheet-is-preprocessor-file',
344-
formatterConfigPath,
332+
base,
345333
'Your `stylesheetPath` option points to a preprocessor file. This is unsupported and you may get unexpected results.',
346334
)
347335
} else if (!stylesheetPath.endsWith('.css')) {
348336
console.error(
349337
'stylesheet-is-not-css-file',
350-
formatterConfigPath,
338+
base,
351339
'Your `stylesheetPath` option does not point to a CSS file. This is unsupported and you may get unexpected results.',
352340
)
353341
}
@@ -361,7 +349,6 @@ export async function createSorter(opts: SorterOptions): Promise<Sorter> {
361349

362350
let api = await getTailwindConfig({
363351
base: opts.base,
364-
formatterConfigPath: opts.formatterConfigPath,
365352
filepath: opts.filepath,
366353
configPath: opts.configPath,
367354
stylesheetPath: opts.stylesheetPath,

tests/sorter.test.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,4 @@ describe('createSorter', () => {
1919
expect(sorted).toBe('bg-red-500 sm:bg-tomato')
2020
})
2121

22-
test('infers base from formatterConfigPath', async () => {
23-
let fixtureDir = path.resolve(__dirname, 'fixtures/basic')
24-
let sorter = await createSorter({
25-
formatterConfigPath: path.join(fixtureDir, 'prettier.config.js'),
26-
filepath: path.join(fixtureDir, 'index.html'),
27-
configPath: './tailwind.config.js',
28-
})
29-
30-
let [sorted] = sorter.sortClassAttributes(['sm:bg-tomato bg-red-500'])
31-
expect(sorted).toBe('bg-red-500 sm:bg-tomato')
32-
})
3322
})

0 commit comments

Comments
 (0)