EnsureChunkConditionsPlugin.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. class EnsureChunkConditionsPlugin {
  7. apply(compiler) {
  8. compiler.plugin("compilation", (compilation) => {
  9. compilation.plugin(["optimize-chunks-basic", "optimize-extracted-chunks-basic"], (chunks) => {
  10. let changed = false;
  11. chunks.forEach((chunk) => {
  12. chunk.modules.slice().forEach((module) => {
  13. if(!module.chunkCondition) return;
  14. if(!module.chunkCondition(chunk)) {
  15. const usedChunks = module._EnsureChunkConditionsPlugin_usedChunks = (module._EnsureChunkConditionsPlugin_usedChunks || []).concat(chunk);
  16. const newChunks = [];
  17. chunk.parents.forEach((parent) => {
  18. if(usedChunks.indexOf(parent) < 0) {
  19. parent.addModule(module);
  20. newChunks.push(parent);
  21. }
  22. });
  23. module.rewriteChunkInReasons(chunk, newChunks);
  24. chunk.removeModule(module);
  25. changed = true;
  26. }
  27. });
  28. });
  29. if(changed) return true;
  30. });
  31. });
  32. }
  33. }
  34. module.exports = EnsureChunkConditionsPlugin;