Skip to content

Commit 44a25ac

Browse files
authored
Merge pull request #21 from marcoroth/stimulus-vite-helpers
Add note about `stimulus-vite-helpers` to README
2 parents fd7cc38 + 2357436 commit 44a25ac

File tree

1 file changed

+36
-13
lines changed

1 file changed

+36
-13
lines changed

README.md

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -140,20 +140,43 @@ Importmap::pin("@hotwired/stimulus-loading", to: "vendor/stimulus-laravel/stimul
140140

141141
### Vite Steps
142142

143-
2. Create a `resources/js/controllers/index.js` and load your controllers from there:
143+
1. Create a `resources/js/controllers/index.js` and chose if you want to register your controllers manually or not:
144144

145-
```js
146-
// This file is auto-generated by `php artisan stimulus:install`
147-
// Run that command whenever you add a new controller or create them with
148-
// `php artisan stimulus:make controllerName`
145+
#### Register controllers manually
146+
147+
```js
148+
// This file is auto-generated by `php artisan stimulus:install`
149+
// Run that command whenever you add a new controller or create them with
150+
// `php artisan stimulus:make controllerName`
149151

150-
import { application } from '../libs/stimulus'
152+
import { application } from '../libs/stimulus'
151153

152-
import HelloController from './hello_controller'
153-
application.register('hello', HelloController)
154-
```
154+
import HelloController from './hello_controller'
155+
application.register('hello', HelloController)
156+
```
157+
158+
#### Register controllers automatically
159+
160+
If you prefer to automatially register your controllers you can use the [`stimulus-vite-helpers`](https://www.npmjs.com/package/stimulus-vite-helpers) NPM package.
161+
162+
```js
163+
// resources/js/controllers/index.js
164+
165+
import { application } from '../libs/stimulus'
166+
import { registerControllers } from 'stimulus-vite-helpers'
155167
156-
3. Create `resources/js/libs/stimulus.js` with the following content:
168+
const controllers = import.meta.glob('./**/*_controller.js', { eager: true })
169+
170+
registerControllers(application, controllers)
171+
```
172+
173+
And install the NPM package:
174+
175+
```bash
176+
npm install stimulus-vite-helpers
177+
```
178+
179+
2. Create `resources/js/libs/stimulus.js` with the following content:
157180

158181
```js
159182
import { Application } from '@hotwired/stimulus'
@@ -167,19 +190,19 @@ window.Stimulus = application
167190
export { application }
168191
```
169192

170-
4. Create a `resources/js/libs/index.js` file (if it doesn't exist) and add the following line to it:
193+
3. Create a `resources/js/libs/index.js` file (if it doesn't exist) and add the following line to it:
171194
172195
```js
173196
import '../controllers'
174197
```
175198
176-
5. Add the following line to your `resources/js/app.js` file:
199+
4. Add the following line to your `resources/js/app.js` file:
177200
178201
```js
179202
import './libs';
180203
```
181204
182-
6. Finally, add the Stimulus package to NPM:
205+
5. Finally, add the Stimulus package to NPM:
183206
184207
```bash
185208
npm install @hotwired/stimulus

0 commit comments

Comments
 (0)