简体   繁体   English

使用Browserify可变地加载AngularJS模块

[英]Load AngularJS modules variably with Browserify

I'm attempting to build a boilerplate using AngularJS, Browserify, and Gulp. 我正在尝试使用AngularJS,Browserify和Gulp构建样板。 I have all the Gulp tasks working correctly, along with Browserify. 我的所有Gulp任务以及Browserify都能正常工作。 I'm now trying to figure out the best way to load all the necessary files for an Angular application. 我现在正在尝试找出为Angular应用程序加载所有必需文件的最佳方法。

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. 但是,对于Angular应用程序所需的其余文件(控制器,服务,指令等),可能必须包含任意数量的文件。 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. 我首先研究了使用fs解析每个目录并以这种方式加载文件,但是没有运气。 I'm now trying to have a controllers/index.js file of the format: 我现在正在尝试使用以下格式的controllers/index.js文件:

'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. cannot find module很好地解析json文件,但是当它尝试从文件中读取路径时,我cannot find module错误。

Are there any better approaches to loading a variable number of AngularJS files into an application using Browserify? 是否有更好的方法使用Browserify将可变数量的AngularJS文件加载到应用程序中?

I ended up coming with a solution that I'm happy with for now. 我最终提出了一个我现在很满意的解决方案。 It involves having a main module for each type: app.controllers , app.services , etc. Those look like this: 它涉及为每种类型都有一个主要模块: app.controllersapp.services等。这些看起来像这样:

var angular = require('angular');

module.exports = angular.module('app.controllers', []);

// Define the list of controllers here
require('./example.js');

where ./example.js is a controller of the format: 其中./example.js是以下格式的控制器:

'use strict';

var controllersModule = require('./_index');

/**
 * @ngInject
 */
function ExampleCtrl() {

  // ViewModel
  var vm = this;

  vm.title = 'Test Title';
  vm.number = 1234;

}

controllersModule.controller('ExampleCtrl', ExampleCtrl);

This process is repeated for all controllers, services, directives, etc. and are then loaded onto the app inside main.js : 对所有控制器,服务,指令等重复此过程,然后将其加载到main.js的应用程序中:

'use strict';

var angular = require('angular');

// angular modules
require('angular-ui-router');
require('./controllers/_index');
require('./services/_index');
require('./directives/_index');

// create and bootstrap application
angular.element(document).ready(function() {

  var requires = [
    'ui.router',
    'app.controllers',
    'app.services',
    'app.directives'
  ];

  angular.module('app', requires);

  angular.module('app').config(require('./routes'));

  angular.bootstrap(document, ['app']);

});

And the Gulp task for my Javascript files now looks like this: 现在,我的Javascript文件的Gulp任务如下所示:

gulp.task('browserify', function() {

  var b = browserify({
    basedir: '.',
    entries: './app/js/main.js',
    debug: true,
    insertGlobals: true
  });

  b.transform({
    global: true
  }, ngAnnotate);

  b.transform({
    global: true
  }, uglifyify);

  b.bundle()
    .pipe(source('main.js'))
    .pipe(streamify(rename({suffix: '.min'})))
    .pipe(gulp.dest('build/js'))
    .pipe(refresh(lrserver));

});

This all works well for me, and is relatively modular! 这一切对我来说都很好,并且是相对模块化的!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM