From 5d8a230d6830ea96e3e4fddae3876d71c9446d7a Mon Sep 17 00:00:00 2001 From: Benoit Tigeot Date: Tue, 25 Apr 2023 13:11:25 +0200 Subject: [PATCH] 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: https://github.com/excid3/esbuild-rails/issues/15 --- README.md | 21 +++++++++++++++++++++ src/index.js | 2 ++ 2 files changed, 23 insertions(+) diff --git a/README.md b/README.md index 94798f9..71fd866 100644 --- a/README.md +++ b/README.md @@ -65,8 +65,29 @@ import controllers from "./**/*_controller.js" controllers.forEach((controller) => { application.register(controller.name, controller.module.default) }) + +// For view components +// import componentControllers from '../components/**/*_controller.js'; +// ... + ``` +Controller identifiers will be the name of the file + the parent folder except in `controllers/` folder: + +``` +app +├── components +│   ├── pref_component.html.erb +│   ├── pref_component.rb +│   └── pref_component_controller.js // identifier 'components--deploy-pref-component' +└── javascript +    └── controllers +    ├── modal_controller.js // identifier 'modal' + └─- admin + └── status_controller.js // identifier 'admin--status' +``` + + #### Import ActionCable channels: ```javascript diff --git a/src/index.js b/src/index.js index ca0005d..77f5ca4 100644 --- a/src/index.js +++ b/src/index.js @@ -40,6 +40,8 @@ const railsPlugin = (options = { matcher: /.+\..+/ }) => ({ const controllerNames = files .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, '-') )