Webpack Uglify causes routing to stop work

2019-02-11 07:59发布

When I uglify webpack bundle, routing stops work without any error message or log message. I am using oclazyload to lazy load.

Route.js

module.exports = function(app) {
    var routeConfig = function($stateProvider) {
        $stateProvider
            .state('home', {
                url: '/',
                templateUrl: 'app/dashboard/dashboard.min.html',
                title: 'Home',
                ncyBreadcrumb: {
                    label: 'Home'
                }
            })
            .state('organizationStructure', {
                url: '/organizationStructure',
                templateUrl: 'app/admin/organizationStructure/manageHierarchy/manageHierarchyShell.min.html',
                'abstract': true,
                ncyBreadcrumb: {
                    skip: true
                },
                resolve: ['$q', '$ocLazyLoad', function($q, $ocLazyLoad) {
                    var deferred = $q.defer();

                    require.ensure([], function() {
                        var mod = require('./organizationStructure.module.js');
                        $ocLazyLoad.load({
                            name: 'app.organizationStructure'
                        });
                        deferred.resolve(mod.controller);
                    });

                    return deferred.promise;
                }]
            })
            .state('organizationStructure.organization', {
                url: '/organization',
                templateUrl: 'app/admin/organizationStructure/manageHierarchy/organization/index.min.html',
                controller: 'ManageOrganization',
                controllerAs: 'vm',
                title: 'Manage Organization',
                ncyBreadcrumb: {
                    label: 'Manage Organization',
                    parent: 'home'
                }
            });
    }

    app.config(routeConfig);
    return routeConfig;
};

Module.js

var app = angular.module('app', [
    'ui.router',
    'restangular',
    'ui.bootstrap',
    'ncy-angular-breadcrumb',
    'oc.lazyLoad'
]);

Base Route

require('./app.route.js')(app);

When I minify the bundle, app routing stops working. Otherwise it works fine. Please provide me a solution. Also I am using ngAnnotate. Dependencies are injected safely in minified file.

2条回答
对你真心纯属浪费
2楼-- · 2019-02-11 08:37

While doing minification you should for array annotation of DI.

You are not using angular di array notation inside you app.js, you need to do below changes.

From

app.config(routeConfig);

To

app.config(['$stateProvider', routeConfig]);

For more Information Refer this SO Answer

查看更多
Ridiculous、
3楼-- · 2019-02-11 08:43

You're using the latest version of ui-router, which has a newer and slightly different way of handling the resolve block. It takes an object map.

Try this:

    resolve: {
       foo: ['$q', '$ocLazyLoad', function($q, $ocLazyLoad) {
                var deferred = $q.defer();

                require.ensure([], function() {
                    var mod = require('./organizationStructure.module.js');
                    $ocLazyLoad.load({
                        name: 'app.organizationStructure'
                    });
                    deferred.resolve(mod.controller);
                });

                return deferred.promise;
       }]
    }  

Here is the ui-router API doc for this: https://github.com/angular-ui/ui-router/wiki#resolve

查看更多
登录 后发表回答