postcss-less

LESS parser for PostCSS

MIT 46 个版本
安装
npm install postcss-less
yarn add postcss-less
pnpm add postcss-less
bun add postcss-less
README

postcss-less

tests cover size

A PostCSS Syntax for parsing LESS

Note: This module requires Node v6.14.4+. poscss-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.

const syntax = require('postcss-less');
postcss(plugins)
  .process(lessText, { syntax: syntax })
  .then(function (result) {
    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.

Stringifying

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

const postcss = require('postcss');
const syntax = require('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)

版本列表
6.0.0 2022-01-07
5.0.0 2021-07-27
4.0.1 2021-02-22
4.0.0 2020-10-21
3.1.4 2019-03-17
3.1.3 2019-03-13
3.1.2 2019-01-24
3.1.1 2019-01-14
3.1.0 2018-11-26
3.0.2 2018-09-26
3.0.1 2018-09-22
3.0.0 2018-09-22
2.0.0 2018-05-28
1.1.5 2018-03-21
1.1.4 2018-03-20
1.1.3 2017-11-10
1.1.2 2017-11-06
1.1.1 2017-10-02
1.1.0 2017-07-10
1.0.2 2017-06-12
1.0.1 2017-05-27
1.0.0 2017-05-17
0.16.1 2017-05-06
0.16.0 2017-05-05
0.15.0 2016-09-12
0.14.0 2016-07-06
0.13.0 2016-06-19
0.12.0 2016-05-16
0.11.1 2016-05-16
0.11.0 2016-05-14
0.10.0 2016-05-03
0.9.0 2016-04-08
0.8.0 2016-03-31
0.7.0 2016-03-30
0.6.0 2016-03-28
0.5.0 2016-03-24
0.4.0 2016-03-18
0.3.0 2016-03-16
0.2.1 2016-03-07
0.2.0 2016-03-03
0.1.5 2016-01-27
0.1.4 2016-01-27
0.1.3 2016-01-27
0.1.2 2016-01-27
0.1.1 2016-01-27
0.1.0 2016-01-27