Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/@tailwindcss-postcss/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,22 @@ describe('processing without specifying a base path', () => {
})
})

test('processing input without a `from` option falls back to the plugin `base`', async () => {
// PostCSS may invoke plugins without `result.opts.from` (some bundlers,
// including Turbopack, do this for certain CSS inputs). When that happens
// `path.dirname(path.resolve(''))` yields the *parent* of CWD, which made
// `@import 'tailwindcss'` resolution walk above the project root and fail
// with "Can't resolve 'tailwindcss' in '<parent of CWD>'". The fallback
// should be the plugin-level `base` (which itself defaults to CWD).
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([
Expand Down
11 changes: 10 additions & 1 deletion packages/@tailwindcss-postcss/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,16 @@ function tailwindcss(opts: PluginOptions = {}): AcceptedPlugin {
}

let context = getContextFromCache(postcss, inputFile, opts)
let inputBasePath = path.dirname(path.resolve(inputFile))
// When PostCSS is invoked without `from` (some bundlers, including
// Turbopack, do this for certain inputs), `inputFile` is `''`.
// `path.resolve('')` returns `process.cwd()`, so `path.dirname(...)`
// would fall back to the *parent* of CWD — wrong, and breaks
// `@import 'tailwindcss'` resolution against the project's
// `node_modules`. Fall back to the plugin-level `base` (which
// already defaults to `process.cwd()` and respects `opts.base`).
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.
Expand Down