MCPcopy
hub / github.com/webpack/webpack / normalizeCacheGroups

Function normalizeCacheGroups

lib/optimize/SplitChunksPlugin.js:493–565  ·  view source on GitHub ↗
(cacheGroups, defaultSizeTypes)

Source from the content-addressed store, hash-verified

491 * @returns {GetCacheGroups} a function to get the cache groups
492 */
493const normalizeCacheGroups = (cacheGroups, defaultSizeTypes) => {
494 if (typeof cacheGroups === "function") {
495 return cacheGroups;
496 }
497 if (typeof cacheGroups === "object" && cacheGroups !== null) {
498 /** @type {((module: Module, context: CacheGroupsContext, results: CacheGroupSource[]) => void)[]} */
499 const handlers = [];
500 for (const key of Object.keys(cacheGroups)) {
501 const option = cacheGroups[key];
502 if (option === false) {
503 continue;
504 }
505 if (typeof option === "string" || option instanceof RegExp) {
506 const source = createCacheGroupSource({}, key, defaultSizeTypes);
507 handlers.push((module, context, results) => {
508 if (checkTest(option, module, context)) {
509 results.push(source);
510 }
511 });
512 } else if (typeof option === "function") {
513 /** @type {WeakMap<OptimizationSplitChunksCacheGroup, CacheGroupSource>} */
514 const cache = new WeakMap();
515 handlers.push((module, context, results) => {
516 const result = option(module);
517 if (result) {
518 const groups = Array.isArray(result) ? result : [result];
519 for (const group of groups) {
520 const cachedSource = cache.get(group);
521 if (cachedSource !== undefined) {
522 results.push(cachedSource);
523 } else {
524 const source = createCacheGroupSource(
525 group,
526 key,
527 defaultSizeTypes
528 );
529 cache.set(group, source);
530 results.push(source);
531 }
532 }
533 }
534 });
535 } else {
536 const source = createCacheGroupSource(option, key, defaultSizeTypes);
537 handlers.push((module, context, results) => {
538 if (
539 checkTest(option.test, module, context) &&
540 checkModuleType(option.type, module) &&
541 checkModuleLayer(option.layer, module)
542 ) {
543 results.push(source);
544 }
545 });
546 }
547 }
548 /**
549 * Returns the matching cache groups.
550 * @param {Module} module the current module

Callers 1

constructorMethod · 0.85

Calls 9

createCacheGroupSourceFunction · 0.85
checkModuleTypeFunction · 0.85
checkModuleLayerFunction · 0.85
isArrayMethod · 0.80
checkTestFunction · 0.70
keysMethod · 0.65
pushMethod · 0.45
getMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected