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