requireJS optimization: undefined is not a functio

2019-01-29 12:31发布

问题:

Without using optimization, application is working fine. But with optimization (minifying JS), the minified file throws "undefined is not a function" when loading homepage.

The issue is : in the main config file, i am using like below which creates loading problem in the line -- new controller() - undefined.

domReady(function() {

        if (domReady) {
    calendar.controller = new controller();
    if (Backbone) {
                Backbone.history.start();
            }
        }
    });

My build.js is below:

({
    'baseUrl': './',
    'dir': '../build/js',
    'paths': {
        'jquery': 'require-jquery',
        'underscore': 'lib/lodash.min',
        'backbone': 'lib/backbone-min',
        'mustache': 'lib/mustache',
        'bootstrap': 'lib/bootstrap.min',
        'fullcalendar': 'lib/fullcalendar.min',
        'controller': 'calendar/controllers/home_controller',
        'jqueryui':'lib/jquery-ui.min',
        'moment':'lib/moment.min',
        'customscroll':'lib/jquery.mCustomScrollbar',
        'mousewheel':'lib/jquery.mousewheel.min',
        'validate':'lib/jquery.validate.min',
        'datatable':'lib/jquery.dataTables.min',
        'blockUi':'lib/jquery.blockUI',
        'fileupload':'lib/fileuploader'
    },
    'shim': {
        'underscore': {
            'exports': '_'
        },
        'backbone': {
            'deps': ['jquery','underscore'],
            'exports': 'Backbone'
        },
        'bootstrap': {
            'deps': ['jquery'],
            'exports': 'jQuery.fn.alert'
        },
        'fullcalendar': {
            'deps': ['jquery']            
        },
        'jqueryui': {
            'deps': ['jquery']            
        },
        'customscroll': {
            'deps': ['jquery']            
        },
        'mousewheel': {
            'deps': ['jquery']            
        },
        'validate': {
            'deps': ['jquery']            
        },
        'datatable': {
            'deps': ['jquery']            
        },
        'fileupload': {
            'deps': ['jquery','jqueryui']            
        },
        'blockUi': {
            'deps': ['jquery']            
        },
        'controller': {
            'deps': ['backbone','fullcalendar','jqueryui','moment','customscroll','mousewheel','validate','datatable','blockUi','fileupload']
        }       

    },
    'locale': 'en-us',
    'optimize': 'uglify',
    'inlineText': true,
    'modules': [

        {
            'name': 'calendar/controllers/home_controller',
            'exclude': ['jquery']

        }

    ]
})

RequireJS version: 2.1.1.

回答1:

It seems the optimizer is not finding your shim config. You will need to specify it using the mainConfigFile build option.

mainConfigFile: 'path/to/main.js'

More information on this config option can be found in require.js API doc - Main config file:

You should use the mainConfigFile build option to specify the file where to find the shim config. Otherwise the optimizer will not know of the shim config. The other option is to duplicate the shim config in the build profile.

As you already have it in your build profile, try the other way around.



标签: requirejs