Skip to content

Commit 2518b6c

Browse files
authored
feat(plugin-lighthouse): export default Chrome flags
1 parent 149f54b commit 2518b6c

File tree

8 files changed

+62
-27
lines changed

8 files changed

+62
-27
lines changed

code-pushup.preset.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { DEFAULT_FLAGS } from 'chrome-launcher/dist/flags.js';
21
import coveragePlugin, {
32
getNxCoveragePaths,
43
} from './dist/packages/plugin-coverage';
@@ -107,11 +106,7 @@ export const lighthouseCoreConfig = async (
107106
url: string,
108107
): Promise<CoreConfig> => {
109108
return {
110-
plugins: [
111-
await lighthousePlugin(url, {
112-
chromeFlags: DEFAULT_FLAGS.concat(['--headless']),
113-
}),
114-
],
109+
plugins: [await lighthousePlugin(url)],
115110
categories: lighthouseCategories,
116111
};
117112
};

e2e/plugin-lighthouse-e2e/mocks/fixtures/code-pushup.config.lh-default.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { DEFAULT_FLAGS } from 'chrome-launcher/dist/flags.js';
21
import lighthousePlugin, {
2+
DEFAULT_CHROME_FLAGS,
33
lighthouseGroupRef,
44
} from '@code-pushup/lighthouse-plugin';
55
import type { CoreConfig } from '@code-pushup/models';
@@ -18,7 +18,7 @@ export default {
1818
// seo category
1919
`hreflang`,
2020
],
21-
chromeFlags: DEFAULT_FLAGS.concat([`--headless`, `--verbose`]),
21+
chromeFlags: DEFAULT_CHROME_FLAGS.concat([`--verbose`]),
2222
}),
2323
],
2424
categories: [

packages/plugin-lighthouse/README.md

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -119,24 +119,27 @@ export default {
119119

120120
## Flags
121121

122-
The plugin accepts a second optional argument, `flags`.
122+
The plugin accepts an optional second argument, `flags`.
123123

124-
`flags` is the Lighthouse [CLI flags](https://github.com/GoogleChrome/lighthouse/blob/7d80178c37a1b600ea8f092fc0b098029799a659/cli/cli-flags.js#L80) as a JS object.
124+
`flags` is a JavaScript object containing Lighthouse [CLI flags](https://github.com/GoogleChrome/lighthouse/blob/7d80178c37a1b600ea8f092fc0b098029799a659/cli/cli-flags.js#L80).
125125

126-
Within the flags object a couple of other external configuration files can be referenced. E.g. `configPath` , `preset` or `budgetPath` reference external `json` or JavaScript files.
126+
Within the `flags` object, external configuration files can be referenced using options like `configPath` , `preset`, or `budgetPath`. These options allow Lighthouse to load custom configurations, audit presets, or performance budgets from external `json` or JavaScript files.
127127

128-
For a complete list the [official documentation of CLI flags](https://github.com/GoogleChrome/lighthouse/blob/main/readme.md#cli-options)
128+
For a complete list of available options, refer to [the official Lighthouse documentation](https://github.com/GoogleChrome/lighthouse/blob/main/readme.md#cli-options).
129129

130130
> [!TIP]
131-
> If you are not used to work with the Lighthouse CLI you would pass flags like this:
131+
> If you are new to working with the Lighthouse CLI, flags can be passed like this:
132132
> `lighthouse https://example.com --output=json --chromeFlags='--headless=shell'`
133133
>
134-
> Now with the plugin it would look like this:
134+
> With the plugin, the configuration would be:
135135
>
136136
> ```ts
137137
> // code-pushup.config.ts
138138
> ...
139-
> lighthousePlugin('https://example.com', { output: 'json', chromeFlags: ['--headless=shell']});
139+
> lighthousePlugin('https://example.com', {
140+
> output: 'json',
141+
> chromeFlags: ['--headless=shell'],
142+
> });
140143
> ```
141144
142145
> [!note]
@@ -149,14 +152,47 @@ For a complete list the [official documentation of CLI flags](https://github.com
149152
150153
## Chrome Flags for Tooling
151154
152-
We recommend using Chrome flags for more stable runs in a tooling environment.
153-
The [`chrome-launcher`](https://www.npmjs.com/package/chrome-launcher) package provides a set of flags dedicated to tooling that they also documented very well.
155+
We recommend using Chrome flags for more stable runs in a tooling environment. The [`chrome-launcher`](https://www.npmjs.com/package/chrome-launcher) package offers a well-documented set of flags specifically designed to ensure reliable execution.
156+
157+
The latest version of `@code-pushup/lighthouse-plugin` provides `DEFAULT_CHROME_FLAGS`, a pre-configured constant that includes Chrome’s default flags for stable, headless execution out of the box. This means you do not need to specify `chromeFlags` manually unless you want to modify them.
158+
159+
### Default Usage
160+
161+
If no `chromeFlags` are provided, the plugin automatically applies the default configuration:
154162
155163
> ```ts
156-
> // code-pushup.config.ts
157-
> import { DEFAULT_FLAGS } from 'chrome-launcher/dist/flags.js';
158-
> ...
159-
> lighthousePlugin('https://example.com', { output: 'json', chromeFlags: DEFAULT_FLAGS });
164+
> import lighthousePlugin from '@code-pushup/lighthouse-plugin';
165+
>
166+
> lighthousePlugin('https://example.com', {
167+
> output: 'json',
168+
> // Defaults to DEFAULT_CHROME_FLAGS internally
169+
> });
170+
> ```
171+
172+
### Adding Extra Flags
173+
174+
If additional Chrome flags are required (e.g., verbose logging or debugging), they can be appended to the default flags:
175+
176+
> ```ts
177+
> import lighthousePlugin, { DEFAULT_CHROME_FLAGS } from '@code-pushup/lighthouse-plugin';
178+
>
179+
> lighthousePlugin('https://example.com', {
180+
> output: 'json',
181+
> chromeFlags: DEFAULT_CHROME_FLAGS.concat(['--verbose']),
182+
> });
183+
> ```
184+
185+
### Overriding Default Flags
186+
187+
To completely override the default flags and provide a custom configuration:
188+
189+
> ```ts
190+
> import lighthousePlugin from '@code-pushup/lighthouse-plugin';
191+
>
192+
> lighthousePlugin('https://example.com', {
193+
> output: 'json',
194+
> chromeFlags: ['--verbose'],
195+
> });
160196
> ```
161197
162198
## Config

packages/plugin-lighthouse/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"@code-pushup/models": "0.51.0",
88
"@code-pushup/utils": "0.51.0",
99
"ansis": "^3.3.0",
10+
"chrome-launcher": "^1.1.1",
1011
"lighthouse": "^12.0.0",
1112
"lighthouse-logger": "2.0.1"
1213
}

packages/plugin-lighthouse/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { lighthousePlugin } from './lib/lighthouse-plugin';
22

33
export { LIGHTHOUSE_REPORT_NAME } from './lib/runner';
44
export {
5+
DEFAULT_CHROME_FLAGS,
56
LIGHTHOUSE_PLUGIN_SLUG,
67
LIGHTHOUSE_OUTPUT_PATH,
78
} from './lib/constants';

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
import { DEFAULT_FLAGS } from 'chrome-launcher/dist/flags.js';
12
import { join } from 'node:path';
23
import { DEFAULT_PERSIST_OUTPUT_DIR } from '@code-pushup/models';
34

5+
// headless is needed to pass CI on Linux and Windows (locally it works without headless too)
6+
export const DEFAULT_CHROME_FLAGS = [...DEFAULT_FLAGS, '--headless'];
7+
48
export const LIGHTHOUSE_PLUGIN_SLUG = 'lighthouse';
59
export const LIGHTHOUSE_OUTPUT_PATH = join(
610
DEFAULT_PERSIST_OUTPUT_DIR,

packages/plugin-lighthouse/src/lib/normalize-flags.unit.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { join } from 'node:path';
33
import { describe, expect, it } from 'vitest';
44
import { getLogMessages } from '@code-pushup/test-utils';
55
import { ui } from '@code-pushup/utils';
6-
import { LIGHTHOUSE_OUTPUT_PATH } from './constants';
6+
import { DEFAULT_CHROME_FLAGS, LIGHTHOUSE_OUTPUT_PATH } from './constants';
77
import { logUnsupportedFlagsInUse, normalizeFlags } from './normalize-flags';
88
import { LIGHTHOUSE_REPORT_NAME } from './runner/constants';
99
import type { LighthouseOptions } from './types';
@@ -46,8 +46,7 @@ describe('normalizeFlags', () => {
4646
const normalizedDefaults = {
4747
verbose: false,
4848
saveAssets: false,
49-
// needed to pass CI on linux and windows (locally it works without headless too)
50-
chromeFlags: ['--headless=shell'],
49+
chromeFlags: DEFAULT_CHROME_FLAGS,
5150
port: 0,
5251
hostname: '127.0.0.1',
5352
view: false,

packages/plugin-lighthouse/src/lib/runner/constants.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from 'lighthouse';
88
import { join } from 'node:path';
99
import type { Audit, Group } from '@code-pushup/models';
10-
import { LIGHTHOUSE_OUTPUT_PATH } from '../constants';
10+
import { DEFAULT_CHROME_FLAGS, LIGHTHOUSE_OUTPUT_PATH } from '../constants';
1111

1212
const { audits, categories } = defaultConfig;
1313

@@ -89,8 +89,7 @@ export const DEFAULT_CLI_FLAGS = {
8989
// https://github.com/GoogleChrome/lighthouse/blob/7d80178c37a1b600ea8f092fc0b098029799a659/cli/cli-flags.js#L80
9090
verbose: false,
9191
saveAssets: false,
92-
// needed to pass CI on linux and windows (locally it works without headless too)
93-
chromeFlags: ['--headless=shell'],
92+
chromeFlags: DEFAULT_CHROME_FLAGS,
9493
port: 0,
9594
hostname: '127.0.0.1',
9695
view: false,

0 commit comments

Comments
 (0)