When using RequireJS, what's the difference between including your script with
<script data-main="scripts/main" src="scripts/require.js"></script>
and
<script src="scripts/require.js"></script>
i.e. what does the data-main
attribute change about loading in a script? I've read through the docs on this, and the different isn't entirely clear to me.
You will typically use a data-main script to set configuration options and then load the first application module. Note: the script tag require.js generates for your data-main module includes the async attribute. This means that you cannot assume that the load and execution of your data-main script will finish prior to other scripts referenced later in the same page.
The documentation mentions that you'll typically use a data-main script to set configuration options and load the first application module — but can't you also do that via a plain old script
tag? Is there a benefit to doing configuration loading the application module with a data-main
attribute?
Is the only different with data-main
the asynchronous loading? Or is there something more?
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 <script data-main="scripts/main" src="scripts/require.js"></script>
Script tags are executed in the order they appear It also means scripts which appear later on the page can depend on things scripts which appear earlier have done. Elements on the page won't render until all the script tags preceding them have loaded and executed.
data-main
is just another way to perform the initial require
call of your application. To illustrate... this:
<script data-main="scripts/main" src="scripts/require.js"></script>
is equivalent to this:
<script src="scripts/require.js"></script> <script>require(["scripts/main"])</script>
The two forms are both asynchronous. This is really all there is to it. Considerations about how many entry points you have or where the RequireJS configuration is going to be located are completely orthogonal to the use of data-main
. To put it differently, these considerations play a role in your use of data-main
to the exact same extent that they play a role in your use of require(["scripts/main"])
.
The part of the documentation you quoted is just obscuring things by mentioning that the script loaded with data-main
creates a script
element in the head
element with the async
attribute set, because this is not different from loading any script through RequireJS. Every single script loaded by RequireJS will have a script
element created for it, in the head
, and have the async
attribute set.
It is common to use data-main
for applications that have only a single entry point, and to put RequireJS' configuration in the module specified in data-main
, but it is not required by any means. For instance, this is a perfectly valid use:
<script> require = { // RequireJS config here... }; </script> <script data-main="scripts/main" src="scripts/require.js"></script> <script> require(["foo"], function (foo) { foo.something(); }); </script>
The configuration is given to RequireJS by setting require
in the global space before loading RequireJS. (If require
is defined before RequireJS is loaded, it will take require
's value as its configuration.) Besides kicking off the application by loading scripts/main
, this code also loads foo
and calls a method on it: two entry points.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With