Skip to content

Commit 30f3a2c

Browse files
committed
feat(plugin-lighthouse): add onlyGroups support
1 parent 9da4e8a commit 30f3a2c

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

packages/plugin-lighthouse/src/lib/binding.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import {
1010
singleQuote,
1111
} from '@code-pushup/utils';
1212
import {
13+
LIGHTHOUSE_GROUP_SLUGS,
1314
LIGHTHOUSE_PLUGIN_SLUG,
1415
LIGHTHOUSE_PLUGIN_TITLE,
1516
} from './constants.js';
17+
import type { LighthouseGroupSlug } from './types.js';
1618

1719
const { name: PACKAGE_NAME } = createRequire(import.meta.url)(
1820
'../../package.json',
@@ -21,6 +23,13 @@ const { name: PACKAGE_NAME } = createRequire(import.meta.url)(
2123
const DEFAULT_URL = 'http://localhost:4200';
2224
const PLUGIN_VAR = 'lhPlugin';
2325

26+
const CATEGORY_TO_GROUP: Record<string, LighthouseGroupSlug> = {
27+
performance: 'performance',
28+
a11y: 'accessibility',
29+
'best-practices': 'best-practices',
30+
seo: 'seo',
31+
};
32+
2433
const CATEGORIES: CategoryCodegenConfig[] = [
2534
{
2635
slug: 'performance',
@@ -86,25 +95,26 @@ export const lighthouseSetupBinding = {
8695
generateConfig: (answers: Record<string, PluginAnswer>) => {
8796
const options = parseAnswers(answers);
8897
const hasCategories = options.categories.length > 0;
89-
const formattedUrls = formatUrls(options.urls);
9098
const imports = [
9199
{
92100
moduleSpecifier: PACKAGE_NAME,
93101
defaultImport: 'lighthousePlugin',
94102
...(hasCategories ? { namedImports: ['lighthouseGroupRefs'] } : {}),
95103
},
96104
];
105+
const pluginCall = formatPluginCall(options);
106+
97107
if (!hasCategories) {
98108
return {
99109
imports,
100-
pluginInit: [`lighthousePlugin(${formattedUrls}),`],
110+
pluginInit: [`${pluginCall},`],
101111
};
102112
}
103113
return {
104114
imports,
105115
pluginDeclaration: {
106116
identifier: PLUGIN_VAR,
107-
expression: `lighthousePlugin(${formattedUrls})`,
117+
expression: pluginCall,
108118
},
109119
pluginInit: [`${PLUGIN_VAR},`],
110120
categories: createCategories(options),
@@ -121,6 +131,19 @@ function parseAnswers(
121131
};
122132
}
123133

134+
function formatPluginCall({ urls, categories }: LighthouseOptions): string {
135+
const formattedUrls = formatUrls(urls);
136+
const groups = categories.flatMap(slug => {
137+
const group = CATEGORY_TO_GROUP[slug];
138+
return group ? [group] : [];
139+
});
140+
if (groups.length === 0 || groups.length === LIGHTHOUSE_GROUP_SLUGS.length) {
141+
return `lighthousePlugin(${formattedUrls})`;
142+
}
143+
const onlyGroups = groups.map(singleQuote).join(', ');
144+
return `lighthousePlugin(${formattedUrls}, { onlyGroups: [${onlyGroups}] })`;
145+
}
146+
124147
function formatUrls([first, ...rest]: [string, ...string[]]): string {
125148
if (rest.length === 0) {
126149
return singleQuote(first);

packages/plugin-lighthouse/src/lib/binding.unit.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,21 @@ describe('lighthouseSetupBinding', () => {
7373
]);
7474
});
7575

76+
it('should pass onlyGroups when not all categories are selected', () => {
77+
const { pluginDeclaration } = binding.generateConfig({
78+
...defaultAnswers,
79+
'lighthouse.categories': ['performance', 'seo'],
80+
});
81+
expect(pluginDeclaration!.expression).toContain(
82+
"onlyGroups: ['performance', 'seo']",
83+
);
84+
});
85+
86+
it('should omit onlyGroups when all categories are selected', () => {
87+
const { pluginDeclaration } = binding.generateConfig(defaultAnswers);
88+
expect(pluginDeclaration!.expression).not.toContain('onlyGroups');
89+
});
90+
7691
it('should use custom URL in plugin declaration', () => {
7792
expect(
7893
binding.generateConfig({

0 commit comments

Comments
 (0)