cssnano Utilities

5.0.1 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates the use of `getArguments` to parse CSS values and `rawCache` as a PostCSS plugin to manage AST node formatting.

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);

view raw JSON →