Difference Between data-main and normal script loading

后端 未结 4 1403
后悔当初
后悔当初 2020-12-31 00:52

When using RequireJS, what\'s the difference between including your script with



        
4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-31 01:19

    data-main is for when you want to have a single entry point to your application. That single script line will load RequireJS along with scripts/main.js and kick off your app.

    The result of

    
    

    is that is appended to the document at runtime; this is the script that will contain your require.config() block and pull in your first application script. If you don't specify a data-main, then you're only loading Require and none of your app scripts, unless you explicitly load a config file and the first module.

    What do you think Require will load if you don't tell it to load anything?


    If you do not use data-main, you must provide an entry point after loading Require (this is how I have always done it, for no good reason other than that's how I learned it.) Read about the configuration options to see how you would do this.

    I use this pattern in development:

    
    
    
    

    As a single entry point, the contents of config.js and the subsequent require(['js/main']) call would be in whichever script is referenced as data-main.


    If you use the static optimizer to build a production bundle, none of this matters because you just load the bundle.

提交回复
热议问题