pryabov
pryabov

Reputation: 854

RequireJS : Can I use require() instead of define()

Suppose there are three modules: m1, m2, m3

Where mi(i=1..3) is empty module:

define(function() {
    return this;
})

And exists additional module useAll that depends on m1, m2, m3 modules.

What is the difference between two implementations of useAll module:

  1. All modules on which depends useAll mentioned in define()

    define(['m1', 'm2', 'm3'], function() {...})

  2. Modules mentioned in require() function

    define(function () { require(['m1']); require(['m2']); require(['m3']); ... })

Is there any issues with script loading time and order?

Upvotes: 0

Views: 172

Answers (1)

user2445933
user2445933

Reputation:

  1. The diferrence is that in the second implementation your require are treated by r.js as nested dependencies and by default they will not get included in optimization process, unless you specify:

    findNestedDependencies : true

    From the docs:

    Finds require() dependencies inside a require() or define call. By default this value is false, because those resources should be considered dynamic/runtime calls. However, for some optimization scenarios, it is desirable to include them in the build.

  2. Another difference, is that the second implementation can load CommonJs modules too.

  3. If you use require to define your module, you cannot return any value as a result of execution. This means, that you cannot use this module as a real dependency to another module

Upvotes: 3

Related Questions