@code-pushup/lighthouse-plugin 0.42.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CONTRIBUTING.md +117 -0
- package/README.md +203 -0
- package/index.js +1530 -0
- package/package.json +51 -0
- package/src/index.d.ts +7 -0
- package/src/lib/constants.d.ts +2 -0
- package/src/lib/lighthouse-plugin.d.ts +3 -0
- package/src/lib/normalize-flags.d.ts +20 -0
- package/src/lib/runner/constants.d.ts +21 -0
- package/src/lib/runner/index.d.ts +3 -0
- package/src/lib/runner/runner.d.ts +3 -0
- package/src/lib/runner/types.d.ts +10 -0
- package/src/lib/runner/utils.d.ts +20 -0
- package/src/lib/types.d.ts +7 -0
- package/src/lib/utils.d.ts +18 -0
package/CONTRIBUTING.md
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
# Contributing
|
|
2
|
+
|
|
3
|
+
## Setup
|
|
4
|
+
|
|
5
|
+
Make sure to install dependencies:
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
### Chrome path
|
|
12
|
+
|
|
13
|
+
In this plugin we provide Lighthouse functionality exposed over the `lighthousePlugin`.
|
|
14
|
+
To test lighthouse properly we work with a predefined testing setup.
|
|
15
|
+
|
|
16
|
+
On some OS there could be a problem finding the path to Chrome.
|
|
17
|
+
|
|
18
|
+
We try to detect it automatically in the [`chrome-path.setup.ts` script](../../testing/test-setup/src/lib/chrome-path.setup.ts).
|
|
19
|
+
There we use `getChromePath` and have `chromium` installed as NPM package, so detecting the path should not cause any problem.
|
|
20
|
+
|
|
21
|
+
However, if no chrome path is detected automatically the error looks like this:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
Runtime error encountered: No Chrome installations found.
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
To prevent this from happening you have to provide the path manually in your `.env`:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
CUSTOM_CHROME_PATH=/Applications/Google Chrome.app/Contents/MacOS/Google Chrome
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
In the CI you can set a static path if needed over the env variable like this:
|
|
34
|
+
|
|
35
|
+
```yml
|
|
36
|
+
# ...
|
|
37
|
+
- name: Set custom Chrome path for Windows only
|
|
38
|
+
if: matrix.os == 'windows-latest'
|
|
39
|
+
run: |
|
|
40
|
+
echo "CUSTOM_CHROME_PATH=C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe" | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8
|
|
41
|
+
shell: pwsh
|
|
42
|
+
|
|
43
|
+
# Optional debug log
|
|
44
|
+
- name: Log all environment variables
|
|
45
|
+
run: printenv
|
|
46
|
+
# ...
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
We consider this path in our `beforeAll` hook in a [`chrome-path.setup.ts` script](../../testing/test-setup/src/lib/chrome-path.setup.ts).
|
|
50
|
+
|
|
51
|
+
### Testing chrome flags
|
|
52
|
+
|
|
53
|
+
1. run `npx chrome-debug --<chromium-flag>` to pass terminal arguments to Chrome. E.g. `npx chrome-debug --headless=shell`.
|
|
54
|
+
`npx chrome-debug --headless=shell --@TODO-PUT-OTHER-EXAMPLE-FOR-FLAG`
|
|
55
|
+
|
|
56
|
+
For a full list of available flags check out [this document](https://peter.sh/experiments/chromium-command-line-switches/).
|
|
57
|
+
|
|
58
|
+
> [!NOTE]
|
|
59
|
+
> To pass chrome flags to lighthouse you have to provide them under `--chrome-flags="<chrome-flags-as-array>"`.
|
|
60
|
+
> E.g. `lighthouse https://example.com --chrome-flage="--headless=shell"`
|
|
61
|
+
|
|
62
|
+
2. Check if the flag got accepted. This is quite unintuitive as we would expect the passed flag to be visible under `chrome://flags/` but as you can see in the screenshot it is not visible.
|
|
63
|
+
<img width="1202" alt="chrome-flags" src="./docs/images/chrome-flags.png">
|
|
64
|
+
Instead open `chrome://version/` and look under the "Command Line" section.
|
|
65
|
+
<img width="1202" alt="chrome-chrome-version" src="./docs/images/chrome-version.png">
|
|
66
|
+
|
|
67
|
+
### Chrome User Data
|
|
68
|
+
|
|
69
|
+
To bootstrap Chrome with a predefined for setting we have to provide a couple of config files that we located under `<project-root>/mock/chromium-user-data`.
|
|
70
|
+
When executing Lighthouse we provide the path to this folder over the `Flag` object.
|
|
71
|
+
|
|
72
|
+
To generate initialise or edit the file structure under `chromium-user-data` do the following steps:
|
|
73
|
+
|
|
74
|
+
1. Spin up Chrome by running `npx chrome-debug --user-data-dir=./packages/plugin-lighthouse/mock/chromium-user-data`
|
|
75
|
+
<img width="1202" alt="chrome-blank-screen" src="./docs/images/chrome-blank-screen.png">
|
|
76
|
+
|
|
77
|
+
2. If you do this the first time you should already see content under `<project-root>/mock/chromium-user-data`
|
|
78
|
+
3. Edit the configuration over the Chrome UI. E.g. adding a profile
|
|
79
|
+
4. Close chromium and open it again, and you should see chromium bootstraps as the configured user
|
|
80
|
+
<img width="1202" alt="chrome-blank-screen-pre-configured" src="./docs/images/chrome-blank-screen-pre-configure.png">
|
|
81
|
+
|
|
82
|
+
To reset the above just delete the folder and apply the settings again.
|
|
83
|
+
|
|
84
|
+
_A helpful chromium setup is preconfigured with the following settings:_
|
|
85
|
+
|
|
86
|
+
- A user profile is set up. This enables certain debugging related options as well as help to visually distinguish between test setups as the header bar is colored.
|
|
87
|
+
<img width="1202" alt="chrome-settings-manage-profile" src="./docs/images/chrome-settings-manage-profile.png">
|
|
88
|
+
|
|
89
|
+
#### Resources
|
|
90
|
+
|
|
91
|
+
- [chromium flags guide](https://www.chromium.org/developers/how-tos/run-chromium-with-flags/)
|
|
92
|
+
|
|
93
|
+
## Troubleshooting
|
|
94
|
+
|
|
95
|
+
1. Verify Chrome Installation
|
|
96
|
+
Ensure Chrome is correctly installed and accessible to the Lighthouse process.
|
|
97
|
+
Run `npx chrome-debug` to test it. Read further under [chrome-path](#chrome-path)
|
|
98
|
+
|
|
99
|
+
2. Increase Timeout
|
|
100
|
+
Lighthouse has a longer runtime which can time out in different environments.
|
|
101
|
+
**Try increasing the test timeout** in `lighthouse-plugin.integration.test.ts` for `runner creation and execution` test suite.
|
|
102
|
+
|
|
103
|
+
3. Turn on debug mode
|
|
104
|
+
Show debug logs of Lighthouse. Set the following environment variable: `DEBUG='*'`
|
|
105
|
+
|
|
106
|
+
4. Understand error messages (⏳ could also be because of timeout problems :D )
|
|
107
|
+
|
|
108
|
+
- Lighthouse error - `Runtime error encountered: No Chrome installations found.`
|
|
109
|
+
Read further under [chrome-path](#chrome-path)
|
|
110
|
+
- Could not find `report.json` (⏳)
|
|
111
|
+

|
|
112
|
+
- Lighthouse Error - `Could Not Connect to Chrome` (⏳)
|
|
113
|
+

|
|
114
|
+
Your Chrome path is set incorrectly. Read further under [chrome-path](#chrome-path)
|
|
115
|
+
- Lighthouse Error - `start lh:<any>:<performancemark>" performance mark has not been set` (⏳)
|
|
116
|
+

|
|
117
|
+
If this error pops up you are able to launch Chrome but had problems to communicate over the ports.
|
package/README.md
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
# @code-pushup/lighthouse-plugin
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@code-pushup/lighthouse-plugin)
|
|
4
|
+
[](https://npmtrends.com/@code-pushup/lighthouse-plugin)
|
|
5
|
+
[](https://www.npmjs.com/package/@code-pushup/lighthouse-plugin?activeTab=dependencies)
|
|
6
|
+
|
|
7
|
+
🕵️ **Code PushUp plugin for measuring web performance and quality with Lighthouse.** 🔥
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
The plugin parses your Lighthouse configuration and lints all audits of the official [Lighthouse](https://github.com/GoogleChrome/lighthouse/blob/main/readme.md#lighthouse-------) CLI.
|
|
12
|
+
|
|
13
|
+
Detected Lighthouse audits are mapped to Code PushUp audits. Audit reports are calculated based on the [original implementation](https://googlechrome.github.io/lighthouse/scorecalc/).
|
|
14
|
+
Additionally, Lighthouse categories are mapped to Code PushUp groups which can make it easier to assemble the categories.
|
|
15
|
+
|
|
16
|
+
For more infos visit the [official docs](https://developer.chrome.com/docs/lighthouse/overview).
|
|
17
|
+
|
|
18
|
+
## Getting started
|
|
19
|
+
|
|
20
|
+
1. If you haven't already, install [@code-pushup/cli](../cli/README.md) and create a configuration file.
|
|
21
|
+
|
|
22
|
+
2. Install as a dev dependency with your package manager:
|
|
23
|
+
|
|
24
|
+
```sh
|
|
25
|
+
npm install --save-dev @code-pushup/lighthouse-plugin
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
```sh
|
|
29
|
+
yarn add --dev @code-pushup/lighthouse-plugin
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
```sh
|
|
33
|
+
pnpm add --save-dev @code-pushup/lighthouse-plugin
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
3. Add this plugin to the `plugins` array in your Code PushUp CLI config file (e.g. `code-pushup.config.ts`).
|
|
37
|
+
|
|
38
|
+
Pass in the URL you want to measure, along with optional [flags](#flags) and [config](#config) data.
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import lighthousePlugin from '@code-pushup/lighthouse-plugin';
|
|
42
|
+
|
|
43
|
+
export default {
|
|
44
|
+
// ...
|
|
45
|
+
plugins: [
|
|
46
|
+
// ...
|
|
47
|
+
await lighthousePlugin('https://example.com'),
|
|
48
|
+
],
|
|
49
|
+
};
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
4. Run the CLI with `npx code-pushup collect` and view or upload the report (refer to [CLI docs](../cli/README.md)).
|
|
53
|
+
|
|
54
|
+
### Optionally set up categories
|
|
55
|
+
|
|
56
|
+
Reference audits (or groups) which you wish to include in custom categories (use `npx code-pushup print-config --onlyPlugins=lighthouse` to list audits and groups).
|
|
57
|
+
|
|
58
|
+
Assign weights based on what influence each Lighthouse audit has on the overall category score (assign weight 0 to only include as extra info, without influencing category score).
|
|
59
|
+
The plugin exports the helper `lighthouseAuditRef` and `lighthouseGroupRef` to reference Lighthouse category references for audits and groups.
|
|
60
|
+
|
|
61
|
+
#### Reference audits directly with `lighthouseGroupRef`
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
import { lighthouseGroupRef } from './utils';
|
|
65
|
+
|
|
66
|
+
export default {
|
|
67
|
+
// ...
|
|
68
|
+
categories: [
|
|
69
|
+
{
|
|
70
|
+
slug: 'performance',
|
|
71
|
+
title: 'Performance',
|
|
72
|
+
refs: [lighthouseGroupRef('performance')],
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
slug: 'a11y',
|
|
76
|
+
title: 'Accessibility',
|
|
77
|
+
refs: [lighthouseGroupRef('accessibility')],
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
slug: 'best-practices',
|
|
81
|
+
title: 'Best Practices',
|
|
82
|
+
refs: [lighthouseGroupRef('best-practices')],
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
slug: 'seo',
|
|
86
|
+
title: 'SEO',
|
|
87
|
+
refs: [lighthouseGroupRef('seo')],
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
slug: 'pwa',
|
|
91
|
+
title: 'PWA',
|
|
92
|
+
isBinary: true,
|
|
93
|
+
refs: [lighthouseGroupRef('pwa')],
|
|
94
|
+
},
|
|
95
|
+
],
|
|
96
|
+
};
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
#### Reference groups with `lighthouseAuditRef`
|
|
100
|
+
|
|
101
|
+
The Lighthouse categories are reflected as groups.
|
|
102
|
+
Referencing individual audits offers more granularity. However, keep maintenance costs of a higher number of audits in mind as well.
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
import { lighthouseAuditRef } from './utils';
|
|
106
|
+
|
|
107
|
+
export default {
|
|
108
|
+
// ...
|
|
109
|
+
categories: [
|
|
110
|
+
{
|
|
111
|
+
slug: 'pwa',
|
|
112
|
+
title: 'PWA',
|
|
113
|
+
isBinary: true,
|
|
114
|
+
refs: [lighthouseAuditRef('installable-manifest', 2), lighthouseAuditRef('splash-screen', 1), lighthouseAuditRef('themed-omnibox', 1), lighthouseAuditRef('content-width', 1), lighthouseAuditRef('themed-omnibox', 2), lighthouseAuditRef('viewport', 2), lighthouseAuditRef('maskable-icon', 1), lighthouseAuditRef('pwa-cross-browser', 0), lighthouseAuditRef('pwa-page-transitions', 0), lighthouseAuditRef('pwa-each-page-has-url', 0)],
|
|
115
|
+
},
|
|
116
|
+
],
|
|
117
|
+
};
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Flags
|
|
121
|
+
|
|
122
|
+
The plugin accepts a second optional argument, `flags`.
|
|
123
|
+
|
|
124
|
+
`flags` is the Lighthouse [CLI flags](https://github.com/GoogleChrome/lighthouse/blob/7d80178c37a1b600ea8f092fc0b098029799a659/cli/cli-flags.js#L80) as a JS object.
|
|
125
|
+
|
|
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.
|
|
127
|
+
|
|
128
|
+
For a complete list the [official documentation of CLI flags](https://github.com/GoogleChrome/lighthouse/blob/main/readme.md#cli-options)
|
|
129
|
+
|
|
130
|
+
> [!TIP]
|
|
131
|
+
> If you are not used to work with the Lighthouse CLI you would pass flags like this:
|
|
132
|
+
> `lighthouse https://example.com --output=json --chromeFlags='--headless=shell'`
|
|
133
|
+
>
|
|
134
|
+
> Now with the plugin it would look like this:
|
|
135
|
+
>
|
|
136
|
+
> ```ts
|
|
137
|
+
> // code-pushup.config.ts
|
|
138
|
+
> ...
|
|
139
|
+
> lighthousePlugin('https://example.com', { output: 'json', chromeFlags: ['--headless=shell']});
|
|
140
|
+
> ```
|
|
141
|
+
|
|
142
|
+
> [!note]
|
|
143
|
+
> The following flags are **not supported** in the current implementation:
|
|
144
|
+
>
|
|
145
|
+
> - `list-all-audits` - Prints a list of all available audits and exits. Alternative: `npx code-pushup print-config --onlyPlugins lighthouse`
|
|
146
|
+
> - `list-locales` - Prints a list of all supported locales and exits.
|
|
147
|
+
> - `list-trace-categories` - Prints a list of all required trace categories and exits.
|
|
148
|
+
> - `view` - Open HTML report in your browser
|
|
149
|
+
|
|
150
|
+
## Chrome Flags for Tooling
|
|
151
|
+
|
|
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.
|
|
154
|
+
|
|
155
|
+
> ```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 });
|
|
160
|
+
> ```
|
|
161
|
+
|
|
162
|
+
## Config
|
|
163
|
+
|
|
164
|
+
The plugin accepts a third optional argument, `config`.
|
|
165
|
+
|
|
166
|
+
`config` is the Lighthouse [configuration](https://github.com/GoogleChrome/lighthouse/blob/7d80178c37a1b600ea8f092fc0b098029799a659/types/config.d.ts#L21) as a JS object.
|
|
167
|
+
|
|
168
|
+
For a complete guide on Lighthouse configuration read the [official documentation on configuring](https://github.com/GoogleChrome/lighthouse/blob/main/docs/configuration.md)
|
|
169
|
+
|
|
170
|
+
> [!TIP]
|
|
171
|
+
> If you are not used to work with the Lighthouse CLI you would pass a config like this:
|
|
172
|
+
> `lighthouse --config-path=path/to/custom-config.js https://example.com`
|
|
173
|
+
>
|
|
174
|
+
> And in a separate file you would place the following object:
|
|
175
|
+
>
|
|
176
|
+
> ```typescript
|
|
177
|
+
> // custom-config.js file
|
|
178
|
+
> export default {
|
|
179
|
+
> extends: 'lighthouse:default',
|
|
180
|
+
> settings: {
|
|
181
|
+
> onlyAudits: ['first-meaningful-paint', 'speed-index', 'interactive'],
|
|
182
|
+
> },
|
|
183
|
+
> };
|
|
184
|
+
> ```
|
|
185
|
+
>
|
|
186
|
+
> Now with the plugin it would look like this:
|
|
187
|
+
>
|
|
188
|
+
> ```ts
|
|
189
|
+
> // code-pushup.config.ts
|
|
190
|
+
> ...
|
|
191
|
+
> lighthousePlugin('https://example.com', undefined, {
|
|
192
|
+
> extends: 'lighthouse:default',
|
|
193
|
+
> settings: {
|
|
194
|
+
> onlyAudits: [
|
|
195
|
+
> 'first-meaningful-paint',
|
|
196
|
+
> 'speed-index',
|
|
197
|
+
> 'interactive',
|
|
198
|
+
> ],
|
|
199
|
+
> }
|
|
200
|
+
> })
|
|
201
|
+
> ```
|
|
202
|
+
|
|
203
|
+
If you want to contribute, please refer to [CONTRIBUTING.md](./CONTRIBUTING.md).
|