Loading jQuery, Underscore and Backbone using RequireJS 2.0.1 and shim

后端 未结 3 1438
小蘑菇
小蘑菇 2021-01-30 00:55

I am experimenting a little bit with RequireJS 2.0.1. My goal is to load correctly jQuery, Underscore and Backbone. From the original RequireJS doc I d

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-30 02:00

    You only need to use "shim" config if the library does not already call define() to declare a module. jQuery does this already, so you can remove that from the shim config. The above code will work as is, but the exports shim config for jQuery will be ignored since jQuery will call define() before the shim work is done.

    The downsides with the shim vs having the script call define() to define a module:

    1. It is less portable/reliable: every developer needs to do the shim config, and keep track of library changes. If the library author does it inline in the library, everyone gets the benefits more efficiently. The code templates at umdjs/umd can help with that code change.

    2. Less optimal code loading: shim config works by loading shim deps before loading the actual library. So it is a bit more sequential loading than parallel. It is faster if all scripts can be loaded in parallel, which is possible when define() is used. If you do a build/optimization for final deployment and combine all the scripts into one script, then this is not really a downside.

提交回复
热议问题