react-rails
react-rails copied to clipboard
Use react-rails inside rails engine
I used react-rails in my main app but some of my rails engine need to use react as well. Do we have any way to use react-rails inside rails engines? I mean we create by my own component on use it inside my engine. Btw: After research a bit about it, I saw this answer about this: https://stackoverflow.com/questions/44447696/including-react-react-rails-into-a-rails-engine. Can we do like that? Is it official way in react-rail?
Thank you guys so much for support this issues.
System configuration
Sprockets: (>= 2.8, < 4.0) React-Rails version: 2.4.7 Rails version: 5.2.1 Ruby version: 2.5.1
The linked SO post seems like the reasonable way to do it. That one focuses on Sprockets but there should likely be a Webpack way of doing it also. (For those seeking, try google webpacker in a rails engine). I'll not say it's 'officially' supported because we have no tests for that and my company does not use it in that way meaning I could accidentally break it, but I don't see why the proposed solution would fail in the future. If you'd like to add a test for it that would also be wonderful and make it more official. Also if you would like to add that to the Readme as an explanation then that would also be appreciated. We rely on people's support to push the boundaries of what is possible so thank you for the question.
Here are other discussions around engines in the repo. https://github.com/reactjs/react-rails/issues/360 https://github.com/reactjs/react-rails/issues/337
I wasn't able to find any documentation in order to do it, and I ended up patching react-rails to get it to work by allowing me to set the Webpacker instance that react-rails uses at run-time (by default it tries to use the root app's webpacker instance, not the engines)
https://github.com/demsullivan/react-rails/tree/webpacker-instance-config
Then in my_engine/lib/my_engine.rb
I have (per https://github.com/rails/webpacker/blob/master/docs/engines.md):
def webpacker
@webpacker ||= ::Webpacker::Instance.new(
root_path: ROOT_PATH,
config_path: ROOT_PATH.join('config/webpacker.yml')
)
end
Then in lib/my_engine/engine.rb
I have:
initializer "react" do |app|
React::ServerRendering::WebpackerManifestContainer.webpacker_instance = MyEngine.webpacker
end
Closing the issue. We have a solution as suggested by @demsullivan.
@alkesh26 is that really a solution? Doesn't it mean that the Engine is overriding the App's webpacker?