Sha256: c939b25c4454cafdb3eb953fe13d0c417d799af06bd0be0bd3da9fcc93b3a4af

Contents?: true

Size: 874 Bytes

Versions: 3

Compression:

Stored size: 874 Bytes

Contents

// FIXME: es-module-shim won't shim the dynamic import without this explicit import
import "@hotwired/stimulus"

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

  paths.forEach(path => registerControllerFromPath(path, under, application))
}

export function parseImportmapJson() {
  return JSON.parse(document.querySelector("script[type=importmap]").text).imports
}

function registerControllerFromPath(path, under, application) {
  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))
}

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
stimulus-rails-0.5.1 app/assets/javascripts/stimulus-importmap-autoloader.js
stimulus-rails-0.5.0 app/assets/javascripts/stimulus-importmap-autoloader.js
stimulus-rails-0.4.2 app/assets/javascripts/stimulus-importmap-autoloader.js