RuntimeChunkPlugin.js 1.4 KB

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