1import { Application } from "@hotwired/stimulus"; 2import { eagerControllers, isApplicationDebug, lazyControllers } from "./controllers.js"; 3const controllerAttribute = "data-controller"; 4const loadControllers = (application, eagerControllers, lazyControllers) => { 5 for (const name in eagerControllers) registerController(name, eagerControllers[name], application); 6 new StimulusLazyControllerHandler(application, lazyControllers).start(); 7}; 8const startStimulusApp = () => { 9 const application = Application.start(); 10 application.debug = isApplicationDebug; 11 loadControllers(application, eagerControllers, lazyControllers); 12 return application; 13}; 14var StimulusLazyControllerHandler = class { 15 constructor(application, lazyControllers) { 16 this.application = application; 17 this.lazyControllers = lazyControllers; 18 } 19 start() { 20 this.lazyLoadExistingControllers(document.documentElement); 21 this.lazyLoadNewControllers(document.documentElement); 22 } 23 lazyLoadExistingControllers(element) { 24 Array.from(element.querySelectorAll(`[${controllerAttribute}]`)).flatMap(extractControllerNamesFrom).forEach((controllerName) => { 25 this.loadLazyController(controllerName); 26 }); 27 } 28 loadLazyController(name) { 29 if (!this.lazyControllers[name]) return; 30 const controllerLoader = this.lazyControllers[name]; 31 delete this.lazyControllers[name]; 32 if (!canRegisterController(name, this.application)) return; 33 this.application.logDebugActivity(name, "lazy:loading");
34 controllerLoader().then((controllerModule) => { 35 this.application.logDebugActivity(name, "lazy:loaded"); 36 registerController(name, controllerModule.default, this.application); 37 }).catch((error) => { 38 console.error(`Error loading controller "${name}":`, error); 39 }); 40 } 41 lazyLoadNewControllers(element) { 42 if (Object.keys(this.lazyControllers).length === 0) return; 43 new MutationObserver((mutationsList) => { 44 for (const { attributeName, target, type } of mutationsList) switch (type) { 45 case "attributes": 46 if (attributeName === controllerAttribute && target.getAttribute(controllerAttribute)) extractControllerNamesFrom(target).forEach((controllerName) => { 47 this.loadLazyController(controllerName); 48 }); 49 break; 50 case "childList": this.lazyLoadExistingControllers(target); 51 } 52 }).observe(element, { 53 attributeFilter: [controllerAttribute], 54 subtree: true, 55 childList: true 56 }); 57 } 58}; 59function registerController(name, controller, application) { 60 if (canRegisterController(name, application)) application.register(name, controller); 61} 62function extractControllerNamesFrom(element) { 63 const controllerNameValue = element.getAttribute(controllerAttribute); 64 if (!controllerNameValue) return []; 65 return controllerNameValue.split(/\s+/).filter((content) => content.length); 66} 67function canRegisterController(name, application) { 68 return !application.router.modulesByIdentifier.has(name); 69} 70export { loadControllers, startStimulusApp };
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.