0

I use r.js optimizer to bundle my project.

In some places, I must load files synchronously like here:

define('module-name', ['adal-angular'], function () {
    var app = angular.module('myApp', ['AdalAngular']);

In the require.config.js I have this:

paths: {
        adal: 'bower_components/adal-angular/dist/adal.min',
        'adal-angular': 'bower_components/adal-angular/dist/adal-angular.min',
}

and Shim:

shim: {
      'adal-angular': ['angular', 'adal'],
}

My optimize configuration contains: findNestedDependencies: true, Yet adal and adal-angular are not included in the bundled file, unless I add this line somewhere in my code: require("adal-angular") - which is redundant and I would like not to do that.

johni
  • 5,342
  • 6
  • 42
  • 70
  • 1
    "I must load files synchronously like here:". RequireJS definitely loads any modules loaded in that snippet asynchronously. Actually, let me add this: in a browser RequireJS *never* loads anything sychronously. – Louis May 22 '16 at 12:03
  • The difference between loading `adal-angular` in an array at the definition of the module, than loading it by executing `require('adal-angular')` is that the function is not executed until it's loaded in the first option. – johni May 22 '16 at 12:04
  • In both cases the module is loaded asynchronously. – Louis May 22 '16 at 12:06
  • Ok, maybe the term is not right. In the first case, the function's executing is delayed until the dependencies are loaded (those mentioned in the array). In the second case it's not. – johni May 22 '16 at 12:07
  • If by "function" you mean the module's factory function, then you are mistaken. A `require([...], cb)` call *will* load the dependencies in the array at the time of the call, but a `require(string)` call *will* load the dependencies *before* the factory function is called because RequireJS scans the code of the module and changes the dependency array itself. I cannot give a detailed explanation in a comment but see [this answer](http://stackoverflow.com/a/21786807/1906307) for the details. – Louis May 22 '16 at 12:11
  • Actually, [this answer](http://stackoverflow.com/a/21023168/1906307) is probably a better explanation. I say there that `require` called with a single string parameter is synchronous which is true insofar as it fails or succeeds immediately. But module *loading* is always asynchronous. – Louis May 22 '16 at 12:28
  • can you show your r.js configuration file? – Vishwanath May 27 '16 at 13:17

0 Answers0