]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Deprecate use of globally exported moment in case of AMD and CommonJS 1220/head
authorIskren Chernev <iskren.chernev@gmail.com>
Sat, 26 Oct 2013 07:08:38 +0000 (00:08 -0700)
committerIskren Chernev <iskren.chernev@gmail.com>
Sat, 26 Oct 2013 07:11:35 +0000 (00:11 -0700)
This is discussed in #1214. If moment is exported as a global in AMD or
CommonJS print a warning once the global is used.

moment.js

index 85fefbe4cc46b16a899fa6e421cc189b9a3fd6f1..a5d5df95b1e2556310c4dfa02857146494da4aa2 100644 (file)
--- a/moment.js
+++ b/moment.js
         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;
         }
     }
     // 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;