Skip to content

Commit 9a1349b

Browse files
committed
readme improvements
1 parent d65c13f commit 9a1349b

File tree

1 file changed

+47
-15
lines changed

1 file changed

+47
-15
lines changed

README.md

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,53 @@ npm install @projectwallace/css-code-coverage
2020

2121
## Usage
2222

23-
### Prerequisites
23+
```ts
24+
import { calculate_coverage } from '@projectwallace/css-code-coverage'
25+
26+
function parse_html(html) {
27+
return new DOMParser().parseFromString(html, 'text/html')
28+
}
29+
30+
let report = calculcate_coverage(coverage_data, parse_html)
31+
```
32+
33+
See [src/index.ts](https://github.com/projectwallace/css-code-coverage/blob/main/src/index.ts) for the data that's returned.
34+
35+
## Collecting CSS Coverage
36+
37+
There are two principal ways of collecting CSS Coverage data:
38+
39+
### Browser devtools
40+
41+
In Edge, Chrome or chromium you can manually collect coverage in the browser's DevTools. In all cases you'll generate coverage data manually and the browser will let you export the data to a JSON file. Note that this JSON contains both JS coverage as well as the CSS coverage. Learn how it works:
42+
43+
- Collect coverage in Microsoft Edge: https://learn.microsoft.com/en-us/microsoft-edge/devtools-guide-chromium/coverage/
44+
- Collect coevrage in Google Chrome: https://developer.chrome.com/docs/devtools/coverage/
45+
46+
Additionally, DevTools Tips writes about it in their [explainer](https://devtoolstips.org/tips/en/detect-unused-code/).
47+
48+
### Coverage API
2449

25-
You have collected browser coverage data of your CSS. There are several ways to do this:
50+
Both Puppeteer and Playwright provide an API to programmatically get the coverage data, allowing you to put that directly into this library. Here is the gist:
51+
52+
```ts
53+
// Start collecting coverage
54+
await page.coverage.startCSSCoverage()
55+
// Load the page, do all sorts of interactions to increase coverage, etc.
56+
await page.goto('http://example.com')
57+
// Stop the coverage and store the result in a variable to pass along
58+
let coverage = await page.coverage.stopCSSCoverage()
59+
60+
// Now we can process it
61+
import { calculate_coverage } from '@projectwallace/css-code-coverage'
62+
63+
function parse_html(html) {
64+
return new DOMParser().parseFromString(html, 'text/html')
65+
}
66+
67+
let report = calculcate_coverage(coverage, parse_html)
68+
```
2669

27-
1. in the browser devtools in [Edge](https://learn.microsoft.com/en-us/microsoft-edge/devtools-guide-chromium/coverage/)/[Chrome](https://developer.chrome.com/docs/devtools/coverage/)/chromium
2870
1. Via the `coverage.startCSSCoverage()` API that headless browsers like [Playwright](https://playwright.dev/docs/api/class-coverage#coverage-start-css-coverage) or [Puppeteer](https://pptr.dev/api/puppeteer.coverage.startcsscoverage/) provide.
2971

3072
Either way you end up with one or more JSON files that contain coverage data.
@@ -50,27 +92,17 @@ for (let file of files) {
5092
}
5193
```
5294

53-
### Bringing it together
54-
55-
```ts
56-
import { calculate_coverage } from '@projectwallace/css-code-coverage'
57-
58-
let report = calculcate_coverage(coverage_data, parse_html)
59-
```
60-
61-
See [src/index.ts](https://github.com/projectwallace/css-code-coverage/blob/main/src/index.ts) for the data that's returned.
62-
6395
### Optional: coverage from `<style>` blocks
6496

65-
Covergae generators also create coverage ranges for `<style>` blocks in HTML. If this applies to your code you should provide a HTML parser that we use to 'scrape' the HTML in case the browser gives us not just plain CSS contents. Depending on where you run this analysis you can use:
97+
Coverage generators also create coverage ranges for `<style>` blocks in HTML. If this applies to your code you should provide a HTML parser that we use to 'scrape' the HTML in case the browser gives us not just plain CSS contents. Depending on where you run this analysis you can use:
6698

6799
1. Browser:
68100
```ts
69101
function parse_html(html) {
70102
return new DOMParser().parseFromString(html, 'text/html')
71103
}
72104
```
73-
1. Node (using [linkedom](https://github.com/WebReflection/linkedom) in this example):
105+
1. Node (using [linkedom](https://github.com/WebReflection/linkedom) in this example, but other parsers could work, too):
74106

75107
```ts
76108
// $ npm install linkedom

0 commit comments

Comments
 (0)