Map Stimulus controller filenames to identifiers as in the Stimulus docs #486
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a 🐛 bug fix.
I haven't written a test for this change because AFAIK bundled configurations are not included in tests. But let me know if you'd like me to add a test. I did run the tests, though, and none are failing. I also manually tested my changes (more on that below).
Summary
I've made minor changes to the code that the Stimulus bundled configuration adds to
index.js
, so that Stimulus controllers are detected and named according to the Stimulus docs: https://stimulus.hotwired.dev/handbook/installing#controller-filenames-map-to-identifiers. Specifically, my changes ensure the following:reading_list_controller.js
gets the identifierreading-list
.reading-list-controller.js
.I manually tested my changes by using the changed JS code in my site's
index.js
. I ensured that it properly detects and registers Stimulus controllers with long multi-word names, whether the filename is separated by underscores or hyphens.Context
I noticed this discrepancy with the Stimulus docs when I was building a new site in Bridgetown 1.0 beta. The site has a Stimulus controller called
reading_list_controller.js
, which was being registered asreading_list
, which does not match thereading-list
identifier in the HTML.As I was fixing that, I also noticed that the Stimulus doc linked above allows for hyphen-separated Stimulus controller filenames, so I added that into this PR as well.
This is my very first PR to Bridgetown, so let me know if I've missed anything. Thanks!