Skip to content

Commit b45f496

Browse files
committed
feat: add global selection options
1 parent 7e4630c commit b45f496

File tree

3 files changed

+83
-17
lines changed

3 files changed

+83
-17
lines changed

packages/plugin-bundle-stats/code-pushup.large-angular.config.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@ const groups = [
182182
patterns: ['**/packages/e2e-test-framework/**'],
183183
icon: '🧪',
184184
},
185+
{
186+
title: 'Packages',
187+
patterns: ['**/packages/**'],
188+
icon: '📦',
189+
},
185190
{ title: 'Node Modules', patterns: ['**/node_modules/**'], icon: '📦' },
186191
];
187192

@@ -259,6 +264,26 @@ const config = {
259264
},
260265
},
261266
},
267+
// Initial bundle size audit
268+
{
269+
title: 'Initial Bundle Size',
270+
slug: 'initial-bundle-size',
271+
selection: {
272+
includeOutputs: [
273+
'**/main.*.js',
274+
'**/polyfill.*.js',
275+
'**/styles.*.css',
276+
],
277+
excludeOutputs: ['**/*.map'],
278+
},
279+
scoring: {
280+
totalSize: 80000000,
281+
penalty: {
282+
artefactSize: [0, 3000000],
283+
blacklist: [],
284+
},
285+
},
286+
},
262287
],
263288
}),
264289
],

packages/plugin-bundle-stats/src/lib/runner/audits/selection.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import {
88
compilePattern as sharedCompilePattern,
99
} from './details/utils/match-pattern.js';
1010

11+
export type Include = {
12+
include: string[];
13+
exclude: string[];
14+
};
15+
1116
export type includeOutputs = {
1217
includeOutputs: string[];
1318
excludeOutputs: string[];
@@ -24,7 +29,7 @@ export type includeEntryPoints = {
2429
};
2530

2631
export type SelectionOptions = Partial<
27-
includeOutputs & includeInputs & includeEntryPoints
32+
Include & includeOutputs & includeInputs & includeEntryPoints
2833
> &
2934
(
3035
| Pick<includeOutputs, 'includeOutputs'>
@@ -248,6 +253,7 @@ export function compilePattern(pattern: string): PatternMatcher {
248253
/**
249254
* Compiles all selection patterns into matchers. Enables efficient pattern reuse.
250255
* Transforms string patterns into cached matcher functions for all selection criteria.
256+
* Merges global include/exclude patterns into all specific selection types.
251257
*
252258
* @param options - Selection options containing pattern arrays for all filter types
253259
* @returns Object with compiled pattern matchers for each selection criteria type
@@ -261,7 +267,27 @@ export function compileSelectionPatterns(
261267
): CompiledPatterns {
262268
const compiled: CompiledPatterns = {} as CompiledPatterns;
263269

264-
for (const [key, patterns] of Object.entries(options)) {
270+
// Extract global patterns
271+
const globalInclude = options.include || [];
272+
const globalExclude = options.exclude || [];
273+
274+
// Merge global patterns with specific patterns for each selection type
275+
const mergedOptions = {
276+
includeOutputs: [...(options.includeOutputs || []), ...globalInclude],
277+
excludeOutputs: [...(options.excludeOutputs || []), ...globalExclude],
278+
includeInputs: [...(options.includeInputs || []), ...globalInclude],
279+
excludeInputs: [...(options.excludeInputs || []), ...globalExclude],
280+
includeEntryPoints: [
281+
...(options.includeEntryPoints || []),
282+
...globalInclude,
283+
],
284+
excludeEntryPoints: [
285+
...(options.excludeEntryPoints || []),
286+
...globalExclude,
287+
],
288+
};
289+
290+
for (const [key, patterns] of Object.entries(mergedOptions)) {
265291
compiled[key as keyof SelectionOptions] = patterns.map(compilePattern);
266292
}
267293

packages/plugin-bundle-stats/src/lib/runner/audits/selection.unit.test.ts

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import {
1111
} from './selection.js';
1212

1313
const emptyPatterns = {
14+
include: [],
15+
exclude: [],
1416
includeOutputs: [],
1517
excludeOutputs: [],
1618
includeInputs: [],
@@ -268,12 +270,8 @@ describe('shouldSelectOutput', () => {
268270
bytes: 400,
269271
},
270272
{
273+
...emptyPatterns,
271274
includeEntryPoints: [vi.fn().mockReturnValue(true)],
272-
excludeEntryPoints: [],
273-
includeOutputs: [],
274-
excludeOutputs: [],
275-
includeInputs: [],
276-
excludeInputs: [],
277275
},
278276
),
279277
).toBe(false);
@@ -289,12 +287,8 @@ describe('shouldSelectOutput', () => {
289287
bytes: 400,
290288
},
291289
{
292-
includeEntryPoints: [],
293-
excludeEntryPoints: [],
290+
...emptyPatterns,
294291
includeOutputs: [includeOutputsMock],
295-
excludeOutputs: [],
296-
includeInputs: [],
297-
excludeInputs: [],
298292
},
299293
);
300294

@@ -312,12 +306,8 @@ describe('shouldSelectOutput', () => {
312306
bytes: 400,
313307
},
314308
{
315-
includeEntryPoints: [],
316-
excludeEntryPoints: [],
309+
...emptyPatterns,
317310
includeOutputs: [includeOutputsMock],
318-
excludeOutputs: [],
319-
includeInputs: [],
320-
excludeInputs: [],
321311
},
322312
);
323313

@@ -405,6 +395,31 @@ describe('compileSelectionPatterns', () => {
405395
excludeEntryPoints: [],
406396
});
407397
});
398+
399+
it('should merge global include/exclude patterns into all specific selection types', () => {
400+
const result = compileSelectionPatterns({
401+
include: ['src/**'],
402+
exclude: ['*.test.*'],
403+
includeOutputs: ['main.js'],
404+
excludeOutputs: ['dev.js'],
405+
includeInputs: ['components/**'],
406+
excludeInputs: ['temp.js'],
407+
});
408+
409+
// Verify that global patterns are merged with specific patterns
410+
expect(result.includeOutputs).toHaveLength(2); // main.js + src/**
411+
expect(result.excludeOutputs).toHaveLength(2); // dev.js + *.test.*
412+
expect(result.includeInputs).toHaveLength(2); // components/** + src/**
413+
expect(result.excludeInputs).toHaveLength(2); // temp.js + *.test.*
414+
expect(result.includeEntryPoints).toHaveLength(1); // only src/**
415+
expect(result.excludeEntryPoints).toHaveLength(1); // only *.test.*
416+
417+
// Test that merged patterns work correctly
418+
expect(result.includeOutputs[0]!('main.js')).toBe(true);
419+
expect(result.includeOutputs[1]!('src/utils.js')).toBe(true);
420+
expect(result.excludeOutputs[0]!('dev.js')).toBe(true);
421+
expect(result.excludeOutputs[1]!('main.test.js')).toBe(true);
422+
});
408423
});
409424

410425
describe('clearSelectionCaches', () => {

0 commit comments

Comments
 (0)