jm2
jm2

Reputation: 763

RequireJS and dependencies of 3rd party libraries eg. Backbone dependent on Underscore and jQuery

I understand that Backbone is dependent on Underscore, jQuery and maybe JSON2. So is it possible to specify that so when I say my module is dependent on Backbone, it will include dependencies of that? Or whats they way around this?

Upvotes: 0

Views: 1413

Answers (1)

Simon Smith
Simon Smith

Reputation: 8044

RequireJS makes this easy with the dependencies that you can declare when define a module. For libraries that don't support AMD (Underscore and Backbone being two primary examples) then use of the shim configuration is required.

Here is an example config:

require.config({
    baseUrl: 'scripts/',
    paths: {
        'backbone': 'lib/backbone',
        'jquery': 'lib/jquery',
        'underscore': 'lib/underscore'
    },
    shim: {
        'backbone': {
            deps: ['underscore', 'jquery'],
            exports: 'Backbone'
        }
    }
});

Now if you require Backbone as a dependency in one of your modules underscore and jquery will be available.

A lot of this is also covered in the documentation.

Upvotes: 2

Related Questions