Sha256: c700b710285ecacb72c9e87586cb9f57552fb205c3c3be04b3c53eecaa5fa3b0

Contents?: true

Size: 825 Bytes

Versions: 3

Compression:

Stored size: 825 Bytes

Contents

import { Application } from "@hotwired/stimulus"

function registerControllersFrom(under) {
  const paths = Object.keys(importmap).filter(path => path.match(new RegExp(`^${under}/.*_controller$`)))
  paths.forEach(path => registerControllerFromPath(path, under))
}

function registerControllerFromPath(path, under) {
  const name = path.replace(`${under}/`, "").replace("_controller", "").replace(/\//g, "--").replace(/_/g, "-")

  import(path)
    .then(module => application.register(name, module.default))
    .catch(error => console.log(`Failed to register controller: ${name} (${path})`, error))
}

const application = Application.start()
const importmap = JSON.parse(document.querySelector("script[type=importmap]").text).imports

registerControllersFrom("controllers")

export { application, registerControllersFrom }

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
stimulus-rails-0.3.11 app/assets/javascripts/stimulus-importmap-autoloader.js
stimulus-rails-0.3.10 app/assets/javascripts/stimulus-importmap-autoloader.js
stimulus-rails-0.3.9 app/assets/javascripts/stimulus-importmap-autoloader.js