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

@3-/cssfmt

v0.1.5

Published

CSS formatter that nests selectors, converts rgba to hex, and moves @import to top / 支持选择器嵌套、rgba转十六进制及@import置顶的CSS格式化工具

Readme

English | 中文


@3-/cssfmt : CSS formatter that nests selectors, converts rgba to hex, and moves @import to the top

Table of Contents

Features

  • AST-Based Reordering: Parse CSS and move @import rules to the top of the stylesheet.
  • Selector Nesting: Compress and structure selectors by nesting them automatically.
  • Color Format Conversion: Regular expression-based conversion of rgba(r, g, b, a) colors to #rrggbbaa hexadecimal format.

Installation

bun add @3-/cssfmt

Usage

import cssfmt from "@3-/cssfmt";

const rawCss = `
body {
  color: rgba(255, 0, 0, 0.5);
  background: blue;
}
@import url(//example.com/theme.css);
body a {
  text-decoration: none;
}
`;

const result = cssfmt(rawCss);
console.log(result);

Output

@import url(//example.com/theme.css);

body {
  color: #ff000080;
  background: blue;
  a {
    text-decoration: none;
  }
}

Design & Architecture

The invocation flow follows a linear pipeline:

graph TD
    A[Input CSS String] --> B[Parse CSS to AST via css-tree]
    B --> C[Extract @import Rules and Other Rules]
    C --> D[Reassemble AST putting @import first]
    D --> E[Generate CSS String from AST]
    E --> F[Apply Selector Nesting via @3-/css2nest]
    F --> G[Convert rgba colors to hex format]
    G --> H[Output Formatted CSS String]

Directory Structure

.
├── src/
│   └── lib.js          # Core formatting pipeline
├── tests/
│   └── lib.test.js     # Verification tests
└── package.json        # Package description and dependencies

Tech Stack

  • css-tree: CSS parser and generator.
  • @3-/css2nest: Selector nesting engine.
  • Bun: Runtime and test runner.

Trivia & History

Historically, browser stylesheets did not support nesting. Developers created preprocessors like Sass in 2006 and Less in 2009 to solve code replication and organize complex styles.

The CSS Nesting Module became an official W3C Working Draft in 2021 and gained native browser support in 2023.

Native CSS nesting uses :is() specificity logic. This differs slightly from preprocessor text-concatenation, causing minor differences in specificity calculations.

The @import rule has strictly required top-of-file placement since early CSS specifications to prevent late-discovered network requests and browser rendering delays.


@3-/cssfmt : 支持选择器嵌套、rgba转十六进制及@import置顶的CSS格式化工具

目录

功能介绍

  • AST重排: 解析CSS并将 @import 规则移动至样式表顶部。
  • 选择器嵌套: 自动对CSS选择器进行嵌套与结构化压缩。
  • 颜色转换: 基于正则表达式,将 rgba(r, g, b, a) 颜色值转换为 #rrggbbaa 十六进制格式。

安装

bun add @3-/cssfmt

使用演示

import cssfmt from "@3-/cssfmt";

const rawCss = `
body {
  color: rgba(255, 0, 0, 0.5);
  background: blue;
}
@import url(//example.com/theme.css);
body a {
  text-decoration: none;
}
`;

const result = cssfmt(rawCss);
console.log(result);

格式化结果

@import url(//example.com/theme.css);

body {
  color: #ff000080;
  background: blue;
  a {
    text-decoration: none;
  }
}

设计思路

模块调用流程如下:

graph TD
    A[输入 CSS 字符串] --> B[使用 css-tree 解析为 AST]
    B --> C[提取并分离 @import 规则与其他规则]
    C --> D[重组 AST 确保 @import 规则置顶]
    D --> E[使用 css-tree 生成 CSS 字符串]
    E --> F[使用 @3-/css2nest 嵌套选择器]
    F --> G[正则匹配将 rgba 颜色转换为十六进制格式]
    G --> H[输出格式化后的 CSS 字符串]

目录结构

.
├── src/
│   └── lib.js          # 核心格式化流水线
├── tests/
│   └── lib.test.js     # 测试验证
└── package.json        # 依赖与配置说明

技术堆栈

  • css-tree: CSS 解析与代码生成。
  • @3-/css2nest: 选择器嵌套转换引擎。
  • Bun: 运行环境与测试执行。

历史趣闻

CSS 早期不支持选择器嵌套。开发者于 2006 年开发 Sass,于 2009 年开发 Less,用以解决选择器重复编写及样式组织问题。

CSS 嵌套规范(CSS Nesting Module)于 2021 年成为 W3C 正式工作草案,并于 2023 年获得主流浏览器原生支持。

原生 CSS 嵌套内部通过 :is() 伪类计算优先级。这与 Sass 编译时直接拼接字符串的逻辑存在细微差异,可能导致最终优先级计算结果不同。

@import 规范要求必须位于样式表最顶部。这能避免浏览器在解析中途发现新网络请求,从而减少页面渲染延迟。


About

This project is an open-source component of i18n.site ⋅ Internationalization Solution.

关于

本项目为 i18n.site ⋅ 国际化解决方案 的开源组件。