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 |
|
15 | Please note: poscss-less is not a LESS compiler. For compiling LESS, please use
|
16 | the official toolset for LESS.
|
17 |
|
18 | ## Getting Started
|
19 |
|
20 | First thing's first, install the module:
|
21 |
|
22 | ```
|
23 | npm install postcss-less --save-dev
|
24 | ```
|
25 |
|
26 | ## LESS Transformations
|
27 |
|
28 | The most common use of `postcss-less` is for applying PostCSS transformations
|
29 | directly to LESS source. eg. ia theme written in LESS which uses [Autoprefixer]
|
30 | to add appropriate vendor prefixes.
|
31 |
|
32 | ```js
|
33 | const syntax = require('postcss-less');
|
34 | postcss(plugins)
|
35 | .process(lessText, { syntax: syntax })
|
36 | .then(function (result) {
|
37 | result.content // LESS with transformations
|
38 | });
|
39 | ```
|
40 |
|
41 | ## Inline Comments
|
42 |
|
43 | Parsing of single-line comments in CSS is also supported.
|
44 |
|
45 | ```less
|
46 | :root {
|
47 | // Main theme color
|
48 | --color: red;
|
49 | }
|
50 | ```
|
51 |
|
52 | Note that you don't need a custom stringifier to handle the output; the default
|
53 | stringifier will automatically convert single line comments into block comments.
|
54 | If 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
|
61 | Determines whether or not a rule has a body, or content.
|
62 |
|
63 | ```js
|
64 | import postCssLess from 'postcss-less';
|
65 | const less = `
|
66 | .class2 {
|
67 | &:extend(.class1);
|
68 | .mixin-name(@param1, @param2);
|
69 | }
|
70 | `;
|
71 | const root = postCssLess.parse(less);
|
72 |
|
73 | root.first.nodes[0].ruleWithoutBody // => true for &:extend
|
74 | root.first.nodes[1].ruleWithoutBody // => true for calling of mixin
|
75 | ```
|
76 | ### rule.nodes
|
77 |
|
78 | An `Array` of child nodes.
|
79 |
|
80 | **Note** that rules without body don't have this property.
|
81 |
|
82 | ```js
|
83 | import postCssLess from 'postcss-less';
|
84 | const less = `
|
85 | .class2 {
|
86 | &:extend(.class1);
|
87 | .mixin-name(@param1, @param2);
|
88 | }
|
89 | `;
|
90 | const root = postCssLess.parse(less);
|
91 |
|
92 | root.first.nodes[0].nodes // => undefined for &:extend
|
93 | root.first.nodes[1].nodes // => undefined for mixin calling
|
94 | ```
|
95 |
|
96 | ### rule.extendRule
|
97 |
|
98 | Determines whether or not a rule is nested (eg.
|
99 | [extended](http://lesscss.org/features/#extend-feature-extend-inside-ruleset)).
|
100 |
|
101 | ```js
|
102 | import postCssLess from 'postcss-less';
|
103 | const less = `
|
104 | .class2 {
|
105 | &:extend(.class1);
|
106 | }
|
107 | `;
|
108 | const root = postCssLess.parse(less);
|
109 |
|
110 | root.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 |
|
119 | Determines whether or not a comment is inline.
|
120 |
|
121 | ```js
|
122 | import postCssLess from 'postcss-less';
|
123 |
|
124 | const root = postCssLess.parse('// Hello world');
|
125 |
|
126 | root.first.inline // => true
|
127 | ```
|
128 |
|
129 | ### comment.block
|
130 |
|
131 | Determines whether or not a comment is a block comment.
|
132 |
|
133 | ```js
|
134 | import postCssLess from 'postcss-less';
|
135 |
|
136 | const root = postCssLess.parse('/* Hello world */');
|
137 |
|
138 | root.first.block // => true
|
139 | ```
|
140 |
|
141 | ### comment.raws.begin
|
142 |
|
143 | Precending characters of a comment node. eg. `//` or `/*`.
|
144 |
|
145 | ### comment.raws.content
|
146 |
|
147 | Raw content of the comment.
|
148 |
|
149 | ```js
|
150 | import postCssLess from 'postcss-less';
|
151 |
|
152 | const root = postCssLess.parse('// Hello world');
|
153 |
|
154 | root.first.raws.content // => '// Hello world'
|
155 | ```
|
156 |
|
157 | ## Stringifying
|
158 |
|
159 | To process LESS code without PostCSS transformations, custom stringifier
|
160 | should be provided.
|
161 |
|
162 | ```js
|
163 | import postcss from 'postcss';
|
164 | import postcssLess from 'postcss-less';
|
165 | import stringify from 'postcss-less/less-stringify';
|
166 |
|
167 | const 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 |
|
183 | postcss()
|
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 |
|
200 | Please, check our guidelines: [CONTRIBUTING.md](./CONTRIBUTING.md)
|
201 |
|
202 | ## Attribution
|
203 |
|
204 | This 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)
|