@emaia/stimulus-dynamic-loader
v1.0.2
Published
Dynamic lazy-loading for Stimulus controllers with Turbo support
Maintainers
Readme
Stimulus Dynamic Loader
Lazy load your Stimulus controllers on demand with Vite's glob imports. Automatically registers controllers as they appear in the DOM.
Features
- ✨ Lazy Loading: Controllers are loaded only when needed
- 🚀 Turbo Integration: Automatically registers controllers on Turbo events
- 🔄 Dynamic DOM: Watches for new elements with MutationObserver
- 📦 Zero Config: Works out of the box with Vite's glob imports
- 🎯 Type Safe: Full TypeScript support
Installation
npm install @emaia/stimulus-dynamic-loaderUsage
Basic Setup
import { Application } from "@hotwired/stimulus";
import { registerControllers } from "@emaia/stimulus-dynamic-loader";
const Stimulus = Application.start();
const controllers = import.meta.glob("./**/*_controller.{js,ts}", {
eager: false
});
registerControllers(Stimulus, controllers)
Custom Configuration
const options = {
// Custom Turbo events (optional)
turboEvents: [
"turbo:load",
"turbo:frame-render",
],
// Custom debounce wait time in ms (optional, default: 100)
debounceWait: 50,
};
registerControllers(Stimulus, controllers, options)With Cleanup
const loader = new StimulusDynamicLoader({
application: Stimulus,
controllers: controllers,
});
// Later, if you need to disconnect
loader.disconnect();How It Works
- Controller Discovery: Scans the DOM for
[data-controller]attributes - Path Mapping: Creates a map of controller names to file paths
- Lazy Loading: Dynamically imports controllers only when found in the DOM
- Auto Registration: Registers controllers with Stimulus automatically
- Event Listening: Responds to Turbo navigation and DOM mutations
- Debouncing: Prevents excessive registration attempts
Behavior
- Controllers are loaded only once per name
- Duplicate controller names trigger a console warning
- Failed imports are logged with detailed error messages
- Case-insensitive controller name matching
- Automatically handles Turbo frame renders and stream updates
- MutationObserver watches for dynamically added elements
Requirements
@hotwired/stimulus^3.0.0- Vite or similar bundler with
import.meta.globsupport
License
MIT
