Sha256: 7796430c6a502d3969a405d910de004ed31c2ca80548d801eff1826edfdee1a2

Contents?: true

Size: 989 Bytes

Versions: 16

Compression:

Stored size: 989 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(new RegExp(`^${under}/`), "")
    .replace("_controller", "")
    .replace(/\//g, "--")
    .replace(/_/g, "-")

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

console.warn("stimulus-importmap-autoload.js has been deprecated in favor of stimulus-loading.js")

Version data entries

16 entries across 16 versions & 3 rubygems

Version Path
rails_devtools-0.1.3 vendor/javascript/stimulus-importmap-autoloader.js
rails_devtools-0.1.2 vendor/javascript/stimulus-importmap-autoloader.js
rails_devtools-0.1.1 vendor/javascript/stimulus-importmap-autoloader.js
stimulus-rails-1.3.4 app/assets/javascripts/stimulus-importmap-autoloader.js
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/stimulus-rails-1.3.3/app/assets/javascripts/stimulus-importmap-autoloader.js
stimulus-rails-1.3.3 app/assets/javascripts/stimulus-importmap-autoloader.js
stimulus-rails-1.3.2 app/assets/javascripts/stimulus-importmap-autoloader.js
stimulus-rails-1.3.1 app/assets/javascripts/stimulus-importmap-autoloader.js
stimulus-rails-1.3.0 app/assets/javascripts/stimulus-importmap-autoloader.js
stimulus-rails-1.2.2 app/assets/javascripts/stimulus-importmap-autoloader.js
stimulus-rails-1.2.1 app/assets/javascripts/stimulus-importmap-autoloader.js
stimulus-rails-1.2.0 app/assets/javascripts/stimulus-importmap-autoloader.js
stimulus-rails-1.1.1 app/assets/javascripts/stimulus-importmap-autoloader.js
stimulus-rails-1.1.0 app/assets/javascripts/stimulus-importmap-autoloader.js
stimulus-rails-1.0.4 app/assets/javascripts/stimulus-importmap-autoloader.js
stimulus-rails-1.0.3 app/assets/javascripts/stimulus-importmap-autoloader.js