I'm attempting to build a boilerplate using AngularJS, Browserify, and Gulp. I have all the Gulp tasks working correctly, along with Browserify. I'm now trying to figure out the best way to load all the necessary files for an Angular application.
I'm creating the application with just these three lines of code:
var angular = require('angular');
require('angular-ui-router');
angular.module('myApp', ['ui.router']);
I then add my router logic with:
angular.module('myApp').config(['$stateProvider', '$locationProvider', '$urlRouterProvider', require('./routes')]);
and that all works fine. However, for the rest of the files necessary for an Angular application (controllers, services, directives, etc.), there could be any number of files that have to be included. This is easy enough to hard-code, with something like:
angular.module('myApp').controller('IndexCtrl', require('./controllers/index'))
but I don't want to have to hard-code lines like that for every file I need to add to the application. I first looked into using fs to parse each directory and load the files that way, but no luck. I'm now trying to have a controllers/index.js file of the format:
'use strict';
var controllers = [
{
'name': 'HomeCtrl',
'path': './controllers/home'
}
];
module.exports = controllers;
which I'm then attempting to parse and load the files:
require('./controllers/index').forEach(function(controller) {
angular.module('myApp').controller(controller.name, require(controller.path));
});
this parses the json file fine, but I get cannot find module errors when it tries to read the path from the file.
Are there any better approaches to loading a variable number of AngularJS files into an application using Browserify?