From: Iskren Chernev Date: Sat, 26 Oct 2013 07:08:38 +0000 (-0700) Subject: Deprecate use of globally exported moment in case of AMD and CommonJS X-Git-Tag: 2.4.0~2^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3e51eff8e13047218aff58f558b22c037c1231b7;p=thirdparty%2Fmoment.git Deprecate use of globally exported moment in case of AMD and CommonJS This is discussed in #1214. If moment is exported as a global in AMD or CommonJS print a warning once the global is used. --- diff --git a/moment.js b/moment.js index 85fefbe4c..a5d5df95b 100644 --- a/moment.js +++ b/moment.js @@ -2270,12 +2270,27 @@ Exposing Moment ************************************/ - function makeGlobal() { + function makeGlobal(deprecate) { + var warned = false, local_moment = moment; /*global ender:false */ - if (typeof ender === 'undefined') { - // here, `this` means `window` in the browser, or `global` on the server - // add `moment` as a global object via a string identifier, - // for Closure Compiler "advanced" mode + if (typeof ender !== 'undefined') { + return; + } + // here, `this` means `window` in the browser, or `global` on the server + // add `moment` as a global object via a string identifier, + // for Closure Compiler "advanced" mode + if (deprecate) { + this.moment = function () { + if (!warned && console && console.warn) { + warned = true; + console.warn( + "Accessing Moment through the global scope is " + + "deprecated, and will be removed in an upcoming " + + "release."); + } + local_moment.apply(null, arguments); + }; + } else { this['moment'] = moment; } } @@ -2283,11 +2298,12 @@ // CommonJS module is defined if (hasModule) { module.exports = moment; - makeGlobal(); + makeGlobal(true); } else if (typeof define === "function" && define.amd) { define("moment", function (require, exports, module) { if (module.config().noGlobal !== true) { - makeGlobal(); + // If user provided noGlobal, he is aware of global + makeGlobal(module.config().noGlobal === undefined); } return moment;