Sha256: 757d2c8efd194ead04b46d45f3d618c9c74e0a420e84ebde766f7a2a15b533a1

Contents?: true

Size: 887 Bytes

Versions: 6

Compression:

Stored size: 887 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.log(`Failed to register controller: ${name} (${path})`, error))
}

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
stimulus-rails-0.6.2 app/assets/javascripts/stimulus-importmap-autoloader.js
stimulus-rails-0.6.1 app/assets/javascripts/stimulus-importmap-autoloader.js
stimulus-rails-0.6.0 app/assets/javascripts/stimulus-importmap-autoloader.js
stimulus-rails-0.5.4 app/assets/javascripts/stimulus-importmap-autoloader.js
stimulus-rails-0.5.3 app/assets/javascripts/stimulus-importmap-autoloader.js
stimulus-rails-0.5.2 app/assets/javascripts/stimulus-importmap-autoloader.js