projectwallace/css-analyzer
À propos
Analytics for CSS
Développement web et applicatifWhat you can do
- 200+ metrics: specificity, complexity, design tokens and more
- Opt-in collectors: you only pay (runtime and bundle size) for what you use; result types are inferred
- Runs in NodeJS and browsers, one dependency
- Powers Project Wallace
Progression mesurée des étoiles
- in 24h
- 0
- in 7 days
- +1
- in 15 days
- +2
- in 1 month
- Historique en cours
Variations nettes sur des périodes qui se chevauchent, pas des totaux quotidiens. Les mesures absentes restent vides. Les étoiles indiquent l’intérêt, pas la qualité.
README.md
OriginalCSS Analyzer
Features
- 200+ metrics: specificity, complexity, design tokens and more
- Opt-in collectors: you only pay (runtime and bundle size) for what you use; result types are inferred
- Runs in NodeJS and browsers, one dependency
- Powers Project Wallace
Install
pnpm add @projectwallace/css-analyzer
Usage
Pick the metrics you need
import { analyze } from '@projectwallace/css-analyzer'
import { colors, font_sizes, z_indexes } from '@projectwallace/css-analyzer/collectors'
const result = analyze(css, {
colors: colors(),
font_sizes: font_sizes(),
z: z_indexes(),
})
result.colors.total // 12
result.colors.total_unique // 5
result.colors.unique.get('red') // { count: 3 }
result.font_sizes.unique // Map { '16px' => { count: 4 }, ... }
- The keys are yours: they become the keys of the result.
- Every collector is a function: call it (
colors()), optionally with options. - Only the parts of the CSS that your collectors need are parsed. A collector set that only looks at values never parses selectors.
All collectors count things the same way: { total, total_unique, unique: Map<string, { count, locations? }> }.
Derived numbers are separate functions, so unused statistics cost nothing:
import { summarize } from '@projectwallace/css-analyzer'
import { summarize_specificity, specificity } from '@projectwallace/css-analyzer/collectors'
summarize(result.colors) // { uniqueness_ratio: 0.41 }
const { s } = analyze(css, { s: specificity() })
summarize_specificity(s) // { total, min, max, sum, mean, mode }
Options
All optional, passed per collector: colors({ locations: true }).
locations: addlocations: [{ line, column, start_offset, end_offset }]to every unique itemsequence: numeric series (nesting, sizes, complexity, specificity) also in source order, e.g.rules({ sequence: true }).sizes.sequence // [2, 3, 1];summarize_numbers()gives min, max, mean, mode, median, range and sumcontexts: per-property counts inby_context(colors,units,border_radiuses)normalize: 'none': keep the authored caseskip_var: skipvar()values (font sizes, families, line heights)
Presets
import {
analyze_design_tokens,
analyze_all,
design_tokens_collectors,
} from '@projectwallace/css-analyzer/presets'
analyze_design_tokens(css, { locations: true })
analyze_all(css)
// Or spread the collector map to narrow, extend or override it
analyze(css, {
...design_tokens_collectors(),
colors: colors({ normalize: 'none' }),
})
Which collectors each preset includes: src/presets.
Typing a collector map
Collectors are plain functions, so their types come for free. With isolatedDeclarations, write the type of an exported map with ReturnType:
import { colors, font_sizes, type ResultOf } from '@projectwallace/css-analyzer/collectors'
export const collectors: {
colors: ReturnType<typeof colors<{ contexts: true }>>
font_sizes: ReturnType<typeof font_sizes>
} = { colors: colors({ contexts: true }), font_sizes: font_sizes() }
export type Report = ResultOf<typeof collectors>
More types are exported from src/collectors/index.ts.
Collectors
| Collector | Counts |
|---|---|
stylesheet |
size, lines, source lines of code, complexity, comments, embedded data: content |
atrules |
every at-rule, with @font-face, @import, @media, @supports, @keyframes, @container, @layer, @property, @function, @scope |
rules, selectors, specificity |
rule sizes and nesting, selector kinds and complexity, specificity |
declarations, properties |
declarations, !important, prefixes, custom properties, shorthands, hacks, anchor names |
colors, gradients |
colors (with formats and per-property counts), gradients |
font_sizes, font_families, line_heights |
font values (also from the font shorthand) |
box_shadows, text_shadows, border_radiuses |
shadows and radii |
durations, timing_functions, animation_names |
transitions and animations |
units, z_indexes, displays, keywords, resets |
other values |
value_prefixes, value_browserhacks, value_complexity |
vendor prefixes, hacks, complexity of values |
All collectors: src/collectors.
Write your own collector
import { analyze, define_collector, create_counter } from '@projectwallace/css-analyzer'
function important_declarations() {
return define_collector(function init() {
const counter = create_counter()
return {
declaration: {
enter(node, ctx) {
if (ctx.is_important) {
counter.add(ctx.property, node)
}
},
},
result() {
return counter.result()
},
}
})
}
analyze(css, { important: important_declarations() })
- Hooks:
stylesheet,rule,atrule,selector,declaration({ properties?, enter }),value({ types, properties?, enter }),comment. Types in src/engine/types.ts. selectorgets style rule selectors only, withoutctx(userulefordepth/atrule_stack).ctxholds lazy, shared facts about the current node: see src/engine/context.ts.counter.add(item, node): the node is wherelocationscome from.- Don't keep nodes or
ctxafter a handler returns.
Utilities
Functions that take a node parsed with @projectwallace/css-parser (parse_value(), parse_selector(), or an at-rule prelude from parse()) and return plain data.
import { parse_selector, parse_value } from '@projectwallace/css-parser'
import { get_specificity } from '@projectwallace/css-analyzer/selectors'
import { get_declared_container_names } from '@projectwallace/css-analyzer/values'
get_specificity(parse_selector('a.b > #c')) // [1, 1, 1]
get_declared_container_names(parse_value('sidebar card / size'))
// [{ value: 'sidebar', line: 1, column: 1, start_offset: 0, end_offset: 7 }, ...]
| Entry point | Source |
|---|---|
/selectors |
src/selectors |
/values |
src/values |
/atrules |
src/atrules |
/properties |
src/properties |
/stylesheet |
src/stylesheet |
/extract |
src/extract |
/utils/collections |
src/utils/collections (DefinedUsed, KeywordSet) |
Match (src/utils/location.ts): { value, line, column, start_offset, end_offset }
valueis always the source text; derived data gets its own field (name,hack,text,unit, ...).- Offsets are relative to the parsed source;
end_offsetis exclusive.
Extractors
The per-declaration logic of the collectors, for linters and editors: extract_*(property, value): Match[].
import { parse_value } from '@projectwallace/css-parser'
import { extract_colors } from '@projectwallace/css-analyzer/extract'
extract_colors('border', parse_value('1px solid RED'))
// [{ value: 'RED', text: 'RED', format: 'named', line: 1, column: 11, start_offset: 10, end_offset: 13 }]
- Single-property extractors (
extract_z_indexes,extract_box_shadows, ...) andextract_gradientstake only the value. - Property sets are exported as
KeywordSets (font_size_properties, ...) to skip declarations early. - Values are returned as written; normalizing is up to you.
Upgrading from 9.x
See MIGRATION.md.
Acknowledgements
Browser hack patterns for value detection were sourced from:
Related projects
- CSS Design Tokens - Get DTCG tokens from CSS (uses this css-analyzer)
- Project Wallace Stylelint plugin - Lint your CSS based on the metrics in this CSS analyzer
- Wallace CLI - CLI tool for @projectwallace/css-analyzer
- Color Sorter - Sort CSS colors by hue, saturation, lightness and opacity
Explorer des projets similaires
Chargement…