Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support jsbundling #44

Merged
merged 6 commits into from
Dec 25, 2024
Merged

Support jsbundling #44

merged 6 commits into from
Dec 25, 2024

Conversation

jorgemanrubia
Copy link
Member

@jorgemanrubia jorgemanrubia commented Dec 23, 2024

Add supports for JS bundling:

  • Detects jsbundling and configures a compatible setup. When a JS file changes, it will reload the HTML instead of reloading the controller that changed. Hot reloading is not possible in the browser as it won't resolve the dependencies outside of the bundling environment.
  • This also reworks the approach to reload stimulus controllers. Instead of parsing the importmaps entry, the engine will expose an endpoint to fetch the source of changed assets. The Stimulus reloader will fetch the changed javascript files using that endpoint so that it won't depend on importmaps anymore.
  • Add options to configure the handled extensions for each kind of change.

Closes #12

@jorgemanrubia jorgemanrubia changed the title Support jss bundling Support jssbundling Dec 23, 2024
@jorgemanrubia jorgemanrubia changed the title Support jssbundling Support jsbundling Dec 23, 2024
@jorgemanrubia jorgemanrubia force-pushed the jsbundling branch 3 times, most recently from e31a423 to 58b182e Compare December 23, 2024 19:30
@jorgemanrubia jorgemanrubia marked this pull request as ready for review December 23, 2024 19:30
@jorgemanrubia jorgemanrubia mentioned this pull request Dec 23, 2024
@jorgemanrubia jorgemanrubia force-pushed the jsbundling branch 2 times, most recently from 68a02f4 to 77b2c69 Compare December 24, 2024 10:05
@jorgemanrubia jorgemanrubia marked this pull request as draft December 24, 2024 10:48
@kobaltz
Copy link

kobaltz commented Dec 25, 2024

I was playing around with this branch and noticed in a very simple jsbundling - esbuild example, the HTML changes will trigger and reinitialize the stimulus controllers! Really cool! ❤️

I also noticed that if you try to modify the stimulus controller (the default hello controller in this example), then it will cause a javascript error.

My steps to reproduce this is basically creating a new esbuild/bootstrap Rails 8 application with propshaft and add the hotwire-spark gem.

group :development do
  gem "hotwire-spark", github: "hotwired/spark", branch: :jsbundling
end

Add the stimulus controller in a simple scaffolded page.

<h1>Welcome#index!!!!</h1>
<p>Find me in app/views/welcome/index.html.erb</p>

<div data-controller="hello">STIMULUS CONTROLLER</div>

And then try to make a change to the text in the hello stimulus controller.

CleanShot 2024-12-24 at 22 22 42

@jorgemanrubia
Copy link
Member Author

Thanks @kobaltz. Yes, I found that issue too. I like the change but this needs more work. Reloading individual classes is not as easy as with import maps as modules can't be resolved by the browser... unless we used importmaps to define them 😅. I still like the change independently of this since it lets us reload JS files faster (without an additional request to reload the html document), but I need to play more with the jsbundling scenario.

@jorgemanrubia jorgemanrubia force-pushed the jsbundling branch 2 times, most recently from ef9b9cc to 4cd2ec6 Compare December 25, 2024 06:40
This reworks the approach to remove the dependency. Instead of parsing the importmaps entry, the engine will now expose an endpoint to fetch the source of changed assets. The Stimulus reloader will fetch the changed javascript files using that endpoint so that it won't depend on importmaps anymore.
@jorgemanrubia jorgemanrubia marked this pull request as ready for review December 25, 2024 09:02
@jorgemanrubia jorgemanrubia merged commit ef803e8 into main Dec 25, 2024
2 checks passed
@nickjj
Copy link

nickjj commented Dec 25, 2024

I wanted to mention this is working quite nicely for the basic tests I ran with Rails 8 using jsbundling, Propshaft and Spark v0.1.10.

  • Install Spark
  • Open page with Stimulus controller
  • Make a change, such as adding console.log("hello") to a piece of JS
  • Do the thing on the page that invokes that piece of JS
  • See hello emit in the console
  • Remove that console.log("hello") line
  • Do the thing on the page that invokes that piece of JS
  • No longer see the console output being emit
  • Change text within a Stimulus controller
  • I didn't observe any console errors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Support jsbundling
3 participants