CSScomb Core

3.0.6 · active · verified Sun Apr 19

CSScomb Core is a foundational framework designed for building custom CSS postprocessors. It provides a robust set of features, including a powerful parser that supports various CSS preprocessor syntaxes (CSS, Less, Sass, SCSS), an API for defining and managing processing options, and utilities for applying these processes to files, directories, or strings. The current stable version, 3.0.6, continues to focus on providing a modular and extensible architecture for linting and transforming CSS code programmatically. Its key differentiators lie in its flexible plugin system, allowing developers to create highly specific styling rules and formatting logic, and its ability to handle different CSS-like syntaxes through a unified interface. While a specific release cadence isn't published, it appears to be maintained as a core component for CSS processing tasks.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to import CSScomb Core, configure it with rules, and then use it to process and lint a CSS string asynchronously.

const Comb = require('csscomb-core');
const fs = require('fs');
const path = require('path');

async function processCssString() {
  // Define a configuration object. In a real application, this would typically
  // be loaded from a .csscomb.json file or similar.
  const config = {
    'always-semicolon': true,
    'color-case': 'lower',
    'leading-zero': false,
    'space-before-colon': 'zero',
    'space-after-colon': 'one',
    'selector-separator-newline': true
  };

  // Instantiate the Comb processor. The first parameter is typically an array
  // of plugin instances if you're dynamically loading them, or an empty array.
  // The second parameter specifies the default syntax.
  const comb = new Comb([], 'css');

  // Load the desired configuration into the comb instance.
  comb.configure(config);

  const rawCss = `
    .my-class {
      color: #FF0000;
      font-size: 16px
    }
    a {display :block ;  padding: 10px}
    div{border: 1px solid #CCC;}
  `;

  try {
    console.log('Original CSS:\n', rawCss);
    
    // Process the string, applying the configured rules.
    const processedCss = await comb.processString(rawCss, { filename: 'example.css', syntax: 'css' });
    console.log('\nProcessed CSS:\n', processedCss);

    // You can also lint the string to find issues without modifying it.
    const lintErrors = await comb.lintString(rawCss, { filename: 'example.css', syntax: 'css' });
    if (lintErrors.length > 0) {
      console.log('\nLinting Errors Found:');
      lintErrors.forEach(error => console.log(`- [${error.line}:${error.column}] ${error.message}`));
    } else {
      console.log('\nNo linting errors found.');
    }

  } catch (error) {
    console.error('Error processing CSS:', error);
  }
}

processCssString();

view raw JSON →