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

Load Stimulus controller from app/component #15

Closed
benoittgt opened this issue Apr 17, 2023 · 2 comments
Closed

Load Stimulus controller from app/component #15

benoittgt opened this issue Apr 17, 2023 · 2 comments

Comments

@benoittgt
Copy link

Hello

First thanks a lot for this project, the video you did on jquery and jquery-ui.

With view component, you can have Stimulus file that are in app/component folder.

So I did this in my app/javascript/controller.js

import { Application } from "@hotwired/stimulus"

const application = Application.start()
window.Stimulus = application

import componentControllers from '../components/**/*_controller.js';

componentControllers.forEach((controller) => {
  console.log(controller.name)
  application.register(controller.name, controller.module.default)
});

import controllers from "./**/*_controller.js"
controllers.forEach((controller) => {
  console.log(controller.name)
  application.register(controller.name, controller.module.default)
})

// enable Stimulus debug mode in development
Stimulus.debug = process.env.NODE_ENV === 'development'

It ouputs controller name but the name looks wrong on the component folder where I host at the moment only one Stimulus file.

Screenshot 2023-04-17 at 18 27 03

@excid3 excid3 added the bug Something isn't working label Apr 19, 2023
benoittgt added a commit to benoittgt/esbuild-rails that referenced this issue Apr 25, 2023
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
benoittgt added a commit to benoittgt/esbuild-rails that referenced this issue Apr 25, 2023
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
@kevynlebouille
Copy link

Hi @benoittgt

Same here ! We ended up with some :

import controllers from "./**/*_controller.js"
controllers.forEach((controller) => {
  application.register(controller.name.replace('controllers--', ''), controller.module.default)
})

import componentControllers from "../components/**/*_controller.js"
componentControllers.forEach((controller) => {
  application.register(controller.name.replace('..--components--', ''), controller.module.default)
})

Thx,
Kevyn

benoittgt added a commit to benoittgt/esbuild-rails that referenced this issue Apr 26, 2023
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
@benoittgt
Copy link
Author

Thanks @kevynlebouille

I just open a patch to fix that. :)

#16

@excid3 excid3 removed the bug Something isn't working label Jun 2, 2023
@excid3 excid3 closed this as completed in 1bcc0f4 Jun 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants