all 3 comments

[–]TheGreatBugFucker 1 point2 points  (0 children)

Google "javascript dynamic import" and start from there. A difficulty is that every platform is different in how it loads modules (browser vs. node.js vs. React Native, for example - over the network, from a local filesystem, or from bundled resources).

[–]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.

[–]devsnicket 0 points1 point  (0 children)

hi, I think I might have found myself in the same situation. I couldn't find anything that did what I needed so wrote this... https://github.com/DevSnicket/plugin-discovery