Skip to content

Commit 63f0930

Browse files
committed
simplify size calculation
add "moduleSize" calculator, add multiply and overhead helper
1 parent aaee63a commit 63f0930

1 file changed

Lines changed: 21 additions & 24 deletions

File tree

lib/Chunk.js

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -261,16 +261,6 @@ class Chunk {
261261
this.modules.forEach(m => m.updateHash(hash));
262262
}
263263

264-
size(options) {
265-
const CHUNK_OVERHEAD = typeof options.chunkOverhead === "number" ? options.chunkOverhead : 10000;
266-
const ENTRY_CHUNK_MULTIPLICATOR = options.entryChunkMultiplicator || 10;
267-
268-
const modulesSize = this.modules.reduce((a, b) => {
269-
return a + b.size();
270-
}, 0);
271-
return modulesSize * (this.isInitial() ? ENTRY_CHUNK_MULTIPLICATOR : 1) + CHUNK_OVERHEAD;
272-
}
273-
274264
canBeIntegrated(other) {
275265
if(other.isInitial()) {
276266
return false;
@@ -283,26 +273,33 @@ class Chunk {
283273
return true;
284274
}
285275

276+
addMultiplierAndOverhead(size, options) {
277+
const overhead = typeof options.chunkOverhead === "number" ? options.chunkOverhead : 10000;
278+
const multiplicator = this.isInitial() ? (options.entryChunkMultiplicator || 10) : 1;
279+
280+
return size * multiplicator + overhead;
281+
}
282+
283+
modulesSize() {
284+
let count = 0;
285+
for(let i = 0; i < this.modules.length; i++) {
286+
count += this.modules[i].size();
287+
}
288+
return count;
289+
}
290+
291+
size(options) {
292+
return this.addMultiplierAndOverhead(this.modulesSize(), options);
293+
}
294+
286295
integratedSize(other, options) {
287296
// Chunk if it's possible to integrate this chunk
288297
if(!this.canBeIntegrated(other)) {
289298
return false;
290299
}
291300

292-
const CHUNK_OVERHEAD = typeof options.chunkOverhead === "number" ? options.chunkOverhead : 10000;
293-
const ENTRY_CHUNK_MULTIPLICATOR = options.entryChunkMultiplicator || 10;
294-
295-
const mergedModules = this.modules.slice();
296-
other.modules.forEach(m => {
297-
if(this.modules.indexOf(m) < 0) {
298-
mergedModules.push(m);
299-
}
300-
}, this);
301-
302-
const modulesSize = mergedModules.reduce((a, m) => {
303-
return a + m.size();
304-
}, 0);
305-
return modulesSize * (this.isInitial() || other.isInitial() ? ENTRY_CHUNK_MULTIPLICATOR : 1) + CHUNK_OVERHEAD;
301+
const modulesSize = this.modulesSize() + other.modulesSize();
302+
return this.addMultiplierAndOverhead(modulesSize, options);
306303
}
307304

308305
getChunkMaps(includeEntries, realHash) {

0 commit comments

Comments
 (0)