From e6381d8ee7c82f838bfcba6cdeccfca924e65041 Mon Sep 17 00:00:00 2001 From: jrburke Date: Mon, 17 Oct 2011 17:58:03 -0700 Subject: [PATCH 1/2] Opt in to AMD registration. --- underscore.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/underscore.js b/underscore.js index d19e26960..ba9b8aea3 100644 --- a/underscore.js +++ b/underscore.js @@ -57,6 +57,19 @@ } else { // Exported as a string, for Closure Compiler "advanced" mode. root['_'] = _; + + // Register as a module with AMD. Use a named module since underscore + // can be used in optimization schemes that do not understand anonymous + // modules, and the if it is used on a page with an AMD loader, a load + // error could occur. This work is done in addition to exporting a global + // on root since the web page could use an AMD loader to load the module + // but still want to use the global reference. Test pages are a good + // example. + if (typeof define === 'function' && define.amd) { + define('underscore', function() { + return _; + }); + } } // Current version. From 6241343b33959ced28eb7744f85168ef9106c544 Mon Sep 17 00:00:00 2001 From: jrburke Date: Wed, 19 Oct 2011 10:07:39 -0700 Subject: [PATCH 2/2] If register as an AMD module, do not create a global, since the goal is to not leak out globals in that case. --- underscore.js | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/underscore.js b/underscore.js index c3fe0a53a..2e27c26ac 100644 --- a/underscore.js +++ b/underscore.js @@ -56,22 +56,17 @@ exports = module.exports = _; } exports._ = _; - } else { - // Exported as a string, for Closure Compiler "advanced" mode. - root['_'] = _; - + } else if (typeof define === 'function' && define.amd) { // Register as a module with AMD. Use a named module since underscore // can be used in optimization schemes that do not understand anonymous // modules, and the if it is used on a page with an AMD loader, a load - // error could occur. This work is done in addition to exporting a global - // on root since the web page could use an AMD loader to load the module - // but still want to use the global reference. Test pages are a good - // example. - if (typeof define === 'function' && define.amd) { - define('underscore', function() { - return _; - }); - } + // error could occur. + define('underscore', function() { + return _; + }); + } else { + // Exported as a string, for Closure Compiler "advanced" mode. + root['_'] = _; } // Current version.