larryq
larryq

Reputation: 16299

Using require.js with 3rd party javascript libraries

I'm just starting to use require.js and think it's great. However, I would like to use it as much as possible instead of dealing with <script> tags in my HTML files.

To that end, is it possible to work with a 3rd party library that doesn't have any define modules in it? This may be asking much I realize but is there a way to call...

require(["3rd_party"], function(3rd) {

});

...where 3rd_party.js is a script located in a js folder that require knows to look in? I know require has mapping libraries, things like require-jquery but wasn't sure if it's possible to use it out of the box with older utility libraries that weren't built with it in mind.

Upvotes: 3

Views: 3766

Answers (1)

kryger
kryger

Reputation: 13181

RequireJS 2.1.0 added the shim config element which allows using non-AMD 3rd party libraries like AMD modules. In your case it would be something like:

shim: {
    '3rd_party': {
        exports: '{the-global-name}' // what variable does the library
                                     // export to the global scope?
    }
}

This mechanism makes custom-build library wrappers like "require-jquery" pretty much obsolete.

More details in the RequireJS docs

Upvotes: 11

Related Questions