RuntimeChunkPlugin.js 1.0 KB

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