postcss-less

LESS parser for PostCSS

postcss-less downloads postcss-less version postcss-less license

postcss-less

Npm Package Weekly Downloads Trend

3 Years
🌟 Show real-time usage chart on postcss-less's README.md, just copy the code below.
## Usage Trend
[![Usage Trend of postcss-less](https://npm-compare.com/img/npm-trend/THREE_YEARS/postcss-less.png)](https://npm-compare.com/postcss-less#timeRange=THREE_YEARS)

Cumulative GitHub Star Trend

🌟 Show GitHub stars trend chart on postcss-less's README.md, just copy the code below.
## GitHub Stars Trend
[![GitHub Stars Trend of postcss-less](https://npm-compare.com/img/github-trend/postcss-less.png)](https://npm-compare.com/postcss-less)

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
postcss-less1,991,29112425.5 kB56 days agoMIT

README for postcss-less

postcss-less

Npm Version MIT License Coverage

A PostCSS Syntax for parsing LESS

Note: postcss-less is not a LESS compiler. For compiling LESS, please use the official toolset for LESS.

Install

Using npm:

npm install postcss-less --save-dev

Please consider becoming a patron if you find this module useful.

Usage

The most common use of postcss-less is for applying PostCSS transformations directly to LESS source. eg. ia theme written in LESS which uses Autoprefixer to add appropriate vendor prefixes.

import postcss from 'postcss';
import syntax from 'postcss-less';

const result = await postcss().process(lessText, { syntax: syntax });

result.content; // LESS with transformations

LESS Specific Parsing

@import

Parsing of LESS-specific @import statements and options are supported.

@import (option) 'file.less';

The AST will contain an AtRule node with:

  • an import: true property
  • a filename: <String> property containing the imported filename
  • an options: <String> property containing any import options specified

Inline Comments

Parsing of single-line comments in CSS is supported.

:root {
  // Main theme color
  --color: red;
}

The AST will contain a Comment node with an inline: true property.

Mixins

Parsing of LESS mixins is supported.

.my-mixin {
  color: black;
}

The AST will contain an AtRule node with a mixin: true property.

!important

Mixins that declare !important will contain an important: true property on their respective node.

Variables

Parsing of LESS variables is supported.

@link-color: #428bca;

The AST will contain an AtRule node with a variable: true property.

Note: LESS variables are strictly parsed - a colon (:) must immediately follow a variable name.

Stringify

To process LESS code without PostCSS transformations, custom stringifier should be provided.

import postcss from 'postcss';
import syntax from 'postcss-less';

const less = `
    // inline comment

    .container {
        .mixin-1();
        .mixin-2;
        .mixin-3 (@width: 100px) {
            width: @width;
        }
    }

    .rotation(@deg:5deg){
      .transform(rotate(@deg));
    }
`;

const result = await postcss().process(less, { syntax });

// will contain the value of `less`
const { content } = result;

Use Cases

  • Lint LESS code with 3rd-party plugins.
  • Apply PostCSS transformations (such as Autoprefixer) directly to the LESS source code

Meta

CONTRIBUTING

LICENSE (MIT)