为什么我不能在使用browserify的循环中要求(...)?

时间:2015-09-19 20:01:09

标签: javascript browserify

这有效

require( './AppCtrl' );

但如果我这样做

[ './AppCtrl' ].forEach( function( name ) {
    require( name );
});

导致错误

_prelude.js:1 Uncaught Error: Cannot find module './AppCtrl's @  _prelude.js:1s @ _prelude.js:1(anonymous function) @ _prelude.js:1(anonymous function) @ index.js:48 @ index.js:3s @ _prelude.js:1(anonymous function) @ _prelude.js:11../config @ app.js:22s @ _prelude.js:1e @ _prelude.js:1(anonymous function) @ _prelude.js:1
 angular.js:12416 Error: [ng:areq] Argument 'AppCtrl' is not a function, got undefined
 http://errors.angularjs.org/1.4.5/ng/areq?p0=AppCtrl&p1=not%20a%20function%2C%20got%20undefined
at REGEX_STRING_REGEXP (angular.js:68)
at assertArg (angular.js:1795)
at assertArgFn (angular.js:1805)
at angular.js:9069
at setupControllers (angular.js:8133)
at nodeLinkFn (angular.js:8173)
at compositeLinkFn (angular.js:7637)
at publicLinkFn (angular.js:7512)
at angular.js:1660
at Scope.parent.$get.Scope.$eval (angular.js:15878)

为了我的最终目标我希望能够做这样的伪代码

 foreach  name {
     angular.module(...).controller( require( name ) );
 }

但是现在我不确定为什么我不能在循环中要求文件?

1 个答案:

答案 0 :(得分:5)

因为Browserify(通过Detective)基于静态分析构建依赖图,因此只会使用字符串文字参数处理require()调用。