@adobe/css-tools

中文文档
4.5.0发布于 2026/05/22MITTypeScriptESM + CJS零依赖近 30 天下载 2.6亿

A modern CSS parser and stringifier with TypeScript support

原始使用文档
文档目录14 节
  1. Install
  2. Usage
  3. API
  4. parse(code, options?)
  5. stringify(ast, options?)
  6. Features
  7. Supported CSS Features
  8. Examples
  9. Error Handling
  10. Source Tracking
  11. Performance
  12. Documentation
  13. Background
  14. License

@adobe/css-tools

A modern CSS parser and stringifier with TypeScript support

npm version License: MIT

Parse CSS into an Abstract Syntax Tree (AST) and convert it back to CSS with configurable formatting. Built with TypeScript for type safety and modern JavaScript features.

Install

npm install @adobe/css-tools

Usage

import { parse, stringify } from '@adobe/css-tools'

// Parse CSS to AST
const ast = parse('body { font-size: 12px; }')

// Stringify AST back to CSS
const css = stringify(ast)
// => "body { font-size: 12px; }"

// Pretty print with custom indentation
const formatted = stringify(ast, { indent: '  ' })
// => "body {\n  font-size: 12px;\n}"

// Minify output
const minified = stringify(ast, { compress: true })
// => "body{font-size:12px}"

API

parse(code, options?)

Parses CSS code and returns an Abstract Syntax Tree (AST).

Parameters:

  • code (string) - The CSS code to parse
  • options (object, optional) - Parsing options
    • silent (boolean) - Silently fail on parse errors instead of throwing
    • source (string) - File path for better error reporting

Returns: CssStylesheetAST - The parsed CSS as an AST

stringify(ast, options?)

Converts a CSS AST back to CSS string with configurable formatting.

Parameters:

  • ast (CssStylesheetAST) - The CSS AST to stringify
  • options (object, optional) - Stringification options
    • indent (string) - Indentation string (default: ' ')
    • compress (boolean) - Whether to compress/minify the output (default: false)

Returns: string - The formatted CSS string

Features

  • Complete CSS Support: All standard CSS features including selectors, properties, values, at-rules, and comments
  • TypeScript Support: Full type definitions for all AST nodes and functions
  • Error Handling: Configurable error handling with detailed position information
  • Formatting Options: Pretty print, minify, or custom formatting
  • Performance Optimized: Efficient parsing and stringification for large CSS files
  • Source Maps: Track original source positions for debugging and tooling

Supported CSS Features

  • Selectors: Element, class, ID, attribute, pseudo-class, pseudo-element selectors
  • Properties: All standard CSS properties and custom properties
  • Values: Colors, lengths, percentages, functions, calc(), etc.
  • At-rules: @media, @keyframes, @import, @charset, @namespace, @font-face, @page, @document, @supports, @container, @layer, @starting-style, @host, @custom-media
  • Comments: Both /* */ and // comments
  • Whitespace: Preserves formatting information
  • Vendor prefixes: Supports vendor-prefixed at-rules and properties
  • Nested rules: Media queries, supports, containers, etc.
  • Complex selectors: Combinators, pseudo-selectors, attribute selectors

Examples

Error Handling

import { parse } from '@adobe/css-tools'

const malformedCss = `
  body { color: red; }
  { color: blue; } /* Missing selector */
  .valid { background: green; }
`

// Parse with silent error handling
const result = parse(malformedCss, { silent: true })

// Check for parsing errors
if (result.stylesheet.parsingErrors) {
  console.log('Parsing errors:', result.stylesheet.parsingErrors.length)
  result.stylesheet.parsingErrors.forEach(error => {
    console.log(`Error at line ${error.line}: ${error.message}`)
  })
}

// Valid rules are still parsed
console.log('Valid rules:', result.stylesheet.rules.length)

Source Tracking

import { parse } from '@adobe/css-tools'

const css = 'body { color: red; }'
const ast = parse(css, { source: 'styles.css' })

// Position information is available
const rule = ast.stylesheet.rules[0]
console.log(rule.position?.source) // "styles.css"
console.log(rule.position?.start) // { line: 1, column: 1 }
console.log(rule.position?.end) // { line: 1, column: 20 }

For more examples, see the Examples documentation.

Performance

The library is optimized for performance and can handle large CSS files efficiently. For benchmarking information, see the benchmark/ directory in the source code.

Documentation

Background

This is a fork of the npm css package, maintained by Adobe with modern improvements including TypeScript support, enhanced performance, and security updates. It provides a robust foundation for CSS tooling, preprocessing, and analysis.

License

MIT

版本与发布记录

@adobe/css-tools 共发布 22 个版本,首次发布于 2022/06/10;过去 12 个月发布了 1 个版本。

版本发布日期距今
4.5.0latest2026/05/225 个月前
4.4.42025/08/131.2 年前
4.4.32025/05/191.5 年前
4.4.22025/02/131.7 年前
4.4.12024/11/152 年前
4.4.02024/06/052 年前

关于 @adobe/css-tools 的常见问题

@adobe/css-tools 是什么?

A modern CSS parser and stringifier with TypeScript support

如何安装 @adobe/css-tools?

在项目目录运行 npm install @adobe/css-tools,或使用 pnpm add / yarn add / bun add。

@adobe/css-tools 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@adobe/css-tools 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@adobe/css-tools 可以商用吗?

@adobe/css-tools 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@adobe/css-tools 的下载量有多少?

根据 npm 官方下载统计,@adobe/css-tools 在 2026/10/05 – 2026/11/03 这 30 天内被下载 256,475,262 次,日均约 8,549,175 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@adobe/css-tools 还在维护吗?

最新版本 4.5.0 发布于 2026/05/22(5 个月前);至今共发布 22 个版本,其中过去 12 个月发布了 1 个;首次发布于 2022/06/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。