UNPKG

5.24 kBMarkdownView Raw
1[PostCSS]: https://github.com/postcss/postcss
2[PostCSS-SCSS]: https://github.com/postcss/postcss-scss
3[LESS]: http://lesless.org
4[Autoprefixer]: https://github.com/postcss/autoprefixer
5[Stylelint]: http://stylelint.io/
6
7# PostCSS LESS Syntax [![Build Status](https://img.shields.io/travis/shellscape/postcss-less.svg?branch=develop)](https://travis-ci.org/webschik/postcss-less) [![npm Version](https://img.shields.io/npm/v/postcss-less.svg)](https://www.npmjs.com/package/postcss-less)
8
9[PostCSS] Syntax for parsing [LESS]
10
11<img align="right" width="95" height="95"
12 title="Philosopher's stone, logo of PostCSS"
13 src="http://postcss.github.io/postcss/logo.svg">
14
15Please note: poscss-less is not a LESS compiler. For compiling LESS, please use
16the official toolset for LESS.
17
18## Getting Started
19
20First thing's first, install the module:
21
22```
23npm install postcss-less --save-dev
24```
25
26## LESS Transformations
27
28The most common use of `postcss-less` is for applying PostCSS transformations
29directly to LESS source. eg. ia theme written in LESS which uses [Autoprefixer]
30to add appropriate vendor prefixes.
31
32```js
33const syntax = require('postcss-less');
34postcss(plugins)
35 .process(lessText, { syntax: syntax })
36 .then(function (result) {
37 result.content // LESS with transformations
38});
39```
40
41## Inline Comments
42
43Parsing of single-line comments in CSS is also supported.
44
45```less
46:root {
47 // Main theme color
48 --color: red;
49}
50```
51
52Note that you don't need a custom stringifier to handle the output; the default
53stringifier will automatically convert single line comments into block comments.
54If you need to support inline comments, please use a [custom PostCSSLess stringifier](#stringifier).
55
56## Rule Node
57
58[PostCSS Rule Node](https://github.com/postcss/postcss/blob/master/docs/api.md#rule-node)
59
60### rule.ruleWithoutBody
61Determines whether or not a rule has a body, or content.
62
63```js
64import postCssLess from 'postcss-less';
65const less = `
66 .class2 {
67 &:extend(.class1);
68 .mixin-name(@param1, @param2);
69 }
70`;
71const root = postCssLess.parse(less);
72
73root.first.nodes[0].ruleWithoutBody // => true for &:extend
74root.first.nodes[1].ruleWithoutBody // => true for calling of mixin
75```
76### rule.nodes
77
78An `Array` of child nodes.
79
80**Note** that rules without body don't have this property.
81
82```js
83import postCssLess from 'postcss-less';
84const less = `
85 .class2 {
86 &:extend(.class1);
87 .mixin-name(@param1, @param2);
88 }
89`;
90const root = postCssLess.parse(less);
91
92root.first.nodes[0].nodes // => undefined for &:extend
93root.first.nodes[1].nodes // => undefined for mixin calling
94```
95
96### rule.extendRule
97
98Determines whether or not a rule is nested (eg.
99 [extended](http://lesscss.org/features/#extend-feature-extend-inside-ruleset)).
100
101```js
102import postCssLess from 'postcss-less';
103const less = `
104 .class2 {
105 &:extend(.class1);
106 }
107`;
108const root = postCssLess.parse(less);
109
110root.first.nodes[0].extendRule // => true
111```
112
113## Comment Node
114
115[PostCSS Comment Node](https://github.com/postcss/postcss/blob/master/docs/api.md#comment-node)
116
117### comment.inline
118
119Determines whether or not a comment is inline.
120
121```js
122import postCssLess from 'postcss-less';
123
124const root = postCssLess.parse('// Hello world');
125
126root.first.inline // => true
127```
128
129### comment.block
130
131Determines whether or not a comment is a block comment.
132
133```js
134import postCssLess from 'postcss-less';
135
136const root = postCssLess.parse('/* Hello world */');
137
138root.first.block // => true
139```
140
141### comment.raws.begin
142
143Precending characters of a comment node. eg. `//` or `/*`.
144
145### comment.raws.content
146
147Raw content of the comment.
148
149```js
150import postCssLess from 'postcss-less';
151
152const root = postCssLess.parse('// Hello world');
153
154root.first.raws.content // => '// Hello world'
155```
156
157## Stringifying
158
159To process LESS code without PostCSS transformations, custom stringifier
160should be provided.
161
162```js
163import postcss from 'postcss';
164import postcssLess from 'postcss-less';
165import stringify from 'postcss-less/less-stringify';
166
167const lessCode = `
168 // Non-css comment
169
170 .container {
171 .mixin-1();
172 .mixin-2;
173 .mixin-3 (@width: 100px) {
174 width: @width;
175 }
176 }
177
178 .rotation(@deg:5deg){
179 .transform(rotate(@deg));
180 }
181`;
182
183postcss()
184 .process(less, {
185 syntax: postcssLess,
186 stringifier: stringify
187 })
188 .then((result) => {
189 console.log(result.content); // this will be value of `lessCode` without changing comments or mixins
190});
191```
192
193## Use Cases
194
195* Lint LESS code with 3rd-party plugins.
196* Apply PostCSS transformations (such as [Autoprefixer](https://github.com/postcss/autoprefixer)) directly to the LESS source code
197
198## Contribution
199
200Please, check our guidelines: [CONTRIBUTING.md](./CONTRIBUTING.md)
201
202## Attribution
203
204This module is based on the [postcss-scss](https://github.com/postcss/postcss-scss) library.
205
206[![npm Downloads](https://img.shields.io/npm/dt/postcss-less.svg)](https://www.npmjs.com/package/postcss-less)
207[![npm License](https://img.shields.io/npm/l/postcss-less.svg)](https://www.npmjs.com/package/postcss-less)
208[![js-strict-standard-style](https://img.shields.io/badge/code%20style-strict-117D6B.svg)](https://github.com/keithamus/eslint-config-strict)