Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Closed
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
Owner

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: http://search.npmjs.org/) 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
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 with 10 additions and 4 deletions.
  1. +10 −4 underscore.js
14 underscore.js
View
@@ -48,16 +48,22 @@
// Create a safe reference to the Underscore object for use below.
var _ = function(obj) { return new wrapper(obj); };
- // 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().
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = _;
}
exports._ = _;
} else {
+ if (typeof define === 'function' && define.amd) {
+ define('underscore', function() {
+ return _;
+ });
+ }
root['_'] = _;
}
Something went wrong with that request. Please try again.