RequireJS order of dependencies

后端 未结 1 1863
遥遥无期
遥遥无期 2021-02-08 16:40

if you have a RequireJS module like so:

define(
    [
        \'#patches\',
        \'backbone\',
        \'underscore\',
        \'react\',
        \'#allCollec         


        
1条回答
  •  孤独总比滥情好
    2021-02-08 17:02

    From the documentation : http://requirejs.org/docs/api.html#mechanics

    "RequireJS waits for all dependencies to load, figures out the right order in which to call the functions that define the modules, then calls the module definition functions once the dependencies for those functions have been called. Note that the dependencies for a given module definition function could be called in any order, due to their sub-dependency relationships and network load order."

    I think this may help: http://www.sitepoint.com/understanding-requirejs-for-effective-javascript-module-loading/ (see "Managing the Order of Dependent Files")

    RequireJS uses Asynchronous Module Loading (AMD) for loading files. Each dependent module will start loading through asynchronous requests in the given order. Even though the file order is considered, we cannot guarantee that the first file is loaded before the second file due to the asynchronous nature. So, RequireJS allows us to use the shim config to define the sequence of files which need to be loaded in correct order. Let’s see how we can create configuration options in RequireJS.

    requirejs.config({
      shim: {
        'source1': ['dependency1','dependency2'],
        'source2': ['source1']
      }
    });
    

    Hope it helps

    EDIT: As said in comments, using Shim for AMD module is a bad idea, use only shim for non AMD modules and manage dependencies order there. For AMD module requirejs will manage the order of loading. A good link from the comments (thanks Daniel Tulp) ==> Requirejs why and when to use shim config

    0 讨论(0)
提交回复
热议问题