Skip to content

Instantly share code, notes, and snippets.

@nicolas-brousse
Last active March 5, 2020 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicolas-brousse/113da0893b2d12680177f89f5aed1147 to your computer and use it in GitHub Desktop.
Save nicolas-brousse/113da0893b2d12680177f89f5aed1147 to your computer and use it in GitHub Desktop.
// Loads stimulus controllers from ../components/ folder
//
// Normal output:
// button/button_controller.js => data-controller="button--button"
// form/text_field/text_field_controller.js => data-controller="form--text-field--text-field"
//
// Expected output:
// button/button_controller.js => data-controller="button"
// form/text_field/text_field_controller.js => data-controller="form--text-field"
import { Application } from "stimulus"
import { definitionsFromContext } from "stimulus/webpack-helpers"
const application = Application.start()
const context = require.context("../components/", true, /_controller\.js$/)
const definitions = definitionsFromContext(context)
definitions.map(definition => {
definition["identifier"] = definition["identifier"].replace(/--/g, "_")
.replace(/_[a-z-]+$/, "")
.replace(/_/g, "--")
return definition
})
application.load(definitions)
import { Application } from "stimulus"
import { definitionsFromContext } from "stimulus/webpack-helpers"
const application = Application.start()
const context = require.context("../components/", true, /_controller\.js$/)
const definitions = definitionsFromContext(context).map(definition => ({
...definition,
identifier: definition.identifier.replace(/--[a-z]+(-[a-z]+)?$/, "")
}))
application.load(definitions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment