Last active
March 5, 2020 10:27
-
-
Save nicolas-brousse/113da0893b2d12680177f89f5aed1147 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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