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. const PLUGIN_NAME = "RuntimeChunkPlugin";
  9. /** @typedef {(entrypoint: { name: string }) => string} RuntimeChunkFunction */
  10. class RuntimeChunkPlugin {
  11. /**
  12. * @param {{ name?: RuntimeChunkFunction }=} options options
  13. */
  14. constructor(options = {}) {
  15. /** @type {{ name: string | RuntimeChunkFunction }} */
  16. this.options = {
  17. name: (entrypoint) => `runtime~${entrypoint.name}`,
  18. ...options
  19. };
  20. }
  21. /**
  22. * Apply the plugin
  23. * @param {Compiler} compiler the compiler instance
  24. * @returns {void}
  25. */
  26. apply(compiler) {
  27. compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
  28. compilation.hooks.addEntry.tap(PLUGIN_NAME, (_, { name: entryName }) => {
  29. if (entryName === undefined) return;
  30. const data =
  31. /** @type {EntryData} */
  32. (compilation.entries.get(entryName));
  33. if (data.options.runtime === undefined && !data.options.dependOn) {
  34. // Determine runtime chunk name
  35. let name = this.options.name;
  36. if (typeof name === "function") {
  37. name = name({ name: entryName });
  38. }
  39. data.options.runtime = name;
  40. }
  41. });
  42. });
  43. }
  44. }
  45. module.exports = RuntimeChunkPlugin;