]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Fix #4031 - Missing ES6 module during tree shaking
authorThomas Stephenson <ovangle@gmail.com>
Tue, 27 Jun 2017 12:36:07 +0000 (22:36 +1000)
committerIskren Chernev <iskren.chernev@gmail.com>
Mon, 7 Aug 2017 19:15:42 +0000 (22:15 +0300)
Cannot import the locale relatively, since the code exists
in a different location in the bundled ES5 module than it
does in ES6.

In order to run tests locally (where the external 'moment'
library can't be loaded), fall back to using the relative import.

src/lib/locale/locales.js

index 99ee11571849f4df7faabc10c9628b83501eb512..7df7ae3c40266e5e783217759efabafafd2e5cf5 100644 (file)
@@ -50,13 +50,21 @@ function loadLocale(name) {
     // TODO: Find a better way to register and load all the locales in Node
     if (!locales[name] && (typeof module !== 'undefined') &&
             module && module.exports) {
+        oldLocale = globalLocale._abbr;
         try {
-            oldLocale = globalLocale._abbr;
-            require('./locale/' + name);
-            // because defineLocale currently also sets the global locale, we
-            // want to undo that for lazy loaded locales
-            getSetGlobalLocale(oldLocale);
-        } catch (e) { }
+            require('moment/locale/' + name);
+        } catch (e) {
+            // In the test environment, the external module 'moment'
+            // can't be resolved because we're running inside it.
+            // Fallback to using the old relative import
+            try {
+                require('./locale/' + name);
+            } catch (e) { }
+        }
+
+        // because defineLocale currently also sets the global locale, we
+        // want to undo that for lazy loaded locales
+        getSetGlobalLocale(oldLocale);
     }
     return locales[name];
 }