Tailwind CSS JIT utility generation relative to PostCSS plugin order
0 reputation · 26 Sept 2023, 10:30 UTC
0 reputation · 26 Sept 2023, 10:30 UTC
Tailwind CSS v3 runs the JIT engine by default as a PostCSS plugin and generates utilities only from class names found in configured content paths. At the integration boundary, other PostCSS plugins may be ordered before or after Tailwind, which changes whether those plugins see raw @tailwind directives or already-generated utility rules.
The goal is to define the expected ordering between Tailwind utility generation and downstream PostCSS transforms, especially for @apply references that Tailwind resolves while processing CSS. Later plugins that alter referenced rules do not retroactively update applied utilities, and dynamically assembled class names may be missed by the content scanner unless safelisted.
Should Tailwind generate utilities before other PostCSS plugins transform the CSS, or after them? Does plugin order change which utilities are available to @apply, and how should the pipeline treat content-scanned classes versus plugin-generated CSS?
33775 reputation · 26 Sept 2023, 22:05 UTC
Tailwind CSS should be positioned before most other PostCSS transformation plugins (such as Autoprefixer or cssnano). Because the JIT engine generates utility rules on-the-fly based on your content scan, these rules must exist in the CSS stream before subsequent plugins can process, prefix, or minify them.
The sequence of your postcss.config.js array determines the visibility of rules:
@apply directives into raw CSS properties. Subsequent plugins then see this expanded CSS and can apply vendor prefixes or optimizations.@tailwind directives. They cannot transform utilities that haven't been generated yet. Furthermore, if a plugin modifies the source CSS in a way that alters Tailwind's directives, the JIT engine may fail to inject styles correctly.It is critical to understand that @apply is resolved during the Tailwind execution phase. If you have a plugin that modifies CSS variables or property values, and that plugin runs after Tailwind, it will not retroactively update the styles that were injected via @apply. The @apply directive essentially copies the utility's current definition into your custom CSS rule at that specific moment in the pipeline.
The JIT scanner operates independently of the PostCSS transformation chain; it reads your source files, not the CSS stream. Therefore:
@tailwind utilities directive location and are available to all downstream plugins.safelist in tailwind.config.js to ensure they are generated regardless of the PostCSS order.To verify your pipeline is ordered correctly, check the following:
backdrop-blur lack vendor prefixes (e.g., -webkit-backdrop-filter), Tailwind is likely running after Autoprefixer.Diagnostic Detail Needed: Are you using any PostCSS plugins that specifically modify CSS syntax or handle pre-processing (like postcss-import)? These typically must run before Tailwind to ensure the JIT engine sees the full CSS tree.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.