title: How to conditionally import different files during build in Angular 6?
body: <h2>Overview</h2>
  
  <p>I have Angular 6 app build by Angular CLI. Application is written in multiple languages. Each language has JSON file with translations of all the application texts. The filenames are <code>${language}.json</code>, e.g. <code>en.json</code>, <code>es.json</code>, <code>pl.json</code>.</p>
  
  <p>I'd like to set the language of the app during build - build the app only in one language. For that on build I need to import a single translation file, which is chosen depending on which language I'm building for.</p>
  
  <h2>Problem</h2>
  
  <p>How to conditionally import single translation file only for the language chosen during build?</p>
  
  <p>In addition I'd like the translation file to be bundled together with the rest of my application's JavaScript, instead of being loaded on demand on run-time.</p>
  
  <h2>Sketch of a solution</h2>
  
  <pre><code>const translations = import(`./translations/${lang}.json`);
  </code></pre>
  
  <p>Actually the above code works in Angular 6/Webpack (the only thing needed is setting <code>compilerOptions.module: 'ESNext'</code> in <code>tsconfig.app.json</code>), but not in a way that I want. Instead of resolving the import during build time and bundling the translation file together with the rest of the code, it creates a separate files (chunks) for all translations, one of which is then loaded on demand during run-time depending on the value of JavaScript <code>lang</code> variable.</p>
  
  <p>So how do I make the import to be resolved during build-time and not run-time? Also where and how do I set <code>lang</code> variable during build, as I don't have access to Webpack configuration (it's hidden inside Angular CLI)</p>
  
  <h2>Webpack alone</h2>
  
  <p>When using Webpack alone, it's possible to implement this for example by using <a href="https://webpack.js.org/plugins/normal-module-replacement-plugin/" rel="nofollow noreferrer">NormalModuleReplacementPlugin</a>. But as Angular CLI hides all the Webpack configuration, I don't know how to integrate it into Angular project. Ejecting Webpack configuration from Angular CLI is the last resort for me.</p>