Skip to content
This repository

Register as AMD module, but still export a global #431

wants to merge 2 commits into from

2 participants

James Burke Jeremy Ashkenas
James Burke

This is in response to this commit, which came out of reports of issues using underscore in an AMD environment.

From what I have heard, the issues are mainly when trying to also use backbone. Those issues could be resolved by merging this pull request.

But in general, if underscore prefers to not get reports of issues with an AMD loader, this pull request also exports a global even if define() is available. This is the same approach that jQuery uses. Ideally this would not be needed, but modular development is just getting bootstrapped on the web, and during this bootstrap period, this approach avoids issues.

If pull requests are not the way you prefer to have this conversation, please let me know what avenue works best.

Jeremy Ashkenas

Thanks for the pull request, but I'm afraid that I've been mulling over this issue since the original AMD commit, and have pretty well decided on yanking it.

Since the original AMD addition, I think that the volume of confused users trying to load Underscore correctly has only increased. There have been any number of things:

  • Folks requesting other module formats for other loaders.

  • If any library that depends on Underscore (and there are many of them: does not yet 'support' AMD, but Underscore does, things get royally screwed up.

  • In an ideal situation, libraries do not have to be modified to support a particular script loader (or group of loaders).

  • JavaScript's upcoming native module support is entirely incompatible with AMD.

  • Loading individual modules piecemeal is a terrifically inefficient way to built a website. Because of this, there's the great RequireJS optimizer, which will turn your modules into ordinary packages.

If JavaScript ever gets a real module system, and that module system is deployed in cross browser, we'll support it. Until that day, you can:

  • Use one of the many script loaders that doesn't require every script you load to be rewritten.

  • Patch your copy of Underscore.js

  • Use a fork, perhaps provided by @jrburke.

  • Use a little RequireJS shim, like this one:

    define(['vendor/underscore', 'vendor/jquery', 'order!vendor/backbone'], function() {
      return Backbone;
Jeremy Ashkenas jashkenas closed this January 11, 2012
Rico Sta. Cruz rstacruz referenced this pull request in theironcook/Backbone.ModelBinder October 02, 2012

Remove AMD/require.js support #83

Timmy Willison timmywil referenced this pull request in jashkenas/backbone June 27, 2013

Reconsider AMD support #2641

David Chambers davidchambers referenced this pull request in davidchambers/airwaves June 30, 2013

Adding AMD support #1

Borjan Tchakaloff bibz referenced this pull request October 08, 2013

UMD-ready Underscore #1303

Joshua Peek josh referenced this pull request in josh/selector-set January 26, 2014

Add UMD wrapper that works with browser/amd #9

Basarat Ali Syed basarat referenced this pull request in borisyankov/DefinitelyTyped February 07, 2014

Export as "backbone" module for AMD loading #1673

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 10 additions and 4 deletions. Show diff stats Hide diff stats

  1. 14  underscore.js
14  underscore.js
@@ -48,16 +48,22 @@
48 48
   // Create a safe reference to the Underscore object for use below.
49 49
   var _ = function(obj) { return new wrapper(obj); };
50 50
-  // Export the Underscore object for **Node.js**, with
-  // backwards-compatibility for the old `require()` API. If we're in
-  // the browser, add `_` as a global object via a string identifier,
-  // for Closure Compiler "advanced" mode.
+  // Export the Underscore object for **Node.js** and **"CommonJS"**, with
+  // backwards-compatibility for the old `require()` API. If we're not in
+  // CommonJS, add `_` to the global object via a string identifier for
+  // the Closure Compiler "advanced" mode, and optionally register as an
+  // AMD module via define().
55 56
   if (typeof exports !== 'undefined') {
56 57
     if (typeof module !== 'undefined' && module.exports) {
57 58
       exports = module.exports = _;
58 59
59 60
     exports._ = _;
60 61
   } else {
+    if (typeof define === 'function' && define.amd) {
+      define('underscore', function() {
+        return _;
+      });
+    }
61 67
     root['_'] = _;
62 68
63 69

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.