Skip to content

Commit d444362

Browse files
authored
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.
1 parent a7ebc9c commit d444362

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
### Fixed
1515

1616
- Fix dependency related warnings when using `@tailwindcss/postcss` on Windows ([#15321](https://github.com/tailwindlabs/tailwindcss/pull/15321))
17+
- Skip creating a compiler for CSS files that should not be processed ([#15340](https://github.com/tailwindlabs/tailwindcss/pull/15340))
1718

1819
## [4.0.0-beta.6] - 2024-12-06
1920

@@ -749,3 +750,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
749750
- Move the CLI into a separate `@tailwindcss/cli` package ([#13095](https://github.com/tailwindlabs/tailwindcss/pull/13095))
750751

751752
## [4.0.0-alpha.1] - 2024-03-06
753+

packages/@tailwindcss-postcss/src/index.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,29 @@ function tailwindcss(opts: PluginOptions = {}): AcceptedPlugin {
6363
postcssPlugin: 'tailwindcss',
6464
async Once(root, { result }) {
6565
env.DEBUG && console.time('[@tailwindcss/postcss] Total time in @tailwindcss/postcss')
66+
67+
// Bail out early if this is guaranteed to be a non-Tailwind CSS file.
68+
{
69+
let canBail = true
70+
root.walkAtRules((node) => {
71+
if (
72+
node.name === 'import' ||
73+
node.name === 'theme' ||
74+
node.name === 'config' ||
75+
node.name === 'plugin' ||
76+
node.name === 'apply'
77+
) {
78+
canBail = false
79+
return false
80+
}
81+
})
82+
if (canBail) {
83+
env.DEBUG &&
84+
console.timeEnd('[@tailwindcss/postcss] Total time in @tailwindcss/postcss')
85+
return
86+
}
87+
}
88+
6689
let inputFile = result.opts.from ?? ''
6790
let context = getContextFromCache(inputFile, opts)
6891
let inputBasePath = path.dirname(path.resolve(inputFile))

0 commit comments

Comments
 (0)