I have a problem with writing a Webpack plugin for a translation service.
The goal is to:
- Get names (and source code) of all required modules during compilation. I need to be able to scan the included source code for special
t()
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). - 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.
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. bundle.[lang].js
). 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).
More details can be found in https://github.com/ckeditor/ckeditor5/issues/387.
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 (before-resolve
), 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 addEntry
ok and when I can use it?).
I was also thinking on connecting Webpack plugin and Webpack loader (because the feature I need is pretty similar to Webpack's style-loader), 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?
PS. I use Webpack 2. If the requirements seem strange to you, please see https://github.com/ckeditor/ckeditor5/issues/387 :).