Skip to content

Commit 4addd10

Browse files
committed
refactor(plugin-lighthouse): consolidate category definitions in binding
1 parent 915c17c commit 4addd10

1 file changed

Lines changed: 26 additions & 32 deletions

File tree

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

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
LIGHTHOUSE_PLUGIN_SLUG,
1515
LIGHTHOUSE_PLUGIN_TITLE,
1616
} from './constants.js';
17-
import type { LighthouseGroupSlug } from './types.js';
1817

1918
const { name: PACKAGE_NAME } = createRequire(import.meta.url)(
2019
'../../package.json',
@@ -23,51 +22,37 @@ const { name: PACKAGE_NAME } = createRequire(import.meta.url)(
2322
const DEFAULT_URL = 'http://localhost:4200';
2423
const PLUGIN_VAR = 'lhPlugin';
2524

26-
const CATEGORY_TO_GROUP: Record<string, LighthouseGroupSlug> = {
27-
performance: 'performance',
28-
a11y: 'accessibility',
29-
'best-practices': 'best-practices',
30-
seo: 'seo',
31-
};
32-
33-
const CATEGORIES: CategoryCodegenConfig[] = [
25+
const CATEGORIES = [
3426
{
3527
slug: 'performance',
3628
title: 'Performance',
3729
description:
3830
'Measure performance and find opportunities to speed up page loads.',
39-
refsExpression: `lighthouseGroupRefs(${PLUGIN_VAR}, 'performance')`,
31+
group: 'performance',
4032
},
4133
{
4234
slug: 'a11y',
4335
title: 'Accessibility',
4436
description:
4537
'Determine if all users access content and navigate your site effectively.',
46-
refsExpression: `lighthouseGroupRefs(${PLUGIN_VAR}, 'accessibility')`,
38+
group: 'accessibility',
4739
},
4840
{
4941
slug: 'best-practices',
5042
title: 'Best Practices',
5143
description:
5244
'Improve code health of your web page following these best practices.',
53-
refsExpression: `lighthouseGroupRefs(${PLUGIN_VAR}, 'best-practices')`,
45+
group: 'best-practices',
5446
},
5547
{
5648
slug: 'seo',
5749
title: 'SEO',
5850
description:
5951
'Ensure that your page is optimized for search engine results ranking.',
60-
refsExpression: `lighthouseGroupRefs(${PLUGIN_VAR}, 'seo')`,
52+
group: 'seo',
6153
},
6254
];
6355

64-
const CATEGORY_CHOICES = CATEGORIES.map(({ slug, title }) => ({
65-
name: title,
66-
value: slug,
67-
}));
68-
69-
const DEFAULT_CATEGORIES = CATEGORIES.map(({ slug }) => slug);
70-
7156
type LighthouseOptions = {
7257
urls: [string, ...string[]];
7358
categories: string[];
@@ -88,8 +73,11 @@ export const lighthouseSetupBinding = {
8873
key: 'lighthouse.categories',
8974
message: 'Lighthouse categories',
9075
type: 'checkbox',
91-
choices: [...CATEGORY_CHOICES],
92-
default: [...DEFAULT_CATEGORIES],
76+
choices: CATEGORIES.map(({ slug, title }) => ({
77+
name: title,
78+
value: slug,
79+
})),
80+
default: CATEGORIES.map(({ slug }) => slug),
9381
},
9482
],
9583
generateConfig: (answers: Record<string, PluginAnswer>) => {
@@ -133,26 +121,32 @@ function parseAnswers(
133121

134122
function formatPluginCall({ urls, categories }: LighthouseOptions): string {
135123
const formattedUrls = formatUrls(urls);
136-
const groups = categories.flatMap(slug => {
137-
const group = CATEGORY_TO_GROUP[slug];
138-
return group ? [group] : [];
139-
});
124+
const groups = CATEGORIES.filter(({ slug }) => categories.includes(slug)).map(
125+
({ group }) => group,
126+
);
140127
if (groups.length === 0 || groups.length === LIGHTHOUSE_GROUP_SLUGS.length) {
141128
return `lighthousePlugin(${formattedUrls})`;
142129
}
143130
const onlyGroups = groups.map(singleQuote).join(', ');
144131
return `lighthousePlugin(${formattedUrls}, { onlyGroups: [${onlyGroups}] })`;
145132
}
146133

134+
function createCategories({
135+
categories,
136+
}: LighthouseOptions): CategoryCodegenConfig[] {
137+
return CATEGORIES.filter(({ slug }) => categories.includes(slug)).map(
138+
({ slug, title, description, group }) => ({
139+
slug,
140+
title,
141+
description,
142+
refsExpression: `lighthouseGroupRefs(${PLUGIN_VAR}, ${singleQuote(group)})`,
143+
}),
144+
);
145+
}
146+
147147
function formatUrls([first, ...rest]: [string, ...string[]]): string {
148148
if (rest.length === 0) {
149149
return singleQuote(first);
150150
}
151151
return `[${[first, ...rest].map(singleQuote).join(', ')}]`;
152152
}
153-
154-
function createCategories({
155-
categories,
156-
}: LighthouseOptions): CategoryCodegenConfig[] {
157-
return CATEGORIES.filter(({ slug }) => categories.includes(slug));
158-
}

0 commit comments

Comments
 (0)