Sha256: bd2654bdd372ad6cd2c6986820e679dc1d3752f95eccee3c6467ef15b471d6a6

Contents?: true

Size: 979 Bytes

Versions: 3

Compression:

Stored size: 979 Bytes

Contents

const registeredControllers = {}

export function eagerLoadAmberComponentControllers(application) {
  const paths = Object.keys(parseImportmapJson()).filter(path => path.match(new RegExp(`/controller$`)))
  paths.forEach(path => registerControllerFromPath(path, application))
}

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

function registerControllerFromPath(path, application) {
  const name = path
    .replace("/controller", "")
    .replace(/\//g, "--")
    .replace(/_/g, "-")

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

function registerController(name, module, application) {
  if (!(name in registeredControllers)) {
    application.register(name, module.default)
    registeredControllers[name] = true
  }
}

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
amber_component-1.2.0 assets/javascripts/amber_component/stimulus_loading.js
amber_component-1.1.1 assets/javascripts/amber_component/stimulus_loading.js
amber_component-1.1.0 assets/javascripts/amber_component/stimulus_loading.js