Every line of code we write today will end up as someone’s legacy code.
Everything that you write is going to disappears some day
RUM Code coverage, chose a 1h timeframe (per day) where 1% traffic run that the test
FES javascript instrimentation visualization
- Instrument JavaScript code – Roman Liutikov – Medium
- Raiders of the Fast Start: Frontend Performance Archaeology - Perform…
Create babel plugin that inject in code with visitors for functions and control flow (loops and switchs): Function
, IfStatement
, (Terminatorless
, SwitchCase
, Loop
, TryStatement
)
- Plugins · Babel
- babel-handbook/plugin-handbook.md at master · jamiebuilds/babel-handbook
- @babel/types · Babel
- babel-plugin-inject/index.js at master · ialpert/babel-plugin-inject - Example of plugin that replace function call
- @babel/helper-module-imports - replace function/property by import via Babel
Or as standalone tool:
Other:
Aka unused CSS RUM
Can't support easily CSSOM changes
Parse + generate result client side (use web worker):
- get CSSOM (for external and inlined stylesheets) from DOM (see step 4)
- download CSS + map
- parse stylesheet to CSSOM: ruleset, selectors, values, etc. (for results from step 1 and 2)
- find which rules/properties has been ignored by the browser (compare results from step 3)
- apply it to a virtual DOM
- apply document updates to the virtual DOM based on
MutationObserver
(for added or removed external and inlined style sheets too, see steps 1, 2 and 3) - apply source map
- generate LCOV report + send to server (
navigator.sendBeacon()
) with generated version (git commit hash)
Server side:
- merge reports https://github.com/mweibel/lcov-result-merger https://gist.github.com/xseignard/5420661
- apply later changes (commits diffs, in that case, the final report should used carefully)
Based on Webdev tools code (Firefox / Chrome):
- https://github.com/mozilla/gecko-dev/blob/master/devtools/client/inspector/rules/rules.js
- https://github.com/mozilla/gecko-dev/blob/master/devtools/client/inspector/rules/views/rule-editor.js
- https://github.com/mozilla/gecko-dev/tree/master/devtools/shared/css
- https://github.com/mozilla/gecko-dev/blob/master/devtools/client/inspector/computed/test/browser_computed_getNodeInfo.js
- https://docs.firefox-dev.tools/getting-started/architecture-overview.html
- https://github.com/ChromeDevTools/devtools-frontend/blob/master/front_end/elements/ComputedStyleModel.js
- https://github.com/ChromeDevTools/devtools-frontend/blob/master/front_end/elements/StylesSidebarPane.js
- 1518615 - (dt-rules) [meta] Convert the Rules view into React/Redux
- Dependency tree for Bug 1540753
An other method to detect ruleset usage is to inject a rarely used property with a custom value and get style with void selector (could be enabled and disabled synchronously): voice-family: "my custom value"
(could be also font-family
, content
, quotes
but could add layout impact).
This method could be hard to implement if the stylesheet use attribute selectors (should detect which element or its parent has attribute change and list which attribute changes to "listen")
It could also use a tracker image with some properties (CSS background or content property)) to direct tracking element but generate too much traffic (each document elements and pseudo element call a beacon).
Some tips, tricks, etc.:
- https://codepen.io/mmems/pen/dwWyxv
TreeWalker
performance vs other methods:TreeWalker
is better (can select text nodes and faster than other methods) - first walk the document (elements) or node iterator
- shared workers or web worker with sessionStorage
- wait for idle? https://developer.mozilla.org/en-US/docs/Web/API/Window/requestIdleCallback
- mutation observer support: IE11, fallback to Mutation event but with setTimeout
- support update that impact pseudo classes:
:empty
:blank
:-moz-only-whitespace
- and other pseudo classes and elements
- Mutation Observer notes - QuirksBlog
- javascript - reliabilty of 'isConnected' field in dom node - Stack Overflow
- jsdom/default-stylesheet.js at master · jsdom/jsdom - jsdom use symbol-tree to store tree nodes
- https://github.com/jsdom/jsdom/blob/master/lib/jsdom/utils.js#L58-L67 - mixin class with interface implementation
- CSS @namespace
- WorkerDOM: JavaScript Concurrency and the DOM - Slide 52 - Convert DOM tree to minimal format (use string dictionary and references)
Libs and tools:
- puppeteer/api.md at master · GoogleChrome/puppeteer - Chrome DevTools Protocol Viewer - Profiler
- DevTools: allow exporting code coverage data. (I0360d0de) · Gerrit Code Review - Chrome devtools can export Coverage data (as JSON)
- istanbuljs/puppeteer-to-istanbul: given coverage information output by puppeteer's API output a format consumable by Istanbul reports
- istanbuljs/v8-to-istanbul: convert from v8 coverage format to istanbul's format
- istanbul/coverage.json.md at master · gotwarlost/istanbul
- 717195 - Ability to export code coverage results for use by analysis tools - chromium - Monorail
- coverage/ - Code Search
- Feature Request: LCOV or Istanbul format code coverage · Issue #1768 · GoogleChrome/puppeteer
- ampproject/worker-dom: The same DOM API and Frameworks you know, but in a Web Worker.
- csstree/csstree: A tool set for working with CSS including fast detailed parser, walker, generator and lexer based on W3C specs and browser implementations
- Parsel: A tiny, permissive CSS selector parser
- fgnass/domino: Server-side DOM implementation based on Mozilla's dom.js
- jsdom/jsdom: A JavaScript implementation of the WHATWG DOM and HTML standards, for use with node.js
- domino/select.js at master · fgnass/domino - chjj/zest: An absurdly fast CSS selector engine.
- jsakas/CSSStyleDeclaration: A work-a-like for the CSSStyleDeclaration in https://github.com/NV/CSSOM, but with CSS2Properties interface
- rafaelw/mutation-summary: A JavaScript library that makes observing changes to the DOM easy
- LeaVerou/rework-utils: Utilities to explore ASTs generated by the Rework CSS parser. Originally written for the Web Almanac.
- reworkcss/css: CSS parser / stringifier for Node.js
- Chrome DevTools CSS Overview Devtool panel - CSSOverviewModel.js
- GitHub - painty/CSS-Used-ChromeExt: Get all css rules used by the selected DOM and its children.
- Firefox DevTool style inspector show unused properties - Why isn't this CSS doing anything? - YouTube
- Finding Dead CSS – CSS Wizardry – CSS Architecture, Web Performance Optimisation, and more, by Harry Roberts
- Here's the thing about "unused CSS" tools | CSS-Tricks
- leeoniya/dropcss: A simple, thorough and fast unused-CSS cleaner - lib use Fast HTML Parser, CSSTree et css-select
- Dust-Me Selectors – Add-ons for Firefox -
/chrome/content/dustmeselectors.js
:DustMeSelectors.evaluateSelectors
- purifycss/purifycss: Remove unused CSS. Also works with single-page apps.
- uncss/uncss: Remove unused styles from CSS
- Web Essentials: Remove unused CSS - Web Essentials: Remove unused CSS - YouTube - Browser Link - Web Essentials - WebEssentials2015/UnusedCss.js at master · madskristensen/WebEssentials2015
In Intellij IDEA/WebStorm/PHPStorm:
- Finding Unused Code with Coverage - Help | IntelliJ IDEA
- Is there a way to load the lcov data inside webstorm? · Issue #6 · StevenLooman/mocha-lcov-reporter
- Webstorm (intellij) coverage integration with the angular cli · Issue #3560 · angular/angular-cli
- Testing JavaScript - Help | IntelliJ IDEA
In Visual Studio Code:
- VSCode LCOV - Visual Studio Marketplace
- Code Coverage - Visual Studio Marketplace
- Coverage Gutters - Visual Studio Marketplace
- Automatic Unit Testing in .NET Core plus Code Coverage in Visual Studio Code - Scott Hanselman
In Visual Studio:
- Code coverage testing - Visual Studio | Microsoft Docs
- OpenCppCoverage/OpenCppCoverage: OpenCppCoverage is an open source code coverage tool for C++ under Windows.
- Testing a Team Services extension and tracking code coverage – ALM | DevOps Rangers
Aka custom data format