optional-locale-chunk-plugin.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. /*eslint-env node*/
  2. /*eslint import/no-nodejs-modules:0 */
  3. /**
  4. * When our locales are codesplit into cache groups, webpack expects that all
  5. * chunks *must* be loaded before the main entrypoint can be executed. However,
  6. * since we will only be using one locale at a time we do not want to load all
  7. * locale chunks, just the one the user has enabled.
  8. *
  9. * This plugin removes the locale chunks from the app entrypoint's immediate
  10. * chunk dependants list, ensuring that the compiled entrypoint will execute
  11. * *without* all locale chunks loaded.
  12. */
  13. const PLUGIN_NAME = 'OptionalLocaleChunkPlugin';
  14. const clearLocaleChunks = chunks =>
  15. chunks
  16. .filter(chunk => chunk.name !== 'app')
  17. .forEach(chunk => {
  18. const mainGroup = Array.from(chunk.groupsIterable)[0];
  19. mainGroup.chunks = mainGroup.chunks.filter(
  20. c => c.name && !c.name.startsWith('locale')
  21. );
  22. });
  23. class OptionalLocaleChunkPlugin {
  24. apply(compiler) {
  25. compiler.hooks.compilation.tap(PLUGIN_NAME, compilation =>
  26. compilation.hooks.afterOptimizeChunks.tap(PLUGIN_NAME, clearLocaleChunks)
  27. );
  28. }
  29. }
  30. module.exports = OptionalLocaleChunkPlugin;