1

I would like to use requireJS. However, I have a lib that needs to be loaded before the DOM. However, this is not what happens with requireJS

<html>
    <head>
        <script data-main="/app" src="/require.js"></script>
    </head>

    <body>
        <bar-foo/>
    </body>
</html>

With app.js

require.config({
    paths: {
        'customElement': '/customElement',
        'barfoo': '/barfoo'
    },
    shim: {
        barfoo: {
            deps: [ 'customElement' ]
        }
    }
});
define(['barfoo'], function() {
    ....
}

For example, if I simply load this script directly in the head (without requireJS) it works fine. Is there a require-config-option so I can load a specific file immediately (synchronously?) ?

3
  • If you need to load it synchronously, just don't use require.js. Why would you want to use it if you don't want async loading? Commented Jan 17, 2014 at 14:44
  • You could as well ask: why use requirejs at all (using, for example the 'defer' attribute instead). Anyway, I've updated the example and added a dependency. In this case I think it makes more sense to use requirejs Commented Jan 17, 2014 at 15:30
  • Well OK, but I'm pretty sure the answer is "no". You can declare dependencies, so maybe that'd help. I'm not a requirejs expert so hopefully somebody else knows a trick. Commented Jan 17, 2014 at 15:41

1 Answer 1

1

Requirejs is known for it's asynchronous power. However when you need some sort of an order in which you want files to be loaded due to dependencies, in require.config there is a shim config:

shim: Configure the dependencies, exports, and custom initialization for older, traditional "browser globals" scripts that do not use define() to declare the dependencies and set a module value.

So let's say you have a backbone app that depends on Underscore and jQuery, and you want them to load in that order then your would:

require.config({
paths: {
    'Backbone': '/Backbone',
    'Underscore': '/Underscore',
    'jQuery': '/jQuery'
},
shim: {
    'Backbone': [ 'Underscore', 'jQuery' ]
}
});

require(['Backbone'], function(Backbone){
  //.....
});

So, RequireJS allows us to use the shim config to define the sequence of files which need to be loaded in correct order.

Sign up to request clarification or add additional context in comments.

3 Comments

true (I've update my example because it was incorrect). The problem with this is that, still, nothing is forced to be loaded synchronous (before the DOM)
You can just hardcode the script src into the dom or take a look at this: github.com/scriptfoundry/require-synchronous/blob/master/…
the project you mention doesn't seem to be very up2date

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.