Skip to content

Commit

Permalink
Improve size calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed May 12, 2023
1 parent d8b53b2 commit c9fe4b5
Show file tree
Hide file tree
Showing 21 changed files with 94 additions and 10 deletions.
2 changes: 1 addition & 1 deletion docs/configuration-options/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2345,7 +2345,7 @@ Set a minimal chunk size target in Byte for code-splitting setups. When this val
Larger values will try to merge any chunk below the limit into other chunks. In that case, it is accepted that entries may load some unnecessary code. The algorithm always tries to merge in a way that minimizes the amount of unnecessary code, though.
Unfortunately, due to the way chunking works, chunk size is measured before any chunk rendering plugins like minifiers ran, which means you should use a high enough limit to take this into account.
Unfortunately, due to the way chunking works, chunk size is measured before any chunk rendering plugins like minifiers ran, which means you should use a high enough limit to take this into account. When calculating the size, it will take tree-shaking of top-level statements into account, though.
### perf
Expand Down
11 changes: 11 additions & 0 deletions src/Module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,17 @@ export default class Module {
return error(properties);
}

// sum up the length of all ast nodes that are included
estimateSize(): number {
let size = 0;
for (const node of this.ast!.body) {
if (node.included) {
size += node.end - node.start;
}
}
return size;
}

getAllExportNames(): Set<string> {
if (this.allExportNames) {
return this.allExportNames;
Expand Down
7 changes: 3 additions & 4 deletions src/utils/chunkAssignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,9 @@ function getPartitionedChunks(
// rendering did not happen yet, but we can detect empty modules
if (module.isIncluded()) {
pure &&= !module.hasEffects();
size += module.originalCode.length;
// we use a trivial size for the default minChunkSize to improve
// performance
size += minChunkSize > 1 ? module.estimateSize() : 1;
}
}
chunkDescription.pure = pure;
Expand Down Expand Up @@ -613,7 +615,6 @@ function getPartitionedChunks(
};
}

// TODO Lukas improve size calculation
function addChunkDependenciesAndAtomsAndGetSideEffectAtoms(
chunkLists: ChunkDescription[][],
chunkByModule: Map<Module, ChunkDescription>,
Expand Down Expand Up @@ -652,8 +653,6 @@ function addChunkDependenciesAndAtomsAndGetSideEffectAtoms(
}
const { containedAtoms } = chunk;
for (const entryIndex of dependentEntries) {
// containedAtoms is mutated after destructuring
// eslint-disable-next-line unicorn/consistent-destructuring
atomsByEntry[entryIndex] |= containedAtoms;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = defineTest({
description: 'takes tree-shaking into account and ignores top-level comments',
options: {
input: ['main1.js', 'main2.js'],
output: {
experimentalMinChunkSize: 100
}
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
define(['exports'], (function (exports) { 'use strict';

console.log('shared');

const main = 'main';

exports.main = main;

}));
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
define(['./main1'], (function (main1) { 'use strict';

console.log('other');

}));
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

console.log('shared');

const main = 'main';

exports.main = main;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

require('./main1.js');

console.log('other');
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
console.log('shared');

const main = 'main';

export { main };
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import './main1.js';

console.log('other');
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
System.register([], (function (exports) {
'use strict';
return {
execute: (function () {

console.log('shared');

const main = exports('main', 'main');

})
};
}));
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
System.register(['./main1.js'], (function () {
'use strict';
return {
setters: [null],
execute: (function () {

console.log('other');

})
};
}));
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const main = 'main';
/* a very long comment exceeding 100 characters
-------------------------------------------- */
import './shared';
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import './shared';
console.log('other');
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
console.log('shared');
/* a very long comment exceeding 100 characters
-------------------------------------------- */
export const unused = 'also very long and very unused';
2 changes: 1 addition & 1 deletion test/cli/samples/empty-chunk-multiple/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ module.exports = defineTest({
description: 'shows warning when multiple chunks empty',
command: 'rollup -c',
error: () => true,
stderr: stderr => assertIncludes(stderr, '(!) Generated empty chunks\n"a" and "b"')
stderr: stderr => assertIncludes(stderr, '(!) Generated empty chunks\n"main1" and "main2"')
});
2 changes: 0 additions & 2 deletions test/cli/samples/empty-chunk-multiple/main.js

This file was deleted.

File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion test/cli/samples/empty-chunk-multiple/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
input: 'main.js',
input: ['main1.js', 'main2.js'],
output: {
format: 'cjs'
}
Expand Down
2 changes: 1 addition & 1 deletion test/function/samples/output-options-hook/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = defineTest({
entryFileNames: '[name].js',
esModule: 'if-default-prop',
experimentalDeepDynamicChunkOptimization: false,
experimentalMinChunkSize: 0,
experimentalMinChunkSize: 1,
exports: 'auto',
extend: false,
externalImportAssertions: true,
Expand Down

0 comments on commit c9fe4b5

Please sign in to comment.