title: Improving on dynamic imports
body: <p>I was recently using <a href="https://momentjs.com/" rel="nofollow noreferrer">moment.js</a> in an app with localization. In order to set the locale and get appropriate date/time formats, you use <code>moment.locale('foo')</code>. However, this isn't enough on its own; you first need to import the locale you need via <code>import 'moment/locale/foo'</code>.</p>
  
  <p>Now say the client wants the ability to dynamically switch languages. You have a dilemma: either you import all the supported languages up front, or you lazy-load by dynamically importing.</p>
  
  <pre><code>import('moment/locale/' + languageCode);
  moment.locale(languageCode);
  </code></pre>
  
  <p>Something about dynamically importing in a compiled environment feels wrong to me (<a href="https://blog.mgechev.com/2019/05/11/dynamic-imports-javascript/" rel="nofollow noreferrer">also see here</a>), but then as my colleague noted, so does importing a bunch of unnecessary stuff up front.</p>
  
  <p>Is there a best practice here?</p>