lodash: Add support for more AMD build optimizers and allow aliasing as the "underscore" module. [jddalton]

Former-commit-id: 6b3fa45d19f6a55aa7565bcb4d9221f6f159e9c9
This commit is contained in:
John-David Dalton
2012-04-24 18:15:47 -04:00
parent bb09d77eb5
commit f9358531ad
5 changed files with 37 additions and 28 deletions

View File

@@ -61,15 +61,14 @@ load('lodash.js');
In an AMD loader like [RequireJS](http://requirejs.org/):
~~~ js
// opt-in
define.amd.lodash = true;
// Lo-Dash is defined as an anonymous module so, through path mapping, it can be
// referenced as the "underscore" module
require({
'paths': {
'lodash': 'path/to/lodash'
'underscore': 'path/to/lodash'
}
},
['lodash'], function(_) {
['underscore'], function(_) {
console.log(_.VERSION);
});
~~~

View File

@@ -2796,23 +2796,20 @@
else {
freeExports._ = lodash;
}
} else {
// in a browser or Rhino
}
// in a browser or Rhino
else {
// Expose Lo-Dash to the global object even when an AMD loader is present in
// case Lo-Dash was injected by a third-party script and not intended to be
// loaded as a module. The global assignment can be reverted in the Lo-Dash
// module via its `noConflict()` method.
window._ = lodash;
// Expose Lo-Dash as an AMD module, but only for AMD loaders that understand
// the issues with loading multiple versions of Lo-Dash in a page that all
// might call `define()`. The loader will indicate they have special
// allowances for multiple Lo-Dash versions by specifying
// `define.amd.lodash=true`. Register as a named module, since Lo-Dash can
// be concatenated with other files that may use `define()`, but not use a
// proper concatenation script that understands anonymous AMD modules.
// Lowercase `lodash` is used because AMD module names are derived from
// file names, and Lo-Dash is normally delivered in a lowercase file name.
// Do this after assigning Lo-Dash the global so that if an AMD module wants
// to call `noConflict()` to hide this version of Lo-Dash, it will work.
if (typeof define == 'function' && typeof define.amd == 'object' && define.amd && define.amd.lodash) {
define('lodash', function() {
// some AMD build optimizers, like r.js, check for specific condition patterns like the following:
if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
// define as an anonymous module so, through path mapping, it can be
// referenced as the "underscore" module
define(function() {
return lodash;
});
}

2
lodash.min.js vendored
View File

@@ -23,4 +23,4 @@ return b==D||b==G?a.length:T(a).length},some:w,sortBy:function(a,b,c){if(q(b))c&
S(b)+"))==null?'':_['escape'](__t))+'"}).replace(c.interpolate||$,function(a,b){return"'+((__t=("+S(b)+"))==null?'':__t)+'"}).replace(c.evaluate||$,function(a,b){return"';"+S(b)+";__p+='"})+"';";c.variable||(a="with(object||{}){"+a+"}");var a='var __t,__j=Array.prototype.join,__p="";function print(){__p+=__j.call(arguments,"")}'+a+"return __p",f=Function(c.variable||"object","_",a);if(b)return f(b,i);d.source="function("+(c.variable||"object")+"){"+a+"}";return d},throttle:function(a,b){var c,d,f,
e,g,i,k=ja(function(){d=g=o},b);return function(){c=arguments;e=this;i||(i=J(function(){i=h;d&&a.apply(e,c);k()},b));g?d=n:f=a.apply(e,c);k();g=n;return f}},times:function(a,b,c){c&&(b=u(b,c));for(c=0;c<a;c++)b(c)},toArray:function(a){if(!a)return[];if(q(a.toArray))return a.toArray();var b=a.length;return b===+b?l.call(a):sa(a)},union:function(){return I(H(arguments,n))},uniq:I,uniqueId:function(a){var b=xa++;return a?a+b:b},values:sa,without:N,wrap:function(a,b){return function(){var c=[a];V.apply(c,
arguments);return b.apply(this,c)}},zip:function(){for(var a=-1,b=ra(O(arguments,"length")),c=Array(b);++a<b;)c[a]=O(arguments,a);return c},all:X,any:w,collect:A,detect:qa,each:t,foldl:z,foldr:ea,head:U,include:pa,inject:z,intersect:ga,methods:L,select:W,tail:ia,take:U,unique:I});p.prototype=i.prototype;na(i);t("pop,push,reverse,shift,sort,splice,unshift".split(","),function(a){var b=v[a];i.prototype[a]=function(){var a=this.o;b.apply(a,arguments);a.length===0&&delete a[0];return this.p?(new p(a)).chain():
a}});t(["concat","join","slice"],function(a){var b=v[a];p.prototype[a]=function(){var a=b.apply(this.o,arguments);return this.p?(new p(a)).chain():a}});P(p.prototype,{chain:function(){this.p=n;return this},value:function(){return this.o}});Z?"object"==typeof module&&module&&module.q==Z?(module.q=i)._=i:Z._=i:(r._=i,"function"==typeof define&&"object"==typeof define.amd&&define.amd&&define.amd.lodash&&define("lodash",function(){return i}))})(this);
a}});t(["concat","join","slice"],function(a){var b=v[a];p.prototype[a]=function(){var a=b.apply(this.o,arguments);return this.p?(new p(a)).chain():a}});P(p.prototype,{chain:function(){this.p=n;return this},value:function(){return this.o}});Z?"object"==typeof module&&module&&module.q==Z?(module.q=i)._=i:Z._=i:(r._=i,"function"==typeof define&&"object"==typeof define.amd&&define.amd&&define(function(){return i}))})(this);

View File

@@ -12,7 +12,7 @@
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<script src="../vendor/qunit/qunit/qunit.js"></script>
<script>var _2, _ = 1;</script>
<script>var _2, _3, _ = 1;</script>
<script src="../lodash.js"></script>
<script src="../vendor/requirejs/require.js"></script>
<script>
@@ -21,16 +21,21 @@
document.write('<script src="test.js"><\/script>');
}
else {
define.amd.lodash = true;
require({
'baseUrl': '../vendor/requirejs/',
'urlArgs': 't=' + (+new Date),
'paths': {
'lodash': '../../lodash'
'lodash': '../../lodash',
'underscore': './../../lodash'
}
},
['lodash'], function(_) {
_2 = _.noConflict();
['lodash', 'underscore'], function(lodash, lodashAsUnderscore) {
_2 = lodash.noConflict();
_2.moduleName = 'lodash';
_3 = lodashAsUnderscore.noConflict();
_3.moduleName = 'underscore';
require(['test.js']);
});
}

View File

@@ -47,9 +47,17 @@
QUnit.module('lodash');
(function() {
test('supports loading lodash.js as a module', function() {
test('supports loading lodash.js as the "lodash" module', function() {
if (window.document && window.require) {
equal((_2 || {}).VERSION, _.VERSION);
equal((_2 || {}).moduleName, 'lodash');
} else {
skipTest(1)
}
});
test('supports loading lodash.js as the "underscore" module', function() {
if (window.document && window.require) {
equal((_3 || {}).moduleName, 'underscore');
} else {
skipTest(1)
}