npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

css-class-positions

v1.0.0

Published

Find CSS class selector positions in any CSS-like file (CSS, SCSS, Less, Sass, Stylus)

Readme

css-class-positions

A fast, zero-dependency scanner that finds where CSS classes are defined in any stylesheet — CSS, SCSS, Less, Sass, Stylus, SugarSS. No AST or parser needed.

[!NOTE] For CSS-like file formats with .className selector syntax. CSS-in-JS (styled-components, Emotion, etc.) is not supported.

Install

npm install css-class-positions

Usage

import { cssClassPositions } from 'css-class-positions'

const positions = cssClassPositions(`
.button { color: red; }
.header { color: blue; }
`, { fileName: 'style.module.scss' })

positions.get('button') // { line: 2, column: 1 }
positions.get('header') // { line: 3, column: 1 }

Returns a Map<string, Position> of decoded class name → 1-based position of the . character. Records the first occurrence of each class (the definition site).

Why

CSS tooling like CSS Modules knows which class names exist in a file, but not where they're defined. That position data is needed for source maps and IDE go-to-definition. Getting it normally requires a full AST parse — and every preprocessor needs its own parser.

This module does it with a single O(n) scanner. No AST, no dependencies, every syntax. It skips the many places where .identifier appears but isn't a class selector — comments, strings, URLs, at-rule headers, property values, Less mixins, preprocessor interpolation — so only real definitions are returned.

Benchmarks

vs. PostCSS parse() + walkRules():

| | css-class-positions | PostCSS | | :--- | ---: | ---: | | Install size | 36 KB (1 package) | 976 KB (7 packages) |

| Input | css-class-positions | PostCSS | Speedup | | :--- | ---: | ---: | ---: | | CSS, 50 classes | 52 µs | 111 µs | 2.1x | | CSS, 500 classes | 478 µs | 1.16 ms | 2.4x | | CSS, 5000 classes | 4.9 ms | 12.6 ms | 2.6x | | SCSS, 50 classes | 107 µs | 237 µs | 2.2x | | SCSS, 500 classes | 1.08 ms | 2.60 ms | 2.4x | | Less, 50 classes | 81 µs | 501 µs | 6.2x |

Speed: mitata, Apple M2 Max, Node 24.11.1. Run pnpm bench to reproduce.

API

cssClassPositions(css, options?)

  • css string — CSS source text
  • options.fileName string — file path (used to infer syntax from extension)
  • options.lineComments boolean | 'auto' — whether // is a comment (default: inferred from fileName, or 'auto')
  • Returns Map<string, Position>

fileName

Pass the file path and the scanner configures itself:

| Extension | // handling | | :--- | :--- | | .css | data (not a comment) | | .scss .less .sass .styl .sss | line comment | | other / not set | heuristic |

lineComments

Override // handling explicitly. This is the main cross-syntax ambiguity: // is data in CSS but a comment in SCSS/Less/Sass/Stylus.

  • false — never a comment
  • true — always a comment
  • 'auto' — heuristic (comment unless it looks like a URL path)

Position

type Position = {
    line: number // 1-based
    column: number // 1-based, UTF-16 code units (matches VS Code, source maps, LSP)
}

The scanner identifies .className patterns while skipping all non-selector contexts:

  • Block comments (/* ... */) and line comments (// ...)
  • Strings ("..." and '...')
  • URL functions — url(), url-prefix(), domain(), regexp()
  • At-rule headers — @layer, @supports, @scope, @container, @when/@else, @document, @nest, @import, @apply, @variant, @custom-selector, @custom-variant
  • @extend / @extends references (SCSS, Sass, Stylus)
  • :export { } blocks (CSS Modules)
  • :extend() arguments (Less)
  • Attribute selectors ([data-type=.foo])
  • Property values (backward look detects : before .)
  • Less mixin calls (.name(), .name;) and definitions (.name() { })
  • Less variable assignments (@var: value;) and detached rulesets (@var: { ... };)
  • Preprocessor interpolation — #{} (SCSS), @{} (Less), -{var} (Stylus)

Identifiers are decoded per the CSS spec, including hex and character escape sequences.

Sponsors