Skip to content
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion .github/scripts/lib/component-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,30 @@ function splitBuild(input, workingDir) {
return {outputs: result.metafile.outputs, byPath};
}

// Each node `start` reaches in a metafile graph (`outputs` or `inputs`),
// following only the given import kinds and skipping external imports, with
// its breadth-first level: the fewest edges from `start`, which is level 0.
function importLevels(graph, start, kinds) {
const levels = new Map();
if (start === undefined || !(start in graph)) return levels;
levels.set(start, 0);
const pending = [start];
for (let index = 0; index < pending.length; index += 1) {
const key = pending[index];
for (const imported of graph[key].imports || []) {
if (!kinds.includes(imported.kind) || imported.external) continue;
if (levels.has(imported.path) || !(imported.path in graph)) continue;
levels.set(imported.path, levels.get(key) + 1);
pending.push(imported.path);
}
}
return levels;
}

// Chunks an entry chunk reaches in a build's metafile outputs, following only
// the given import kinds.
// the given import kinds. The set's order is the order `eagerText`
// concatenates before gzip, so a change of order changes every gzip figure.
// It holds the same chunks as `importLevels` over the same kinds.
function reachableChunks(outputs, entryOutput, kinds) {
const seen = new Set();
const pending = [entryOutput];
Expand Down Expand Up @@ -454,6 +476,7 @@ module.exports = {
APP_EXTERNALS,
extractAtRef,
gzipBytes,
importLevels,
sizeBuildOptions,
reachableChunks,
eagerText,
Expand Down
40 changes: 40 additions & 0 deletions .github/scripts/lib/component-size.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ const {
bundleImport,
bundleImportsByEntry,
classNamesIn,
importLevels,
measureComponentImport,
packageStylesheetPath,
reachableChunks,
rulesForClasses,
} = require('./component-size.js');

Expand Down Expand Up @@ -48,6 +50,44 @@ afterEach(() => {
fs.rmSync(root, {recursive: true, force: true});
});

describe('chunk walks', () => {
// A metafile `outputs` graph: entry imports a and b statically, b imports c,
// a imports c, and entry loads lazy.js through import().
const outputs = {
'entry.js': {
imports: [
{path: 'a.js', kind: 'import-statement'},
{path: 'b.js', kind: 'import-statement'},
{path: 'lazy.js', kind: 'dynamic-import'},
{path: 'react', kind: 'import-statement', external: true},
],
},
'a.js': {imports: [{path: 'c.js', kind: 'import-statement'}]},
'b.js': {imports: [{path: 'c.js', kind: 'import-statement'}]},
'c.js': {imports: []},
'lazy.js': {imports: []},
};

it('keeps the eager chunks in the order the gzip figures concatenate', () => {
expect([
...reachableChunks(outputs, 'entry.js', ['import-statement']),
]).toEqual(['entry.js', 'b.js', 'c.js', 'a.js']);
});

it('levels the same chunks breadth-first from the start at 0', () => {
const levels = importLevels(outputs, 'entry.js', ['import-statement']);
expect(Object.fromEntries(levels)).toEqual({
'entry.js': 0,
'a.js': 1,
'b.js': 1,
'c.js': 2,
});
expect(new Set(levels.keys())).toEqual(
reachableChunks(outputs, 'entry.js', ['import-statement']),
);
});
});

describe('component import size', () => {
it('bundles every module the import reaches, package imports included', () => {
const root = tree({
Expand Down
Loading