RuntimeChunkPlugin.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. /** @typedef {import("../Compilation").EntryData} EntryData */
  7. /** @typedef {import("../Compiler")} Compiler */
  8. /** @typedef {import("../Entrypoint")} Entrypoint */
  9. class RuntimeChunkPlugin {
  10. constructor(options) {
  11. this.options = {
  12. /**
  13. * @param {Entrypoint} entrypoint entrypoint name
  14. * @returns {string} runtime chunk name
  15. */
  16. name: entrypoint => `runtime~${entrypoint.name}`,
  17. ...options
  18. };
  19. }
  20. /**
  21. * Apply the plugin
  22. * @param {Compiler} compiler the compiler instance
  23. * @returns {void}
  24. */
  25. apply(compiler) {
  26. compiler.hooks.thisCompilation.tap("RuntimeChunkPlugin", compilation => {
  27. compilation.hooks.addEntry.tap(
  28. "RuntimeChunkPlugin",
  29. (_, { name: entryName }) => {
  30. if (entryName === undefined) return;
  31. const data =
  32. /** @type {EntryData} */
  33. (compilation.entries.get(entryName));
  34. if (data.options.runtime === undefined && !data.options.dependOn) {
  35. // Determine runtime chunk name
  36. let name = this.options.name;
  37. if (typeof name === "function") {
  38. name = name({ name: entryName });
  39. }
  40. data.options.runtime = name;
  41. }
  42. }
  43. );
  44. });
  45. }
  46. }
  47. module.exports = RuntimeChunkPlugin;