|
| 1 | +# @code-pushup/typescript-plugin |
| 2 | + |
| 3 | +[](https://www.npmjs.com/package/@code-pushup/typescript-plugin) |
| 4 | +[](https://npmtrends.com/@code-pushup/typescript-plugin) |
| 5 | +[](https://www.npmjs.com/package/@code-pushup/typescript-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/typescript/blob/main/readme.md#typescript-------) 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/typescript/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/typescript/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/typescript-plugin |
| 26 | + ``` |
| 27 | + |
| 28 | + ```sh |
| 29 | + yarn add --dev @code-pushup/typescript-plugin |
| 30 | + ``` |
| 31 | + |
| 32 | + ```sh |
| 33 | + pnpm add --save-dev @code-pushup/typescript-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 typescriptPlugin from '@code-pushup/typescript-plugin'; |
| 42 | + |
| 43 | + export default { |
| 44 | + // ... |
| 45 | + plugins: [ |
| 46 | + // ... |
| 47 | + await typescriptPlugin('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=typescript` 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 `typescriptAuditRef` and `typescriptGroupRef` to reference Lighthouse category references for audits and groups. |
| 60 | + |
| 61 | +#### Reference audits directly with `typescriptGroupRef` |
| 62 | + |
| 63 | +```ts |
| 64 | +import { typescriptGroupRef } from './utils'; |
| 65 | + |
| 66 | +export default { |
| 67 | + // ... |
| 68 | + categories: [ |
| 69 | + { |
| 70 | + slug: 'performance', |
| 71 | + title: 'Performance', |
| 72 | + refs: [typescriptGroupRef('performance')], |
| 73 | + }, |
| 74 | + { |
| 75 | + slug: 'a11y', |
| 76 | + title: 'Accessibility', |
| 77 | + refs: [typescriptGroupRef('accessibility')], |
| 78 | + }, |
| 79 | + { |
| 80 | + slug: 'best-practices', |
| 81 | + title: 'Best Practices', |
| 82 | + refs: [typescriptGroupRef('best-practices')], |
| 83 | + }, |
| 84 | + { |
| 85 | + slug: 'seo', |
| 86 | + title: 'SEO', |
| 87 | + refs: [typescriptGroupRef('seo')], |
| 88 | + }, |
| 89 | + { |
| 90 | + slug: 'pwa', |
| 91 | + title: 'PWA', |
| 92 | + isBinary: true, |
| 93 | + refs: [typescriptGroupRef('pwa')], |
| 94 | + }, |
| 95 | + ], |
| 96 | +}; |
| 97 | +``` |
| 98 | + |
| 99 | +#### Reference groups with `typescriptAuditRef` |
| 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 { typescriptAuditRef } from './utils'; |
| 106 | + |
| 107 | +export default { |
| 108 | + // ... |
| 109 | + categories: [ |
| 110 | + { |
| 111 | + slug: 'pwa', |
| 112 | + title: 'PWA', |
| 113 | + isBinary: true, |
| 114 | + refs: [typescriptAuditRef('installable-manifest', 2), typescriptAuditRef('splash-screen', 1), typescriptAuditRef('themed-omnibox', 1), typescriptAuditRef('content-width', 1), typescriptAuditRef('themed-omnibox', 2), typescriptAuditRef('viewport', 2), typescriptAuditRef('maskable-icon', 1), typescriptAuditRef('pwa-cross-browser', 0), typescriptAuditRef('pwa-page-transitions', 0), typescriptAuditRef('pwa-each-page-has-url', 0)], |
| 115 | + }, |
| 116 | + ], |
| 117 | +}; |
| 118 | +``` |
| 119 | + |
| 120 | +## Flags |
| 121 | + |
| 122 | +The plugin accepts an optional second argument, `flags`. |
| 123 | + |
| 124 | +`flags` is a JavaScript object containing Lighthouse [CLI flags](https://github.com/GoogleChrome/typescript/blob/7d80178c37a1b600ea8f092fc0b098029799a659/cli/cli-flags.js#L80). |
| 125 | + |
| 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. |
| 127 | + |
| 128 | +For a complete list of available options, refer to [the official Lighthouse documentation](https://github.com/GoogleChrome/typescript/blob/main/readme.md#cli-options). |
| 129 | + |
| 130 | +> [!TIP] |
| 131 | +> If you are new to working with the Lighthouse CLI, flags can be passed like this: |
| 132 | +> `typescript https://example.com --output=json --chromeFlags='--headless=shell'` |
| 133 | +> |
| 134 | +> With the plugin, the configuration would be: |
| 135 | +> |
| 136 | +> ```ts |
| 137 | +> // code-pushup.config.ts |
| 138 | +> ... |
| 139 | +> typescriptPlugin('https://example.com', { |
| 140 | +> output: 'json', |
| 141 | +> chromeFlags: ['--headless=shell'], |
| 142 | +> }); |
| 143 | +> ``` |
| 144 | +
|
| 145 | +> [!note] |
| 146 | +> The following flags are **not supported** in the current implementation: |
| 147 | +> |
| 148 | +> - `list-all-audits` - Prints a list of all available audits and exits. Alternative: `npx code-pushup print-config --onlyPlugins typescript` |
| 149 | +> - `list-locales` - Prints a list of all supported locales and exits. |
| 150 | +> - `list-trace-categories` - Prints a list of all required trace categories and exits. |
| 151 | +> - `view` - Open HTML report in your browser |
| 152 | +
|
| 153 | +## Chrome Flags for Tooling |
| 154 | +
|
| 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/typescript-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: |
| 162 | +
|
| 163 | +> ```ts |
| 164 | +> import typescriptPlugin from '@code-pushup/typescript-plugin'; |
| 165 | +> |
| 166 | +> typescriptPlugin('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 typescriptPlugin, { DEFAULT_CHROME_FLAGS } from '@code-pushup/typescript-plugin'; |
| 178 | +> |
| 179 | +> typescriptPlugin('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 typescriptPlugin from '@code-pushup/typescript-plugin'; |
| 191 | +> |
| 192 | +> typescriptPlugin('https://example.com', { |
| 193 | +> output: 'json', |
| 194 | +> chromeFlags: ['--verbose'], |
| 195 | +> }); |
| 196 | +> ``` |
| 197 | +
|
| 198 | +## Config |
| 199 | +
|
| 200 | +The plugin accepts a third optional argument, `config`. |
| 201 | +
|
| 202 | +`config` is the Lighthouse [configuration](https://github.com/GoogleChrome/typescript/blob/7d80178c37a1b600ea8f092fc0b098029799a659/types/config.d.ts#L21) as a JS object. |
| 203 | +
|
| 204 | +For a complete guide on Lighthouse configuration read the [official documentation on configuring](https://github.com/GoogleChrome/typescript/blob/main/docs/configuration.md) |
| 205 | +
|
| 206 | +> [!TIP] |
| 207 | +> If you are not used to work with the Lighthouse CLI you would pass a config like this: |
| 208 | +> `typescript --config-path=path/to/custom-config.js https://example.com` |
| 209 | +> |
| 210 | +> And in a separate file you would place the following object: |
| 211 | +> |
| 212 | +> ```typescript |
| 213 | +> // custom-config.js file |
| 214 | +> export default { |
| 215 | +> extends: 'typescript:default', |
| 216 | +> settings: { |
| 217 | +> onlyAudits: ['first-meaningful-paint', 'speed-index', 'interactive'], |
| 218 | +> }, |
| 219 | +> }; |
| 220 | +> ``` |
| 221 | +> |
| 222 | +> Now with the plugin it would look like this: |
| 223 | +> |
| 224 | +> ```ts |
| 225 | +> // code-pushup.config.ts |
| 226 | +> ... |
| 227 | +> typescriptPlugin('https://example.com', undefined, { |
| 228 | +> extends: 'typescript:default', |
| 229 | +> settings: { |
| 230 | +> onlyAudits: [ |
| 231 | +> 'first-meaningful-paint', |
| 232 | +> 'speed-index', |
| 233 | +> 'interactive', |
| 234 | +> ], |
| 235 | +> } |
| 236 | +> }) |
| 237 | +> ``` |
| 238 | +
|
| 239 | +If you want to contribute, please refer to [CONTRIBUTING.md](./CONTRIBUTING.md). |
0 commit comments