* Applies the plugin by registering its hooks on the compiler. * @param {Compiler} compiler the compiler instance * @returns {void}
(compiler)
| 878 | * @returns {void} |
| 879 | */ |
| 880 | apply(compiler) { |
| 881 | const cachedMakePathsRelative = makePathsRelative.bindContextCache( |
| 882 | compiler.context, |
| 883 | compiler.root |
| 884 | ); |
| 885 | compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => { |
| 886 | const logger = compilation.getLogger(`webpack.${PLUGIN_NAME}`); |
| 887 | let alreadyOptimized = false; |
| 888 | compilation.hooks.unseal.tap(PLUGIN_NAME, () => { |
| 889 | alreadyOptimized = false; |
| 890 | }); |
| 891 | compilation.hooks.optimizeChunks.tap( |
| 892 | { |
| 893 | name: PLUGIN_NAME, |
| 894 | stage: STAGE_ADVANCED |
| 895 | }, |
| 896 | (chunks) => { |
| 897 | if (alreadyOptimized) return; |
| 898 | alreadyOptimized = true; |
| 899 | logger.time("prepare"); |
| 900 | const chunkGraph = compilation.chunkGraph; |
| 901 | const moduleGraph = compilation.moduleGraph; |
| 902 | // Give each selected chunk an index (to create strings from chunks) |
| 903 | /** @type {Map<Chunk, bigint>} */ |
| 904 | const chunkIndexMap = new Map(); |
| 905 | const ZERO = BigInt("0"); |
| 906 | const ONE = BigInt("1"); |
| 907 | const START = ONE << BigInt("31"); |
| 908 | let index = START; |
| 909 | for (const chunk of chunks) { |
| 910 | chunkIndexMap.set( |
| 911 | chunk, |
| 912 | index | BigInt((Math.random() * 0x7fffffff) | 0) |
| 913 | ); |
| 914 | index <<= ONE; |
| 915 | } |
| 916 | /** |
| 917 | * Returns key of the chunks. |
| 918 | * @param {Iterable<Chunk, undefined, undefined>} chunks list of chunks |
| 919 | * @returns {bigint | Chunk} key of the chunks |
| 920 | */ |
| 921 | const getKey = (chunks) => { |
| 922 | const iterator = chunks[Symbol.iterator](); |
| 923 | let result = iterator.next(); |
| 924 | if (result.done) return ZERO; |
| 925 | const first = result.value; |
| 926 | result = iterator.next(); |
| 927 | if (result.done) return first; |
| 928 | let key = |
| 929 | /** @type {bigint} */ (chunkIndexMap.get(first)) | |
| 930 | /** @type {bigint} */ (chunkIndexMap.get(result.value)); |
| 931 | while (!(result = iterator.next()).done) { |
| 932 | const raw = chunkIndexMap.get(result.value); |
| 933 | key ^= /** @type {bigint} */ (raw); |
| 934 | } |
| 935 | return key; |
| 936 | }; |
| 937 | /** |
nothing calls this directly
no test coverage detected