RequireJS没有在优化后调用的define()回调

时间:2016-06-17 20:38:30

标签: javascript requirejs

我正在开发一个使用requireJS和angular的网络应用。解决方案很好但是我在优化后遇到了问题。

我构建应用程序的方式是通过r.js.我执行它的目标是我的main.js,其中有一个define()语句,指向解决方案中的所有其他文件作为依赖项。然后它在我的bin /目录中生成一个新的main.js,它引用的所有js文件连接到一个main.js文件中。到目前为止一切都很好。

然而,当我尝试使用优化的js在浏览器中加载应用程序时,我注意到没有调用任何define()回调,甚至没有依赖项的调用([])据我所知requireJS,只要解决了所有依赖关系,就会执行模块的回调。

当js文件全部位于各自的位置时,这一切都正常工作,具有匿名名称,预构建。它只是在一个文件中完全相同的js,所以我不知道可能有什么不同。除了优化器给出了模块名称(这是它应该做的)

我唯一能想到的是我错过了对主模块的引导'require'调用(位于以下优化文件的底部)但是我没有调用'require'我的非构建解决方案,它工作正常。

我调用r.js的脚本位于一个名为“build /”的目录中,该目录是'js /'的兄弟,它是我的js所在的根目录。

我的问题示例:首先定义('../js / common / module',[],function()...尽管没有依赖关系,但它的回调永远不会被调用。

有谁知道这是什么问题?任何帮助都 很多 赞赏。

define('../js/common/module',[], function () {
    return {
        app: angular.module('common', []),
        ngContext: true
    }
});
define('../js/customer_account_list/module',['../common/module'], function () {
    return {
        app: angular.module('customer_account_list', ['common']),
        ngContext: true
    }
});
define('../js/mod',['./customer_account_list/module'], function () {
    angular.module('app', ['customer_account_list']);
});
 
define('../js/common/service/app_config',['../module'], function (module) {

    /**
    * @class appConfigService
    * @memberOf common.service
    * @description calls back to the server to get the application configuration    
    **/
    var appConfigService = function () {
        url = window.location.protocol + '//' + window.location.host + window.location.pathname + 'config.json';
        var configReq = new XMLHttpRequest();
        configReq.open("GET", url, false);
        configReq.send();
        return JSON.parse(configReq.response);
    }

    if (module.ngContext) {
        module.app.service('appConfig', appConfigService);
    }
});

 
define('../js/common/service/feature_manager',['../module'], function (module) {

    /**
    * @class featureManagerServ
    * @memberOf common.service
    * @description  manages the set of features in the product portal
    **/
    function featureManagerServ(appConfig) {

        this.featuresEnabled = {
            'customer_account_list': appConfig.features['customer_account_list']
        };

        /**
        * @function
        * @memberOf common.service.featureManagerServ
        * @param {string} featureKey - feature identifier 
        * @description computes whether or not the feature should be enabled
        **/
        this.featureEnabled = function (featureKey) {
            return !!this.featuresEnabled[featureKey];
        };

        /**
        * @function
        * @memberOf common.service.featureManagerServ
        * @param {string} featureKey - feature identifier
        * @param {Object} directive - Ng directive
        * @description handles whether or not the feature will be bootstrapped
        **/
        this.bootstrapFeature = function (featureKey, directive) {
            if (this.featureEnabled(featureKey)) {
                return directive;
            } else {

            }
        };
    };

    if (module.ngContext) {
        module.app.service('featureManager', ['appConfig', featureManagerServ]);
    }

});

define('../js/customer_account_list/directives/customer_account_list_ctrl',[], function () {
    function customerAccountListCtrl($scope, customerAccounts) {
        $scope.customerAccounts = customerAccounts.getCustomerAccounts();
        $scope.onAccountClick = function (ind) {
            var s = 0;
        }
    }

    return ['$scope', 'customerAccounts', customerAccountListCtrl];
});

define('../js/customer_account_list/directives/customer_account_list',['../module', './customer_account_list_ctrl'], function (module, ctrl) {

    /**
    * @class customerAccountList
    * @memberOf customer_account_list.directive
    * @description customer account list component    
    **/
    module.app.directive('customerAccountList', ['featureManager', function (featureManager) {
        return featureManager.bootstrapFeature('customer_account_list', {
            restrict: 'E',
            templateUrl: 'js/customer_account_list/views/customer_account_list.html',
            scope: {
                url: '='
            },
            controller: ctrl
        });
    }]);
});

define('../js/customer_account_list/service/customer_accounts',['../module'], function (module) {

    /**
    * @class customerAccountsServ
    * @memberOf customer_account_list.service
    * @description provides list of customers user is authenticated to view/edit   
    **/
    function customerAccountsServ($http, appConfig) {

        /**
        * @function
        * @memberOf customer_account_list.service.customerAccountsServ
        * @description provides list of customers user is authenticated to view/edit   
        **/
        this.getCustomerAccounts = function () {
            return ['AcmeFood', 'AcmeCoffee','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9'];
        }

    }

    if (module.ngContext) {
        module.app.service('customerAccounts', ['$http', 'appConfig', customerAccountsServ]);
    }

});

define('../js/main.js',['./mod',
        './common/module',
        './common/service/app_config',
        './common/service/feature_manager',
        './customer_account_list/module',
        './customer_account_list/directives/customer_account_list',
        './customer_account_list/directives/customer_account_list_ctrl',
        './customer_account_list/service/customer_accounts'],
        function () {
            angular.element().ready(function () {
                angular.bootstrap(document, ['app'], {
                    strictDi: false
                });
            });
        });

0 个答案:

没有答案