pluginjector
Simple no nonsense dependency injection for extensibility and testability
Install from NPM
Help with development on GitHub
Donations accepted. 😃 😃 😃 $5 $5 😘 😘 😘
- Usage -
Note: Uses ES6/ES2015 features and may fail on Node versions prior to v6.
Pass your core module object to pluginjector then inject plugins
const pluginjector = const inject = const myNewModule =
Overwrite properties in your module
const myModule = val: 1 const myPlugin = val: 2 const newModule = myPlugin // true
Overwrite methods in your module
const myModule = {return 'method'} const myPlugin = {return 'plugin'} const newModule = myPlugin // true
Namespace your plugins
const myPlugin method return 'method' } const myNewModule = // true
this
to your methods with a simple flag
Bind more detailed look at this later
const myModule = val: 1 const myPlugin = pluginjectorBindThis: true { return thisval } const newModule = myPlugin // true
Pass in files to be imported
const newModule = const newNamespacedModule = // filenames are automatically namespaced in camelCase const nModule = // true
Include a default directory and your users can pass in just the plugin name.
const inject = myModule dir: '../path/to/default/directory' const newModule = const newNamespacedModule =
Lazily inject different plugins as needed
const inject = const newModule = // true
Original core is shallow copied to minimize possibility of undesired mutations
let inject = const newModule = inject = const anotherNewModule = // true // true
Optionally use core module as prototype instead of shallow copy
const inject = const newModule = // true
this
binding
More detail to understand handling of const myModule = val: 1 const myPlugin = // `this` will naturally point to parent module for first layer // of methods unless you namespace it { return thisval } obj1: // all methods here will get `this` bound to parent module pluginjectorBindThis: true // flag, any truthy value { return thisval } { return thisobj2val } obj2: // no flag, so these will not get special binding val:2 { return thisval } obj3: // falsey flag doesn't count pluginjectorBindThis: false val:3 { return thisval } let newModule = myModulemyPlugin // true // true // true // true // true // with namespace,but no flag, `this` points to `newModule.plugin`newModule = myModuleplugin: myPlugin // false, `this.val` does not exist