title: How to write Webpack plugin which adds modules to the bundle on the fly based on other modules?
body: <p>I have a problem with writing a Webpack plugin for a translation service.</p>
  
  <p>The goal is to:</p>
  
  <ol>
  <li>Get names (and source code) of all required modules during compilation. I need to be able to scan the included source code for special <code>t()</code> function usage but I want to scan only those modules which will be included in the bundle (which, depending on build configuration, can be a subset of all project modules).</li>
  <li>Based on the gathered modules, I want to create additional modules (with translations) on the fly and add them to the bundle. Those modules need to be able to import their own dependencies.</li>
  </ol>
  
  <p>An additional requirement is that the Webpack's code splitting feature should work with the modules created on the fly (I want to extract them to separate files – e.g. <code>bundle.[lang].js</code>). Also, which may be out of the scope of this question, I must make those chunks with translations optional (so you don't have to load all languages, but just one).</p>
  
  <p>More details can be found in <a href="https://github.com/ckeditor/ckeditor5/issues/387">https://github.com/ckeditor/ckeditor5/issues/387</a>.</p>
  
  <p>I've been trying multiple solutions, but Webpack 2's documentation is not very helpful. I can get all the modules by listening to module resolution hooks (<code>before-resolve</code>), but I don't know when all the dependencies are resolved and I don't know if I can add more modules after that (and how to do that – is <code>addEntry</code> ok and when I can use it?).</p>
  
  <p>I was also thinking on connecting Webpack plugin and Webpack loader (because the feature I need is pretty similar to <a href="https://github.com/webpack/style-loader">Webpack's style-loader</a>), but from the plugin level I can only add path to the loader, not the loader itself, so I can't pass the config object as a parameter – am I wrong?</p>
  
  <p>PS. I use Webpack 2. If the requirements seem strange to you, please see <a href="https://github.com/ckeditor/ckeditor5/issues/387">https://github.com/ckeditor/ckeditor5/issues/387</a> :).</p>