RuntimeChunkPlugin.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. module.exports = class RuntimeChunkPlugin {
  7. constructor(options) {
  8. this.options = Object.assign(
  9. {
  10. name: entrypoint => `runtime~${entrypoint.name}`
  11. },
  12. options
  13. );
  14. }
  15. apply(compiler) {
  16. compiler.hooks.thisCompilation.tap("RuntimeChunkPlugin", compilation => {
  17. compilation.hooks.optimizeChunksAdvanced.tap("RuntimeChunkPlugin", () => {
  18. for (const entrypoint of compilation.entrypoints.values()) {
  19. const chunk = entrypoint.getRuntimeChunk();
  20. let name = this.options.name;
  21. if (typeof name === "function") {
  22. name = name(entrypoint);
  23. }
  24. if (
  25. chunk.getNumberOfModules() > 0 ||
  26. !chunk.preventIntegration ||
  27. chunk.name !== name
  28. ) {
  29. const newChunk = compilation.addChunk(name);
  30. newChunk.preventIntegration = true;
  31. entrypoint.unshiftChunk(newChunk);
  32. newChunk.addGroup(entrypoint);
  33. entrypoint.setRuntimeChunk(newChunk);
  34. }
  35. }
  36. });
  37. });
  38. }
  39. };