What is postcss-less?
The postcss-less npm package is a tool that allows you to parse LESS syntax with PostCSS, enabling you to use PostCSS plugins with LESS files. It is a syntax plugin for PostCSS, a tool for transforming styles with JavaScript plugins.
What are postcss-less's main functionalities?
Parsing LESS Syntax
This feature allows you to parse LESS files using PostCSS by specifying the postcss-less syntax. This is useful for applying PostCSS plugins to LESS files.
const postcss = require('postcss');
const postcssLess = require('postcss-less');
postcss().process(lessCode, { syntax: postcssLess }).then(result => {
console.log(result.css);
});
Stringifying LESS
This feature enables you to convert a PostCSS AST (Abstract Syntax Tree) back into a LESS string. This is useful when you want to generate LESS code after manipulating the AST.
const postcss = require('postcss');
const postcssLess = require('postcss-less');
const root = postcss.parse(lessCode, { syntax: postcssLess });
const less = root.toString(postcssLess);
Other packages similar to postcss-less
postcss-scss
This package is similar to postcss-less but is designed to work with SCSS syntax. It allows you to use PostCSS plugins with SCSS files.
postcss-sass
Similar to postcss-less, postcss-sass enables you to parse SASS syntax with PostCSS. It is another syntax plugin for PostCSS that targets SASS files instead of LESS.
postcss-styl
This package is a PostCSS syntax for parsing Stylus. Like postcss-less, it allows you to use PostCSS plugins with Stylus files, providing similar functionality for a different preprocessor language.
PostCSS LESS Syntax - Work in Progress
This project is not stable and is in development. If you'd like to contribute, please submit a Pull Request.
A LESS parser for PostCSS.
This module does not compile LESS. It simply parses mixins and variables so that PostCSS plugins can then transform LESS source code alongside CSS.
Use Cases
- lint your LESS code with a plugin such as Stylelint.
- apply PostCSS transformations (such as Autoprefixer) directly to the LESS source code
Usage
LESS Transformations
The main use case of this plugin is to apply PostCSS transformations directly
to LESS source code. For example, if you ship a theme written in LESS and need
Autoprefixer to add the appropriate vendor prefixes to it.
const syntax = require('postcss-less');
postcss(plugins).process(lessText, { syntax: syntax }).then(function (result) {
result.content
});
Parser options
It makes sense to use this option with mixinsAsAtRules
if you want to parse mixins on the root level as AtRule
and all inner mixins as Rule
nodes.
Options help you to tell postcss-less
to fake LESS-specific syntax. For example, Stylelint requires the next options:
const syntax = require('postcss-less');
const postcssParserOptions = {
syntax: syntax,
mixinsAsAtRules: true,
innerMixinsAsRules: true
};
postcss().process(lessText, postcssParserOptions).then(function (result) {
result.content
});
This module also enables parsing of single-line comments in CSS source code.
:root {
--color: red;
}
Note that you don't need a special stringifier to handle the output; the default
one will automatically convert single line comments into block comments.
Restrictions
Skipping mixins without body
postcss-less
parses all mixins without body as custom nodes with specifix types:
.foo();
.foo;
PostCSS
skips all these mixins, so you won't have them in the result.
Skipping inner extend
postcss-less
parses nested &:extend
constructions as custom nodes with type nested-extend
.
PostCSS
doesn't parse this node type and you won't have these constructions in the result.
Contribution
Please, check our guidelines: CONTRIBUTING.md
Attribution
This module is based on the work of postcss-scss library and includes the LESS
parser efforts of github:gilt/postcss-less