Is it possible to generate config based on the output css file? #413
Replies: 5 comments 14 replies
-
i actually managed to create an almost working prototype with a custom postcss plugin and was able to generate a file import { getDefaultConfig } from 'tw-merge'
const myCustomPostcssPlugin = () => {
// parse css rules, get used css classes
// generate new config, filter default config by used classes + add custom classes from tailwind config
const newConfig = doSomething(getDefaultConfig())
const dirPath = __dirname + '/test/_generated'
tryCreateDir(dirPath)
fs.writeFileSync(dirPath + '/tw-config.ts', `
import { createTailwindMerge } from 'tw-merge'
export const twMerge = createTailwindMerge(${() => newConfig})
`);
} |
Beta Was this translation helpful? Give feedback.
-
This is an interesting idea, indeed! I also thought about something like this a few times but never got around to build it due to lack of time. There is also an open discussion on the Tailwind repo to add support for something like that: tailwindlabs/tailwindcss#10348 I think it would be a rather large undertaking to read a CSS file directly, but it's possible. Some reasons that make this difficult:
The gist is that I would find a generator like that really valuable, but it would be too much for me to handle next to maintaining the current codebase, my day job and other priorities. If you're up for the challenge, feel free to build it yourself! I'd be happy to work on the necessary API within tailwind-merge for that. Related: #185 |
Beta Was this translation helpful? Give feedback.
-
for point 1 my idea was to separate the "main" class and modifiers, i.e for point 2, that's true. For initial implementation I could probably ignore those classes completely, since they don't conform to "utility-first" approach for point 3, I have thought about it and Idk whether it's worth the effort to support it, at least for POC. Since the tw stylesheet is deterministic and the classes always have the same order, it might be ok to leave I would appreciate your guidance in this. Maybe you could point me to some tests in |
Beta Was this translation helpful? Give feedback.
-
i think eventually what i want to do is to create a minimal "tailwind-styled-components" like package that supports tw-merge out of the box + simpler api where API example:
I already have |
Beta Was this translation helpful? Give feedback.
-
Hey @dcastil, i have created this plugin. It seems to work well & passes all tests from this repo (except for ring VS shadow test, which i probably won't support because it's a very special case and there's a workaround). I also haven't tested custom prefix and custom separators, but I don't foresee any issues, i just didn't have time to write those tests for custom tailwind configs. Implementation TLDR. the way it works is I have a postcss plugin that parses styles after tailwind plugin and then generates a minimized config that has this structure:
I think this is a pretty small & efficient way to store this metadata that i then use it to create "twMerge" that does all the merging logic based on this config Given that there's 1:1 mapping between tailwind generated css, there's an out of the box support for plugins |
Beta Was this translation helpful? Give feedback.
-
I wonder if it makes sense to generate tw-merge config from the output css file. This way it wouldn't need custom code for tw plugins and would only use styles that are used on the codebase as opposed to loading everything. It would also support any custom tw extensions out of the box.
The idea is basically this: add a vite plugin (that's what i use at the moment) to use postcss to parse output css file (should work for development and production), generate a map of conflicting classes and generate a file that exports twMerge function.
This function can then be used in development and the resulted bundle is as lean and mean as possible
Beta Was this translation helpful? Give feedback.
All reactions