Skip to content

Babel plugin that exposes function / constructor arguments as runtime metadata

Notifications You must be signed in to change notification settings

hypothesis/babel-plugin-inject-args

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

babel-plugin-inject-args

npm

Babel plugin that processes specially annotated functions or classes and exposes the names of their parameters as runtime-accessible metadata. This can be used by dependency injection containers to call the function or instantiate the class and automatically resolve dependencies.

To enable this metadata for a function or class, add the string @inject in a leading comment. The plugin will add a $inject property to the object which contains an array of parameter names. For example, this source:

// @inject
function createThing(paramA, paramB) {
}

Is transformed into:

// @inject
function createThing(paramA, paramB) {
}

createThing.$inject = ['paramA', 'paramB'];

ES classes are also supported:

// @inject
class Thing {
  constructor(paramA, paramB) {  }
}

Is transformed into:

// @inject
class Thing {
  constructor(paramA, paramB) {  }
}

Thing.$inject = ['paramA', 'paramB'];

Usage

To use the plugin, first add the dependency to your project:

npm install --save-dev babel-plugin-inject-args

Then enable the plugin in your Babel configuration, for example in a .babelrc file:

{
  "plugins": ["inject-args"]
}

Further reading

For information on understanding the plugin implementation, see https://github.com/jamiebuilds/babel-handbook.

About

Babel plugin that exposes function / constructor arguments as runtime metadata

Resources

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published