MCPcopy
hub / github.com/webpack/webpack / apply

Method apply

lib/optimize/RealContentHashPlugin.js:201–553  ·  lib/optimize/RealContentHashPlugin.js::RealContentHashPlugin.apply

* Applies the plugin by registering its hooks on the compiler. * @param {Compiler} compiler the compiler instance * @returns {void}

(compiler)

Source from the content-addressed store, hash-verified

199 * @returns {void}
200 */
201 apply(compiler) {
202 compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
203 const cacheAnalyse = compilation.getCache(
204 class="st">"RealContentHashPlugin|analyse"
205 );
206 const cacheGenerate = compilation.getCache(
207 class="st">"RealContentHashPlugin|generate"
208 );
209 const hooks = RealContentHashPlugin.getCompilationHooks(compilation);
210 compilation.hooks.processAssets.tapPromise(
211 {
212 name: PLUGIN_NAME,
213 stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_HASH
214 },
215 async () => {
216 const assets = compilation.getAssets();
217 /** @type {AssetInfoForRealContentHash[]} */
218 const assetsWithInfo = [];
219 /** @type {Map<string, [AssetInfoForRealContentHash]>} */
220 const hashToAssets = new Map();
221 class="cm">// Inline `[contenthash:<digest>]` digest per hash, so the recomputed
222 class="cm">// real hash is re-encoded in it instead of `output.hashDigest`.
223 /** @type {Map<string, string>} */
224 const hashToDigest = new Map();
225 for (const { source, info, name } of assets) {
226 const cachedSource = toCachedSource(source);
227 const content = /** @type {string} */ (cachedSource.source());
228 /** @type {Hashes} */
229 const hashes = new Set();
230 addToList(info.contenthash, hashes);
231 if (info.contenthashDigest) {
232 for (const hash of Object.keys(info.contenthashDigest)) {
233 hashToDigest.set(hash, info.contenthashDigest[hash]);
234 }
235 }
236 /** @type {AssetInfoForRealContentHash} */
237 const data = {
238 name,
239 info,
240 source: cachedSource,
241 newSource: undefined,
242 newSourceWithoutOwn: undefined,
243 content,
244 ownHashes: undefined,
245 contentComputePromise: undefined,
246 contentComputeWithoutOwnPromise: undefined,
247 referencedHashes: undefined,
248 hashes
249 };
250 assetsWithInfo.push(data);
251 for (const hash of hashes) {
252 const list = hashToAssets.get(hash);
253 if (list === undefined) {
254 hashToAssets.set(hash, [data]);
255 } else {
256 list.push(data);
257 }
258 }

Callers

nothing calls this directly

Calls 15

toCachedSourceFunction · 0.85
addToListFunction · 0.85
compareSelectFunction · 0.85
createHashFunction · 0.85
tapMethod · 0.80
getAssetsMethod · 0.80
sourceMethod · 0.80
mergeEtagsMethod · 0.80
getLazyHashedEtagMethod · 0.80
sortMethod · 0.80
isUsedMethod · 0.80

Tested by

no test coverage detected