Skip to content

Instantly share code, notes, and snippets.

@shelldandy
Created September 30, 2017 02:37
Show Gist options
  • Select an option

  • Save shelldandy/fb9b9e3e70394fbaff4670534c1be387 to your computer and use it in GitHub Desktop.

Select an option

Save shelldandy/fb9b9e3e70394fbaff4670534c1be387 to your computer and use it in GitHub Desktop.
Modules via data-attributes
// Require and use modules via data attributes
// your modules need to take an element as a param
// the modules live in this same folder to work
const moduleElements = document.querySelectorAll('[data-module]')
Array.prototype.forEach.call(moduleElements, el => {
const name = el.getAttribute('data-module')
const Module = require(`./${name}`)
Module(el)
})
@shelldandy
Copy link
Copy Markdown
Author

This obviously implies you are using some sort of bundler like webpack or browserify

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment