cssnano Utilities
cssnano-utils is a foundational library providing essential utility methods and a PostCSS plugin specifically designed for use within the cssnano ecosystem. It offers functionalities like `rawCache` for managing raw value formatting of AST nodes, `getArguments` for parsing CSS function arguments, and `sameParent` for checking node relationships within the PostCSS AST. Currently at version 5.0.1, this package does not have an independent release cadence but updates in tandem with its parent project, cssnano, which sees frequent bug fix and minor releases (e.g., v7.1.5, v7.1.4, v7.1.3 within a short timeframe). Its key differentiator is its specialized nature, providing low-level, performance-optimized utilities that are integral to how cssnano plugins analyze and transform CSS, rather than being a standalone CSS processing tool.
Common errors
-
Error: PostCSS plugin <plugin-name> was not found
cause The PostCSS plugin provided by `cssnano-utils` (e.g., `rawCache`) was not correctly imported or passed to PostCSS.fixEnsure `import { rawCache } from 'cssnano-utils';` is used and `rawCache()` is included in the `postcss` plugin array: `postcss([rawCache()])`. -
TypeError: Cannot read properties of undefined (reading 'walkDecls')
cause This usually indicates that a PostCSS `root` or `node` object is `undefined` when attempting to traverse the AST, often due to an empty CSS input or incorrect plugin structure.fixVerify that your PostCSS plugin receives a valid `root` object and that the input CSS is not empty or malformed. Ensure your plugin adheres to the `postcssPlugin` structure.
Warnings
- breaking cssnano-utils, as a core component of cssnano, enforces strict Node.js engine requirements. Running with unsupported Node.js versions (e.g., older than 18.12.0) will lead to runtime errors or unexpected behavior.
- gotcha cssnano-utils is tightly coupled with PostCSS and requires it as a peer dependency. Version mismatches between `postcss` and `cssnano-utils` (or the `cssnano` package it's part of) can lead to plugin loading failures, AST traversal errors, or incorrect CSS transformations.
- deprecated Direct manipulation of PostCSS AST nodes' `raws` properties for formatting can be brittle. `rawCache` plugin is provided to manage raw value formatting more robustly, but relying on internal `raws` for custom plugins can be problematic across PostCSS versions.
Install
-
npm install cssnano-utils -
yarn add cssnano-utils -
pnpm add cssnano-utils
Imports
- rawCache
const { rawCache } = require('cssnano-utils');import { rawCache } from 'cssnano-utils'; - getArguments
import getArguments from 'cssnano-utils';
import { getArguments } from 'cssnano-utils'; - sameParent
const sameParent = require('cssnano-utils').sameParent;import { sameParent } from 'cssnano-utils';
Quickstart
import postcss from 'postcss';
import { rawCache, getArguments } from 'cssnano-utils';
async function processCssWithUtils(cssInput) {
const myPlugin = (opts = {}) => {
return {
postcssPlugin: 'my-custom-plugin',
Declaration(decl) {
// Example using getArguments
if (decl.prop === 'transform' && decl.value.includes('rotate')) {
const args = getArguments(decl.value);
console.log(`Arguments for transform: ${JSON.stringify(args)}`);
}
},
// Example using rawCache as a PostCSS plugin
OnceExit(root) {
root.walkDecls('color', (decl) => {
decl.value = 'red'; // Modify a value
});
}
};
};
myPlugin.postcss = true;
const result = await postcss([myPlugin(), rawCache()]).process(cssInput, { from: undefined });
console.log('Processed CSS:\n', result.css);
return result.css;
}
const sampleCss = `
h1 {
color: green; /* original color */
transform: rotate(30deg) scale(1.2, 0.5);
}
/* Example of a more complex rule */
@media screen and (min-width: 768px) {
h1 {
font-size: 24px;
}
}
`;
processCssWithUtils(sampleCss);