You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+35-24Lines changed: 35 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,7 @@
13
13
14
14
# less-loader
15
15
16
-
A Less loader for webpack. Compiles Less to CSS.
16
+
A Less loader for webpack that compiles Less files into CSS.
17
17
18
18
## Getting Started
19
19
@@ -35,7 +35,7 @@ or
35
35
pnpm add -D less less-loader
36
36
```
37
37
38
-
Then add the loader to your `webpack`config. For example:
38
+
Then add the loader to your `webpack`configuration. For example:
39
39
40
40
**webpack.config.js**
41
41
@@ -57,7 +57,7 @@ module.exports = {
57
57
};
58
58
```
59
59
60
-
And run `webpack` via your preferred method.
60
+
Finally, run `webpack`using the method you normally use (e.g., via CLI or an npm script).
61
61
62
62
## Options
63
63
@@ -78,11 +78,13 @@ type lessOptions = import('less').options | ((loaderContext: LoaderContext) => i
78
78
79
79
Default: `{ relativeUrls:true }`
80
80
81
-
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/#ruleoptions--rulequery). 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:
81
+
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/#ruleoptions--rulequery). See the [Less documentation](http://lesscss.org/usage/#command-line-usage-options) for all available options in dash-case.
82
+
83
+
Since we're passing these options to Less programmatically, you need to pass them in camelCase here:
82
84
83
85
#### `object`
84
86
85
-
Use an object to pass options through to Less.
87
+
Use an object to pass options directly to Less.
86
88
87
89
**webpack.config.js**
88
90
@@ -116,7 +118,7 @@ module.exports = {
116
118
117
119
#### `function`
118
120
119
-
Allows setting the options passed through to Less based off of the loader context.
121
+
Allows setting the Less options dynamically based on the loader context.
120
122
121
123
```js
122
124
module.exports= {
@@ -166,10 +168,10 @@ type additionalData =
166
168
167
169
Default: `undefined`
168
170
169
-
Prepends/Appends `Less` code to the actual entry file.
171
+
Prepends or Appends `Less` code to the actual entry file.
170
172
In this case, the `less-loader` will not override the source but just **prepend** the entry's content.
171
173
172
-
This is especially useful when some of your Less variables depend on the environment:
174
+
This is especially useful when some of your Less variables depend on the environment.
173
175
174
176
> 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.
175
177
@@ -277,7 +279,8 @@ type sourceMap = boolean;
277
279
278
280
Default: depends on the `compiler.devtool` value
279
281
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.
282
+
By default generation of source maps depends on the [`devtool`](https://webpack.js.org/configuration/devtool/) option.
283
+
All values enable source map generation except `eval` and `false` value.
281
284
282
285
**webpack.config.js**
283
286
@@ -318,7 +321,7 @@ type webpackImporter = boolean | "only";
318
321
319
322
Default: `true`
320
323
321
-
Enables/Disables the default `webpack` importer.
324
+
Enables or disables the default `webpack` importer.
322
325
323
326
This can improve performance in some cases. Use it with caution because aliases and `@import` from [`node_modules`](https://webpack.js.org/configuration/resolve/#resolvemodules) will not work.
324
327
@@ -354,14 +357,16 @@ Type:
354
357
typeimplementation=object|string;
355
358
```
356
359
357
-
> less-loader compatible with Less 3 and 4 versions
360
+
> less-loader compatible with both Less 3 and 4 versions
358
361
359
362
The special `implementation` option determines which implementation of Less to use. Overrides the locally installed `peerDependency` version of `less`.
360
363
361
364
**This option is only really useful for downstream tooling authors to ease the Less 3-to-4 transition.**
362
365
363
366
#### `object`
364
367
368
+
Example using a Less instance:
369
+
365
370
**webpack.config.js**
366
371
367
372
```js
@@ -388,6 +393,8 @@ module.exports = {
388
393
389
394
#### `string`
390
395
396
+
Example using a resolved Less module path:
397
+
391
398
**webpack.config.js**
392
399
393
400
```js
@@ -422,7 +429,7 @@ type lessLogAsWarnOrErr = boolean;
422
429
423
430
Default: `false`
424
431
425
-
`Less` warnings and errors will be webpack warnings and errors, not just logs.
432
+
`Less` warnings and errors will be treated as webpack warnings and errors, instead of being logged silently.
426
433
427
434
**warning.less**
428
435
@@ -432,7 +439,7 @@ div {
432
439
}
433
440
```
434
441
435
-
If `lessLogAsWarnOrErr` is set to `false` it will be just a log and webpack will compile successfully, but if you set this option to `true` webpack will compile fail with a warning.
442
+
If `lessLogAsWarnOrErr` is set to `false` it will be just a log and webpack will compile successfully, but if you set this option to `true` webpack will compile fail with a warning(or error), and can break the build if configured accordingly.
436
443
437
444
**webpack.config.js**
438
445
@@ -462,7 +469,7 @@ module.exports = {
462
469
463
470
### Normal usage
464
471
465
-
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.
472
+
Chain the `less-loader` with [`css-loader`](https://github.com/webpack-contrib/css-loader) and [`style-loader`](https://github.com/webpack-contrib/style-loader) to immediately apply all styles to the DOM.
466
473
467
474
**webpack.config.js**
468
475
@@ -474,13 +481,13 @@ module.exports = {
474
481
test:/\.less$/i,
475
482
use: [
476
483
{
477
-
loader:"style-loader", //creates style nodes from JS strings
484
+
loader:"style-loader", //Creates style nodes from JS strings
478
485
},
479
486
{
480
-
loader:"css-loader", //translates CSS into CommonJS
487
+
loader:"css-loader", //Translates CSS into CommonJS
481
488
},
482
489
{
483
-
loader:"less-loader", //compiles Less to CSS
490
+
loader:"less-loader", //Compiles Less to CSS
484
491
},
485
492
],
486
493
},
@@ -529,7 +536,7 @@ If you want to edit the original Less files inside Chrome, [there's a good blog
529
536
530
537
### In production
531
538
532
-
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.
539
+
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, improving performance and cacheability.
533
540
534
541
### Imports
535
542
@@ -545,7 +552,7 @@ Thus you can import your Less modules from `node_modules`.
545
552
@import"bootstrap/less/bootstrap";
546
553
```
547
554
548
-
Using `~` is deprecated and can be removed from your code (**we recommend it**), but we still support it for historical reasons.
555
+
Using `~`prefix (e.g., @import "~bootstrap/less/bootstrap";) is deprecated and can be removed from your code (**we recommend it**), but we still support it for historical reasons.
549
556
Why you can removed it? The loader will first try to resolve `@import` as relative, if it cannot be resolved, the loader will try to resolve `@import` inside [`node_modules`](https://webpack.js.org/configuration/resolve/#resolvemodules).
550
557
551
558
Default resolver options can be modified by [`resolve.byDependency`](https://webpack.js.org/configuration/resolve/#resolvebydependency):
@@ -610,7 +617,7 @@ module.exports = {
610
617
611
618
### Plugins
612
619
613
-
In order to use [plugins](http://lesscss.org/usage/#plugins), simply set the `plugins` option like this:
620
+
In order to use [Less plugins](http://lesscss.org/usage/#plugins), simply set the `plugins` option like this:
614
621
615
622
**webpack.config.js**
616
623
@@ -635,7 +642,7 @@ module.exports = {
635
642
636
643
> [!NOTE]
637
644
>
638
-
> Access to the [loader context](https://webpack.js.org/api/loaders/#the-loader-context) inside the custom plugin can be done using the `pluginManager.webpackLoaderContext` property.
645
+
> Access to the [loader context](https://webpack.js.org/api/loaders/#the-loader-context) inside a custom plugin can be done using the `pluginManager.webpackLoaderContext` property.
639
646
640
647
```js
641
648
module.exports= {
@@ -651,7 +658,9 @@ module.exports = {
651
658
652
659
### Extracting style sheets
653
660
654
-
Bundling CSS with webpack has some nice advantages like referencing images and fonts with hashed urls or [hot module replacement](https://webpack.js.org/concepts/hot-module-replacement/) in development. In production, on the other hand, it's not a good idea to apply your style sheets depending on JS execution. Rendering may be delayed or even a [FOUC](https://en.wikipedia.org/wiki/Flash_of_unstyled_content) might be visible. Thus it's often still better to have them as separate files in your final production build.
661
+
Bundling CSS with webpack has some nice advantages like referencing images and fonts with hashed urls or [Hot Module Replacement(HMR)](https://webpack.js.org/concepts/hot-module-replacement/) in development.
662
+
663
+
In production, on the other hand, it's not a good idea to apply your style sheets depending on JS execution. Rendering may be delayed or even a [FOUC](https://en.wikipedia.org/wiki/Flash_of_unstyled_content) might be visible. Thus it's often still better to have them as separate files in your final production build.
655
664
656
665
There are two possibilities to extract a style sheet from the bundle:
657
666
@@ -660,11 +669,13 @@ There are two possibilities to extract a style sheet from the bundle:
660
669
661
670
### CSS modules gotcha
662
671
663
-
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).
672
+
There is a known problem when using Less with [CSS modules](https://github.com/css-modules/css-modules) regarding relative file paths in `url(...)` statements.
673
+
[See this issue for an explanation](https://github.com/webpack-contrib/less-loader/issues/109#issuecomment-253797335).
664
674
665
675
## Contributing
666
676
667
-
Please take a moment to read our contributing guidelines if you haven't yet done so.
677
+
We welcome all contributions!
678
+
If you're new here, please take a moment to review our contributing guidelines before submitting issues or pull requests.
0 commit comments