ChunkFormatHelpers.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. * Returns } Object containing chunk 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: [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. * Processes the provided chunk.
  41. * @param {Chunk} chunk The chunk to get information for
  42. * @param {Hash} hash The hash to update
  43. * @param {ChunkHashContext} chunkHashContext The chunk hash context
  44. * @returns {void}
  45. */
  46. return (chunk, hash, { chunkGraph }) => {
  47. if (chunk.hasRuntime()) return;
  48. const { entries, runtimeChunk } = getChunkInfo(chunk, chunkGraph);
  49. hash.update(name);
  50. hash.update("1");
  51. if (runtimeChunk && runtimeChunk.hash) {
  52. // https://github.com/webpack/webpack/issues/19439
  53. // Any change to runtimeChunk should trigger a hash update,
  54. // we shouldn't depend on or inspect its internal implementation.
  55. // import __webpack_require__ from "./runtime-main.e9400aee33633a3973bd.js";
  56. hash.update(runtimeChunk.hash);
  57. }
  58. updateHashForEntryStartup(hash, chunkGraph, entries, chunk);
  59. };
  60. }
  61. module.exports = {
  62. createChunkHashHandler,
  63. getChunkInfo
  64. };