Extend AngularJS applications by injecting module dependencies at build time


Extend AngularJS applications by injecting module dependencies at build time (using static code transformations).

For example transforms this:

angular.module('moduleName', ['dependency']);

into that

angular.module('moduleName', ['dependency', 'additionalDependency']);

Nowadays having a project using Grunt (or any other similar tool) for workflow automation and asset management is not uncommon. So the idea with this small library is to provide a way to implement a simple plugin system for AngularJS applications using only static code transformations. This has two main advantages (compared to extending the module with dynamic tricks):

  • Keeps the code clean from boilerplate
  • The modules can be still declared in any order.

The main use case for such a system is when you have an AngularJS based application and you want to allow third party modules (plugins) to exend you core app without hardcoding those new dependencies.

var angularExtend = require('angular-extend');
var src = "angular.module('moduleName', ['aModule']);";
var res = angularExtend(src, {moduleName: ['aPlugin']});
res.out will be:
angular.module('moduleName', ['aModule', "aPlugin"]);
res.changed is a boolean and will tell if any extension was applied at all.

A grunt plugin using this library will be soon available at

The transformation will work only if you follow the convention of delaring modules like

angular.module(<string>, [<strings>]);

Using variables or functions to define the module name, will fail.

Thanks to olov and his project ng-annotate for the inspiration he gave me to build this little tool.