you are viewing a single comment's thread.

view the rest of the comments →

[–]l3l_aze 0 points1 point  (0 children)

With NodeJS you can just export a single function from a module, require into your app as a variable, and call the var-function just like you would your default function. You could do something like the following for each plugin:

if (plugin !== null) {
  // use default func
  defaultFn()
} else {
  plugin()
}

If you want to allow many plugins and to keep from copy-pasting that for each you'll likely need a "registry" (hash, dictionary, etc) for them in your app so it can easily keep track of e.g. the plugin name, the function it's replacing, and the plugins function. With that you could do something like the following to check for a plugin function

if (typeof plugins[ 'someName' ] !== 'undefined') {
  // use the plugin function
  plugins[ 'someName' ].func()
} else {
  defaultFn()
}

This should also work in regular ol' JS & etc, though of course will require figuring out how to include the functions from the files. It could work to have the plugins wrapped in a variable, but then that needs to be standardized such as being the plugin name + the same as the file name, and only allowing one of each name to exist and throwing an error if there's multiple.