postcss-values-parser is a JavaScript library designed to parse CSS property values into an Abstract Syntax Tree (AST). It is built upon the PostCSS ecosystem, leveraging PostCSS's tokenizer and following similar node and traversal patterns, ensuring consistency for developers familiar with PostCSS plugins. The current stable version is 7.0.0, which marked a significant rewrite, now building on `css-tree` for improved correctness and performance, and transitioning to a pure ESM package. The library aims to provide robust AST traversal, convenience methods for stringifying nodes, and specific properties for parsing units, colors, and variables, making it a powerful tool for manipulating CSS values within a PostCSS workflow. Release cadence appears to be driven by significant refactors and PostCSS version updates, with maintenance releases addressing bug fixes.
npm install postcss-values-parserVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates parsing a complex CSS value, traversing its AST using `walk` and `walkWords`, and performing a basic modification of a node (changing a color value).
Migrate your project to use ES modules (`import { parse } from 'postcss-values-parser';`) or stick to an earlier version if CommonJS is a strict requirement.Upgrade your Node.js environment to version 20.19.0 or newer. Check your `nvm` or system Node.js installation.
Call `registerWalkers(root)` after parsing the value to enable helper methods. For example: `const root = parse(value); registerWalkers(root); root.walkWords(...)`.
Ensure your project's `postcss` dependency meets the `^8.4.14` requirement for `postcss-values-parser` v6.x, or `^8.4.14` for v7.x as well. Update PostCSS if necessary.
Review the v3.0.0 release notes and documentation carefully. Existing code written for v2.x will likely require significant refactoring to adapt to the new AST structure and API.
Convert your importing file to an ES module (e.g., rename `.js` to `.mjs` or add `"type": "module"` to `package.json`), and use `import { parse } from 'postcss-values-parser';`.After parsing, call `registerWalkers(root)` to enable these helper methods: `const root = parse(value); registerWalkers(root); root.walkWords(...)`.
Update your PostCSS dependency to meet the version required by `postcss-values-parser`. For example, `npm install postcss@^8.4.14`.