mirror of
https://github.com/hanzoai/tailwindcss.git
synced 2026-09-24 17:03:14 +00:00
Fall back to the plugin base when PostCSS has no from option (#19980)
## Summary
`@tailwindcss/postcss` derives `inputBasePath` from `result.opts.from`:
```ts
let inputFile = result.opts.from ?? ''
let inputBasePath = path.dirname(path.resolve(inputFile))
```
When PostCSS calls the plugin without `from` (some bundlers, including
Turbopack, do this for certain CSS inputs), `inputFile` is `''`,
`path.resolve('')` returns `process.cwd()`, and `path.dirname(...)`
therefore returns the **parent of CWD**. The downstream `compileAst({
base: inputBasePath })` call then asks the resolver to find
`tailwindcss` from one level above the project root, which fails with:
```
Can't resolve 'tailwindcss' in '<parent of CWD>'
```
The plugin already computes `base = opts.base ?? process.cwd()` near the
top. Reusing that as the fallback gives a sensible default (CWD) and
respects an explicit `opts.base` when set.
```diff
-let inputBasePath = path.dirname(path.resolve(inputFile))
+let inputBasePath = inputFile
+ ? path.dirname(path.resolve(inputFile))
+ : base
```
## Test plan
Added a test in `packages/@tailwindcss-postcss/src/index.test.ts` that
processes `@import 'tailwindcss'` via `processor.process(input)` with no
`from` option. Before the fix, this throws `Error: Can't resolve
'tailwindcss' in '<parent of CWD>'`; after the fix, the import resolves
and the processor returns non-empty CSS.
I wasn't able to run the suite locally — `pnpm build` requires `cargo`
for `@tailwindcss/oxide` and I don't have a Rust toolchain set up — so
the test has been written to match existing conventions in
`index.test.ts` (vitest, plain
`postcss([tailwindcss({...})]).process(...)`), and I'm relying on CI to
verify.
---------
Co-authored-by: rebasecase <rebasecase@localhost>
Co-authored-by: Robin Malfait <[email protected]>
This commit is contained in:
committed by
GitHub
co-authored by
rebasecase
Robin Malfait
parent
af1861bc01
commit
58cf6c7b74
@@ -140,6 +140,16 @@ describe('processing without specifying a base path', () => {
|
||||
})
|
||||
})
|
||||
|
||||
test('fallback to `base` directory when `result.opts.from` is not provided', async () => {
|
||||
let processor = postcss([
|
||||
tailwindcss({ base: `${__dirname}/fixtures/example-project`, optimize: { minify: false } }),
|
||||
])
|
||||
|
||||
let result = await processor.process(`@import 'tailwindcss'`)
|
||||
|
||||
expect(result.css.length).toBeGreaterThan(0)
|
||||
})
|
||||
|
||||
describe('plugins', () => {
|
||||
test('local CJS plugin', async () => {
|
||||
let processor = postcss([
|
||||
|
||||
@@ -115,7 +115,7 @@ function tailwindcss(opts: PluginOptions = {}): AcceptedPlugin {
|
||||
}
|
||||
|
||||
let context = getContextFromCache(postcss, inputFile, opts)
|
||||
let inputBasePath = path.dirname(path.resolve(inputFile))
|
||||
let inputBasePath = inputFile ? path.dirname(path.resolve(inputFile)) : base
|
||||
|
||||
// Whether this is the first build or not, if it is, then we can
|
||||
// optimize the build by not creating the compiler until we need it.
|
||||
|
||||
Reference in New Issue
Block a user