ChunkFormatHelpers.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Natsu @xiaoxiaojx
  4. */
  5. "use strict";
  6. const { updateHashForEntryStartup } = require("./StartupHelpers");
  7. /** @typedef {import("../ChunkGraph")} ChunkGraph */
  8. /** @typedef {import("../Module")} Module */
  9. /** @typedef {import("../Chunk")} Chunk */
  10. /** @typedef {import("../Entrypoint")} Entrypoint */
  11. /** @typedef {import("../util/Hash")} Hash */
  12. /** @typedef {import("../Compilation").ChunkHashContext} ChunkHashContext */
  13. /**
  14. * Gets information about a chunk including its entries and runtime chunk
  15. * @param {Chunk} chunk The chunk to get information for
  16. * @param {ChunkGraph} chunkGraph The chunk graph containing the chunk
  17. * @returns {{entries: Array<[Module, Entrypoint | undefined]>, runtimeChunk: Chunk|null}} Object containing chunk entries and runtime chunk
  18. */
  19. function getChunkInfo(chunk, chunkGraph) {
  20. const entries = [
  21. ...chunkGraph.getChunkEntryModulesWithChunkGroupIterable(chunk)
  22. ];
  23. const runtimeChunk =
  24. entries.length > 0
  25. ? /** @type {Entrypoint[][]} */
  26. (entries)[0][1].getRuntimeChunk()
  27. : null;
  28. return {
  29. entries,
  30. runtimeChunk
  31. };
  32. }
  33. /**
  34. * Creates a chunk hash handler
  35. * @param {string} name The name of the chunk
  36. * @returns {(chunk: Chunk, hash: Hash, { chunkGraph }: ChunkHashContext) => void} The chunk hash handler
  37. */
  38. function createChunkHashHandler(name) {
  39. /**
  40. * @param {Chunk} chunk The chunk to get information for
  41. * @param {Hash} hash The hash to update
  42. * @param {ChunkHashContext} chunkHashContext The chunk hash context
  43. * @returns {void}
  44. */
  45. return (chunk, hash, { chunkGraph }) => {
  46. if (chunk.hasRuntime()) return;
  47. const { entries, runtimeChunk } = getChunkInfo(chunk, chunkGraph);
  48. hash.update(name);
  49. hash.update("1");
  50. if (runtimeChunk && runtimeChunk.hash) {
  51. // https://github.com/webpack/webpack/issues/19439
  52. // Any change to runtimeChunk should trigger a hash update,
  53. // we shouldn't depend on or inspect its internal implementation.
  54. // import __webpack_require__ from "./runtime-main.e9400aee33633a3973bd.js";
  55. hash.update(runtimeChunk.hash);
  56. }
  57. updateHashForEntryStartup(hash, chunkGraph, entries, chunk);
  58. };
  59. }
  60. module.exports = {
  61. createChunkHashHandler,
  62. getChunkInfo
  63. };