MCPcopy
hub / github.com/webpack/webpack / runGrouping

Function runGrouping

lib/util/smartGrouping.js:104–225  ·  view source on GitHub ↗
(itemsWithGroups)

Source from the content-addressed store, hash-verified

102 * @returns {(I | G)[]} groups items
103 */
104 const runGrouping = (itemsWithGroups) => {
105 const totalSize = itemsWithGroups.size;
106 for (const entry of itemsWithGroups) {
107 for (const group of entry.groups) {
108 if (group.alreadyGrouped) continue;
109 const items = group.items;
110 if (items === undefined) {
111 group.items = new Set([entry]);
112 } else {
113 items.add(entry);
114 }
115 }
116 }
117 /** @type {Map<Group<I, G>, { items: Items<I, G>, options: GroupOptions | false | undefined, used: boolean }>} */
118 const groupMap = new Map();
119 for (const group of allGroups.values()) {
120 if (group.items) {
121 const items = group.items;
122 group.items = undefined;
123 groupMap.set(group, {
124 items,
125 options: undefined,
126 used: false
127 });
128 }
129 }
130 /** @type {(I | G)[]} */
131 const results = [];
132 for (;;) {
133 /** @type {Group<I, G> | undefined} */
134 let bestGroup;
135 let bestGroupSize = -1;
136 /** @type {Items<I, G> | undefined} */
137 let bestGroupItems;
138 /** @type {GroupOptions | false | undefined} */
139 let bestGroupOptions;
140 for (const [group, state] of groupMap) {
141 const { items, used } = state;
142 let options = state.options;
143 if (options === undefined) {
144 const groupConfig = group.config;
145 state.options = options =
146 (groupConfig.getOptions &&
147 groupConfig.getOptions(
148 group.name,
149 Array.from(items, ({ item }) => item)
150 )) ||
151 false;
152 }
153
154 const force = options && options.force;
155 if (!force) {
156 if (bestGroupOptions && bestGroupOptions.force) continue;
157 if (used) continue;
158 if (items.size <= 1 || totalSize - items.size <= 1) {
159 continue;
160 }
161 }

Callers 1

smartGroupingFunction · 0.85

Calls 7

getOptionsMethod · 0.65
addMethod · 0.45
valuesMethod · 0.45
setMethod · 0.45
deleteMethod · 0.45
getMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected