Run data-main js before resolving dependencies #546

stealthcode opened this Issue Nov 30, 2012 · 1 comment

2 participants


The documentation suggests that you set your configuration within a data-main JavaScript file. However, the way it is currently implemented does not guarantee that the contents of your data-main is executed before the execution of any other require or define blocks referenced in your program. A concrete example is posted on StackOverflow. This prevents the data-main referenced javascript file from being used as a central webapp configuration file.

The following code snippet summarizes how I force the requirejs configuration to execute before any other code.

<script data-main="js/" src="js/vendor/require.js"></script>
<script type="text/javascript">
require(['main'], function() {
    require(['viewmodel/vm', 'ko'], 
        function(viewmodel, ko) {
requirejs member

This is outside of the capability of the loader -- the data-main fetch is an async JS script load like all the other the loader does, so it is possible, even most likely, that it will not run before that inline require call.

The basic idea behind data-main is to get out of HTML-space as soon as possible for JS code, and do all JS inside .js files. However, if doing inline require/define calls in the HTML, then using data-main is not recommended.

Closing this as working as designed, but feel free to continue discussion in the ticket.

@jrburke jrburke closed this Dec 1, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment