MCPcopy
hub / github.com/tailwindlabs/prettier-plugin-tailwindcss / getTailwindConfig

Function getTailwindConfig

src/sorter.ts:107–202  ·  view source on GitHub ↗
(options: TailwindConfigOptions)

Source from the content-addressed store, hash-verified

105 * @internal
106 */
107export async function getTailwindConfig(options: TailwindConfigOptions): Promise<UnifiedApi> {
108 let base = options.base ?? process.cwd()
109 let inputDir = options.filepath ? path.dirname(options.filepath) : base
110
111 let configPath = resolveIfRelative(base, options.configPath)
112 let stylesheetPath = resolveIfRelative(base, options.stylesheetPath)
113
114 // Locate Tailwind CSS itself
115 //
116 // We resolve this like we're in `inputDir` for better monorepo support as
117 // Prettier may be configured at the workspace root but Tailwind CSS is
118 // installed for a workspace package rather than the entire monorepo
119 let [mod, pkgDir] = await resolveTailwindPath({ packageName: options.packageName }, inputDir)
120
121 // Locate project stylesheet relative to the formatter config file
122 //
123 // We resolve this relative to the config file because it is *required*
124 // to work with a project's custom config. Given that, resolving it
125 // relative to where the path is defined makes the most sense.
126 let stylesheet = resolveStylesheet(stylesheetPath, base)
127
128 // Locate *explicit* v3 configs relative to the formatter config file
129 //
130 // We use this as a signal that we should always use v3 to format files even
131 // when the local install is v4 — which means we'll use the bundled v3.
132 let jsConfig = resolveJsConfigPath(configPath)
133
134 // Locate the closest v3 config file
135 //
136 // Note:
137 // We only need to do this when a stylesheet has not been provided otherwise
138 // we'd know for sure this was a v4 project regardless of what local Tailwind
139 // CSS installation is present. Additionally, if the local version is v4 we
140 // skip this as we assume that the user intends to use that version.
141 //
142 // The config path is resolved in one of two ways:
143 //
144 // 1. When automatic, relative to the input file
145 //
146 // This ensures monorepos can load the "closest" JS config for a given file
147 // which is important when a workspace package includes Tailwind CSS *and*
148 // Prettier is configured globally instead of per-package.
149 //
150 // 2. When explicit via `configPath`, relative to `base`
151 if (!stylesheet && !mod?.__unstable__loadDesignSystem) {
152 jsConfig = jsConfig ?? findClosestJsConfig(inputDir)
153 }
154
155 // We've found a JS config either because it was specified by the user
156 // or because it was automatically located. This means we should use v3.
157 if (jsConfig) {
158 if (!stylesheet) {
159 return pathToApiMap.remember(`${pkgDir}:${jsConfig}`, async () => {
160 const { loadV3 } = await import('./versions/v3')
161 return loadV3(pkgDir, jsConfig)
162 })
163 }
164

Callers 1

createSorterFunction · 0.70

Calls 8

resolveIfRelativeFunction · 0.85
resolveTailwindPathFunction · 0.85
resolveStylesheetFunction · 0.85
resolveJsConfigPathFunction · 0.85
findClosestJsConfigFunction · 0.85
loadV3Function · 0.85
loadV4Function · 0.85
rememberMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…