Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Properly handles Stimulus controller in parent folders
If you try to register Stimulus controllers from another path than `controller/` the regex do not process it properly. Also we do not need to add `controllers--` for controller in controller folder. See [doc](https://stimulus.hotwired.dev/handbook/installing#controller-filenames-map-to-identifiers). Examples with dedicated stimulus view components controllers. ``js import { Application } from "@hotwired/stimulus" const application = Application.start() window.Stimulus = application import componentControllers from '../components/**/*_controller.js'; componentControllers.forEach((controller) => { application.register(controller.name, controller.module.default) }); import controllers from "./**/*_controller.js" controllers.forEach((controller) => { application.register(controller.name, controller.module.default) }) ``` To try this code. ```js const ok =[ 'controllers/release_target_controller.js', 'controllers/toggle_controller.js', '../components/deploy_slack_pref_component_controller.js' ].map((module) => module .replace(/_controller.[j|t]s$/, "") .replace(/^controllers\//, "") // do not namespace controllers in controllers directory .replace(/\.\.\//, "") // do not use parent folder anotation for controller name .replace(/\//g, "--") .replace(/_/g, '-') ) console.log(ok) // [ // 'release-target', // 'toggle', // 'components--deploy-slack-pref-component' // ] ``` Fix: excid3#15
- Loading branch information