Skip creating a compiler for CSS files that should not be processed (#15340)

This PR skips creating a compiler in the `@tailwindcss/postcss`
implementation if we know that the CSS file we are handling is
definitely not a Tailwind CSS file.

This is a performance improvement for initial builds where some CSS
files would've been handling by Tailwind CSS but shouldn't. E.g.: When
setting up custom fonts in Next.js applications, each font will have
it's own CSS file that is passed to `@tailwindcss/postcss`.

Since they don't contain `@import` or any other Tailwind CSS directives,
we can just skip them.
This commit is contained in:
Robin Malfait 2024-12-09 11:55:33 +01:00 committed by GitHub
parent a7ebc9cda1
commit d444362d5e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 0 deletions

View File

@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Fix dependency related warnings when using `@tailwindcss/postcss` on Windows ([#15321](https://github.com/tailwindlabs/tailwindcss/pull/15321))
- Skip creating a compiler for CSS files that should not be processed ([#15340](https://github.com/tailwindlabs/tailwindcss/pull/15340))
## [4.0.0-beta.6] - 2024-12-06
@ -749,3 +750,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Move the CLI into a separate `@tailwindcss/cli` package ([#13095](https://github.com/tailwindlabs/tailwindcss/pull/13095))
## [4.0.0-alpha.1] - 2024-03-06

View File

@ -63,6 +63,29 @@ function tailwindcss(opts: PluginOptions = {}): AcceptedPlugin {
postcssPlugin: 'tailwindcss',
async Once(root, { result }) {
env.DEBUG && console.time('[@tailwindcss/postcss] Total time in @tailwindcss/postcss')
// Bail out early if this is guaranteed to be a non-Tailwind CSS file.
{
let canBail = true
root.walkAtRules((node) => {
if (
node.name === 'import' ||
node.name === 'theme' ||
node.name === 'config' ||
node.name === 'plugin' ||
node.name === 'apply'
) {
canBail = false
return false
}
})
if (canBail) {
env.DEBUG &&
console.timeEnd('[@tailwindcss/postcss] Total time in @tailwindcss/postcss')
return
}
}
let inputFile = result.opts.from ?? ''
let context = getContextFromCache(inputFile, opts)
let inputBasePath = path.dirname(path.resolve(inputFile))