aviflombaum / shadcn-rails

https://shadcn.rails-components.com
MIT License
476 stars 32 forks source link

Better way to require tailwind_merge #17

Open aviflombaum opened 11 months ago

aviflombaum commented 11 months ago

I'm currently requiring tailwind merge in app/helpers/components_helper.rb because I can't otherwise get the installing application to register the dependency. There's got to be a better way and I think it's actually including an Engine that can require it and letting the installing app use that (i.e making it a real gem not just a way to get generators in your application, even if it does nothing else). But also, what's the issue with the require in the helper file really?

sergitejada commented 10 months ago

Possible Idea: Using a Rails Engine could be an excellent way to address the issue you're facing with the tailwind_merge dependency in your component library.

Why an Engine?

A Rails Engine is essentially a mini Rails application that can be mounted within another Rails application. It can have its own views, helpers, models, controllers, and most importantly, its own gems and dependencies.

Steps to implement this solution:

Conversion to Engine: Convert your component library into a mountable Engine. This allows you to encapsulate all components and associated dependencies into a unified package.

Manage Dependencies: Within your Engine's .gemspec, you can add tailwind_merge as a dependency. This way, any application that uses your Engine will automatically install the tailwind_merge gem.

Initialization: Within the structure of your Engine, you can require tailwind_merge in an initializer file. This ensures that, when the Engine is loaded into an application, tailwind_merge will also be loaded.

Documentation: Provide clear instructions to users of your library on how to add and configure the Engine in their applications. They won't need to worry about setting up tailwind_merge as it will be handled internally by your Engine.

Benefits:

Encapsulation: Separate your component library from the main application logic, offering a cleaner integration.

Dependency Management: Dependencies like tailwind_merge are managed internally, reducing the chance of conflicts and setup issues for end users of your library.

Extensibility: In the future, if you decide to add more features or dependencies to your library, having an Engine makes this process easier.

By adopting a Rails Engine for your component library, you're not only providing a solution to the current problem but also setting a solid foundation for future expansions and improvements.