Tailwind styles from Twig not included in component-level CSS (Drupal SDC + Webpack) #19920
Replies: 2 comments 2 replies
-
|
For each component, have a For each component CSS file:
However, I personally would not split the CSS per component - you will get a lot of duplication. Tailwind is best used "globally". Hence instead, I'd keep the CSS in a singular global CSS file. Yes, there would be some redundant CSS on some pages. However, having the CSS file in the global CSS file included in the global CSS file cache means it would only be downloaded to the browser once and then cached for all other pages, negating the byte transfer cost. |
Beta Was this translation helpful? Give feedback.
-
|
@wongjn Do you mean duplicates of directives? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a Drupal project where I’m using Tailwind CSS v3 with Webpack. There is a main.js file that imports main.css, and Tailwind classes used inside Twig files are being built correctly.
My Tailwind config looks like this:
Everything works perfectly in this setup.
However, I need to generate separate CSS and JS files for each component instead of having everything bundled only into main.js and main.css.
When using Single Directory Components (SDC) in Drupal, if a component has its own CSS and JS files inside its folder, they are automatically attached when the component is used. Therefore, having a separate CSS file per component is more beneficial than building all component styles into a single global file.
I updated my Webpack configuration to support component-based entries. Now it builds separate CSS and JS files for each component, but only when I add CSS/JS inside the component’s src folder.
The problem is:
Tailwind classes used inside component Twig files are still only included in the global main.css, not in the component-specific CSS files.
So currently:
• Component JS/CSS files are generated ✔️
• But Tailwind styles from Twig are only compiled into main.css ❌
Here is my Webpack config:
buttoncomponent exampleScreen.Recording.2026-04-08.at.15.51.09-2.mp4
In short, I want Tailwind to generate component-level CSS based on Twig usage inside each component, not only in the global bundle.
Beta Was this translation helpful? Give feedback.
All reactions