Search code examples
javascriptrequirejsamd

RequireJS - dependency parameter vs in definition require equivalence


Is

define(['a', 'b'],
   function(require, exports, module) { } );

equivalent to this

define(function(require, exports, module) { 
      require(['a', 'b'])
});

If not, how is it different?


Solution

  • Your first bit of code should actually be

    define(['a', 'b'], function(a, b) { } );
    

    and the second bit as

    define(function(require, exports, module) { 
        var a = require('a');
        var b = require('b');
    });
    

    When you use the second bit of code requirejs has to parse the function.toString() to find the require statements and add them as a dependency.

    http://requirejs.org/docs/api.html#define