toy
toy

Reputation: 12141

Why I have to use 'include' again in grunt-requirejs to optimize into a single js file?

I'm trying to use grunt-requirejs to optimize my requirejs project into a single production js file. However, I'm not sure if my Gruntfile.js is correct because I have to specify which model to include during the optimization again, which seems wrong to me as I specify mainConfigFile already. I thought it's going to read everything from my requirejs file. I asked this because I have a lot of modules and I don't want to DRY.

Here's my Gruntfile.js

requirejs : {

            compile : {
                options: {
                    almond: true,
                    baseUrl : 'src/Web/Scripts',
                    mainConfigFile : 'src/Web/Scripts/main-new.js',
                    out: 'src/Web/Scripts/out.js',
                    optimize: 'uglify2',

                    include: ['main-new', 
                        'app/app.js',
                        'app/viewmodels/home/home.mobile.js',
                        'zepto', 
                        'hammer',
                        'zepto-hammer',
                        'zepto-dragswipe',
                        'knockout',
                        'knockout-validation',
                        'knockout-postbox',
                        'knockout-mapping',
                        'knockout-notification',
                        'q',
                        'underscore'
                    ]
                }
            }
        },

And this is my main-new.js file

requirejs.config({
        baseUrl: '/m/Scripts',
        paths: {
            'jquery': 'vendors/jquery/jquery-1.10.2',
            'jquery-cookie': 'vendors/jquery/jquery.cookie-1.4.1',
            'zepto': 'vendors/zepto/zepto',
            'hammer': 'vendors/hammer/hammer',
            'zepto-hammer': 'vendors/zepto/zepto.hammer',
            'zepto-dragswipe': 'vendors/zepto/zepto.dragswipe',
            'knockout': 'vendors/knockout/knockout-3.1.0',
            'knockout-validation': 'vendors/knockout/knockout.validation',
            'knockout-postbox': 'vendors/knockout/knockout-postbox',
            'knockout-mapping': 'vendors/knockout/knockout.mapping',
            'knockout-notification': 'vendors/knockout/knockout-notification-1.1.0',
            'viewmodels': 'app/viewmodels',
            'service': 'app/services',
            'config': 'app/config',
            'helpers': 'app/helpers',
            'q': 'vendors/q/q-0.9.6',
            'underscore': 'vendors/underscore/underscore-min'
        },
        shim: {
            'knockout-validation': ['knockout'],
            'knockout-mapping': ['knockout'],
            'knockout-postbox': ['knockout'],
            'knockout-notification': ['knockout'],
            'jquery-cookie': ['jquery'],
            'hammer': ['zepto'],
            'zepto-hammer': ['zepto', 'hammer'],
            'zepto-dragswipe': ['zepto', 'hammer'],
            'zepto': {
                exports: 'Zepto'
            },
            'underscore': {
                exports: '_'
            }
        }
    });

Upvotes: 0

Views: 109

Answers (1)

ant_Ti
ant_Ti

Reputation: 2425

In include section you should put modules that is required dynamically like:

require(['path/' + moduleName], function() { ... })

Because r.js can't predict at build time what you will be requiring in runtime.

Everything else should be resolved by dependency arrays in your modules.

Upvotes: 0

Related Questions