@projectwallace/css-code-coverage 0.4.1 → 0.6.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/README.md CHANGED
@@ -23,11 +23,13 @@ npm install @projectwallace/css-code-coverage
23
23
  ```ts
24
24
  import { calculate_coverage } from '@projectwallace/css-code-coverage'
25
25
 
26
- function parse_html(html) {
27
- return new DOMParser().parseFromString(html, 'text/html')
28
- }
26
+ let report = await calculcate_coverage(coverage_data)
29
27
 
30
- let report = calculcate_coverage(coverage_data, parse_html)
28
+ // => report.line_coverage_ratio: 0.80
29
+ // => report.byte_coverage_ratio: 0.85
30
+ // => report.total_lines: 1000
31
+ // => report.covered_lines: 800
32
+ // etc.
31
33
  ```
32
34
 
33
35
  See [src/index.ts](https://github.com/projectwallace/css-code-coverage/blob/main/src/index.ts) for the data that's returned.
@@ -36,12 +38,30 @@ See [src/index.ts](https://github.com/projectwallace/css-code-coverage/blob/main
36
38
 
37
39
  There are two principal ways of collecting CSS Coverage data:
38
40
 
41
+ ### Coverage API (preferred)
42
+
43
+ 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:
44
+
45
+ ```ts
46
+ // Start collecting coverage
47
+ await page.coverage.startCSSCoverage()
48
+ // Load the page, do all sorts of interactions to increase coverage, etc.
49
+ await page.goto('http://example.com')
50
+ // Stop the coverage and store the result in a variable to pass along
51
+ let coverage = await page.coverage.stopCSSCoverage()
52
+
53
+ // Now we can process it
54
+ import { calculate_coverage } from '@projectwallace/css-code-coverage'
55
+
56
+ let report = await calculcate_coverage(coverage)
57
+ ```
58
+
39
59
  ### Browser devtools
40
60
 
41
61
  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
62
 
43
63
  - 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/
64
+ - Collect coverage in Google Chrome: https://developer.chrome.com/docs/devtools/coverage/
45
65
 
46
66
  Additionally, DevTools Tips writes about it in their [explainer](https://devtoolstips.org/tips/en/detect-unused-code/).
47
67
 
@@ -67,46 +87,3 @@ for (let file of files) {
67
87
  coverage_data.push(...parse_coverage(json_content))
68
88
  }
69
89
  ```
70
-
71
- ### Coverage API
72
-
73
- 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:
74
-
75
- ```ts
76
- // Start collecting coverage
77
- await page.coverage.startCSSCoverage()
78
- // Load the page, do all sorts of interactions to increase coverage, etc.
79
- await page.goto('http://example.com')
80
- // Stop the coverage and store the result in a variable to pass along
81
- let coverage = await page.coverage.stopCSSCoverage()
82
-
83
- // Now we can process it
84
- import { calculate_coverage } from '@projectwallace/css-code-coverage'
85
-
86
- function parse_html(html) {
87
- return new DOMParser().parseFromString(html, 'text/html')
88
- }
89
-
90
- let report = calculcate_coverage(coverage, parse_html)
91
- ```
92
-
93
- ### Optional: coverage from `<style>` blocks
94
-
95
- 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:
96
-
97
- 1. Browser:
98
- ```ts
99
- function parse_html(html) {
100
- return new DOMParser().parseFromString(html, 'text/html')
101
- }
102
- ```
103
- 1. Node (using [linkedom](https://github.com/WebReflection/linkedom) in this example, but other parsers could work, too):
104
-
105
- ```ts
106
- // $ npm install linkedom
107
- import { DOMParser } from 'linkedom'
108
-
109
- function parse_html(html: string) {
110
- return new DOMParser().parseFromString(html, 'text/html')
111
- }
112
- ```