The goal is to create component modules as Angular Library and keep it separate from the main application. So, components library maintained as standalone package published to NPM, and then can be imported into different applications. Component styles written using Tailwind's @apply
utility - https://tailwindcss.com/docs/extracting-components
Requisitions
- Both components library and main application use Tailwind CSS.
- Tailwind's
purge
(PurgeCSS) should be applied to final project including components from external library.
Problem
I have problem with components styling in library using TailwindCSS, as final AMD and ES2015 builds contain inlined styles, which can't be processed by Tailwind configuration in main application.
It works great as soon as I'm using Angular Library locally at the same main project (see example of the components "local presentment"). But when I'm publishing library as NPM package, classes inlined into modules together with @apply
utility which can't be processed correctly.
Example of the released library component code (ES2015 module):
styles: ["@layer components{.first-component-base {@apply w-full;} .first-component-base.first-component-disabled {@apply opacity-50;} .first-component-base .first-component-wrapper {@apply rounded-lg bg-skin-fill;} .first-component-base .first-component-wrapper .first-component-block {@apply max-w-7xl mx-auto py-3 px-3 sm:px-6 lg:px-8;} .first-component-text {@apply ml-3 font-medium text-white truncate;}}\n"]
As you can see, built module styles contain @apply
which can't be processed by Tailwind configuration of main application if you add components library as NPM package.
Source code example, and README here - https://github.com/maxcoffer/angular-tailwind-library
Possible Solutions
This is what I've tried so far, or tested as examples, but it doesn't work exactly perfect or has own downsides.
Use components extraction as TailwindCSS
plugin
(see documentation) - huge pain in the ass as you have no intellisense support for that method, and you need to write all styles within JS/TS which can be included into Tailwind configuration.Use inline Tailwind class names instead of component extraction with
@apply
- does work at some point, until you have complex components with dynamic behavioural styling (which is why I'm using SCSS and@apply
).Write it like others, using CSS in JS/TS or even create configuration for each component with JS objects - again, no intellisense, no SCSS, no way to write styles using standard component styling methods.
Question
Can you suggest how to use @apply
utility while making components (or any other efficient method without huge customisation), but also extract Tailwind classes during NPM package build process? Or how to extract inlined @apply
from modules at the main application level?