@projectwallace/css-code-coverage 0.1.2 → 0.1.3
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 +50 -46
- package/dist/css-code-coverage.js +2 -0
- package/dist/src/filter-entries.d.ts +1 -1
- package/dist/src/index.d.ts +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -19,52 +19,33 @@ npm install @projectwallace/css-code-coverage
|
|
|
19
19
|
|
|
20
20
|
### Prerequisites
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
1. You 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:
|
|
51
|
-
|
|
52
|
-
1. Browser:
|
|
53
|
-
```ts
|
|
54
|
-
function parse_html(html) {
|
|
55
|
-
return new DOMParser().parseFromString(html, 'text/html')
|
|
56
|
-
}
|
|
57
|
-
```
|
|
58
|
-
1. Node (using [linkedom](https://github.com/WebReflection/linkedom) in this example):
|
|
59
|
-
|
|
60
|
-
```ts
|
|
61
|
-
// $ npm install linkedom
|
|
62
|
-
import { DOMParser } from 'linkedom'
|
|
63
|
-
|
|
64
|
-
function parse_html(html: string) {
|
|
65
|
-
return new DOMParser().parseFromString(html, 'text/html')
|
|
66
|
-
}
|
|
67
|
-
```
|
|
22
|
+
You have collected browser coverage data of your CSS. There are several ways to do this:
|
|
23
|
+
|
|
24
|
+
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
|
|
25
|
+
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.
|
|
26
|
+
|
|
27
|
+
Either way you end up with one or more JSON files that contain coverage data.
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
// Read a single JSON or a folder full of JSON files with coverage data
|
|
31
|
+
// Coverage data looks like this:
|
|
32
|
+
// {
|
|
33
|
+
// url: 'https://www.projectwallace.com/style.css',
|
|
34
|
+
// text: 'a { color: blue; text-decoration: underline; }', etc.
|
|
35
|
+
// ranges: [
|
|
36
|
+
// { start: 0, end: 46 }
|
|
37
|
+
// ]
|
|
38
|
+
// }
|
|
39
|
+
import { parse_coverage } from '@projectwallace/css-code-coverage'
|
|
40
|
+
|
|
41
|
+
let files = await fs.glob('./css-coverage/**/*.json')
|
|
42
|
+
let coverage_data = []
|
|
43
|
+
|
|
44
|
+
for (let file of files) {
|
|
45
|
+
let json_content = await fs.readFile(file, 'urf-8')
|
|
46
|
+
coverage_data.push(...parse_coverage(json_content))
|
|
47
|
+
}
|
|
48
|
+
```
|
|
68
49
|
|
|
69
50
|
### Bringing it together
|
|
70
51
|
|
|
@@ -73,3 +54,26 @@ import { calculate_coverage } from '@projectwallace/css-code-coverage'
|
|
|
73
54
|
|
|
74
55
|
let report = calculcate_coverage(coverage_data, parse_html)
|
|
75
56
|
```
|
|
57
|
+
|
|
58
|
+
See [src/index.ts](https://github.com/projectwallace/css-code-coverage/blob/main/src/index.ts) for the data that's returned.
|
|
59
|
+
|
|
60
|
+
### Optional: coverage from `<style>` blocks
|
|
61
|
+
|
|
62
|
+
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:
|
|
63
|
+
|
|
64
|
+
1. Browser:
|
|
65
|
+
```ts
|
|
66
|
+
function parse_html(html) {
|
|
67
|
+
return new DOMParser().parseFromString(html, 'text/html')
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
1. Node (using [linkedom](https://github.com/WebReflection/linkedom) in this example):
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
// $ npm install linkedom
|
|
74
|
+
import { DOMParser } from 'linkedom'
|
|
75
|
+
|
|
76
|
+
function parse_html(html: string) {
|
|
77
|
+
return new DOMParser().parseFromString(html, 'text/html')
|
|
78
|
+
}
|
|
79
|
+
```
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { Coverage } from './parse-coverage.ts';
|
|
2
2
|
import { Parser } from './types.ts';
|
|
3
|
-
export declare function filter_coverage(coverage: Coverage[], parse_html
|
|
3
|
+
export declare function filter_coverage(coverage: Coverage[], parse_html?: Parser): Coverage[];
|
package/dist/src/index.d.ts
CHANGED
|
@@ -32,7 +32,7 @@ export type CoverageResult = CoverageData & {
|
|
|
32
32
|
* 4. Calculate used/unused CSS bytes (fastest path, no inspection of the actual CSS needed)
|
|
33
33
|
* 5. Calculate line-coverage, byte-coverage per stylesheet
|
|
34
34
|
*/
|
|
35
|
-
export declare function calculate_coverage(coverage: Coverage[], parse_html
|
|
35
|
+
export declare function calculate_coverage(coverage: Coverage[], parse_html?: Parser): CoverageResult;
|
|
36
36
|
export type { Coverage, Range } from './parse-coverage.ts';
|
|
37
37
|
export { parse_coverage } from './parse-coverage.ts';
|
|
38
38
|
export type { Parser } from './types.ts';
|