How does AMD (require.js) handle multiple classes in a single non-amd js file?

拈花ヽ惹草 提交于 2019-12-12 17:28:08

问题


I'm using a bunch of components from the MootoolsMore library which are all compiled into a single .js file. Is there a way to define the library once in the shim and have access to all the class with in it? The following doesn't work, but how would I do something like this?

shim:{
 mootoolsMore:{
  deps    : ["mootools"]
  exports : ["Slider", "Sortables"]
 }
}

// then inside my module I could access the Slider component like so
define( function(require) {
  var Slider = require('mootoolsMore').Slider

回答1:


There is no need for "shim" functionality that is specific to RequireJS only. You can just use standard AMD loader API:

require(['js!path/to/MooToolsCore.js'], function(){
    // nesting in order to insure that Moo Core runs first
    require(['js!path/to/MooToolsMore.js'], function(){
        // Your Slider and Sortables will be in global.
        // just use them.
        window.Slider(/* ... */)
    })
})

Note, RequireJS does not need "js!" plugin to be declared explicitly, but, instead, just looks at the extension of the file. If it's ".js" it runs the file through "js" plugin. This is NON-standard behavior (as in not in AMD spec), but on RequireJS you should be able to replace line like:

['js!path/to/MooToolsMore.js']

with this:

['path/to/MooToolsMore.js']


来源:https://stackoverflow.com/questions/14265370/how-does-amd-require-js-handle-multiple-classes-in-a-single-non-amd-js-file

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