Using require.js with 3rd party javascript libraries

感情迁移 提交于 2019-12-23 22:24:17

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/15592308/using-require-js-with-3rd-party-javascript-libraries

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!