Closed knpwrs closed 2 years ago
CSS modules make JIT difficult because each css file is individually evaluated and the postcss plugin isn't given awareness of this, so each file gets its own scope. AKA, this does happen in other frameworks, like Next that uses CSS modules. Next has a concept of :global
which almost helps, but can result in similar things as you have above.
You could work around this by running a follow up plugin on the output of the css processes, that combines media queries and then another pass with like UnCSS which can remove duplicates.
Again tho yeah, the problem is that the plugins are ran per file without any context or provided any stylesheet that's a global file to put the discovered props into. With CSS modules, i think the best route forward for a minimal amount of props shipped, is to individually import a color palette, or sizes, from open props, and drop them into a global file you have. then all the module files can just use them, without jit props, and you can add or remove prop packs as you need. this will lead to shipping more reds, for exaple, then you may be using, but it's not going to bulk up your stylesheet by an amount to be worried about.
Is there a way to implement the way Tailwind does it?
probably! it's all code and connective tissue could get made so that jit-props gets given a file like app.css
to inject the discovered props into. looks like with sveltekit and tailwind, there's a file established as a global space and then their plugin uses that file for the destination of injection. jit-props can be provided a selector context for props, it could be extended to accept a file also.
going to close this here as we should continue to convo in the jit-props plugin
I am not sure if this happens with other frameworks as well. In SvelteKit, using the following
postcss.config.cjs
file:And the following
svelte.config.js
file:And with two
.svelte
files, each defining a class withcolor: var(--red-6);
, I get the following CSS output in both development and production:In Tailwind's SvelteKit setup docs their jit setup generates a single CSS file to import into your app. Is something like this possible with Open Props? Essentially generate a single CSS file that contains only the props I use and then include that CSS file in the app.