Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using require.js with 3rd party javascript libraries

Tags:

requirejs

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.

like image 500
larryq Avatar asked Mar 17 '26 17:03

larryq


1 Answers

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

like image 141
kryger Avatar answered Mar 19 '26 14:03

kryger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!