Skip to content

Commit

Permalink
Add export for Stimulus controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
excid3 committed Oct 2, 2021
1 parent 3ca2862 commit 572ebf5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
11 changes: 3 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,9 @@ import { Application } from "@hotwired/stimulus"
const application = Application.start()

import * as controllers from "./**/*_controller.js"
for (let i=0; i < controllers.filenames.length; i++) {
const name = controllers.filenames[i]
.replace("./", "")
.replace("_controller.js", "")
.replace(/\//g, "--")
.replace(/_/g, '-')
application.register(name, controllers.default[i].default)
}
controllers.namesWithModule.forEach((controller) => {
application.register(controller.name, controller.module.default)
})
```

Import ActionCable channels:
Expand Down
11 changes: 11 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ const railsPlugin = (options = {}) => ({
})
).sort().filter(path => path.endsWith('.js'));

const controllerNames = files
.map((module) => module
.replace("./", "")
.replace("_controller.js", "")
.replace(/\//g, "--")
.replace(/_/g, '-')
)

const importerCode = `
${files
.map((module, index) => `import * as module${index} from '${module}'`)
Expand All @@ -41,6 +49,9 @@ const railsPlugin = (options = {}) => ({
export const filenames = [${files
.map((module) => `'${module}'`)
.join(',')}]
export const namesWithModule = [${controllerNames
.map((module, index) => `{name: '${module}', module: module${index}}`)
.join(',')}]
`;

return { contents: importerCode, resolveDir: args.pluginData.resolveDir };
Expand Down

0 comments on commit 572ebf5

Please sign in to comment.