RuntimeChunkPlugin.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. const PLUGIN_NAME = "RuntimeChunkPlugin";
  10. /** @typedef {(entrypoint: { name: string }) => string} RuntimeChunkFunction */
  11. class RuntimeChunkPlugin {
  12. /**
  13. * @param {{ name?: RuntimeChunkFunction }=} options options
  14. */
  15. constructor(options) {
  16. this.options = {
  17. /** @type {RuntimeChunkFunction} */
  18. name: entrypoint => `runtime~${entrypoint.name}`,
  19. ...options
  20. };
  21. }
  22. /**
  23. * Apply the plugin
  24. * @param {Compiler} compiler the compiler instance
  25. * @returns {void}
  26. */
  27. apply(compiler) {
  28. compiler.hooks.thisCompilation.tap(PLUGIN_NAME, compilation => {
  29. compilation.hooks.addEntry.tap(PLUGIN_NAME, (_, { 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 =
  37. /** @type {string | RuntimeChunkFunction} */
  38. (this.options.name);
  39. if (typeof name === "function") {
  40. name = name({ name: entryName });
  41. }
  42. data.options.runtime = name;
  43. }
  44. });
  45. });
  46. }
  47. }
  48. module.exports = RuntimeChunkPlugin;