@@ -10,9 +10,11 @@ import {
1010 singleQuote ,
1111} from '@code-pushup/utils' ;
1212import {
13+ LIGHTHOUSE_GROUP_SLUGS ,
1314 LIGHTHOUSE_PLUGIN_SLUG ,
1415 LIGHTHOUSE_PLUGIN_TITLE ,
1516} from './constants.js' ;
17+ import type { LighthouseGroupSlug } from './types.js' ;
1618
1719const { name : PACKAGE_NAME } = createRequire ( import . meta. url ) (
1820 '../../package.json' ,
@@ -21,6 +23,13 @@ const { name: PACKAGE_NAME } = createRequire(import.meta.url)(
2123const DEFAULT_URL = 'http://localhost:4200' ;
2224const 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+
2433const 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+
124147function formatUrls ( [ first , ...rest ] : [ string , ...string [ ] ] ) : string {
125148 if ( rest . length === 0 ) {
126149 return singleQuote ( first ) ;
0 commit comments