Skip to content

Instantly share code, notes, and snippets.

@jjt
Created August 9, 2012 18:36
Show Gist options
  • Select an option

  • Save jjt/3306911 to your computer and use it in GitHub Desktop.

Select an option

Save jjt/3306911 to your computer and use it in GitHub Desktop.
RequireJS "global" dependencies

I'd like the ability to have single-instance, page-wide libraries like jQ/Zepto or Underscore/lodash available with their standard global vars ($, _) for every module to use without having to explicitly type in their deps in every define call (see not-so-dry.js).

How I handle this currently is by using separate script tags for the global libs before the tag for RequireJS, leaving them out of the RequireJS system entirely. While this solution works, it adds sync loading/execution requirements handled by the order of the markup.

While some modules wouldn't need DOM methods, thus not having jQ/Zepto/etc as a dep, I don't see much benefit to my web app(s) being able to load those modules first, then jQ/Zepto/etc, then the view/template modules that do the on-screen work.

What about if I were to define the non-AMD version of these libs in a shim block (AFAIK, that should still "pollute" the global namespace), and then reference them as deps in the main require block? Would that ensure that these libs would be loaded and executed before the other modules? See shim-main.js.

// Not so DRY:
// module_1.js
define(["underscore", "zepto", "momentjs",...], function(_, $, Moment, ... ) {
// ...
});
// module_2.js
define(["underscore", "zepto", "momentjs",...], function(_, $, Moment, ... ) {
// ...
});
// module_3.js
define(["underscore", "zepto", "momentjs",...], function(_, $, Moment, ... ) {
// ...
});
// Called with <script data-main="config.js"...>
// config.js
require.config({
deps: ["main"],
shim: {
zepto: {
exports: "$"
},
underscore: {
exports: "_"
}
});
// main.js
require(["zepto","underscore"], function() {
// ...
});
@jjt
Copy link
Author

jjt commented Aug 9, 2012

Agreed on sprawling module responsibility. At worst though, modules should be able to tolerate _ and $ being in the global scope.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment