less-loader 4.0.6 → 6.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +79 -1
- package/README.md +453 -181
- package/dist/LessError.js +34 -0
- package/dist/cjs.js +1 -1
- package/dist/index.js +37 -22
- package/dist/options.json +48 -0
- package/dist/utils.js +166 -0
- package/package.json +68 -61
- package/dist/createWebpackLessPlugin.js +0 -97
- package/dist/formatLessError.js +0 -62
- package/dist/getOptions.js +0 -41
- package/dist/processResult.js +0 -38
- package/dist/removeSourceMappingUrl.js +0 -19
- package/dist/stringifyLoader.js +0 -14
package/README.md
CHANGED
|
@@ -1,131 +1,431 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<a href="https://github.com/webpack/webpack">
|
|
3
|
+
<img width="200" height="200" src="https://webpack.js.org/assets/icon-square-big.svg">
|
|
4
|
+
</a>
|
|
5
|
+
</div>
|
|
6
|
+
|
|
1
7
|
[![npm][npm]][npm-url]
|
|
2
8
|
[![node][node]][node-url]
|
|
3
|
-
[![npm-stats][npm-stats]][npm-url]
|
|
4
9
|
[![deps][deps]][deps-url]
|
|
5
|
-
[![
|
|
6
|
-
[![
|
|
7
|
-
[![coverage][coverage]][coverage-url]
|
|
10
|
+
[![tests][tests]][tests-url]
|
|
11
|
+
[![cover][cover]][cover-url]
|
|
8
12
|
[![chat][chat]][chat-url]
|
|
13
|
+
[![size][size]][size-url]
|
|
9
14
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
<a href="https://github.com/webpack/webpack">
|
|
14
|
-
<img width="200" height="200"
|
|
15
|
-
src="https://webpack.js.org/assets/icon-square-big.svg">
|
|
16
|
-
</a>
|
|
17
|
-
</div>
|
|
15
|
+
# less-loader
|
|
16
|
+
|
|
17
|
+
A Less loader for webpack. Compiles Less to CSS.
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
## Getting Started
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
To begin, you'll need to install `less-loader`:
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
```console
|
|
24
|
+
$ npm install less-loader --save-dev
|
|
25
|
+
```
|
|
24
26
|
|
|
27
|
+
Then add the loader to your `webpack` config. For example:
|
|
25
28
|
|
|
26
|
-
|
|
29
|
+
**webpack.config.js**
|
|
27
30
|
|
|
28
|
-
```
|
|
29
|
-
|
|
31
|
+
```js
|
|
32
|
+
module.exports = {
|
|
33
|
+
module: {
|
|
34
|
+
rules: [
|
|
35
|
+
{
|
|
36
|
+
test: /\.less$/,
|
|
37
|
+
loader: 'less-loader', // compiles Less to CSS
|
|
38
|
+
},
|
|
39
|
+
],
|
|
40
|
+
},
|
|
41
|
+
};
|
|
30
42
|
```
|
|
31
43
|
|
|
32
|
-
|
|
44
|
+
And run `webpack` via your preferred method.
|
|
45
|
+
|
|
46
|
+
## Options
|
|
47
|
+
|
|
48
|
+
| Name | Type | Default | Description |
|
|
49
|
+
| :-------------------------------------: | :------------------: | :----------------------: | :----------------------------------------------- |
|
|
50
|
+
| **[`lessOptions`](#lessoptions)** | `{Object\|Function}` | `{ relativeUrls: true }` | Options for Less. |
|
|
51
|
+
| **[`prependData`](#prependdata)** | `{String\|Function}` | `undefined` | Prepends Less code before the actual entry file. |
|
|
52
|
+
| **[`appendData`](#appenddata)** | `{String\|Function}` | `undefined` | Prepends Less code after the actual entry file. |
|
|
53
|
+
| **[`sourceMap`](#sourcemap)** | `{Boolean}` | `compiler.devtool` | Enables/Disables generation of source maps. |
|
|
54
|
+
| **[`implementation`](#implementation)** | `{Object}` | `less` | Setup Less implementation to use. |
|
|
33
55
|
|
|
34
|
-
|
|
56
|
+
### `lessOptions`
|
|
35
57
|
|
|
36
|
-
|
|
58
|
+
Type: `Object|Function`
|
|
59
|
+
Default: `{ relativeUrls: true }`
|
|
60
|
+
|
|
61
|
+
You can pass any Less specific options to the `less-loader` through the `lessOptions` property in the [loader options](https://webpack.js.org/configuration/module/#rule-options-rule-query). See the [Less documentation](http://lesscss.org/usage/#command-line-usage-options) for all available options in dash-case. Since we're passing these options to Less programmatically, you need to pass them in camelCase here:
|
|
62
|
+
|
|
63
|
+
#### `Object`
|
|
64
|
+
|
|
65
|
+
Use an object to pass options through to Less.
|
|
66
|
+
|
|
67
|
+
**webpack.config.js**
|
|
37
68
|
|
|
38
69
|
```js
|
|
39
|
-
// webpack.config.js
|
|
40
70
|
module.exports = {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
71
|
+
module: {
|
|
72
|
+
rules: [
|
|
73
|
+
{
|
|
74
|
+
test: /\.less$/,
|
|
75
|
+
use: [
|
|
76
|
+
{
|
|
77
|
+
loader: 'style-loader',
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
loader: 'css-loader',
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
loader: 'less-loader',
|
|
84
|
+
options: {
|
|
85
|
+
lessOptions: {
|
|
86
|
+
strictMath: true,
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
],
|
|
91
|
+
},
|
|
92
|
+
],
|
|
93
|
+
},
|
|
54
94
|
};
|
|
55
95
|
```
|
|
56
96
|
|
|
57
|
-
|
|
97
|
+
#### `Function`
|
|
98
|
+
|
|
99
|
+
Allows setting the options passed through to Less based off of the loader context.
|
|
58
100
|
|
|
59
101
|
```js
|
|
60
|
-
// webpack.config.js
|
|
61
102
|
module.exports = {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
103
|
+
module: {
|
|
104
|
+
rules: [
|
|
105
|
+
{
|
|
106
|
+
test: /\.less$/,
|
|
107
|
+
use: [
|
|
108
|
+
'style-loader',
|
|
109
|
+
'css-loader',
|
|
110
|
+
{
|
|
111
|
+
loader: 'less-loader',
|
|
112
|
+
options: {
|
|
113
|
+
lessOptions: (loaderContext) => {
|
|
114
|
+
// More information about available properties https://webpack.js.org/api/loaders/
|
|
115
|
+
const { resourcePath, rootContext } = loaderContext;
|
|
116
|
+
const relativePath = path.relative(rootContext, resourcePath);
|
|
117
|
+
|
|
118
|
+
if (relativePath === 'styles/foo.less') {
|
|
119
|
+
return {
|
|
120
|
+
paths: ['absolute/path/c', 'absolute/path/d'],
|
|
121
|
+
};
|
|
74
122
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
123
|
+
|
|
124
|
+
return {
|
|
125
|
+
paths: ['absolute/path/a', 'absolute/path/b'],
|
|
126
|
+
};
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
],
|
|
131
|
+
},
|
|
132
|
+
],
|
|
133
|
+
},
|
|
78
134
|
};
|
|
79
135
|
```
|
|
80
136
|
|
|
81
|
-
|
|
137
|
+
### `prependData`
|
|
82
138
|
|
|
83
|
-
|
|
139
|
+
Type: `String|Function`
|
|
140
|
+
Default: `undefined`
|
|
141
|
+
|
|
142
|
+
Prepends `Less` code before the actual entry file.
|
|
143
|
+
|
|
144
|
+
This is especially useful when some of your Less variables depend on the environment:
|
|
145
|
+
|
|
146
|
+
> ℹ Since you're injecting code, this will break the source mappings in your entry file. Often there's a simpler solution than this, like multiple Less entry files.
|
|
147
|
+
|
|
148
|
+
#### `String`
|
|
149
|
+
|
|
150
|
+
```js
|
|
151
|
+
module.exports = {
|
|
152
|
+
module: {
|
|
153
|
+
rules: [
|
|
154
|
+
{
|
|
155
|
+
test: /\.less$/,
|
|
156
|
+
use: [
|
|
157
|
+
'style-loader',
|
|
158
|
+
'css-loader',
|
|
159
|
+
{
|
|
160
|
+
loader: 'less-loader',
|
|
161
|
+
options: {
|
|
162
|
+
prependData: `@env: ${process.env.NODE_ENV};`,
|
|
163
|
+
},
|
|
164
|
+
},
|
|
165
|
+
],
|
|
166
|
+
},
|
|
167
|
+
],
|
|
168
|
+
},
|
|
169
|
+
};
|
|
170
|
+
```
|
|
84
171
|
|
|
85
|
-
|
|
172
|
+
#### `Function`
|
|
86
173
|
|
|
87
174
|
```js
|
|
88
|
-
|
|
175
|
+
module.exports = {
|
|
176
|
+
module: {
|
|
177
|
+
rules: [
|
|
178
|
+
{
|
|
179
|
+
test: /\.less$/,
|
|
180
|
+
use: [
|
|
181
|
+
'style-loader',
|
|
182
|
+
'css-loader',
|
|
183
|
+
{
|
|
184
|
+
loader: 'less-loader',
|
|
185
|
+
options: {
|
|
186
|
+
prependData: (loaderContext) => {
|
|
187
|
+
// More information about available properties https://webpack.js.org/api/loaders/
|
|
188
|
+
const { resourcePath, rootContext } = loaderContext;
|
|
189
|
+
const relativePath = path.relative(rootContext, resourcePath);
|
|
190
|
+
|
|
191
|
+
if (relativePath === 'styles/foo.less') {
|
|
192
|
+
return '@value: 100px;';
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return '@value: 200px;';
|
|
196
|
+
},
|
|
197
|
+
},
|
|
198
|
+
},
|
|
199
|
+
],
|
|
200
|
+
},
|
|
201
|
+
],
|
|
202
|
+
},
|
|
203
|
+
};
|
|
204
|
+
```
|
|
89
205
|
|
|
90
|
-
|
|
91
|
-
filename: "[name].[contenthash].css",
|
|
92
|
-
disable: process.env.NODE_ENV === "development"
|
|
93
|
-
});
|
|
206
|
+
### `appendData`
|
|
94
207
|
|
|
208
|
+
Type: `String|Function`
|
|
209
|
+
Default: `undefined`
|
|
210
|
+
|
|
211
|
+
AppendData `Less` code after the actual entry file.
|
|
212
|
+
|
|
213
|
+
This can be useful when you need to rewrite some of your Less variables.:
|
|
214
|
+
|
|
215
|
+
> ℹ Since you're injecting code, this will break the source mappings in your entry file. Often there's a simpler solution than this, like multiple Less entry files.
|
|
216
|
+
|
|
217
|
+
#### `String`
|
|
218
|
+
|
|
219
|
+
```js
|
|
95
220
|
module.exports = {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
]
|
|
221
|
+
module: {
|
|
222
|
+
rules: [
|
|
223
|
+
{
|
|
224
|
+
test: /\.less$/,
|
|
225
|
+
use: [
|
|
226
|
+
'style-loader',
|
|
227
|
+
'css-loader',
|
|
228
|
+
{
|
|
229
|
+
loader: 'less-loader',
|
|
230
|
+
options: {
|
|
231
|
+
appendData: `@env: ${process.env.NODE_ENV};`,
|
|
232
|
+
},
|
|
233
|
+
},
|
|
234
|
+
],
|
|
235
|
+
},
|
|
236
|
+
],
|
|
237
|
+
},
|
|
114
238
|
};
|
|
115
239
|
```
|
|
116
240
|
|
|
117
|
-
|
|
241
|
+
#### `Function`
|
|
242
|
+
|
|
243
|
+
```js
|
|
244
|
+
module.exports = {
|
|
245
|
+
module: {
|
|
246
|
+
rules: [
|
|
247
|
+
{
|
|
248
|
+
test: /\.less$/,
|
|
249
|
+
use: [
|
|
250
|
+
'style-loader',
|
|
251
|
+
'css-loader',
|
|
252
|
+
{
|
|
253
|
+
loader: 'less-loader',
|
|
254
|
+
options: {
|
|
255
|
+
appendData: (loaderContext) => {
|
|
256
|
+
// More information about available properties https://webpack.js.org/api/loaders/
|
|
257
|
+
const { resourcePath, rootContext } = loaderContext;
|
|
258
|
+
const relativePath = path.relative(rootContext, resourcePath);
|
|
259
|
+
|
|
260
|
+
if (relativePath === 'styles/foo.less') {
|
|
261
|
+
return '@value: 100px;';
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
return '@value: 200px;';
|
|
265
|
+
},
|
|
266
|
+
},
|
|
267
|
+
},
|
|
268
|
+
],
|
|
269
|
+
},
|
|
270
|
+
],
|
|
271
|
+
},
|
|
272
|
+
};
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
### `sourceMap`
|
|
276
|
+
|
|
277
|
+
Type: `Boolean`
|
|
278
|
+
Default: depends on the `compiler.devtool` value
|
|
279
|
+
|
|
280
|
+
By default generation of source maps depends on the [`devtool`](https://webpack.js.org/configuration/devtool/) option. All values enable source map generation except `eval` and `false` value.
|
|
281
|
+
|
|
282
|
+
**webpack.config.js**
|
|
283
|
+
|
|
284
|
+
```js
|
|
285
|
+
module.exports = {
|
|
286
|
+
module: {
|
|
287
|
+
rules: [
|
|
288
|
+
{
|
|
289
|
+
test: /\.less$/i,
|
|
290
|
+
use: [
|
|
291
|
+
'style-loader',
|
|
292
|
+
{
|
|
293
|
+
loader: 'css-loader',
|
|
294
|
+
options: {
|
|
295
|
+
sourceMap: true,
|
|
296
|
+
},
|
|
297
|
+
},
|
|
298
|
+
{
|
|
299
|
+
loader: 'less-loader',
|
|
300
|
+
options: {
|
|
301
|
+
sourceMap: true,
|
|
302
|
+
},
|
|
303
|
+
},
|
|
304
|
+
],
|
|
305
|
+
},
|
|
306
|
+
],
|
|
307
|
+
},
|
|
308
|
+
};
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
### `implementation`
|
|
312
|
+
|
|
313
|
+
Type: `Object`
|
|
314
|
+
Default: `less`
|
|
315
|
+
|
|
316
|
+
> ⚠ less-loader compatible with Less version 3 only
|
|
317
|
+
|
|
318
|
+
The special `implementation` option determines which implementation of Less to use.
|
|
319
|
+
The `implementation` options either accepts `less` as a module.
|
|
320
|
+
This is useful if you want to use Less with a smaller version. Do not forget that then you must install your own version of Less.
|
|
321
|
+
|
|
322
|
+
For example, to use custom Less implementation, you'd pass:
|
|
323
|
+
|
|
324
|
+
```js
|
|
325
|
+
module.exports = {
|
|
326
|
+
module: {
|
|
327
|
+
rules: [
|
|
328
|
+
{
|
|
329
|
+
test: /\.less$/,
|
|
330
|
+
use: [
|
|
331
|
+
'style-loader',
|
|
332
|
+
'css-loader',
|
|
333
|
+
{
|
|
334
|
+
loader: 'less-loader',
|
|
335
|
+
options: {
|
|
336
|
+
implementation: require('less'),
|
|
337
|
+
},
|
|
338
|
+
},
|
|
339
|
+
],
|
|
340
|
+
},
|
|
341
|
+
],
|
|
342
|
+
},
|
|
343
|
+
};
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
## Examples
|
|
347
|
+
|
|
348
|
+
### Normal usage
|
|
349
|
+
|
|
350
|
+
Chain the `less-loader` with the [`css-loader`](https://github.com/webpack-contrib/css-loader) and the [`style-loader`](https://github.com/webpack-contrib/style-loader) to immediately apply all styles to the DOM.
|
|
351
|
+
|
|
352
|
+
**webpack.config.js**
|
|
353
|
+
|
|
354
|
+
```js
|
|
355
|
+
module.exports = {
|
|
356
|
+
module: {
|
|
357
|
+
rules: [
|
|
358
|
+
{
|
|
359
|
+
test: /\.less$/,
|
|
360
|
+
use: [
|
|
361
|
+
{
|
|
362
|
+
loader: 'style-loader', // creates style nodes from JS strings
|
|
363
|
+
},
|
|
364
|
+
{
|
|
365
|
+
loader: 'css-loader', // translates CSS into CommonJS
|
|
366
|
+
},
|
|
367
|
+
{
|
|
368
|
+
loader: 'less-loader', // compiles Less to CSS
|
|
369
|
+
},
|
|
370
|
+
],
|
|
371
|
+
},
|
|
372
|
+
],
|
|
373
|
+
},
|
|
374
|
+
};
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
Unfortunately, Less doesn't map all options 1-by-1 to camelCase. When in doubt, [check their executable](https://github.com/less/less.js/blob/3.x/bin/lessc) and search for the dash-case option.
|
|
378
|
+
|
|
379
|
+
### Source maps
|
|
380
|
+
|
|
381
|
+
To enable sourcemaps for CSS, you'll need to pass the `sourceMap` property in the loader's options. If this is not passed, the loader will respect the setting for webpack source maps, set in `devtool`.
|
|
382
|
+
|
|
383
|
+
**webpack.config.js**
|
|
384
|
+
|
|
385
|
+
```javascript
|
|
386
|
+
module.exports = {
|
|
387
|
+
devtool: 'source-map', // any "source-map"-like devtool is possible
|
|
388
|
+
module: {
|
|
389
|
+
rules: [
|
|
390
|
+
{
|
|
391
|
+
test: /\.less$/,
|
|
392
|
+
use: [
|
|
393
|
+
'style-loader',
|
|
394
|
+
{
|
|
395
|
+
loader: 'css-loader',
|
|
396
|
+
options: {
|
|
397
|
+
sourceMap: true,
|
|
398
|
+
},
|
|
399
|
+
},
|
|
400
|
+
{
|
|
401
|
+
loader: 'less-loader',
|
|
402
|
+
options: {
|
|
403
|
+
sourceMap: true,
|
|
404
|
+
},
|
|
405
|
+
},
|
|
406
|
+
],
|
|
407
|
+
},
|
|
408
|
+
],
|
|
409
|
+
},
|
|
410
|
+
};
|
|
411
|
+
```
|
|
412
|
+
|
|
413
|
+
If you want to edit the original Less files inside Chrome, [there's a good blog post](https://medium.com/@toolmantim/getting-started-with-css-sourcemaps-and-in-browser-sass-editing-b4daab987fb0). The blog post is about Sass but it also works for Less.
|
|
414
|
+
|
|
415
|
+
### In production
|
|
416
|
+
|
|
417
|
+
Usually, it's recommended to extract the style sheets into a dedicated file in production using the [MiniCssExtractPlugin](https://github.com/webpack-contrib/mini-css-extract-plugin). This way your styles are not dependent on JavaScript.
|
|
118
418
|
|
|
119
419
|
### Imports
|
|
120
420
|
|
|
121
|
-
Starting with less-loader 4, you can now choose between Less' builtin resolver and webpack's resolver. By default, webpack's resolver is used.
|
|
421
|
+
Starting with `less-loader` 4, you can now choose between Less' builtin resolver and webpack's resolver. By default, webpack's resolver is used.
|
|
122
422
|
|
|
123
423
|
#### webpack resolver
|
|
124
424
|
|
|
125
|
-
webpack provides an [advanced mechanism to resolve files](https://webpack.js.org/configuration/resolve/). The less-loader applies a Less plugin that passes all queries to the webpack resolver. Thus you can import your Less modules from `node_modules`. Just prepend them with a `~` which tells webpack to look up the [`modules`](https://webpack.js.org/configuration/resolve/#resolve-modules).
|
|
425
|
+
webpack provides an [advanced mechanism to resolve files](https://webpack.js.org/configuration/resolve/). The `less-loader` applies a Less plugin that passes all queries to the webpack resolver. Thus you can import your Less modules from `node_modules`. Just prepend them with a `~` which tells webpack to look up the [`modules`](https://webpack.js.org/configuration/resolve/#resolve-modules).
|
|
126
426
|
|
|
127
427
|
```css
|
|
128
|
-
@import
|
|
428
|
+
@import '~bootstrap/less/bootstrap';
|
|
129
429
|
```
|
|
130
430
|
|
|
131
431
|
It's important to only prepend it with `~`, because `~/` resolves to the home-directory. webpack needs to distinguish between `bootstrap` and `~bootstrap`, because CSS and Less files have no special syntax for importing relative files. Writing `@import "file"` is the same as `@import "./file";`
|
|
@@ -137,66 +437,78 @@ Using webpack's resolver, you can import any file type. You just need a loader t
|
|
|
137
437
|
```js
|
|
138
438
|
// webpack.config.js
|
|
139
439
|
module.exports = {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
440
|
+
module: {
|
|
441
|
+
rules: [
|
|
442
|
+
{
|
|
443
|
+
test: /\.js$/,
|
|
444
|
+
issuer: /\.less$/,
|
|
445
|
+
use: [
|
|
446
|
+
{
|
|
447
|
+
loader: 'js-to-less-loader',
|
|
448
|
+
},
|
|
449
|
+
],
|
|
450
|
+
},
|
|
451
|
+
],
|
|
452
|
+
},
|
|
150
453
|
};
|
|
151
454
|
```
|
|
152
455
|
|
|
153
456
|
#### Less resolver
|
|
154
457
|
|
|
155
|
-
If you specify the `paths` option,
|
|
458
|
+
If you specify the `paths` option, modules will be searched in the given `paths`. This is Less' default behavior. `paths` should be an array with absolute paths:
|
|
459
|
+
|
|
460
|
+
**webpack.config.js**
|
|
156
461
|
|
|
157
462
|
```js
|
|
158
|
-
// webpack.config.js
|
|
159
463
|
module.exports = {
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
464
|
+
module: {
|
|
465
|
+
rules: [
|
|
466
|
+
{
|
|
467
|
+
test: /\.less$/,
|
|
468
|
+
use: [
|
|
469
|
+
{
|
|
470
|
+
loader: 'style-loader',
|
|
471
|
+
},
|
|
472
|
+
{
|
|
473
|
+
loader: 'css-loader',
|
|
474
|
+
},
|
|
475
|
+
{
|
|
476
|
+
loader: 'less-loader',
|
|
477
|
+
options: {
|
|
478
|
+
lessOptions: {
|
|
479
|
+
paths: [path.resolve(__dirname, 'node_modules')],
|
|
480
|
+
},
|
|
481
|
+
},
|
|
482
|
+
},
|
|
483
|
+
],
|
|
484
|
+
},
|
|
485
|
+
],
|
|
486
|
+
},
|
|
177
487
|
};
|
|
178
488
|
```
|
|
179
489
|
|
|
180
|
-
In this case, all webpack features like importing non-Less files or aliasing won't work of course.
|
|
181
|
-
|
|
182
490
|
### Plugins
|
|
183
491
|
|
|
184
|
-
In order to use [plugins](http://lesscss.org/usage/#plugins), simply set the
|
|
492
|
+
In order to use [plugins](http://lesscss.org/usage/#plugins), simply set the
|
|
493
|
+
`plugins` option like this:
|
|
185
494
|
|
|
186
495
|
```js
|
|
187
496
|
// webpack.config.js
|
|
188
|
-
const CleanCSSPlugin = require(
|
|
497
|
+
const CleanCSSPlugin = require('less-plugin-clean-css');
|
|
189
498
|
|
|
190
499
|
module.exports = {
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
500
|
+
...
|
|
501
|
+
{
|
|
502
|
+
loader: 'less-loader',
|
|
503
|
+
options: {
|
|
504
|
+
lessOptions: {
|
|
505
|
+
plugins: [
|
|
506
|
+
new CleanCSSPlugin({ advanced: true }),
|
|
507
|
+
],
|
|
508
|
+
},
|
|
509
|
+
},
|
|
510
|
+
},
|
|
511
|
+
...
|
|
200
512
|
};
|
|
201
513
|
```
|
|
202
514
|
|
|
@@ -206,74 +518,34 @@ Bundling CSS with webpack has some nice advantages like referencing images and f
|
|
|
206
518
|
|
|
207
519
|
There are two possibilities to extract a style sheet from the bundle:
|
|
208
520
|
|
|
209
|
-
- [extract-loader](https://github.com/peerigon/extract-loader) (simpler, but specialized on the css-loader's output)
|
|
210
|
-
- [
|
|
211
|
-
|
|
212
|
-
### Source maps
|
|
213
|
-
|
|
214
|
-
To enable CSS source maps, you'll need to pass the `sourceMap` option to the less-loader *and* the css-loader. Your `webpack.config.js` should look like this:
|
|
215
|
-
|
|
216
|
-
```javascript
|
|
217
|
-
module.exports = {
|
|
218
|
-
...
|
|
219
|
-
module: {
|
|
220
|
-
rules: [{
|
|
221
|
-
test: /\.less$/,
|
|
222
|
-
use: [{
|
|
223
|
-
loader: "style-loader"
|
|
224
|
-
}, {
|
|
225
|
-
loader: "css-loader", options: {
|
|
226
|
-
sourceMap: true
|
|
227
|
-
}
|
|
228
|
-
}, {
|
|
229
|
-
loader: "less-loader", options: {
|
|
230
|
-
sourceMap: true
|
|
231
|
-
}
|
|
232
|
-
}]
|
|
233
|
-
}]
|
|
234
|
-
}
|
|
235
|
-
};
|
|
236
|
-
```
|
|
237
|
-
|
|
238
|
-
Also checkout the [sourceMaps example](https://github.com/webpack-contrib/less-loader/tree/master/examples/sourceMaps).
|
|
239
|
-
|
|
240
|
-
If you want to edit the original Less files inside Chrome, [there's a good blog post](https://medium.com/@toolmantim/getting-started-with-css-sourcemaps-and-in-browser-sass-editing-b4daab987fb0). The blog post is about Sass but it also works for Less.
|
|
521
|
+
- [`extract-loader`](https://github.com/peerigon/extract-loader) (simpler, but specialized on the css-loader's output)
|
|
522
|
+
- [MiniCssExtractPlugin](https://github.com/webpack-contrib/mini-css-extract-plugin) (more complex, but works in all use-cases)
|
|
241
523
|
|
|
242
524
|
### CSS modules gotcha
|
|
243
525
|
|
|
244
526
|
There is a known problem with Less and [CSS modules](https://github.com/css-modules/css-modules) regarding relative file paths in `url(...)` statements. [See this issue for an explanation](https://github.com/webpack-contrib/less-loader/issues/109#issuecomment-253797335).
|
|
245
527
|
|
|
246
|
-
|
|
528
|
+
## Contributing
|
|
529
|
+
|
|
530
|
+
Please take a moment to read our contributing guidelines if you haven't yet done so.
|
|
247
531
|
|
|
248
|
-
|
|
249
|
-
<tr>
|
|
250
|
-
<td align="center">
|
|
251
|
-
<a href="https://github.com/jhnns"><img width="150" height="150" src="https://github.com/jhnns.png?s=150"></a>
|
|
252
|
-
<br>
|
|
253
|
-
<a href="https://github.com/jhnns">Johannes Ewald</a>
|
|
254
|
-
</td>
|
|
255
|
-
<tr>
|
|
256
|
-
</table>
|
|
532
|
+
[CONTRIBUTING](./.github/CONTRIBUTING.md)
|
|
257
533
|
|
|
534
|
+
## License
|
|
535
|
+
|
|
536
|
+
[MIT](./LICENSE)
|
|
258
537
|
|
|
259
538
|
[npm]: https://img.shields.io/npm/v/less-loader.svg
|
|
260
|
-
[npm-stats]: https://img.shields.io/npm/dm/less-loader.svg
|
|
261
539
|
[npm-url]: https://npmjs.com/package/less-loader
|
|
262
|
-
|
|
263
540
|
[node]: https://img.shields.io/node/v/less-loader.svg
|
|
264
541
|
[node-url]: https://nodejs.org
|
|
265
|
-
|
|
266
542
|
[deps]: https://david-dm.org/webpack-contrib/less-loader.svg
|
|
267
543
|
[deps-url]: https://david-dm.org/webpack-contrib/less-loader
|
|
268
|
-
|
|
269
|
-
[
|
|
270
|
-
[
|
|
271
|
-
|
|
272
|
-
[
|
|
273
|
-
[
|
|
274
|
-
|
|
275
|
-
[
|
|
276
|
-
[coverage-url]: https://codecov.io/gh/webpack-contrib/less-loader
|
|
277
|
-
|
|
278
|
-
[chat]: https://badges.gitter.im/webpack-contrib/webpack.svg
|
|
279
|
-
[chat-url]: https://gitter.im/webpack-contrib/webpack
|
|
544
|
+
[tests]: https://github.com/webpack-contrib/less-loader/workflows/less-loader/badge.svg
|
|
545
|
+
[tests-url]: https://github.com/webpack-contrib/less-loader/actions
|
|
546
|
+
[cover]: https://codecov.io/gh/webpack-contrib/less-loader/branch/master/graph/badge.svg
|
|
547
|
+
[cover-url]: https://codecov.io/gh/webpack-contrib/less-loader
|
|
548
|
+
[chat]: https://img.shields.io/badge/gitter-webpack%2Fwebpack-brightgreen.svg
|
|
549
|
+
[chat-url]: https://gitter.im/webpack/webpack
|
|
550
|
+
[size]: https://packagephobia.now.sh/badge?p=less-loader
|
|
551
|
+
[size-url]: https://packagephobia.now.sh/result?p=less-loader
|